2525
2626package java .util .regex ;
2727
28+ import java .util .ArrayList ;
2829import java .util .Objects ;
2930
3031/**
@@ -143,11 +144,11 @@ public String toString() {
143144 */
144145 Pattern pat ;
145146
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 ;
151152
152153 /**
153154 * The range within the sequence that is to be matched. Anchors will match at
@@ -241,11 +242,23 @@ public String toString() {
241242
242243 private Object [] replacementParts ;
243244
245+ /**
246+ * the raw result from JavaScript regex execution
247+ */
248+ private String [] 秘results ;
249+
244250 private String [] results ;
245251
246252 private String strString ;
247253
248254 private int groupCount ;
255+
256+ /**
257+ * raw group count
258+ */
259+ private int 秘groupCount ;
260+
261+ private boolean 秘haveGroups ;
249262
250263 /**
251264 * No default constructor.
@@ -273,8 +286,8 @@ public MatchResult toMatchResult() {
273286 Matcher result = new Matcher (pat , cs .toString ());
274287 result .first = first ;
275288 result .last = last ;
276- result .groupCount = groupCount ;
277- result .results = results .clone ();
289+ result .秘groupCount = 秘groupCount ;
290+ result .秘results = 秘results .clone ();
278291 return result ;
279292 }
280293
@@ -297,9 +310,6 @@ public Matcher usePattern(Pattern newPattern) {
297310 if (newPattern == null )
298311 throw new IllegalArgumentException ("Pattern cannot be null" );
299312 pat = newPattern ;
300-
301- // int parentGroupCount = Math.max(newPattern.capturingGroupCount, 10);
302- // groups = new int[parentGroupCount * 2];
303313 clearGroups ();
304314 return this ;
305315 }
@@ -342,7 +352,7 @@ static RegExp clone(RegExp rg) {
342352 public Matcher reset () {
343353 first = -1 ;
344354 last = 0 ;
345- groupCount = 0 ;
355+ 秘groupCount = 0 ;
346356 oldLast = -1 ;
347357 clearGroups ();
348358 appendPos = 0 ;
@@ -431,17 +441,15 @@ boolean search(int from, int anchor) {
431441 RegExp rg = pat .regexp ;
432442 rg .lastIndex = from ;
433443 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 );
436446 this .oldLast = last ;
437447 return result ;
438448 }
439449
440450 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 ;
445453 }
446454
447455 /**
@@ -607,6 +615,8 @@ private String processRepl(String replacement) {
607615 null );
608616 }
609617 this .replacement = replacement ;
618+ if (replacement != null && replacement .indexOf ("$" ) >= 0 )
619+ 秘updateGroups ();
610620 // Process substitution string to replace group references with groups
611621 int index = 0 ;
612622 int replacementPos = 0 ;
@@ -940,6 +950,7 @@ public String group(int group) {
940950 // this array is coming from JavaScript RegExp; some values may be "undefined"
941951 // so we force them to be null.
942952 return results [group ] == null ? null : results [group ];
953+ // JavaScript does not need this
943954// if ((groups[group*2] == -1) || (groups[group*2+1] == -1))
944955// return null;
945956// return getSubSequence(groups[group * 2], groups[group * 2 + 1]).toString();
@@ -974,7 +985,7 @@ public String group(int group) {
974985 public String group (String name ) {
975986 int group = getMatchedGroupIndex (name );
976987 return group < 0 || group >= results .length ? null : group (group );
977- //
988+ // JavaScript does not need this
978989// if ((groups[group*2] == -1) || (groups[group*2+1] == -1))
979990// return null;
980991// return getSubSequence(groups[group * 2], groups[group * 2 + 1]).toString();
@@ -996,9 +1007,35 @@ public String group(String name) {
9961007 */
9971008 @ Override
9981009 public int groupCount () {
1010+ 秘updateGroups ();
9991011 return groupCount ;
10001012 }
10011013
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+
10021039 /**
10031040 * Attempts to match the entire region against the pattern.
10041041 *
@@ -1033,8 +1070,8 @@ boolean match(int from, int anchor) {
10331070 return result ;
10341071 }
10351072
1036- private int indexRE (String [] results ) {
1037- return /** @j2sNative results .index || */
1073+ private int indexRE (String [] r ) {
1074+ return /** @j2sNative r .index || */
10381075 0 ;
10391076 }
10401077
@@ -1050,7 +1087,7 @@ private boolean checkRE(String[] r, String s) {
10501087 first = -1 ;
10511088 return false ;
10521089 }
1053- groupCount = r .length - 1 ;
1090+ 秘groupCount = r .length - 1 ;
10541091 int f0 = this .first ;
10551092 first = indexRE (r );
10561093 last = first + r [0 ].length ();
@@ -1060,7 +1097,7 @@ private boolean checkRE(String[] r, String s) {
10601097 // a longer string
10611098 return false ;
10621099 }
1063- if (groupCount < 0 )
1100+ if (秘groupCount < 0 )
10641101 return false ;
10651102 switch (acceptMode ) {
10661103 case STARTANCHOR :
@@ -1330,11 +1367,10 @@ int getMatchedGroupIndex(String name) {
13301367 Objects .requireNonNull (name , "Group name" );
13311368 if (first < 0 )
13321369 throw new IllegalStateException ("No match found" );
1333- if (pat .namedGroups == null )
1334- pat .秘setNameGroups ();
1370+ 秘updateGroups ();
13351371 if (pat .namedGroups == null || !pat .namedGroups ().containsKey (name ))
13361372 throw new IllegalArgumentException ("No group with name <" + name + ">" );
1337- return pat .namedGroups ().get (name );
1373+ return pat .namedGroups ().get (name ). intValue () + 1 ;
13381374 }
13391375 /**
13401376 * Returns the start index of the previous match.
@@ -1380,9 +1416,9 @@ public int start(int group) {
13801416 throw new IllegalStateException ("No match available" );
13811417 if (group < 0 || group > groupCount ())
13821418 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 ];
13861422 }
13871423
13881424 /**
@@ -1404,9 +1440,8 @@ public int start(int group) {
14041440 * @since 1.8
14051441 */
14061442 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 ];
14101445 }
14111446
14121447 /**
@@ -1452,9 +1487,10 @@ public int end(int group) {
14521487 throw new IllegalStateException ("No match available" );
14531488 if (group < 0 || group > groupCount ())
14541489 throw new IndexOutOfBoundsException ("No group " + group );
1455- return NOT_AVAILABLE ;
1490+ if (group == 0 )
1491+ return end ();
1492+ return groups [group * 2 + 1 ];
14561493
1457- // return groups[group * 2 + 1];
14581494 }
14591495
14601496 /**
@@ -1476,9 +1512,8 @@ public int end(int group) {
14761512 * @since 1.8
14771513 */
14781514 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 ];
14821517 }
14831518
14841519}
0 commit comments