186
186
import com .oracle .truffle .api .strings .TruffleString .CodePointLengthNode ;
187
187
import com .oracle .truffle .api .strings .TruffleString .CodeRange ;
188
188
import com .oracle .truffle .api .strings .TruffleString .Encoding ;
189
+ import com .oracle .truffle .api .strings .TruffleString .IndexOfStringNode ;
190
+ import com .oracle .truffle .api .strings .TruffleString .LastIndexOfStringNode ;
189
191
import com .oracle .truffle .api .strings .TruffleStringBuilder ;
190
192
import com .oracle .truffle .api .strings .TruffleStringIterator ;
191
193
@@ -752,20 +754,7 @@ protected ArgumentClinicProvider getArgumentClinic() {
752
754
static int rfind (TruffleString self , TruffleString sub , int start , int end ,
753
755
@ Shared ("cpLen" ) @ Cached TruffleString .CodePointLengthNode codePointLengthNode ,
754
756
@ Shared ("lastIndexOf" ) @ Cached TruffleString .LastIndexOfStringNode lastIndexOfStringNode ) {
755
- int cpLen = codePointLengthNode .execute (self , TS_ENCODING );
756
- int cpStart = adjustStartIndex (start , cpLen );
757
- int cpEnd = adjustEndIndex (end , cpLen );
758
- if (sub .isEmpty () && cpStart == cpLen ) {
759
- return cpLen ;
760
- }
761
- if (cpStart >= cpLen ) {
762
- return -1 ;
763
- }
764
- int idx = lastIndexOfStringNode .execute (self , sub , cpEnd , cpStart , TS_ENCODING );
765
- if (idx < 0 ) {
766
- return -1 ;
767
- }
768
- return idx ;
757
+ return lastIndexOf (self , sub , start , end , codePointLengthNode , lastIndexOfStringNode );
769
758
}
770
759
771
760
@ Specialization
@@ -795,20 +784,7 @@ protected ArgumentClinicProvider getArgumentClinic() {
795
784
static int find (TruffleString self , TruffleString sub , int start , int end ,
796
785
@ Shared ("cpLen" ) @ Cached TruffleString .CodePointLengthNode codePointLengthNode ,
797
786
@ Shared ("indexOf" ) @ Cached TruffleString .IndexOfStringNode indexOfStringNode ) {
798
- int cpLen = codePointLengthNode .execute (self , TS_ENCODING );
799
- int cpStart = adjustStartIndex (start , cpLen );
800
- int cpEnd = adjustEndIndex (end , cpLen );
801
- if (sub .isEmpty () && cpStart == cpLen ) {
802
- return cpLen ;
803
- }
804
- if (cpStart >= cpLen ) {
805
- return -1 ;
806
- }
807
- int idx = indexOfStringNode .execute (self , sub , cpStart , cpEnd , TS_ENCODING );
808
- if (idx < 0 ) {
809
- return -1 ;
810
- }
811
- return idx ;
787
+ return indexOf (self , sub , start , end , codePointLengthNode , indexOfStringNode );
812
788
}
813
789
814
790
@ Specialization
@@ -1731,6 +1707,30 @@ static int len(Object self,
1731
1707
}
1732
1708
}
1733
1709
1710
+ private static int indexOf (TruffleString self , TruffleString sub , int start , int end , CodePointLengthNode codePointLengthNode , IndexOfStringNode indexOfStringNode ) {
1711
+ int cpLen = codePointLengthNode .execute (self , TS_ENCODING );
1712
+ int cpStart = adjustStartIndex (start , cpLen );
1713
+ int cpEnd = adjustEndIndex (end , cpLen );
1714
+ if (cpStart < cpEnd ) {
1715
+ return indexOfStringNode .execute (self , sub , cpStart , cpEnd , TS_ENCODING );
1716
+ } else if (sub .isEmpty () && cpStart == cpEnd && cpStart <= cpLen ) {
1717
+ return cpStart ;
1718
+ }
1719
+ return -1 ;
1720
+ }
1721
+
1722
+ private static int lastIndexOf (TruffleString self , TruffleString sub , int start , int end , CodePointLengthNode codePointLengthNode , LastIndexOfStringNode lastIndexOfStringNode ) {
1723
+ int cpLen = codePointLengthNode .execute (self , TS_ENCODING );
1724
+ int cpStart = adjustStartIndex (start , cpLen );
1725
+ int cpEnd = adjustEndIndex (end , cpLen );
1726
+ if (cpStart < cpEnd ) {
1727
+ return lastIndexOfStringNode .execute (self , sub , cpEnd , cpStart , TS_ENCODING );
1728
+ } else if (sub .isEmpty () && cpStart == cpEnd && cpStart <= cpLen ) {
1729
+ return cpStart ;
1730
+ }
1731
+ return -1 ;
1732
+ }
1733
+
1734
1734
@ Builtin (name = "index" , minNumOfPositionalArgs = 2 , parameterNames = {"$self" , "sub" , "start" , "end" })
1735
1735
@ ArgumentClinic (name = "start" , conversion = ArgumentClinic .ClinicConversion .SliceIndex , defaultValue = "0" , useDefaultForNone = true )
1736
1736
@ ArgumentClinic (name = "end" , conversion = ArgumentClinic .ClinicConversion .SliceIndex , defaultValue = "Integer.MAX_VALUE" , useDefaultForNone = true )
@@ -1745,16 +1745,11 @@ protected ArgumentClinicProvider getArgumentClinic() {
1745
1745
public int index (TruffleString self , TruffleString sub , int start , int end ,
1746
1746
@ Shared ("cpLen" ) @ Cached TruffleString .CodePointLengthNode codePointLengthNode ,
1747
1747
@ Shared ("indexOf" ) @ Cached TruffleString .IndexOfStringNode indexOfStringNode ) {
1748
- int cpLen = codePointLengthNode .execute (self , TS_ENCODING );
1749
- int cpStart = adjustStartIndex (start , cpLen );
1750
- int cpEnd = adjustEndIndex (end , cpLen );
1751
- if (cpStart < cpLen ) {
1752
- int idx = indexOfStringNode .execute (self , sub , cpStart , cpEnd , TS_ENCODING );
1753
- if (idx >= 0 ) {
1754
- return idx ;
1755
- }
1748
+ int idx = indexOf (self , sub , start , end , codePointLengthNode , indexOfStringNode );
1749
+ if (idx < 0 ) {
1750
+ throw raise (ValueError , ErrorMessages .SUBSTRING_NOT_FOUND );
1756
1751
}
1757
- throw raise ( ValueError , ErrorMessages . SUBSTRING_NOT_FOUND ) ;
1752
+ return idx ;
1758
1753
}
1759
1754
1760
1755
@ Specialization
@@ -1782,19 +1777,11 @@ protected ArgumentClinicProvider getArgumentClinic() {
1782
1777
public int rindex (TruffleString self , TruffleString sub , int start , int end ,
1783
1778
@ Shared ("cpLen" ) @ Cached TruffleString .CodePointLengthNode codePointLengthNode ,
1784
1779
@ Shared ("lastIndexOf" ) @ Cached TruffleString .LastIndexOfStringNode lastIndexOfStringNode ) {
1785
- int cpLen = codePointLengthNode .execute (self , TS_ENCODING );
1786
- int cpStart = adjustStartIndex (start , cpLen );
1787
- int cpEnd = adjustEndIndex (end , cpLen );
1788
- if (sub .isEmpty () && cpStart == cpLen ) {
1789
- return cpLen ;
1790
- }
1791
- if (cpStart < cpLen ) {
1792
- int idx = lastIndexOfStringNode .execute (self , sub , cpEnd , cpStart , TS_ENCODING );
1793
- if (idx >= 0 ) {
1794
- return idx ;
1795
- }
1780
+ int idx = lastIndexOf (self , sub , start , end , codePointLengthNode , lastIndexOfStringNode );
1781
+ if (idx < 0 ) {
1782
+ throw raise (ValueError , ErrorMessages .SUBSTRING_NOT_FOUND );
1796
1783
}
1797
- throw raise ( ValueError , ErrorMessages . SUBSTRING_NOT_FOUND ) ;
1784
+ return idx ;
1798
1785
}
1799
1786
1800
1787
@ Specialization
0 commit comments