Skip to content

Commit

Permalink
Merge pull request beeware#680 from onyb/add-bytes-title
Browse files Browse the repository at this point in the history
Implement bytes.title method
  • Loading branch information
eliasdorneles authored Oct 25, 2017
2 parents ff9e131 + 83793ad commit 613023c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
30 changes: 27 additions & 3 deletions python/common/org/python/types/Bytes.java
Original file line number Diff line number Diff line change
Expand Up @@ -1000,13 +1000,17 @@ public static boolean _isalpha(byte[] input) {
return false;
}
for (byte ch : input) {
if (!(ch >= 'A' && ch <= 'Z') && !(ch >= 'a' && ch <= 'z')) {
if (!_isalpha(ch)) {
return false;
}
}
return true;
}

public static boolean _isalpha(byte ch) {
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
}

@org.python.Method(
__doc__ = "B.isalpha() -> bool\n\nReturn True if all characters in B are alphabetic\nand there is at least one character in B, False otherwise."
)
Expand Down Expand Up @@ -1074,6 +1078,7 @@ public org.python.Object isspace() {
__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("bytes.istitle has not been implemented.");
}

Expand Down Expand Up @@ -1267,11 +1272,30 @@ public org.python.Object swapcase() {
return new Bytes(_swapcase(this.value));
}

public static byte[] _title(byte[] input) {
byte[] result = new byte[input.length];
boolean flag = true;

for (int idx = 0; idx < input.length; ++idx) {
byte lc = input[idx];
if (flag && _isalpha(lc)) {
flag = false;
result[idx] = (byte) Character.toUpperCase((char) lc);
} else if (!_isalpha(lc)) {
flag = true;
result[idx] = lc;
} else {
result[idx] = (byte) Character.toLowerCase((char) lc);
}
}
return result;
}

@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("bytes.title has not been implemented.");
public org.python.Object title() {
return new Bytes(_title(this.value));
}

@org.python.Method(
Expand Down
7 changes: 7 additions & 0 deletions tests/datatypes/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,13 @@ def test_strip(self):
print(b" \n".strip())
""")

def test_title(self):
self.assertCodeExecution(r"""
print(b"abcd".title())
print(b"coca cola".title())
print(b"they are from UK, are they not?".title())
""")


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

0 comments on commit 613023c

Please sign in to comment.