@@ -830,24 +830,29 @@ public PList doSplit(String self, PNone sep, PNone maxsplit) {
830
830
@ TruffleBoundary
831
831
@ Specialization
832
832
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 );
839
834
}
840
835
841
836
@ Specialization
842
837
@ TruffleBoundary
843
838
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
+
844
844
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 ();
850
854
}
855
+ getAppendNode ().execute (list , self .substring (lastEnd , self .length ()));
851
856
return list ;
852
857
}
853
858
0 commit comments