@@ -3774,7 +3774,7 @@ protected Object findStringByteIndex(Object rubyString, Object rubyPattern, int
3774
3774
3775
3775
}
3776
3776
3777
- @ Primitive (name = "string_byte_character_index " , lowerFixnum = 1 )
3777
+ @ Primitive (name = "byte_index_to_character_index " , lowerFixnum = 1 )
3778
3778
public abstract static class StringByteCharacterIndexNode extends PrimitiveArrayArgumentsNode {
3779
3779
@ Specialization
3780
3780
protected int byteIndexToCodePointIndex (Object string , int byteIndex ,
@@ -3786,16 +3786,29 @@ protected int byteIndexToCodePointIndex(Object string, int byteIndex,
3786
3786
}
3787
3787
}
3788
3788
3789
+ // Named 'string_byte_index' in Rubinius.
3790
+ @ Primitive (name = "character_index_to_byte_index" , lowerFixnum = 1 )
3791
+ public abstract static class StringByteIndexFromCharIndexNode extends PrimitiveArrayArgumentsNode {
3792
+ @ Specialization
3793
+ protected Object byteIndexFromCharIndex (Object string , int characterIndex ,
3794
+ @ Cached TruffleString .CodePointIndexToByteIndexNode codePointIndexToByteIndexNode ,
3795
+ @ Cached RubyStringLibrary libString ) {
3796
+ return codePointIndexToByteIndexNode .execute (libString .getTString (string ), 0 , characterIndex ,
3797
+ libString .getTEncoding (string ));
3798
+ }
3799
+ }
3800
+
3789
3801
/** Search pattern in string starting after offset characters, and return a character index or nil */
3790
- @ Primitive (name = "string_character_index" , lowerFixnum = 2 )
3802
+ @ Primitive (name = "string_character_index" , lowerFixnum = 3 )
3791
3803
public abstract static class StringCharacterIndexNode extends PrimitiveArrayArgumentsNode {
3792
3804
3793
3805
protected final RubyStringLibrary libString = RubyStringLibrary .create ();
3794
3806
protected final RubyStringLibrary libPattern = RubyStringLibrary .create ();
3795
3807
@ Child SingleByteOptimizableNode singleByteOptimizableNode = SingleByteOptimizableNode .create ();
3796
3808
3797
3809
@ Specialization (guards = "singleByteOptimizableNode.execute(string, stringEncoding)" )
3798
- protected Object singleByteOptimizable (Object rubyString , Object rubyPattern , int codePointOffset ,
3810
+ protected Object singleByteOptimizable (
3811
+ Object rubyString , Object rubyPattern , RubyEncoding compatibleEncoding , int codePointOffset ,
3799
3812
@ Bind ("libString.getTString(rubyString)" ) AbstractTruffleString string ,
3800
3813
@ Bind ("libString.getEncoding(rubyString)" ) RubyEncoding stringEncoding ,
3801
3814
@ Bind ("libPattern.getTString(rubyPattern)" ) AbstractTruffleString pattern ,
@@ -3808,12 +3821,11 @@ protected Object singleByteOptimizable(Object rubyString, Object rubyPattern, in
3808
3821
// When single-byte optimizable, the byte length and the codepoint length are the same.
3809
3822
int stringByteLength = string .byteLength (stringEncoding .tencoding );
3810
3823
3811
- assert codePointOffset + pattern .byteLength (
3812
- patternEncoding . tencoding ) <= stringByteLength : "already checked in the caller, String#index" ;
3824
+ assert codePointOffset + pattern .byteLength (patternEncoding . tencoding ) <= stringByteLength
3825
+ : "already checked in the caller, String#index" ;
3813
3826
3814
- int found = byteIndexOfStringNode .execute (string , pattern , codePointOffset ,
3815
- stringByteLength ,
3816
- stringEncoding .tencoding );
3827
+ int found = byteIndexOfStringNode .execute (string , pattern , codePointOffset , stringByteLength ,
3828
+ compatibleEncoding .tencoding );
3817
3829
3818
3830
if (foundProfile .profile (found >= 0 )) {
3819
3831
return found ;
@@ -3823,7 +3835,8 @@ protected Object singleByteOptimizable(Object rubyString, Object rubyPattern, in
3823
3835
}
3824
3836
3825
3837
@ Specialization (guards = "!singleByteOptimizableNode.execute(string, stringEncoding)" )
3826
- protected Object multiByte (Object rubyString , Object rubyPattern , int codePointOffset ,
3838
+ protected Object multiByte (
3839
+ Object rubyString , Object rubyPattern , RubyEncoding compatibleEncoding , int codePointOffset ,
3827
3840
@ Bind ("libString.getTString(rubyString)" ) AbstractTruffleString string ,
3828
3841
@ Bind ("libString.getEncoding(rubyString)" ) RubyEncoding stringEncoding ,
3829
3842
@ Bind ("libPattern.getTString(rubyPattern)" ) AbstractTruffleString pattern ,
@@ -3838,7 +3851,7 @@ protected Object multiByte(Object rubyString, Object rubyPattern, int codePointO
3838
3851
3839
3852
int stringCodePointLength = codePointLengthNode .execute (string , stringEncoding .tencoding );
3840
3853
int found = indexOfStringNode .execute (string , pattern , codePointOffset , stringCodePointLength ,
3841
- stringEncoding .tencoding );
3854
+ compatibleEncoding .tencoding );
3842
3855
3843
3856
if (foundProfile .profile (found >= 0 )) {
3844
3857
return found ;
@@ -3849,11 +3862,12 @@ protected Object multiByte(Object rubyString, Object rubyPattern, int codePointO
3849
3862
}
3850
3863
3851
3864
/** Search pattern in string starting after offset bytes, and return a byte index or nil */
3852
- @ Primitive (name = "string_byte_index" , lowerFixnum = 2 )
3865
+ @ Primitive (name = "string_byte_index" , lowerFixnum = 3 )
3853
3866
public abstract static class StringByteIndexNode extends PrimitiveArrayArgumentsNode {
3854
3867
3855
3868
@ Specialization
3856
- protected Object stringByteIndex (Object rubyString , Object rubyPattern , int byteOffset ,
3869
+ protected Object stringByteIndex (
3870
+ Object rubyString , Object rubyPattern , RubyEncoding compatibleEncoding , int byteOffset ,
3857
3871
@ Cached RubyStringLibrary libString ,
3858
3872
@ Cached RubyStringLibrary libPattern ,
3859
3873
@ Cached TruffleString .ByteIndexOfStringNode byteIndexOfStringNode ,
@@ -3862,18 +3876,17 @@ protected Object stringByteIndex(Object rubyString, Object rubyPattern, int byte
3862
3876
assert byteOffset >= 0 ;
3863
3877
3864
3878
var string = libString .getTString (rubyString );
3865
- var stringEncoding = libString .getEncoding (rubyString ).tencoding ;
3866
- int stringByteLength = string .byteLength (stringEncoding );
3879
+ int stringByteLength = libString .byteLength (rubyString );
3867
3880
3868
3881
var pattern = libPattern .getTString (rubyPattern );
3869
- var patternEncoding = libPattern .getEncoding (rubyPattern ).tencoding ;
3870
- int patternByteLength = pattern .byteLength (patternEncoding );
3882
+ int patternByteLength = libPattern .byteLength (rubyPattern );
3871
3883
3872
3884
if (indexOutOfBoundsProfile .profile (byteOffset + patternByteLength > stringByteLength )) {
3873
3885
return nil ;
3874
3886
}
3875
3887
3876
- int found = byteIndexOfStringNode .execute (string , pattern , byteOffset , stringByteLength , stringEncoding );
3888
+ int found = byteIndexOfStringNode .execute (string , pattern , byteOffset , stringByteLength ,
3889
+ compatibleEncoding .tencoding );
3877
3890
if (foundProfile .profile (found >= 0 )) {
3878
3891
return found ;
3879
3892
}
@@ -3882,18 +3895,6 @@ protected Object stringByteIndex(Object rubyString, Object rubyPattern, int byte
3882
3895
}
3883
3896
}
3884
3897
3885
- // Named 'string_byte_index' in Rubinius.
3886
- @ Primitive (name = "string_byte_index_from_char_index" , lowerFixnum = 1 )
3887
- public abstract static class StringByteIndexFromCharIndexNode extends PrimitiveArrayArgumentsNode {
3888
- @ Specialization
3889
- protected Object byteIndexFromCharIndex (Object string , int characterIndex ,
3890
- @ Cached TruffleString .CodePointIndexToByteIndexNode codePointIndexToByteIndexNode ,
3891
- @ Cached RubyStringLibrary libString ) {
3892
- return codePointIndexToByteIndexNode .execute (libString .getTString (string ), 0 , characterIndex ,
3893
- libString .getTEncoding (string ));
3894
- }
3895
- }
3896
-
3897
3898
// Port of Rubinius's String::previous_byte_index.
3898
3899
//
3899
3900
// This method takes a byte index, finds the corresponding character the byte index belongs to, and then returns
@@ -3984,7 +3985,7 @@ protected Object stringRindex(Object rubyString, Object rubyPattern, int byteOff
3984
3985
assert byteOffset >= 0 ;
3985
3986
3986
3987
// Throw an exception if the encodings are not compatible.
3987
- checkEncodingNode .executeCheckEncoding (rubyString , rubyPattern );
3988
+ var compatibleEncoding = checkEncodingNode .executeCheckEncoding (rubyString , rubyPattern );
3988
3989
3989
3990
var string = libString .getTString (rubyString );
3990
3991
var stringEncoding = libString .getEncoding (rubyString ).tencoding ;
@@ -4007,7 +4008,7 @@ protected Object stringRindex(Object rubyString, Object rubyPattern, int byteOff
4007
4008
}
4008
4009
4009
4010
int result = lastByteIndexOfStringNode .execute (string , pattern , normalizedStart + patternByteLength , 0 ,
4010
- stringEncoding );
4011
+ compatibleEncoding . tencoding );
4011
4012
4012
4013
if (result < 0 ) {
4013
4014
noMatchProfile .enter ();
0 commit comments