Skip to content

Commit

Permalink
Fix str.isspace and str.split
Browse files Browse the repository at this point in the history
  • Loading branch information
jessicapaz committed Oct 12, 2017
1 parent ef5ba51 commit 9c6cea0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
10 changes: 6 additions & 4 deletions python/common/org/python/types/Str.java
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ public org.python.Object isspace() {
return new org.python.types.Bool(false);
}
for (char ch : this.value.toCharArray()) {
if (" \t\n\r".indexOf(ch) == -1) {
if (!Character.isWhitespace(ch)) {
return new org.python.types.Bool(false);
}
}
Expand Down Expand Up @@ -1394,8 +1394,10 @@ public org.python.Object split(org.python.Object sep, org.python.Object maxsplit
}
}

java.lang.String value = this.value.toString();
if (sep == null) {
sep = new org.python.types.Str(" ");
value = value.trim();
sep = new org.python.types.Str("\\s+");
} else if (!(sep instanceof org.python.types.Str)) {
if (org.Python.VERSION < 0x03060000) {
throw new org.python.exceptions.TypeError("Can't convert '" + sep.typeName() + "' object to str implicitly");
Expand All @@ -1406,10 +1408,10 @@ public org.python.Object split(org.python.Object sep, org.python.Object maxsplit

java.lang.String[] result;
if (maxsplit == null) {
result = this.value.toString().split(((org.python.types.Str) sep).toString());
result = value.split(((org.python.types.Str) sep).toString());
} else {
int number = java.lang.Integer.parseInt(maxsplit.toString());
result = this.value.toString().split(((org.python.types.Str) sep).toString(), number + 1);
result = value.split(((org.python.types.Str) sep).toString(), number + 1);
}
org.python.types.List result_list = new org.python.types.List();
for (java.lang.String w : result) {
Expand Down
9 changes: 5 additions & 4 deletions tests/datatypes/test_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def test_isdigit(self):
""")

def test_isspace(self):
self.assertCodeExecution("""
for s in [''' \t \r''', ' ', '\t\tnope\t\t', '']:
self.assertCodeExecution(r"""
for s in ['\x1f \v \f \n \t \r', ' ', '\t\tnope\t\t', '']:
print(s.isspace())
""")

Expand Down Expand Up @@ -231,8 +231,8 @@ def test_case_changes(self):
""")

def test_split(self):
self.assertCodeExecution("""
for s in ['hello, world', 'HEllo, WORLD', 'átomo', '']:
self.assertCodeExecution(r"""
for s in ['\vhello, world', '\nHEllo, WORLD\f', 'átomo', '']:
print(s.split())
print(s.split("o"))
print(s.split(maxsplit=2))
Expand All @@ -241,6 +241,7 @@ def test_split(self):
print(s.split(5))
except TypeError as err:
print(err)
""")

def test_index(self):
Expand Down

0 comments on commit 9c6cea0

Please sign in to comment.