50
50
import com .oracle .graal .python .builtins .objects .list .ListBuiltins .ListAppendNode ;
51
51
import com .oracle .graal .python .builtins .objects .list .PList ;
52
52
import com .oracle .graal .python .nodes .function .PythonBuiltinBaseNode ;
53
+ import com .oracle .graal .python .nodes .function .PythonBuiltinNode ;
53
54
import com .oracle .graal .python .nodes .function .builtins .PythonBinaryBuiltinNode ;
54
55
import com .oracle .graal .python .nodes .function .builtins .PythonTernaryBuiltinNode ;
55
56
import com .oracle .graal .python .nodes .function .builtins .PythonUnaryBuiltinNode ;
@@ -304,7 +305,9 @@ int start(byte[] bs) {
304
305
}
305
306
}
306
307
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 );
308
311
309
312
@ SuppressWarnings ("unused" )
310
313
protected List <byte []> splitWhitespace (byte [] bytes , int maxsplit ) {
@@ -323,7 +326,7 @@ protected List<byte[]> splitDelimiter(byte[] bytes, byte[] sep, int maxsplit) {
323
326
@ Child private BytesNodes .ToBytesNode sepToBytesNode ;
324
327
@ Child private ListAppendNode appendNode ;
325
328
@ Child private CastToIntegerFromIndexNode castIntNode ;
326
- @ Child private SplitNode recursiveNode ;
329
+ @ Child private AbstractSplitNode recursiveNode ;
327
330
328
331
// taken from JPython
329
332
private static final int SWAP_CASE = 0x20 ;
@@ -346,6 +349,10 @@ protected List<byte[]> splitDelimiter(byte[] bytes, byte[] sep, int maxsplit) {
346
349
}
347
350
}
348
351
352
+ public static AbstractSplitNode create () {
353
+ return AbstractBytesBuiltinsFactory .AbstractSplitNodeGen .create ();
354
+ }
355
+
349
356
private ConditionProfile getIsEmptyProfile () {
350
357
if (isEmptySepProfile == null ) {
351
358
CompilerDirectives .transferToInterpreterAndInvalidate ();
@@ -394,10 +401,10 @@ private CastToIntegerFromIndexNode getCastIntNode() {
394
401
return castIntNode ;
395
402
}
396
403
397
- private SplitNode getRecursiveNode () {
404
+ private AbstractSplitNode getRecursiveNode () {
398
405
if (recursiveNode == null ) {
399
406
CompilerDirectives .transferToInterpreterAndInvalidate ();
400
- recursiveNode = insert (AbstractBytesBuiltinsFactory . SplitNodeFactory .create ());
407
+ recursiveNode = insert (AbstractSplitNode .create ());
401
408
}
402
409
return recursiveNode ;
403
410
}
@@ -445,75 +452,123 @@ protected static boolean isSpace(byte b) {
445
452
// split()
446
453
// rsplit()
447
454
@ 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 ) {
449
456
byte [] splitBs = getSelfToBytesNode ().execute (bytes );
450
- if (bytes instanceof PByteArray ) {
451
- return getByteArrayResult (splitWhitespace (splitBs , -1 ));
452
- }
453
457
return getBytesResult (splitWhitespace (splitBs , -1 ));
454
458
}
455
459
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
+
456
466
// split(sep=...)
457
467
// rsplit(sep=...)
458
468
@ 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 ) {
460
475
return split (bytes , sep , -1 );
461
476
}
462
477
463
478
// split(sep=..., maxsplit=...)
464
479
// rsplit(sep=..., maxsplit=...)
465
480
@ Specialization (guards = "!isPNone(sep)" )
466
- PList split (Object bytes , Object sep , int maxsplit ) {
481
+ PList split (PBytes bytes , Object sep , int maxsplit ) {
467
482
byte [] sepBs = getSepToBytesNode ().execute (sep );
468
483
if (getIsEmptyProfile ().profile (sepBs .length == 0 )) {
469
484
throw raise (PythonErrorType .ValueError , "empty separator" );
470
485
}
471
486
byte [] splitBs = getSelfToBytesNode ().execute (bytes );
472
- if (bytes instanceof PByteArray ) {
473
- return getByteArrayResult (splitDelimiter (splitBs , sepBs , maxsplit ));
474
- }
475
487
return getBytesResult (splitDelimiter (splitBs , sepBs , maxsplit ));
476
488
}
477
489
478
490
@ 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 ) {
480
517
return split (bytes , sep , getIntValue (maxsplit ));
481
518
}
482
519
483
520
@ Specialization (guards = "!isPNone(sep)" )
484
- PList split (Object bytes , Object sep , PInt maxsplit ) {
521
+ PList split (PByteArray bytes , Object sep , PInt maxsplit ) {
485
522
return split (bytes , sep , getIntValue (maxsplit ));
486
523
}
487
524
488
525
@ 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 ));
491
528
}
492
529
493
530
// split(maxsplit=...)
494
531
// rsplit(maxsplit=...)
495
532
@ Specialization
496
- PList split (Object bytes , @ SuppressWarnings ("unused" ) PNone sep , int maxsplit ) {
533
+ PList split (PBytes bytes , @ SuppressWarnings ("unused" ) PNone sep , int maxsplit ) {
497
534
byte [] splitBs = getSelfToBytesNode ().execute (bytes );
498
- if (bytes instanceof PByteArray ) {
499
- return getByteArrayResult (splitWhitespace (splitBs , maxsplit ));
500
- }
501
535
return getBytesResult (splitWhitespace (splitBs , maxsplit ));
502
536
}
503
537
504
538
@ Specialization
505
- PList split (Object bytes , PNone sep , long maxsplit ) {
539
+ PList split (PBytes bytes , PNone sep , long maxsplit ) {
506
540
return split (bytes , sep , getIntValue (maxsplit ));
507
541
}
508
542
509
543
@ Specialization
510
- PList split (Object bytes , PNone sep , PInt maxsplit ) {
544
+ PList split (PBytes bytes , PNone sep , PInt maxsplit ) {
511
545
return split (bytes , sep , getIntValue (maxsplit ));
512
546
}
513
547
514
548
@ 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 ));
517
572
}
518
573
519
574
}
@@ -537,6 +592,8 @@ protected List<byte[]> splitWhitespace(byte[] bytes, int maxsplit) {
537
592
result .add (bytes );
538
593
return result ;
539
594
}
595
+
596
+ int countSplit = maxsplit ;
540
597
int p , q ; // Indexes of unsplit text and whitespace
541
598
542
599
// Scan over leading whitespace
@@ -545,7 +602,7 @@ protected List<byte[]> splitWhitespace(byte[] bytes, int maxsplit) {
545
602
546
603
// At this point if p<limit it points to the start of a word.
547
604
// While we have some splits left (if maxsplit started>=0)
548
- while (p < size && maxsplit -- != 0 ) {
605
+ while (p < size && countSplit -- != 0 ) {
549
606
// Delimit a word at p
550
607
// Skip q over the non-whitespace at p
551
608
for (q = p ; q < size && !isSpace (bytes [q ]); q ++) {
@@ -578,6 +635,7 @@ protected List<byte[]> splitDelimiter(byte[] bytes, byte[] sep, int maxsplit) {
578
635
// should not happen, and should be threated outside this method
579
636
return result ;
580
637
}
638
+ int countSplit = maxsplit ;
581
639
int begin = 0 ;
582
640
583
641
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) {
594
652
}
595
653
begin = offset + sep .length ;
596
654
offset = begin - 1 ;
597
- if (--maxsplit == 0 ) {
655
+ if (--countSplit == 0 ) {
598
656
break ;
599
657
}
600
658
}
@@ -626,6 +684,7 @@ protected List<byte[]> splitWhitespace(byte[] bytes, int maxsplit) {
626
684
return result ;
627
685
}
628
686
687
+ int countSplit = maxsplit ;
629
688
int offset = 0 ;
630
689
631
690
int p , q ; // Indexes of unsplit text and whitespace
@@ -639,7 +698,7 @@ protected List<byte[]> splitWhitespace(byte[] bytes, int maxsplit) {
639
698
640
699
// At this point storage[q-1] is the rightmost non-space byte, or
641
700
// 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 ) {
643
702
// Delimit the word whose last byte is storage[q-1]
644
703
// Skip p backwards over the non-whitespace
645
704
for (p = q ; p > offset ; --p ) {
@@ -678,6 +737,8 @@ protected List<byte[]> splitDelimiter(byte[] bytes, byte[] sep, int maxsplit) {
678
737
// should not happen, and should be threated outside this method
679
738
return result ;
680
739
}
740
+
741
+ int countSplit = maxsplit ;
681
742
int end = size ;
682
743
683
744
outer : for (int offset = size - 1 ; offset >= 0 ; offset --) {
@@ -693,7 +754,7 @@ protected List<byte[]> splitDelimiter(byte[] bytes, byte[] sep, int maxsplit) {
693
754
result .add (0 , new byte [0 ]);
694
755
}
695
756
end = offset ;
696
- if (--maxsplit == 0 ) {
757
+ if (--countSplit == 0 ) {
697
758
break ;
698
759
}
699
760
}
0 commit comments