56
56
import org .junit .BeforeClass ;
57
57
58
58
import com .oracle .truffle .api .CompilerDirectives ;
59
+ import com .oracle .truffle .api .strings .TranscodingErrorHandler ;
59
60
import com .oracle .truffle .api .strings .TruffleString ;
60
61
import com .oracle .truffle .api .strings .TruffleStringBuilder ;
61
62
import com .oracle .truffle .regex .RegexSyntaxException .ErrorCode ;
@@ -131,49 +132,42 @@ Value execRegex(Value compiledRegex, String input, int fromIndex) {
131
132
}
132
133
133
134
Value execRegex (Value compiledRegex , Encodings .Encoding encoding , String input , int fromIndex ) {
134
- return execRegex (compiledRegex , encoding , TruffleString .fromJavaStringUncached (input , encoding .getTStringEncoding ()), fromIndex );
135
- }
136
-
137
- Value execRegex (Value compiledRegex , Encodings .Encoding encoding , TruffleString input , int fromIndex ) {
138
- TruffleString converted = input .switchEncodingUncached (encoding .getTStringEncoding ());
135
+ TruffleString converted = toTruffleString (input , encoding );
139
136
int length = converted .byteLength (encoding .getTStringEncoding ()) >> encoding .getStride ();
140
- return execRegex (compiledRegex , encoding , converted , fromIndex , length , 0 , length );
141
- }
142
-
143
- Value execRegex (Value compiledRegex , Encodings .Encoding encoding , TruffleString input , int fromIndex , int toIndex , int regionFrom , int regionTo ) {
144
- return compiledRegex .invokeMember ("exec" , input .switchEncodingUncached (encoding .getTStringEncoding ()), fromIndex , toIndex , regionFrom , regionTo );
137
+ return compiledRegex .invokeMember ("exec" , converted , fromIndex , length , 0 , length );
145
138
}
146
139
147
140
Value execRegexBoolean (Value compiledRegex , Encodings .Encoding encoding , String input , int fromIndex ) {
148
- return execRegexBoolean (compiledRegex , encoding , TruffleString .fromJavaStringUncached (input , encoding .getTStringEncoding ()), fromIndex );
149
- }
150
-
151
- Value execRegexBoolean (Value compiledRegex , Encodings .Encoding encoding , TruffleString input , int fromIndex ) {
152
- TruffleString converted = input .switchEncodingUncached (encoding .getTStringEncoding ());
141
+ TruffleString converted = toTruffleString (input , encoding );
153
142
int length = converted .byteLength (encoding .getTStringEncoding ()) >> encoding .getStride ();
154
- return execRegexBoolean ( compiledRegex , encoding , converted , fromIndex , length , 0 , length );
143
+ return compiledRegex . invokeMember ( "execBoolean" , converted , fromIndex , length , 0 , length );
155
144
}
156
145
157
- Value execRegexBoolean (Value compiledRegex , Encodings .Encoding encoding , TruffleString input , int fromIndex , int toIndex , int regionFrom , int regionTo ) {
158
- return compiledRegex .invokeMember ("execBoolean" , input .switchEncodingUncached (encoding .getTStringEncoding ()), fromIndex , toIndex , regionFrom , regionTo );
146
+ private static TruffleString toTruffleString (String input , Encodings .Encoding encoding ) {
147
+ TruffleString tStringUTF16 = TruffleString .fromJavaStringUncached (input , TruffleString .Encoding .UTF_16 );
148
+ return tStringUTF16 .switchEncodingUncached (encoding .getTStringEncoding (), TranscodingErrorHandler .DEFAULT_KEEP_SURROGATES_IN_UTF8 );
159
149
}
160
150
161
151
void testBoolean (String pattern , String flags , Map <String , String > options , String input , int fromIndex , boolean isMatch ) {
152
+ testBoolean (pattern , flags , options , getTRegexEncoding (), input , fromIndex , isMatch );
153
+ }
154
+
155
+ void testBoolean (String pattern , String flags , Map <String , String > options , Encodings .Encoding encoding , String input , int fromIndex , boolean isMatch ) {
162
156
String expectedResult = isMatch ? "Match" : "NoMatch" ;
163
- Source .Builder source = sourceBuilder (pattern , flags , options , getTRegexEncoding () ).option ("regexDummyLang.BooleanMatch" , "true" );
157
+ Source .Builder source = sourceBuilder (pattern , flags , options , encoding ).option ("regexDummyLang.BooleanMatch" , "true" );
164
158
try {
165
159
Value compiledRegex = compileRegex (context , source );
166
- Value result = execRegexBoolean (compiledRegex , getTRegexEncoding () , input , fromIndex );
160
+ Value result = execRegexBoolean (compiledRegex , encoding , input , fromIndex );
167
161
if (result .asBoolean () != isMatch ) {
168
162
String actualResult = result .asBoolean () ? "Match" : "NoMatch" ;
169
- printTable (pattern , flags , input , fromIndex , expectedResult , actualResult );
163
+ printTable (pattern , flags , encoding , input , fromIndex , expectedResult , actualResult );
170
164
if (ASSERTS ) {
171
165
Assert .fail (options + regexSlashes (pattern , flags ) + ' ' + quote (input ) + " expected: " + expectedResult + ", actual: " + actualResult );
172
166
}
173
167
}
174
168
} catch (PolyglotException e ) {
175
169
if (!ASSERTS && e .isSyntaxError ()) {
176
- printTable (pattern , flags , input , fromIndex , expectedResult , syntaxErrorToString (e .getMessage ()));
170
+ printTable (pattern , flags , encoding , input , fromIndex , expectedResult , syntaxErrorToString (e .getMessage ()));
177
171
} else {
178
172
throw e ;
179
173
}
@@ -198,24 +192,24 @@ void test(String pattern, String flags, Map<String, String> options, Encodings.E
198
192
test (compiledRegex , pattern , flags , options , encoding , input , fromIndex , isMatch , captureGroupBoundsAndLastGroup );
199
193
} catch (PolyglotException e ) {
200
194
if (!ASSERTS && e .isSyntaxError ()) {
201
- printTable (pattern , flags , input , fromIndex , expectedResultToString (captureGroupBoundsAndLastGroup ), syntaxErrorToString (e .getMessage ()));
195
+ printTable (pattern , flags , encoding , input , fromIndex , expectedResultToString (captureGroupBoundsAndLastGroup ), syntaxErrorToString (e .getMessage ()));
202
196
} else {
203
197
throw e ;
204
198
}
205
199
}
206
- testBoolean (pattern , flags , options , input , fromIndex , isMatch );
200
+ testBoolean (pattern , flags , options , encoding , input , fromIndex , isMatch );
207
201
}
208
202
209
203
void test (Value compiledRegex , String pattern , String flags , Map <String , String > options , Encodings .Encoding encoding , String input , int fromIndex , boolean isMatch ,
210
204
int ... captureGroupBoundsAndLastGroup ) {
211
205
Value result = execRegex (compiledRegex , encoding , input , fromIndex );
212
206
int groupCount = compiledRegex .getMember ("groupCount" ).asInt ();
213
- validateResult (pattern , flags , options , input , fromIndex , result , groupCount , isMatch , captureGroupBoundsAndLastGroup );
207
+ validateResult (pattern , flags , options , encoding , input , fromIndex , result , groupCount , isMatch , captureGroupBoundsAndLastGroup );
214
208
215
209
if (TEST_REGION_FROM_TO ) {
216
210
TruffleStringBuilder sb = TruffleStringBuilder .create (encoding .getTStringEncoding ());
217
211
sb .appendCodePointUncached ('_' );
218
- sb .appendStringUncached (TruffleString . fromJavaStringUncached (input , encoding . getTStringEncoding () ));
212
+ sb .appendStringUncached (toTruffleString (input , encoding ));
219
213
sb .appendCodePointUncached ('_' );
220
214
TruffleString padded = sb .toStringUncached ();
221
215
int length = padded .byteLength (encoding .getTStringEncoding ()) >> encoding .getStride ();
@@ -227,39 +221,40 @@ void test(Value compiledRegex, String pattern, String flags, Map<String, String>
227
221
if ((boundsAdjusted .length & 1 ) == 1 ) {
228
222
boundsAdjusted [boundsAdjusted .length - 1 ] = captureGroupBoundsAndLastGroup [boundsAdjusted .length - 1 ];
229
223
}
230
- Value resultSubstring = execRegex ( compiledRegex , encoding , padded , fromIndex + 1 , length - 1 , 1 , length - 1 );
231
- validateResult (pattern , flags , options , input , fromIndex + 1 , resultSubstring , groupCount , isMatch , boundsAdjusted );
224
+ Value resultSubstring = compiledRegex . invokeMember ( "exec" , padded , fromIndex + 1 , length - 1 , 1 , length - 1 );
225
+ validateResult (pattern , flags , options , encoding , input , fromIndex + 1 , resultSubstring , groupCount , isMatch , boundsAdjusted );
232
226
}
233
227
}
234
228
235
- private static void validateResult (String pattern , String flags , Map <String , String > options , String input , int fromIndex , Value result , int groupCount , boolean isMatch ,
229
+ private static void validateResult (String pattern , String flags , Map <String , String > options , Encodings .Encoding encoding , String input , int fromIndex , Value result , int groupCount ,
230
+ boolean isMatch ,
236
231
int ... captureGroupBoundsAndLastGroup ) {
237
232
if (isMatch != result .getMember ("isMatch" ).asBoolean ()) {
238
- fail (pattern , flags , options , input , fromIndex , result , groupCount , captureGroupBoundsAndLastGroup );
233
+ fail (pattern , flags , options , encoding , input , fromIndex , result , groupCount , captureGroupBoundsAndLastGroup );
239
234
return ;
240
235
}
241
236
if (isMatch ) {
242
237
if (ASSERTS ) {
243
238
assertEquals (captureGroupBoundsAndLastGroup .length / 2 , groupCount );
244
239
}
245
240
if (captureGroupBoundsAndLastGroup .length / 2 != groupCount ) {
246
- fail (pattern , flags , options , input , fromIndex , result , groupCount , captureGroupBoundsAndLastGroup );
241
+ fail (pattern , flags , options , encoding , input , fromIndex , result , groupCount , captureGroupBoundsAndLastGroup );
247
242
return ;
248
243
}
249
244
for (int i = 0 ; i < groupCount ; i ++) {
250
245
if (captureGroupBoundsAndLastGroup [Group .groupNumberToBoundaryIndexStart (i )] != result .invokeMember ("getStart" , i ).asInt () ||
251
246
captureGroupBoundsAndLastGroup [Group .groupNumberToBoundaryIndexEnd (i )] != result .invokeMember ("getEnd" , i ).asInt ()) {
252
- fail (pattern , flags , options , input , fromIndex , result , groupCount , captureGroupBoundsAndLastGroup );
247
+ fail (pattern , flags , options , encoding , input , fromIndex , result , groupCount , captureGroupBoundsAndLastGroup );
253
248
return ;
254
249
}
255
250
}
256
251
} else if (result .getMember ("isMatch" ).asBoolean ()) {
257
- fail (pattern , flags , options , input , fromIndex , result , groupCount , captureGroupBoundsAndLastGroup );
252
+ fail (pattern , flags , options , encoding , input , fromIndex , result , groupCount , captureGroupBoundsAndLastGroup );
258
253
return ;
259
254
}
260
255
int lastGroup = captureGroupBoundsAndLastGroup .length % 2 == 1 ? captureGroupBoundsAndLastGroup [captureGroupBoundsAndLastGroup .length - 1 ] : -1 ;
261
256
if (lastGroup != result .getMember ("lastGroup" ).asInt ()) {
262
- fail (pattern , flags , options , input , fromIndex , result , groupCount , captureGroupBoundsAndLastGroup );
257
+ fail (pattern , flags , options , encoding , input , fromIndex , result , groupCount , captureGroupBoundsAndLastGroup );
263
258
return ;
264
259
}
265
260
// print(pattern, input, fromIndex, result, groupCount, captureGroupBoundsAndLastGroup);
@@ -305,7 +300,7 @@ void expectSyntaxError(String pattern, String flags, Map<String, String> options
305
300
String msg = e .getMessage ();
306
301
int pos = e .getSourceLocation ().getCharIndex ();
307
302
if (!msg .contains (expectedMessage )) {
308
- printTable (pattern , flags , input , fromIndex , syntaxErrorToString (expectedMessage ), syntaxErrorToString (msg ));
303
+ printTable (pattern , flags , encoding , input , fromIndex , syntaxErrorToString (expectedMessage ), syntaxErrorToString (msg ));
309
304
if (ASSERTS ) {
310
305
Assert .fail (String .format ("/%s/%s : expected syntax error message containing \" %s\" , but was \" %s\" " , pattern , flags , expectedMessage , msg ));
311
306
}
@@ -317,7 +312,7 @@ void expectSyntaxError(String pattern, String flags, Map<String, String> options
317
312
return ;
318
313
}
319
314
Value result = execRegex (compiledRegex , encoding , input , fromIndex );
320
- printTable (pattern , flags , input , fromIndex , syntaxErrorToString (expectedMessage ), actualResultToString (result , compiledRegex .getMember ("groupCount" ).asInt (), false ));
315
+ printTable (pattern , flags , encoding , input , fromIndex , syntaxErrorToString (expectedMessage ), actualResultToString (result , compiledRegex .getMember ("groupCount" ).asInt (), false ));
321
316
if (ASSERTS ) {
322
317
Assert .fail (String .format ("/%s/%s : expected \" %s\" , but no exception was thrown" , pattern , flags , expectedMessage ));
323
318
}
@@ -338,29 +333,30 @@ private static String generateErrorPosArrow(int pos) {
338
333
return sb .append ('^' ).toString ();
339
334
}
340
335
341
- private static void fail (String pattern , String flags , Map <String , String > options , String input , int fromIndex , Value result , int groupCount , int ... captureGroupBoundsAndLastGroup ) {
336
+ private static void fail (String pattern , String flags , Map <String , String > options , Encodings .Encoding encoding , String input , int fromIndex , Value result , int groupCount ,
337
+ int ... captureGroupBoundsAndLastGroup ) {
342
338
String expectedResult = expectedResultToString (captureGroupBoundsAndLastGroup );
343
339
String actualResult = actualResultToString (result , groupCount , captureGroupBoundsAndLastGroup .length % 2 == 1 );
344
- printTable (pattern , flags , input , fromIndex , expectedResult , actualResult );
340
+ printTable (pattern , flags , encoding , input , fromIndex , expectedResult , actualResult );
345
341
if (ASSERTS ) {
346
342
Assert .fail (options + regexSlashes (pattern , flags ) + ' ' + quote (input ) + " expected: " + expectedResult + ", actual: " + actualResult );
347
343
}
348
344
}
349
345
350
- private static void print (String pattern , String flags , String input , int fromIndex , Value result , int groupCount , int ... captureGroupBoundsAndLastGroup ) {
346
+ private static void print (String pattern , String flags , Encodings . Encoding encoding , String input , int fromIndex , Value result , int groupCount , int ... captureGroupBoundsAndLastGroup ) {
351
347
String actualResult = actualResultToString (result , groupCount , captureGroupBoundsAndLastGroup .length % 2 == 1 );
352
- printTable (pattern , flags , input , fromIndex , actualResult , "" );
348
+ printTable (pattern , flags , encoding , input , fromIndex , actualResult , "" );
353
349
}
354
350
355
- private static void printTable (String pattern , String flags , String input , int fromIndex , String expectedResult , String actualResult ) {
351
+ private static void printTable (String pattern , String flags , Encodings . Encoding encoding , String input , int fromIndex , String expectedResult , String actualResult ) {
356
352
if (TABLE_OMIT_FROM_INDEX ) {
357
- String format = "%-20s%-20s%-30s%s%n" ;
358
- printTableHeader (format , "Pattern" , "Input" , "Expected result" , "TRegex result" );
359
- System .out .printf (format , regexSlashes (pattern , flags ), quote (input ), expectedResult , actualResult );
353
+ String format = "%-20s%-12s%- 20s%-30s%s%n" ;
354
+ printTableHeader (format , "Pattern" , "Encoding" , " Input" , "Expected result" , "TRegex result" );
355
+ System .out .printf (format , regexSlashes (pattern , flags ), encoding , quote (input ), expectedResult , actualResult );
360
356
} else {
361
357
String format = "%-16s%-16s%-10s%-20s%s%n" ;
362
- printTableHeader (format , "Pattern" , "Input" , "Offset" , "Expected result" , "TRegex result" );
363
- System .out .printf (format , regexSlashes (pattern , flags ), quote (input ), fromIndex , expectedResult , actualResult );
358
+ printTableHeader (format , "Pattern" , "Encoding" , " Input" , "Offset" , "Expected result" , "TRegex result" );
359
+ System .out .printf (format , regexSlashes (pattern , flags ), encoding , quote (input ), fromIndex , expectedResult , actualResult );
364
360
}
365
361
}
366
362
0 commit comments