@@ -709,17 +709,68 @@ Object doGeneric(@SuppressWarnings("unused") Object self) {
709
709
}
710
710
}
711
711
712
- @ Builtin (name = "rstrip " , minNumOfPositionalArgs = 1 , maxNumOfPositionalArgs = 2 , keywordArguments = {"bytes" })
712
+ @ Builtin (name = "lstrip " , minNumOfPositionalArgs = 1 , maxNumOfPositionalArgs = 2 , keywordArguments = {"bytes" })
713
713
@ GenerateNodeFactory
714
- abstract static class RStripNode extends PythonBuiltinNode {
714
+ abstract static class LStripNode extends PythonBinaryBuiltinNode {
715
715
@ Specialization
716
+ PByteArray strip (PByteArray self , @ SuppressWarnings ("unused" ) PNone bytes ,
717
+ @ Cached ("create()" ) BytesNodes .ToBytesNode toBytesNode ) {
718
+ byte [] bs = toBytesNode .execute (self );
719
+ int i = 0 ;
720
+ for (i = 0 ; i < bs .length ; i ++) {
721
+ if (!isWhitespace (bs [i ])) {
722
+ break ;
723
+ }
724
+ }
725
+ return newBytesFrom (bs , i );
726
+ }
727
+
716
728
@ TruffleBoundary
729
+ private static boolean isWhitespace (byte b ) {
730
+ return Character .isWhitespace (b );
731
+ }
732
+
733
+ @ Specialization
734
+ PByteArray strip (PByteArray self , PBytes bytes ,
735
+ @ Cached ("create()" ) BytesNodes .ToBytesNode selfToBytesNode ,
736
+ @ Cached ("create()" ) BytesNodes .ToBytesNode otherToBytesNode ) {
737
+ byte [] stripBs = selfToBytesNode .execute (bytes );
738
+ byte [] bs = otherToBytesNode .execute (self );
739
+ int i = 0 ;
740
+ outer : for (i = 0 ; i < bs .length ; i ++) {
741
+ for (byte b : stripBs ) {
742
+ if (b == bs [i ]) {
743
+ continue outer ;
744
+ }
745
+ }
746
+ break ;
747
+ }
748
+ return newBytesFrom (bs , i );
749
+ }
750
+
751
+ private PByteArray newBytesFrom (byte [] bs , int i ) {
752
+ byte [] out ;
753
+ if (i != 0 ) {
754
+ int len = bs .length - i ;
755
+ out = new byte [len ];
756
+ System .arraycopy (bs , i , out , 0 , len );
757
+ } else {
758
+ out = bs ;
759
+ }
760
+ return factory ().createByteArray (out );
761
+ }
762
+ }
763
+
764
+ @ Builtin (name = "rstrip" , minNumOfPositionalArgs = 1 , maxNumOfPositionalArgs = 2 , keywordArguments = {"bytes" })
765
+ @ GenerateNodeFactory
766
+ abstract static class RStripNode extends PythonBinaryBuiltinNode {
767
+ @ Specialization
717
768
PByteArray strip (PByteArray self , @ SuppressWarnings ("unused" ) PNone bytes ,
718
769
@ Cached ("create()" ) BytesNodes .ToBytesNode toBytesNode ) {
719
770
byte [] bs = toBytesNode .execute (self );
720
771
int len = bs .length ;
721
772
for (int i = bs .length - 1 ; i >= 0 ; i --) {
722
- if (Character . isWhitespace (bs [i ])) {
773
+ if (isWhitespace (bs [i ])) {
723
774
len --;
724
775
} else {
725
776
break ;
@@ -728,8 +779,12 @@ PByteArray strip(PByteArray self, @SuppressWarnings("unused") PNone bytes,
728
779
return newBytesUpTo (bs , len );
729
780
}
730
781
731
- @ Specialization
732
782
@ TruffleBoundary
783
+ private static boolean isWhitespace (byte b ) {
784
+ return Character .isWhitespace (b );
785
+ }
786
+
787
+ @ Specialization
733
788
PByteArray strip (PByteArray self , PBytes bytes ,
734
789
@ Cached ("create()" ) BytesNodes .ToBytesNode selfToBytesNode ,
735
790
@ Cached ("create()" ) BytesNodes .ToBytesNode otherToBytesNode ) {
@@ -740,10 +795,10 @@ PByteArray strip(PByteArray self, PBytes bytes,
740
795
for (byte b : stripBs ) {
741
796
if (b == bs [i ]) {
742
797
len --;
743
- } else {
744
- break outer ;
798
+ continue outer ;
745
799
}
746
800
}
801
+ break ;
747
802
}
748
803
return newBytesUpTo (bs , len );
749
804
}
@@ -759,5 +814,4 @@ private PByteArray newBytesUpTo(byte[] bs, int len) {
759
814
return factory ().createByteArray (out );
760
815
}
761
816
}
762
-
763
817
}
0 commit comments