25
25
26
26
package java .util .regex ;
27
27
28
+ import java .util .ArrayList ;
28
29
import java .util .Objects ;
29
30
30
31
/**
@@ -143,11 +144,11 @@ public String toString() {
143
144
*/
144
145
Pattern pat ;
145
146
146
- // /**
147
- // * The storage used by groups. They may contain invalid values if
148
- // * a group was skipped during the matching.
149
- // */
150
- // int[] groups;
147
+ /**
148
+ * The storage used by groups. They may contain invalid values if
149
+ * a group was skipped during the matching.
150
+ */
151
+ int [] groups ;
151
152
152
153
/**
153
154
* The range within the sequence that is to be matched. Anchors will match at
@@ -241,11 +242,23 @@ public String toString() {
241
242
242
243
private Object [] replacementParts ;
243
244
245
+ /**
246
+ * the raw result from JavaScript regex execution
247
+ */
248
+ private String [] 秘results ;
249
+
244
250
private String [] results ;
245
251
246
252
private String strString ;
247
253
248
254
private int groupCount ;
255
+
256
+ /**
257
+ * raw group count
258
+ */
259
+ private int 秘groupCount ;
260
+
261
+ private boolean 秘haveGroups ;
249
262
250
263
/**
251
264
* No default constructor.
@@ -273,8 +286,8 @@ public MatchResult toMatchResult() {
273
286
Matcher result = new Matcher (pat , cs .toString ());
274
287
result .first = first ;
275
288
result .last = last ;
276
- result .groupCount = groupCount ;
277
- result .results = results .clone ();
289
+ result .秘groupCount = 秘groupCount ;
290
+ result .秘results = 秘results .clone ();
278
291
return result ;
279
292
}
280
293
@@ -297,9 +310,6 @@ public Matcher usePattern(Pattern newPattern) {
297
310
if (newPattern == null )
298
311
throw new IllegalArgumentException ("Pattern cannot be null" );
299
312
pat = newPattern ;
300
-
301
- // int parentGroupCount = Math.max(newPattern.capturingGroupCount, 10);
302
- // groups = new int[parentGroupCount * 2];
303
313
clearGroups ();
304
314
return this ;
305
315
}
@@ -342,7 +352,7 @@ static RegExp clone(RegExp rg) {
342
352
public Matcher reset () {
343
353
first = -1 ;
344
354
last = 0 ;
345
- groupCount = 0 ;
355
+ 秘groupCount = 0 ;
346
356
oldLast = -1 ;
347
357
clearGroups ();
348
358
appendPos = 0 ;
@@ -431,17 +441,15 @@ boolean search(int from, int anchor) {
431
441
RegExp rg = pat .regexp ;
432
442
rg .lastIndex = from ;
433
443
acceptMode = (anchor == UNSPECIFIED ? NOANCHOR : anchor );
434
- results = execRE (rg , s );
435
- boolean result = checkRE (results , s );
444
+ 秘results = execRE (rg , s );
445
+ boolean result = checkRE (秘results , s );
436
446
this .oldLast = last ;
437
447
return result ;
438
448
}
439
449
440
450
private void clearGroups () {
441
- // for (int i = 0; i < groups.length; i++)
442
- // groups[i] = -1;
443
- // for (int i = 0; i < locals.length; i++)
444
- // locals[i] = -1;
451
+ 秘haveGroups = false ;
452
+ groups = null ;
445
453
}
446
454
447
455
/**
@@ -607,6 +615,8 @@ private String processRepl(String replacement) {
607
615
null );
608
616
}
609
617
this .replacement = replacement ;
618
+ if (replacement != null && replacement .indexOf ("$" ) >= 0 )
619
+ 秘updateGroups ();
610
620
// Process substitution string to replace group references with groups
611
621
int index = 0 ;
612
622
int replacementPos = 0 ;
@@ -940,6 +950,7 @@ public String group(int group) {
940
950
// this array is coming from JavaScript RegExp; some values may be "undefined"
941
951
// so we force them to be null.
942
952
return results [group ] == null ? null : results [group ];
953
+ // JavaScript does not need this
943
954
// if ((groups[group*2] == -1) || (groups[group*2+1] == -1))
944
955
// return null;
945
956
// return getSubSequence(groups[group * 2], groups[group * 2 + 1]).toString();
@@ -974,7 +985,7 @@ public String group(int group) {
974
985
public String group (String name ) {
975
986
int group = getMatchedGroupIndex (name );
976
987
return group < 0 || group >= results .length ? null : group (group );
977
- //
988
+ // JavaScript does not need this
978
989
// if ((groups[group*2] == -1) || (groups[group*2+1] == -1))
979
990
// return null;
980
991
// return getSubSequence(groups[group * 2], groups[group * 2 + 1]).toString();
@@ -996,9 +1007,35 @@ public String group(String name) {
996
1007
*/
997
1008
@ Override
998
1009
public int groupCount () {
1010
+ 秘updateGroups ();
999
1011
return groupCount ;
1000
1012
}
1001
1013
1014
+ private void 秘updateGroups () {
1015
+ if (秘haveGroups || 秘groupCount <= 0 || 秘results == null )
1016
+ return ;
1017
+ 秘haveGroups = true ;
1018
+ pat .秘setNameGroups ();
1019
+ ArrayList <String > names = pat .秘groupNames ;
1020
+ int pt = start ();
1021
+ groupCount = -1 ;
1022
+ groups = new int [names .size () * 2 ];
1023
+ results = new String [秘results .length ];
1024
+ for (int i = 0 , gpt = 0 , n = names .size (); i < n ; i ++) {
1025
+ String name = names .get (i );
1026
+ String r = 秘results [i ];
1027
+ int len = (r == null ? 0 : r .length ());
1028
+ if (name == null || !name .startsWith ("秘" )) {
1029
+ groups [gpt ++] = pt ;
1030
+ groups [gpt ++] = pt + len ;
1031
+ pat .namedGroups ().put (name , groupCount );
1032
+ results [++groupCount ] = r ;
1033
+ } else {
1034
+ pt += len ;
1035
+ }
1036
+ }
1037
+ }
1038
+
1002
1039
/**
1003
1040
* Attempts to match the entire region against the pattern.
1004
1041
*
@@ -1033,8 +1070,8 @@ boolean match(int from, int anchor) {
1033
1070
return result ;
1034
1071
}
1035
1072
1036
- private int indexRE (String [] results ) {
1037
- return /** @j2sNative results .index || */
1073
+ private int indexRE (String [] r ) {
1074
+ return /** @j2sNative r .index || */
1038
1075
0 ;
1039
1076
}
1040
1077
@@ -1050,7 +1087,7 @@ private boolean checkRE(String[] r, String s) {
1050
1087
first = -1 ;
1051
1088
return false ;
1052
1089
}
1053
- groupCount = r .length - 1 ;
1090
+ 秘groupCount = r .length - 1 ;
1054
1091
int f0 = this .first ;
1055
1092
first = indexRE (r );
1056
1093
last = first + r [0 ].length ();
@@ -1060,7 +1097,7 @@ private boolean checkRE(String[] r, String s) {
1060
1097
// a longer string
1061
1098
return false ;
1062
1099
}
1063
- if (groupCount < 0 )
1100
+ if (秘groupCount < 0 )
1064
1101
return false ;
1065
1102
switch (acceptMode ) {
1066
1103
case STARTANCHOR :
@@ -1330,11 +1367,10 @@ int getMatchedGroupIndex(String name) {
1330
1367
Objects .requireNonNull (name , "Group name" );
1331
1368
if (first < 0 )
1332
1369
throw new IllegalStateException ("No match found" );
1333
- if (pat .namedGroups == null )
1334
- pat .秘setNameGroups ();
1370
+ 秘updateGroups ();
1335
1371
if (pat .namedGroups == null || !pat .namedGroups ().containsKey (name ))
1336
1372
throw new IllegalArgumentException ("No group with name <" + name + ">" );
1337
- return pat .namedGroups ().get (name );
1373
+ return pat .namedGroups ().get (name ). intValue () + 1 ;
1338
1374
}
1339
1375
/**
1340
1376
* Returns the start index of the previous match.
@@ -1380,9 +1416,9 @@ public int start(int group) {
1380
1416
throw new IllegalStateException ("No match available" );
1381
1417
if (group < 0 || group > groupCount ())
1382
1418
throw new IndexOutOfBoundsException ("No group " + group );
1383
- return NOT_AVAILABLE ;
1384
- //
1385
- // return groups[group * 2];
1419
+ if ( group == 0 )
1420
+ return start ();
1421
+ return groups [group * 2 ];
1386
1422
}
1387
1423
1388
1424
/**
@@ -1404,9 +1440,8 @@ public int start(int group) {
1404
1440
* @since 1.8
1405
1441
*/
1406
1442
public int start (String name ) {
1407
- getMatchedGroupIndex (name );
1408
- return NOT_AVAILABLE ;
1409
- // return groups[getMatchedGroupIndex(name) * 2];
1443
+ int g = getMatchedGroupIndex (name );
1444
+ return groups [g * 2 ];
1410
1445
}
1411
1446
1412
1447
/**
@@ -1452,9 +1487,10 @@ public int end(int group) {
1452
1487
throw new IllegalStateException ("No match available" );
1453
1488
if (group < 0 || group > groupCount ())
1454
1489
throw new IndexOutOfBoundsException ("No group " + group );
1455
- return NOT_AVAILABLE ;
1490
+ if (group == 0 )
1491
+ return end ();
1492
+ return groups [group * 2 + 1 ];
1456
1493
1457
- // return groups[group * 2 + 1];
1458
1494
}
1459
1495
1460
1496
/**
@@ -1476,9 +1512,8 @@ public int end(int group) {
1476
1512
* @since 1.8
1477
1513
*/
1478
1514
public int end (String name ) {
1479
- getMatchedGroupIndex (name );
1480
- return NOT_AVAILABLE ;
1481
- // return groups[getMatchedGroupIndex(name) * 2 + 1];
1515
+ int g = getMatchedGroupIndex (name );
1516
+ return groups [g * 2 + 1 ];
1482
1517
}
1483
1518
1484
1519
}
0 commit comments