Skip to content

Commit

Permalink
Merge pull request beeware#679 from barmvicente/bytes-strip
Browse files Browse the repository at this point in the history
Adding methods Bytes.strip/rstrip/lstrip
  • Loading branch information
eliasdorneles authored Oct 22, 2017
2 parents 65a70f9 + 0c4b5b0 commit ff9e131
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
31 changes: 25 additions & 6 deletions python/common/org/python/types/Bytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -1131,8 +1131,18 @@ public org.python.Object lower() {
@org.python.Method(
__doc__ = "B.lstrip([bytes]) -> bytes\n\nStrip leading bytes contained in the argument.\nIf the argument is omitted, strip leading ASCII whitespace."
)
public org.python.Object lstrip(java.util.List<org.python.Object> args, java.util.Map<java.lang.String, org.python.Object> kwargs, java.util.List<org.python.Object> default_args, java.util.Map<java.lang.String, org.python.Object> default_kwargs) {
throw new org.python.exceptions.NotImplementedError("bytes.lstrip has not been implemented.");
public Bytes lstrip() {
int start = 0;
int end = this.value.length;

while (start < end && Character.isWhitespace(this.value[start])) {
start++;
}

byte[] slice = Arrays.copyOfRange(this.value, start, end);


return new Bytes(slice);
}

@org.python.Method(
Expand Down Expand Up @@ -1194,8 +1204,17 @@ public org.python.Object rsplit(java.util.List<org.python.Object> args, java.uti
@org.python.Method(
__doc__ = "B.rstrip([bytes]) -> bytes\n\nStrip trailing bytes contained in the argument.\nIf the argument is omitted, strip trailing ASCII whitespace."
)
public org.python.Object rstrip(java.util.List<org.python.Object> args, java.util.Map<java.lang.String, org.python.Object> kwargs, java.util.List<org.python.Object> default_args, java.util.Map<java.lang.String, org.python.Object> default_kwargs) {
throw new org.python.exceptions.NotImplementedError("bytes.rstrip has not been implemented.");
public Bytes rstrip() {
int start = 0;
int end = this.value.length - 1;

while (end > start && Character.isWhitespace(this.value[end])) {
end--;
}

byte[] slice = Arrays.copyOfRange(this.value, start, end + 1);

return new Bytes(slice);
}

@org.python.Method(
Expand Down Expand Up @@ -1224,8 +1243,8 @@ public org.python.Object startswith(org.python.Object prefix, org.python.Object
@org.python.Method(
__doc__ = "B.strip([bytes]) -> bytes\n\nStrip leading and trailing bytes contained in the argument.\nIf the argument is omitted, strip leading and trailing ASCII whitespace."
)
public org.python.Object strip(java.util.List<org.python.Object> args, java.util.Map<java.lang.String, org.python.Object> kwargs, java.util.List<org.python.Object> default_args, java.util.Map<java.lang.String, org.python.Object> default_kwargs) {
throw new org.python.exceptions.NotImplementedError("bytes.strip has not been implemented.");
public org.python.Object strip() {
return this.lstrip().rstrip();
}

public static byte[] _swapcase(byte[] input) {
Expand Down
15 changes: 15 additions & 0 deletions tests/datatypes/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,21 @@ def test_startswith(self):
print(b"abcde".startswith((b"da", b"aaa"), -4))
""")

def test_strip(self):
self.assertCodeExecution(r"""
print(b"abcde".lstrip())
print(b" abcde".lstrip())
print(b"\n \t abcde".lstrip())
print(b"abcde".rstrip())
print(b"abcde ".rstrip())
print(b"abcde \t \n".rstrip())
print(b"abcde".strip())
print(b"abcde ".strip())
print(b" abcde \t \n".strip())
print(b"".strip())
print(b" \n".strip())
""")


class UnaryBytesOperationTests(UnaryOperationTestCase, TranspileTestCase):
data_type = 'bytes'
Expand Down

0 comments on commit ff9e131

Please sign in to comment.