Skip to content

Commit

Permalink
Merge pull request beeware#668 from tapumar/master
Browse files Browse the repository at this point in the history
Add isupper, lower and upper for Bytes and ByteArray
  • Loading branch information
freakboy3742 authored Oct 11, 2017
2 parents 88052e3 + a35511d commit 9e710f2
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 8 deletions.
8 changes: 4 additions & 4 deletions python/common/org/python/types/ByteArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -784,8 +784,8 @@ public org.python.Object istitle(java.util.List<org.python.Object> args, java.ut
@org.python.Method(
__doc__ = "B.isupper() -> bool\n\nReturn True if all cased characters in B are uppercase and there is\nat least one cased character in B, False otherwise."
)
public org.python.Object isupper(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("bytearray.isupper has not been implemented.");
public org.python.Object isupper() {
return new Bool(Bytes._isupper(this.value));
}

@org.python.Method(
Expand All @@ -805,8 +805,8 @@ public org.python.Object ljust(java.util.List<org.python.Object> args, java.util
@org.python.Method(
__doc__ = "B.lower() -> copy of B\n\nReturn a copy of B with all ASCII characters converted to lowercase."
)
public org.python.Object lower(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("bytearray.lower has not been implemented.");
public org.python.Object lower() {
return new ByteArray(Bytes._lower(this.value));
}

@org.python.Method(
Expand Down
31 changes: 27 additions & 4 deletions python/common/org/python/types/Bytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -1007,11 +1007,25 @@ public org.python.Object istitle(java.util.List<org.python.Object> args, java.ut
throw new org.python.exceptions.NotImplementedError("bytes.istitle has not been implemented.");
}

public static boolean _isupper(byte[] input) {
if (input.length == 0) {
return false;
}
byte[] value = new byte[input.length];
for (int i = 0; i < input.length; i++) {
byte b = input[i];
if ((b < 65 || b > 90) && b != ' ') {
return false;
}
}
return true;
}

@org.python.Method(
__doc__ = "B.isupper() -> bool\n\nReturn True if all cased characters in B are uppercase and there is\nat least one cased character in B, False otherwise."
)
public org.python.Object isupper(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.isupper has not been implemented.");
public org.python.Object isupper() {
return new Bool(_isupper(this.value));
}

@org.python.Method(
Expand All @@ -1028,11 +1042,20 @@ public org.python.Object ljust(java.util.List<org.python.Object> args, java.util
throw new org.python.exceptions.NotImplementedError("bytes.ljust has not been implemented.");
}

public static byte[] _lower(byte[] input) {
byte[] result = new byte[input.length];
for (int idx = 0; idx < input.length; ++idx) {
char lc = (char) input[idx];
result[idx] = (byte) Character.toLowerCase(lc);
}
return result;
}

@org.python.Method(
__doc__ = "B.lower() -> copy of B\n\nReturn a copy of B with all ASCII characters converted to lowercase."
)
public org.python.Object lower(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.lower has not been implemented.");
public org.python.Object lower() {
return new Bytes(_lower(this.value));
}

@org.python.Method(
Expand Down
20 changes: 20 additions & 0 deletions tests/datatypes/test_bytearray.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,26 @@ def test_isalpha(self):
print(bytearray(b'hello world ').isalpha())
""")

def test_isupper(self):
self.assertCodeExecution("""
print(bytearray(b'abc').isupper())
print(bytearray(b'ABC').isupper())
print(bytearray(b'').isupper())
print(bytearray(b'Abccc').isupper())
print(bytearray(b'HELLO WORD').isupper())
print(bytearray(b'@#$%!').isupper())
print(bytearray(b'hello world').isupper())
print(bytearray(b'hello world ').isupper())
""")

def test_lower(self):
self.assertCodeExecution("""
print(bytearray(b"abc").lower())
print(bytearray(b"HELLO WORLD!").lower())
print(bytearray(b"hElLO wOrLd").lower())
print(bytearray(b"[Hello] World").lower())
""")


class UnaryBytearrayOperationTests(UnaryOperationTestCase, TranspileTestCase):
data_type = 'bytearray'
Expand Down
19 changes: 19 additions & 0 deletions tests/datatypes/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ def test_islower(self):
""")
# self.assertCodeExecution("""""")

def test_isupper(self):
self.assertCodeExecution("""
print(b'abc'.isupper())
print(b''.isupper())
print(b'Abccc'.isupper())
print(b'HELLO WORD'.isupper())
print(b'@#$%!'.isupper())
print(b'hello world'.isupper())
print(b'hello world '.isupper())
""")

def test_getattr(self):
self.assertCodeExecution("""
x = b'hello, world'
Expand Down Expand Up @@ -319,6 +330,14 @@ def test_upper(self):
print(b'\x46\x55\x43\x4B'.upper())
""")

def test_lower(self):
self.assertCodeExecution("""
print(b"abc".lower())
print(b"HELLO WORLD!".lower())
print(b"hElLO wOrLd".lower())
print(b"[Hello] World".lower())
""")

def test_isspace(self):
self.assertCodeExecution("""
print(b'testisspace'.isspace())
Expand Down

0 comments on commit 9e710f2

Please sign in to comment.