Skip to content

Commit

Permalink
Merge pull request beeware#714 from hcheng761/master
Browse files Browse the repository at this point in the history
Str.java "rsplit" method
  • Loading branch information
eliasdorneles authored Dec 19, 2017
2 parents 4242b2e + a11f4ca commit 917e54a
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 4 deletions.
125 changes: 121 additions & 4 deletions python/common/org/python/types/Str.java
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ public org.python.Object __getitem__(org.python.Object index) {
throw new org.python.exceptions.IndexError("string index out of range");
} else {
if (slice) {
if (this.value.length() < 2){
if (this.value.length() < 2) {
throw new org.python.exceptions.IndexError("string index out of range");
} else {
sliced = this.value.substring(1, 2);
Expand Down Expand Up @@ -1345,10 +1345,127 @@ public org.python.Object rpartition(org.python.types.Object sep) {
"delimiter string, starting at the end of the string and\n" +
"working to the front. If maxsplit is given, at most maxsplit\n" +
"splits are done. If sep is not specified, any whitespace string\n" +
"is a separator.\n"
"is a separator.\n",
default_args = {"sep", "maxsplit"}
)
public org.python.Object rsplit() {
throw new org.python.exceptions.NotImplementedError("rsplit() has not been implemented.");
public org.python.Object rsplit(org.python.Object sep, org.python.Object maxsplit) {
if (maxsplit == null) {
return this.split(sep, null);
}
if (this.value.isEmpty()) { //handles empty strings
if (sep == null) {
if (maxsplit instanceof org.python.types.Int) {
return new org.python.types.List();
}
throw new org.python.exceptions.TypeError("'" + maxsplit.typeName() + "' cannot be interpreted as an integer");
} else if (sep instanceof org.python.types.Str) {
if (maxsplit instanceof org.python.types.Int) {
org.python.types.List result_list = new org.python.types.List();
result_list.append(new org.python.types.Str(""));
return result_list;
}
throw new org.python.exceptions.TypeError("'" + maxsplit.typeName() + "' cannot be interpreted as an integer");
}
}

java.lang.String value = this.value.toString();
java.lang.String sepStr = "";
java.lang.Boolean wspace = false;
if (sep == null) {
wspace = true;
} 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");
} else {
throw new org.python.exceptions.TypeError("must be str or None, not " + sep.typeName());
}
} else {
sepStr = ((org.python.types.Str) sep).toString();
}

org.python.types.List result_list = new org.python.types.List();
if (wspace) { //handles whitespace delimiters (default case)
value = value.replaceAll("\\s++$", ""); //trim trailing spaces
int temp = value.length() - 1, j = value.length() - 1;
int count = 0, number = 0;
for (int i = 0; i < value.length(); i++) { //count number of whitespace sequences
if (value.charAt(i) == ' ') {
while (value.charAt(i) == ' ') {
i++;
}
count++;
}
}
if (java.lang.Integer.parseInt(maxsplit.toString()) >= 0) {
number = java.lang.Integer.parseInt(maxsplit.toString()); //check for positive maxsplit
} else {
number = count;
}
int numEnd = 0;
if (number > count) { //prevent going out of bounds later
numEnd = number - count;
}
for (int i = number; i > numEnd; i--) {
java.lang.StringBuilder sb = new StringBuilder();
for (j = temp; j >= 0; j--) {
if (value.charAt(j) == ' ') {
while (value.charAt(j) == ' ' && j != 0) {
j--;
}
temp = j;
result_list.insert(new org.python.types.Int(0), new org.python.types.Str(sb.toString()));
break;
} else {
sb.insert(0, value.charAt(j));
}
}
}
if (j != 0) {
result_list.insert(new org.python.types.Int(0), new org.python.types.Str(value.substring(0, j + 1)));
} else if (j == 0 && value.charAt(j) != ' ') {
result_list.insert(new org.python.types.Int(0), new org.python.types.Str(value.substring(0, 1)));
}
} else { //handles non-whitespace and non-default whitespace delimiters (Ex. rsplit("e",12) rsplit(" ",2))
int lastIndex = 0, count = 0, number = 0;
while (lastIndex != -1) {
lastIndex = value.indexOf(sepStr, lastIndex);
if (lastIndex != -1) {
count++;
lastIndex += sepStr.length();
}
}
if (count == 0) {
result_list.insert(new org.python.types.Int(0), new org.python.types.Str(value)); //if no matches found, simply return array containing original string
} else {
int numEnd = 0;
if (java.lang.Integer.parseInt(maxsplit.toString()) >= 0) {
number = java.lang.Integer.parseInt(maxsplit.toString());
} else {
number = count;
}
if (number > count) {
numEnd = number - count;
}
int temp = value.length(), j = 0;
for (int i = number; i > numEnd; i--) {
for (j = temp; j >= 0; j--) {
if (value.substring(j, temp).contains(sepStr)) {
if (i == 0) { //prevent going over string bounds
result_list.insert(new org.python.types.Int(0), new org.python.types.Str(value.substring(j + sepStr.length(), value.length())));
temp = j;
j--;
} else {
result_list.insert(new org.python.types.Int(0), new org.python.types.Str(value.substring(j + sepStr.length(), temp)));
temp = j;
}
break;
}
}
}
result_list.insert(new org.python.types.Int(0), new org.python.types.Str(value.substring(0, j)));
}
}
return result_list;
}

@org.python.Method(
Expand Down
14 changes: 14 additions & 0 deletions tests/datatypes/test_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,20 @@ def __str__(self):
except TypeError as err:
print(err)
""")
def test_rsplit(self):
self.assertCodeExecution(r"""
for s in ['This is for rsplit', ' eeed d ded eded ', 'átomo', '',' t e s t i n g r s p l i t ', 'a b c']:
print(s.rsplit())
print(s.rsplit("e"))
print(s.rsplit(maxsplit=2))
print(s.rsplit(maxsplit=-5))
print(s.rsplit("e",-9))
print(s.rsplit(" ", -10))
try:
print(s.split(2))
except TypeError as err:
print(err)
""")

def test_isnumeric(self):
self.assertCodeExecution("""
Expand Down

0 comments on commit 917e54a

Please sign in to comment.