36
36
***** END LICENSE BLOCK *****/
37
37
package org .truffleruby .core .regexp ;
38
38
39
- import static org .truffleruby .core .string .StringUtils .EMPTY_STRING_ARRAY ;
40
-
41
39
import java .nio .charset .StandardCharsets ;
42
40
import java .util .Arrays ;
43
- import java .util .Iterator ;
44
41
45
42
import com .oracle .truffle .api .strings .AbstractTruffleString ;
46
- import com .oracle .truffle .api .strings .TruffleString ;
47
43
import com .oracle .truffle .api .strings .TruffleStringBuilder ;
48
44
import org .jcodings .Encoding ;
49
45
import org .jcodings .specific .EUCJPEncoding ;
50
46
import org .jcodings .specific .SJISEncoding ;
51
47
import org .jcodings .specific .USASCIIEncoding ;
52
48
import org .jcodings .specific .UTF8Encoding ;
53
- import org .joni .NameEntry ;
54
49
import org .joni .Option ;
55
50
import org .joni .Regex ;
56
51
import org .joni .Syntax ;
60
55
import org .truffleruby .collections .ByteArrayBuilder ;
61
56
import org .truffleruby .core .encoding .Encodings ;
62
57
import org .truffleruby .core .encoding .RubyEncoding ;
63
- import org .truffleruby .core .encoding .TStringUtils ;
64
58
import org .truffleruby .core .string .ATStringWithEncoding ;
65
59
import org .truffleruby .core .string .TStringBuilder ;
66
60
import org .truffleruby .core .string .TStringWithEncoding ;
69
63
import org .truffleruby .language .backtrace .BacktraceFormatter ;
70
64
import org .truffleruby .language .control .DeferredRaiseException ;
71
65
import org .truffleruby .language .control .RaiseException ;
72
- import org .truffleruby .parser .ReOptions ;
73
66
74
67
import com .oracle .truffle .api .CompilerDirectives .TruffleBoundary ;
75
68
import com .oracle .truffle .api .nodes .Node ;
76
69
import org .truffleruby .parser .RubyDeferredWarnings ;
77
70
78
- public final class ClassicRegexp implements ReOptions {
79
-
80
- private final Regex pattern ;
81
- private final TStringWithEncoding str ;
82
- private final RegexpOptions options ;
83
-
84
- public Encoding getEncoding () {
85
- return pattern .getEncoding ();
86
- }
71
+ public final class ClassicRegexp {
87
72
88
73
public static Regex makeRegexp (RubyDeferredWarnings rubyDeferredWarnings ,
89
74
TStringBuilder processedSource , RegexpOptions options ,
@@ -109,23 +94,6 @@ public static String getRegexErrorMessage(AbstractTruffleString source, Exceptio
109
94
return BacktraceFormatter .formatJavaThrowableMessage (e ) + ": /" + source + "/" + options .toOptionsString ();
110
95
}
111
96
112
- public ClassicRegexp (TStringWithEncoding strEnc , RegexpOptions originalOptions )
113
- throws DeferredRaiseException {
114
- if (strEnc .encoding .isDummy ) {
115
- throw new UnsupportedOperationException ("can't make regexp with dummy encoding" );
116
- }
117
-
118
- RegexpOptions [] optionsArray = new RegexpOptions []{ originalOptions };
119
- RubyEncoding [] fixedEnc = new RubyEncoding []{ null };
120
- TStringBuilder unescaped = preprocess (strEnc , strEnc .encoding , fixedEnc , RegexpSupport .ErrorMode .RAISE );
121
- final RubyEncoding computedEnc = computeRegexpEncoding (optionsArray , strEnc .encoding , fixedEnc );
122
- final RegexpOptions options = optionsArray [0 ];
123
- final TruffleString source = strEnc .forceEncoding (computedEnc ).tstring ;
124
- this .pattern = makeRegexp (null , unescaped , options , computedEnc , source , null );
125
- this .options = options ;
126
- this .str = strEnc ;
127
- }
128
-
129
97
@ TruffleBoundary
130
98
@ SuppressWarnings ("fallthrough" )
131
99
private static boolean unescapeNonAscii (TStringBuilder to , TStringWithEncoding str , RubyEncoding enc ,
@@ -835,34 +803,33 @@ public static void appendOptions(TStringBuilder to, RegexpOptions options) {
835
803
}
836
804
837
805
@ SuppressWarnings ("unused" )
838
- public ByteArrayBuilder toByteArrayBuilder ( ) {
806
+ public static TStringWithEncoding toS ( TStringWithEncoding source , RegexpOptions options ) {
839
807
RegexpOptions newOptions = (RegexpOptions ) options .clone ();
840
- var byteArray = str .getInternalByteArray ();
808
+ var byteArray = source .getInternalByteArray ();
841
809
int p = 0 ;
842
810
int len = byteArray .getLength ();
843
811
844
812
TStringBuilder result = TStringBuilder .create (len );
845
813
result .append ((byte ) '(' );
846
814
result .append ((byte ) '?' );
847
815
848
- again : do {
816
+ do {
849
817
if (len >= 4 && byteArray .get (p ) == '(' && byteArray .get (p + 1 ) == '?' ) {
850
- boolean err = true ;
851
818
p += 2 ;
852
- if (( len -= 2 ) > 0 ) {
853
- do {
854
- if (byteArray .get (p ) == 'm' ) {
855
- newOptions = newOptions .setMultiline (true );
856
- } else if (byteArray .get (p ) == 'i' ) {
857
- newOptions = newOptions .setIgnorecase (true );
858
- } else if (byteArray .get (p ) == 'x' ) {
859
- newOptions = newOptions .setExtended (true );
860
- } else {
861
- break ;
862
- }
863
- p ++;
864
- } while (--len > 0 );
865
- }
819
+ len -= 2 ;
820
+ do {
821
+ if (byteArray .get (p ) == 'm' ) {
822
+ newOptions = newOptions .setMultiline (true );
823
+ } else if (byteArray .get (p ) == 'i' ) {
824
+ newOptions = newOptions .setIgnorecase (true );
825
+ } else if (byteArray .get (p ) == 'x' ) {
826
+ newOptions = newOptions .setExtended (true );
827
+ } else {
828
+ break ;
829
+ }
830
+ p ++;
831
+ } while (--len > 0 );
832
+
866
833
if (len > 1 && byteArray .get (p ) == '-' ) {
867
834
++p ;
868
835
--len ;
@@ -883,9 +850,10 @@ public ByteArrayBuilder toByteArrayBuilder() {
883
850
if (byteArray .get (p ) == ')' ) {
884
851
--len ;
885
852
++p ;
886
- continue again ;
853
+ continue ;
887
854
}
888
855
856
+ boolean err = true ;
889
857
if (byteArray .get (p ) == ':' && byteArray .get (p + len - 1 ) == ')' ) {
890
858
p ++;
891
859
try {
@@ -894,7 +862,7 @@ public ByteArrayBuilder toByteArrayBuilder() {
894
862
p + byteArray .getOffset (),
895
863
p + byteArray .getOffset () + (len -= 2 ),
896
864
Option .DEFAULT ,
897
- str .encoding .jcoding ,
865
+ source .encoding .jcoding ,
898
866
Syntax .DEFAULT ,
899
867
new RegexWarnCallback ());
900
868
err = false ;
@@ -906,7 +874,7 @@ public ByteArrayBuilder toByteArrayBuilder() {
906
874
if (err ) {
907
875
newOptions = options ;
908
876
p = 0 ;
909
- len = str .byteLength ();
877
+ len = source .byteLength ();
910
878
}
911
879
}
912
880
@@ -925,17 +893,16 @@ public ByteArrayBuilder toByteArrayBuilder() {
925
893
}
926
894
}
927
895
result .append ((byte ) ':' );
928
- appendRegexpString (result , str , p , len );
896
+ appendRegexpString (result , source , p , len );
929
897
930
898
result .append ((byte ) ')' );
931
- result .setEncoding (Encodings .getBuiltInEncoding (getEncoding ()));
932
- return result ;
933
- //return RubyString.newString(getRuntime(), result, getEncoding()).infectBy(this);
899
+ result .setEncoding (source .encoding );
900
+ return result .toTStringWithEnc ();
934
901
} while (true );
935
902
}
936
903
937
904
@ TruffleBoundary
938
- public void appendRegexpString (TStringBuilder to , TStringWithEncoding fullStr , int start , int len ) {
905
+ public static void appendRegexpString (TStringBuilder to , TStringWithEncoding fullStr , int start , int len ) {
939
906
var str = fullStr .substring (start , len );
940
907
941
908
final var enc = str .encoding .jcoding ;
@@ -981,24 +948,6 @@ public void appendRegexpString(TStringBuilder to, TStringWithEncoding fullStr, i
981
948
}
982
949
}
983
950
984
- public String [] getNames () {
985
- int nameLength = pattern .numberOfNames ();
986
- if (nameLength == 0 ) {
987
- return EMPTY_STRING_ARRAY ;
988
- }
989
-
990
- RubyEncoding encoding = Encodings .getBuiltInEncoding (pattern .getEncoding ());
991
- String [] names = new String [nameLength ];
992
- int j = 0 ;
993
- for (Iterator <NameEntry > i = pattern .namedBackrefIterator (); i .hasNext ();) {
994
- NameEntry e = i .next ();
995
- // intern() to improve footprint
996
- names [j ++] = TStringUtils .bytesToJavaStringOrThrow (e .name , e .nameP , e .nameEnd - e .nameP , encoding ).intern ();
997
- }
998
-
999
- return names ;
1000
- }
1001
-
1002
951
// Code that used to be in ParserSupport but copied here as ParserSupport is coupled with the JRuby lexer & parser.
1003
952
// Needed until https://github.com/ruby/prism/issues/1997 is fixed.
1004
953
0 commit comments