@@ -709,15 +709,30 @@ Object doGeneric(@SuppressWarnings("unused") Object self) {
709
709
}
710
710
}
711
711
712
- @ Builtin (name = "lstrip" , minNumOfPositionalArgs = 1 , maxNumOfPositionalArgs = 2 , keywordArguments = {"bytes" })
713
- @ GenerateNodeFactory
714
- abstract static class LStripNode extends PythonBinaryBuiltinNode {
712
+ abstract static class AStripNode extends PythonBinaryBuiltinNode {
713
+ int mod () {
714
+ throw new RuntimeException ();
715
+ }
716
+
717
+ int stop (@ SuppressWarnings ("unused" ) byte [] bs ) {
718
+ throw new RuntimeException ();
719
+ }
720
+
721
+ int start (@ SuppressWarnings ("unused" ) byte [] bs ) {
722
+ throw new RuntimeException ();
723
+ }
724
+
725
+ PByteArray newBytesFrom (@ SuppressWarnings ("unused" ) byte [] bs , @ SuppressWarnings ("unused" ) int i ) {
726
+ throw new RuntimeException ();
727
+ }
728
+
715
729
@ Specialization
716
730
PByteArray strip (PByteArray self , @ SuppressWarnings ("unused" ) PNone bytes ,
717
731
@ Cached ("create()" ) BytesNodes .ToBytesNode toBytesNode ) {
718
732
byte [] bs = toBytesNode .execute (self );
719
- int i = 0 ;
720
- for (i = 0 ; i < bs .length ; i ++) {
733
+ int i = start (bs );
734
+ int stop = stop (bs );
735
+ for (; i != stop ; i += mod ()) {
721
736
if (!isWhitespace (bs [i ])) {
722
737
break ;
723
738
}
@@ -736,8 +751,9 @@ PByteArray strip(PByteArray self, PBytes bytes,
736
751
@ Cached ("create()" ) BytesNodes .ToBytesNode otherToBytesNode ) {
737
752
byte [] stripBs = selfToBytesNode .execute (bytes );
738
753
byte [] bs = otherToBytesNode .execute (self );
739
- int i = 0 ;
740
- outer : for (i = 0 ; i < bs .length ; i ++) {
754
+ int i = start (bs );
755
+ int stop = stop (bs );
756
+ outer : for (; i != stop ; i += mod ()) {
741
757
for (byte b : stripBs ) {
742
758
if (b == bs [i ]) {
743
759
continue outer ;
@@ -748,7 +764,13 @@ PByteArray strip(PByteArray self, PBytes bytes,
748
764
return newBytesFrom (bs , i );
749
765
}
750
766
751
- private PByteArray newBytesFrom (byte [] bs , int i ) {
767
+ }
768
+
769
+ @ Builtin (name = "lstrip" , minNumOfPositionalArgs = 1 , maxNumOfPositionalArgs = 2 , keywordArguments = {"bytes" })
770
+ @ GenerateNodeFactory
771
+ abstract static class LStripNode extends AStripNode {
772
+ @ Override
773
+ PByteArray newBytesFrom (byte [] bs , int i ) {
752
774
byte [] out ;
753
775
if (i != 0 ) {
754
776
int len = bs .length - i ;
@@ -759,52 +781,30 @@ private PByteArray newBytesFrom(byte[] bs, int i) {
759
781
}
760
782
return factory ().createByteArray (out );
761
783
}
762
- }
763
784
764
- @ Builtin (name = "rstrip" , minNumOfPositionalArgs = 1 , maxNumOfPositionalArgs = 2 , keywordArguments = {"bytes" })
765
- @ GenerateNodeFactory
766
- abstract static class RStripNode extends PythonBinaryBuiltinNode {
767
- @ Specialization
768
- PByteArray strip (PByteArray self , @ SuppressWarnings ("unused" ) PNone bytes ,
769
- @ Cached ("create()" ) BytesNodes .ToBytesNode toBytesNode ) {
770
- byte [] bs = toBytesNode .execute (self );
771
- int len = bs .length ;
772
- for (int i = bs .length - 1 ; i >= 0 ; i --) {
773
- if (isWhitespace (bs [i ])) {
774
- len --;
775
- } else {
776
- break ;
777
- }
778
- }
779
- return newBytesUpTo (bs , len );
785
+ @ Override
786
+ int mod () {
787
+ return 1 ;
780
788
}
781
789
782
- @ TruffleBoundary
783
- private static boolean isWhitespace (byte b ) {
784
- return Character . isWhitespace ( b ) ;
790
+ @ Override
791
+ int stop (byte [] bs ) {
792
+ return bs . length ;
785
793
}
786
794
787
- @ Specialization
788
- PByteArray strip (PByteArray self , PBytes bytes ,
789
- @ Cached ("create()" ) BytesNodes .ToBytesNode selfToBytesNode ,
790
- @ Cached ("create()" ) BytesNodes .ToBytesNode otherToBytesNode ) {
791
- byte [] stripBs = selfToBytesNode .execute (bytes );
792
- byte [] bs = otherToBytesNode .execute (self );
793
- int len = bs .length ;
794
- outer : for (int i = bs .length - 1 ; i >= 0 ; i --) {
795
- for (byte b : stripBs ) {
796
- if (b == bs [i ]) {
797
- len --;
798
- continue outer ;
799
- }
800
- }
801
- break ;
802
- }
803
- return newBytesUpTo (bs , len );
795
+ @ Override
796
+ int start (byte [] bs ) {
797
+ return 0 ;
804
798
}
799
+ }
805
800
806
- private PByteArray newBytesUpTo (byte [] bs , int len ) {
801
+ @ Builtin (name = "rstrip" , minNumOfPositionalArgs = 1 , maxNumOfPositionalArgs = 2 , keywordArguments = {"bytes" })
802
+ @ GenerateNodeFactory
803
+ abstract static class RStripNode extends AStripNode {
804
+ @ Override
805
+ PByteArray newBytesFrom (byte [] bs , int i ) {
807
806
byte [] out ;
807
+ int len = i + 1 ;
808
808
if (len != bs .length ) {
809
809
out = new byte [len ];
810
810
System .arraycopy (bs , 0 , out , 0 , len );
@@ -813,5 +813,20 @@ private PByteArray newBytesUpTo(byte[] bs, int len) {
813
813
}
814
814
return factory ().createByteArray (out );
815
815
}
816
+
817
+ @ Override
818
+ int mod () {
819
+ return -1 ;
820
+ }
821
+
822
+ @ Override
823
+ int stop (byte [] bs ) {
824
+ return -1 ;
825
+ }
826
+
827
+ @ Override
828
+ int start (byte [] bs ) {
829
+ return bs .length - 1 ;
830
+ }
816
831
}
817
832
}
0 commit comments