5050import com .oracle .graal .python .builtins .objects .list .ListBuiltins .ListAppendNode ;
5151import com .oracle .graal .python .builtins .objects .list .PList ;
5252import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
53+ import com .oracle .graal .python .nodes .function .PythonBuiltinNode ;
5354import com .oracle .graal .python .nodes .function .builtins .PythonBinaryBuiltinNode ;
5455import com .oracle .graal .python .nodes .function .builtins .PythonTernaryBuiltinNode ;
5556import com .oracle .graal .python .nodes .function .builtins .PythonUnaryBuiltinNode ;
@@ -304,7 +305,9 @@ int start(byte[] bs) {
304305 }
305306 }
306307
307- abstract static class AbstractSplitNode extends PythonTernaryBuiltinNode {
308+ abstract static class AbstractSplitNode extends PythonBuiltinNode {
309+
310+ abstract PList execute (Object bytes , Object sep , Object maxsplit );
308311
309312 @ SuppressWarnings ("unused" )
310313 protected List <byte []> splitWhitespace (byte [] bytes , int maxsplit ) {
@@ -323,7 +326,7 @@ protected List<byte[]> splitDelimiter(byte[] bytes, byte[] sep, int maxsplit) {
323326 @ Child private BytesNodes .ToBytesNode sepToBytesNode ;
324327 @ Child private ListAppendNode appendNode ;
325328 @ Child private CastToIntegerFromIndexNode castIntNode ;
326- @ Child private SplitNode recursiveNode ;
329+ @ Child private AbstractSplitNode recursiveNode ;
327330
328331 // taken from JPython
329332 private static final int SWAP_CASE = 0x20 ;
@@ -346,6 +349,10 @@ protected List<byte[]> splitDelimiter(byte[] bytes, byte[] sep, int maxsplit) {
346349 }
347350 }
348351
352+ public static AbstractSplitNode create () {
353+ return AbstractBytesBuiltinsFactory .AbstractSplitNodeGen .create ();
354+ }
355+
349356 private ConditionProfile getIsEmptyProfile () {
350357 if (isEmptySepProfile == null ) {
351358 CompilerDirectives .transferToInterpreterAndInvalidate ();
@@ -394,10 +401,10 @@ private CastToIntegerFromIndexNode getCastIntNode() {
394401 return castIntNode ;
395402 }
396403
397- private SplitNode getRecursiveNode () {
404+ private AbstractSplitNode getRecursiveNode () {
398405 if (recursiveNode == null ) {
399406 CompilerDirectives .transferToInterpreterAndInvalidate ();
400- recursiveNode = insert (AbstractBytesBuiltinsFactory . SplitNodeFactory .create ());
407+ recursiveNode = insert (AbstractSplitNode .create ());
401408 }
402409 return recursiveNode ;
403410 }
@@ -445,75 +452,123 @@ protected static boolean isSpace(byte b) {
445452 // split()
446453 // rsplit()
447454 @ Specialization
448- PList split (Object bytes , @ SuppressWarnings ("unused" ) PNone sep , @ SuppressWarnings ("unused" ) PNone maxsplit ) {
455+ PList split (PBytes bytes , @ SuppressWarnings ("unused" ) PNone sep , @ SuppressWarnings ("unused" ) PNone maxsplit ) {
449456 byte [] splitBs = getSelfToBytesNode ().execute (bytes );
450- if (bytes instanceof PByteArray ) {
451- return getByteArrayResult (splitWhitespace (splitBs , -1 ));
452- }
453457 return getBytesResult (splitWhitespace (splitBs , -1 ));
454458 }
455459
460+ @ Specialization
461+ PList split (PByteArray bytes , @ SuppressWarnings ("unused" ) PNone sep , @ SuppressWarnings ("unused" ) PNone maxsplit ) {
462+ byte [] splitBs = getSelfToBytesNode ().execute (bytes );
463+ return getByteArrayResult (splitWhitespace (splitBs , -1 ));
464+ }
465+
456466 // split(sep=...)
457467 // rsplit(sep=...)
458468 @ Specialization (guards = "!isPNone(sep)" )
459- PList split (Object bytes , Object sep , @ SuppressWarnings ("unused" ) PNone maxsplit ) {
469+ PList split (PBytes bytes , Object sep , @ SuppressWarnings ("unused" ) PNone maxsplit ) {
470+ return split (bytes , sep , -1 );
471+ }
472+
473+ @ Specialization (guards = "!isPNone(sep)" )
474+ PList split (PByteArray bytes , Object sep , @ SuppressWarnings ("unused" ) PNone maxsplit ) {
460475 return split (bytes , sep , -1 );
461476 }
462477
463478 // split(sep=..., maxsplit=...)
464479 // rsplit(sep=..., maxsplit=...)
465480 @ Specialization (guards = "!isPNone(sep)" )
466- PList split (Object bytes , Object sep , int maxsplit ) {
481+ PList split (PBytes bytes , Object sep , int maxsplit ) {
467482 byte [] sepBs = getSepToBytesNode ().execute (sep );
468483 if (getIsEmptyProfile ().profile (sepBs .length == 0 )) {
469484 throw raise (PythonErrorType .ValueError , "empty separator" );
470485 }
471486 byte [] splitBs = getSelfToBytesNode ().execute (bytes );
472- if (bytes instanceof PByteArray ) {
473- return getByteArrayResult (splitDelimiter (splitBs , sepBs , maxsplit ));
474- }
475487 return getBytesResult (splitDelimiter (splitBs , sepBs , maxsplit ));
476488 }
477489
478490 @ Specialization (guards = "!isPNone(sep)" )
479- PList split (Object bytes , Object sep , long maxsplit ) {
491+ PList split (PBytes bytes , Object sep , long maxsplit ) {
492+ return split (bytes , sep , getIntValue (maxsplit ));
493+ }
494+
495+ @ Specialization (guards = "!isPNone(sep)" )
496+ PList split (PBytes bytes , Object sep , PInt maxsplit ) {
497+ return split (bytes , sep , getIntValue (maxsplit ));
498+ }
499+
500+ @ Specialization (guards = "!isPNone(sep)" )
501+ PList split (PBytes bytes , Object sep , Object maxsplit ) {
502+ return getRecursiveNode ().execute (bytes , sep , getCastIntNode ().execute (maxsplit ));
503+ }
504+
505+ @ Specialization (guards = "!isPNone(sep)" )
506+ PList split (PByteArray bytes , Object sep , int maxsplit ) {
507+ byte [] sepBs = getSepToBytesNode ().execute (sep );
508+ if (getIsEmptyProfile ().profile (sepBs .length == 0 )) {
509+ throw raise (PythonErrorType .ValueError , "empty separator" );
510+ }
511+ byte [] splitBs = getSelfToBytesNode ().execute (bytes );
512+ return getByteArrayResult (splitDelimiter (splitBs , sepBs , maxsplit ));
513+ }
514+
515+ @ Specialization (guards = "!isPNone(sep)" )
516+ PList split (PByteArray bytes , Object sep , long maxsplit ) {
480517 return split (bytes , sep , getIntValue (maxsplit ));
481518 }
482519
483520 @ Specialization (guards = "!isPNone(sep)" )
484- PList split (Object bytes , Object sep , PInt maxsplit ) {
521+ PList split (PByteArray bytes , Object sep , PInt maxsplit ) {
485522 return split (bytes , sep , getIntValue (maxsplit ));
486523 }
487524
488525 @ Specialization (guards = "!isPNone(sep)" )
489- PList split (Object bytes , Object sep , Object maxsplit ) {
490- return ( PList ) getRecursiveNode ().execute (bytes , sep , getCastIntNode ().execute (maxsplit ));
526+ PList split (PByteArray bytes , Object sep , Object maxsplit ) {
527+ return getRecursiveNode ().execute (bytes , sep , getCastIntNode ().execute (maxsplit ));
491528 }
492529
493530 // split(maxsplit=...)
494531 // rsplit(maxsplit=...)
495532 @ Specialization
496- PList split (Object bytes , @ SuppressWarnings ("unused" ) PNone sep , int maxsplit ) {
533+ PList split (PBytes bytes , @ SuppressWarnings ("unused" ) PNone sep , int maxsplit ) {
497534 byte [] splitBs = getSelfToBytesNode ().execute (bytes );
498- if (bytes instanceof PByteArray ) {
499- return getByteArrayResult (splitWhitespace (splitBs , maxsplit ));
500- }
501535 return getBytesResult (splitWhitespace (splitBs , maxsplit ));
502536 }
503537
504538 @ Specialization
505- PList split (Object bytes , PNone sep , long maxsplit ) {
539+ PList split (PBytes bytes , PNone sep , long maxsplit ) {
506540 return split (bytes , sep , getIntValue (maxsplit ));
507541 }
508542
509543 @ Specialization
510- PList split (Object bytes , PNone sep , PInt maxsplit ) {
544+ PList split (PBytes bytes , PNone sep , PInt maxsplit ) {
511545 return split (bytes , sep , getIntValue (maxsplit ));
512546 }
513547
514548 @ Specialization
515- PList split (Object bytes , PNone sep , Object maxsplit ) {
516- return (PList ) getRecursiveNode ().execute (bytes , sep , getCastIntNode ().execute (maxsplit ));
549+ PList split (PBytes bytes , PNone sep , Object maxsplit ) {
550+ return getRecursiveNode ().execute (bytes , sep , getCastIntNode ().execute (maxsplit ));
551+ }
552+
553+ @ Specialization
554+ PList split (PByteArray bytes , @ SuppressWarnings ("unused" ) PNone sep , int maxsplit ) {
555+ byte [] splitBs = getSelfToBytesNode ().execute (bytes );
556+ return getByteArrayResult (splitWhitespace (splitBs , maxsplit ));
557+ }
558+
559+ @ Specialization
560+ PList split (PByteArray bytes , PNone sep , long maxsplit ) {
561+ return split (bytes , sep , getIntValue (maxsplit ));
562+ }
563+
564+ @ Specialization
565+ PList split (PByteArray bytes , PNone sep , PInt maxsplit ) {
566+ return split (bytes , sep , getIntValue (maxsplit ));
567+ }
568+
569+ @ Specialization
570+ PList split (PByteArray bytes , PNone sep , Object maxsplit ) {
571+ return getRecursiveNode ().execute (bytes , sep , getCastIntNode ().execute (maxsplit ));
517572 }
518573
519574 }
@@ -537,6 +592,8 @@ protected List<byte[]> splitWhitespace(byte[] bytes, int maxsplit) {
537592 result .add (bytes );
538593 return result ;
539594 }
595+
596+ int countSplit = maxsplit ;
540597 int p , q ; // Indexes of unsplit text and whitespace
541598
542599 // Scan over leading whitespace
@@ -545,7 +602,7 @@ protected List<byte[]> splitWhitespace(byte[] bytes, int maxsplit) {
545602
546603 // At this point if p<limit it points to the start of a word.
547604 // While we have some splits left (if maxsplit started>=0)
548- while (p < size && maxsplit -- != 0 ) {
605+ while (p < size && countSplit -- != 0 ) {
549606 // Delimit a word at p
550607 // Skip q over the non-whitespace at p
551608 for (q = p ; q < size && !isSpace (bytes [q ]); q ++) {
@@ -578,6 +635,7 @@ protected List<byte[]> splitDelimiter(byte[] bytes, byte[] sep, int maxsplit) {
578635 // should not happen, and should be threated outside this method
579636 return result ;
580637 }
638+ int countSplit = maxsplit ;
581639 int begin = 0 ;
582640
583641 outer : for (int offset = 0 ; offset < size - sep .length + 1 ; offset ++) {
@@ -594,7 +652,7 @@ protected List<byte[]> splitDelimiter(byte[] bytes, byte[] sep, int maxsplit) {
594652 }
595653 begin = offset + sep .length ;
596654 offset = begin - 1 ;
597- if (--maxsplit == 0 ) {
655+ if (--countSplit == 0 ) {
598656 break ;
599657 }
600658 }
@@ -626,6 +684,7 @@ protected List<byte[]> splitWhitespace(byte[] bytes, int maxsplit) {
626684 return result ;
627685 }
628686
687+ int countSplit = maxsplit ;
629688 int offset = 0 ;
630689
631690 int p , q ; // Indexes of unsplit text and whitespace
@@ -639,7 +698,7 @@ protected List<byte[]> splitWhitespace(byte[] bytes, int maxsplit) {
639698
640699 // At this point storage[q-1] is the rightmost non-space byte, or
641700 // q=offset if there aren't any. While we have some splits left ...
642- while (q > offset && maxsplit -- != 0 ) {
701+ while (q > offset && countSplit -- != 0 ) {
643702 // Delimit the word whose last byte is storage[q-1]
644703 // Skip p backwards over the non-whitespace
645704 for (p = q ; p > offset ; --p ) {
@@ -678,6 +737,8 @@ protected List<byte[]> splitDelimiter(byte[] bytes, byte[] sep, int maxsplit) {
678737 // should not happen, and should be threated outside this method
679738 return result ;
680739 }
740+
741+ int countSplit = maxsplit ;
681742 int end = size ;
682743
683744 outer : for (int offset = size - 1 ; offset >= 0 ; offset --) {
@@ -693,7 +754,7 @@ protected List<byte[]> splitDelimiter(byte[] bytes, byte[] sep, int maxsplit) {
693754 result .add (0 , new byte [0 ]);
694755 }
695756 end = offset ;
696- if (--maxsplit == 0 ) {
757+ if (--countSplit == 0 ) {
697758 break ;
698759 }
699760 }
0 commit comments