Skip to content

Commit ee64126

Browse files
committed
don't use regex matching for str.split
1 parent 385993c commit ee64126

File tree

1 file changed

+16
-11
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str

1 file changed

+16
-11
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/str/StringBuiltins.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -830,24 +830,29 @@ public PList doSplit(String self, PNone sep, PNone maxsplit) {
830830
@TruffleBoundary
831831
@Specialization
832832
public PList doSplit(String self, String sep, PNone maxsplit) {
833-
PList list = factory().createList();
834-
String[] strs = self.split(Pattern.quote(sep));
835-
for (String s : strs) {
836-
getAppendNode().execute(list, s);
837-
}
838-
return list;
833+
return doSplit(self, sep, -1);
839834
}
840835

841836
@Specialization
842837
@TruffleBoundary
843838
public PList doSplit(String self, String sep, int maxsplit) {
839+
if (sep.isEmpty()) {
840+
throw raise(ValueError, "empty separator");
841+
}
842+
int splits = maxsplit == -1 ? Integer.MAX_VALUE : maxsplit;
843+
844844
PList list = factory().createList();
845-
// Python gives the maximum number of splits, Java wants the max number of resulting
846-
// parts
847-
String[] strs = self.split(Pattern.quote(sep), maxsplit + 1);
848-
for (String s : strs) {
849-
getAppendNode().execute(list, s);
845+
int lastEnd = 0;
846+
while (splits > 0) {
847+
int nextIndex = self.indexOf(sep, lastEnd);
848+
if (nextIndex == -1) {
849+
break;
850+
}
851+
splits--;
852+
getAppendNode().execute(list, self.substring(lastEnd, nextIndex));
853+
lastEnd = nextIndex + sep.length();
850854
}
855+
getAppendNode().execute(list, self.substring(lastEnd, self.length()));
851856
return list;
852857
}
853858

0 commit comments

Comments
 (0)