Skip to content

Commit

Permalink
Implement bytearray.title and bytearray.istitle
Browse files Browse the repository at this point in the history
  • Loading branch information
onyb committed Oct 27, 2017
1 parent 859637f commit 318de78
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 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 @@ -788,8 +788,8 @@ public org.python.Object isspace() {
@org.python.Method(
__doc__ = "B.istitle() -> bool\n\nReturn True if B is a titlecased string and there is at least one\ncharacter in B, i.e. uppercase characters may only follow uncased\ncharacters and lowercase characters only cased ones. Return False\notherwise."
)
public org.python.Object istitle(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.istitle has not been implemented.");
public org.python.Object istitle() {
return new Bool(Bytes._istitle(this.value));
}

@org.python.Method(
Expand Down Expand Up @@ -928,8 +928,8 @@ public org.python.Object swapcase(java.util.List<org.python.Object> args, java.u
@org.python.Method(
__doc__ = "B.title() -> copy of B\n\nReturn a titlecased version of B, i.e. ASCII words start with uppercase\ncharacters, all remaining cased characters have lowercase."
)
public org.python.Object title(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.title has not been implemented.");
public org.python.Object title() {
return new ByteArray(Bytes._title(this.value));
}

@org.python.Method(
Expand Down
26 changes: 15 additions & 11 deletions python/common/org/python/types/Bytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -1074,23 +1074,27 @@ public org.python.Object isspace() {
return new Bool(_isspace(this.value));
}

@org.python.Method(
__doc__ = "B.istitle() -> bool\n\nReturn True if B is a titlecased string and there is at least one\ncharacter in B, i.e. uppercase characters may only follow uncased\ncharacters and lowercase characters only cased ones. Return False\notherwise."
)
public org.python.Object istitle() {
if (this.value.length == 0) {
return new org.python.types.Bool(false);
public static boolean _istitle(byte[] input) {
if (input.length == 0) {
return false;
}

if (Arrays.equals(this.value, _title(this.value))) {
for (int idx = 0; idx < this.value.length; ++idx) {
if (_isalpha(this.value[idx])) {
return new org.python.types.Bool(true);
if (Arrays.equals(input, _title(input))) {
for (int idx = 0; idx < input.length; ++idx) {
if (_isalpha(input[idx])) {
return true;
}
}
}

return new org.python.types.Bool(false);
return false;
}

@org.python.Method(
__doc__ = "B.istitle() -> bool\n\nReturn True if B is a titlecased string and there is at least one\ncharacter in B, i.e. uppercase characters may only follow uncased\ncharacters and lowercase characters only cased ones. Return False\notherwise."
)
public org.python.Object istitle() {
return new org.python.types.Bool(_istitle(this.value));
}

public static boolean _isupper(byte[] input) {
Expand Down
24 changes: 24 additions & 0 deletions tests/datatypes/test_bytearray.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,30 @@ def test_center(self):
print(bytearray(b'pybee').center(True, bytearray(b'a')))
""")

def test_title(self):
self.assertCodeExecution(r"""
print(bytearray(b"").title())
print(bytearray(b"abcd").title())
print(bytearray(b"NOT").title())
print(bytearray(b"coca cola").title())
print(bytearray(b"they are from UK, are they not?").title())
print(bytearray(b'/@.').title())
print(bytearray(b'\x46\x55\x43\x4B').title())
print(bytearray(b"py.bee").title())
""")

def test_istitle(self):
self.assertCodeExecution(r"""
print(bytearray(b"").istitle())
print(bytearray(b"abcd").istitle())
print(bytearray(b"NOT").istitle())
print(bytearray(b"coca cola").istitle())
print(bytearray(b"they are from UK, are they not?").istitle())
print(bytearray(b'/@.').istitle())
print(bytearray(b'\x46\x55\x43\x4B').istitle())
print(bytearray(b"py.bee").title())
""")


class UnaryBytearrayOperationTests(UnaryOperationTestCase, TranspileTestCase):
data_type = 'bytearray'
Expand Down

0 comments on commit 318de78

Please sign in to comment.