Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Pattern/Matcher fixed issue #253 #217

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified sources/net.sf.j2s.core/dist/swingjs/SwingJS-site.zip
Binary file not shown.
3 changes: 2 additions & 1 deletion sources/net.sf.j2s.core/dist/swingjs/differences.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1468,7 +1468,8 @@ Formatter/Regex limitations
Some browsers cannot process Regex "look-behind" process such as (?<=\W)
java.util.regex.Matcher and Pattern use JavaScript's RegExp object rather than
the native Java object. These are not identical. Only flags /igm are supported.
Matcher.start(groupID) is not supported.
Matcher.start(groupID)/.end(groupID) is supported.
(x*)? failure returns null, not empty string.

java.util.Formatter will function correctly for all standard %... patterns.

Expand Down
2 changes: 1 addition & 1 deletion sources/net.sf.j2s.core/dist/swingjs/timestamp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20230304111725
20230311212845
Binary file modified sources/net.sf.j2s.core/dist/swingjs/ver/3.3.1/SwingJS-site.zip
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -1468,7 +1468,8 @@ Formatter/Regex limitations
Some browsers cannot process Regex "look-behind" process such as (?<=\W)
java.util.regex.Matcher and Pattern use JavaScript's RegExp object rather than
the native Java object. These are not identical. Only flags /igm are supported.
Matcher.start(groupID) is not supported.
Matcher.start(groupID)/.end(groupID) is supported.
(x*)? failure returns null, not empty string.

java.util.Formatter will function correctly for all standard %... patterns.

Expand Down
2 changes: 1 addition & 1 deletion sources/net.sf.j2s.core/dist/swingjs/ver/3.3.1/timestamp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20230304111725
20230311212845
Binary file modified sources/net.sf.j2s.java.core/dist/SwingJS-site.zip
Binary file not shown.
3 changes: 2 additions & 1 deletion sources/net.sf.j2s.java.core/dist/differences.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1468,7 +1468,8 @@ Formatter/Regex limitations
Some browsers cannot process Regex "look-behind" process such as (?<=\W)
java.util.regex.Matcher and Pattern use JavaScript's RegExp object rather than
the native Java object. These are not identical. Only flags /igm are supported.
Matcher.start(groupID) is not supported.
Matcher.start(groupID)/.end(groupID) is supported.
(x*)? failure returns null, not empty string.

java.util.Formatter will function correctly for all standard %... patterns.

Expand Down
3 changes: 2 additions & 1 deletion sources/net.sf.j2s.java.core/doc/Differences.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1468,7 +1468,8 @@ Formatter/Regex limitations
Some browsers cannot process Regex "look-behind" process such as (?<=\W)
java.util.regex.Matcher and Pattern use JavaScript's RegExp object rather than
the native Java object. These are not identical. Only flags /igm are supported.
Matcher.start(groupID) is not supported.
Matcher.start(groupID)/.end(groupID) is supported.
(x*)? failure returns null, not empty string.

java.util.Formatter will function correctly for all standard %... patterns.

Expand Down
107 changes: 71 additions & 36 deletions sources/net.sf.j2s.java.core/src/java/util/regex/Matcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

package java.util.regex;

import java.util.ArrayList;
import java.util.Objects;

/**
Expand Down Expand Up @@ -143,11 +144,11 @@ public String toString() {
*/
Pattern pat;

// /**
// * The storage used by groups. They may contain invalid values if
// * a group was skipped during the matching.
// */
// int[] groups;
/**
* The storage used by groups. They may contain invalid values if
* a group was skipped during the matching.
*/
int[] groups;

/**
* The range within the sequence that is to be matched. Anchors will match at
Expand Down Expand Up @@ -241,11 +242,23 @@ public String toString() {

private Object[] replacementParts;

/**
* the raw result from JavaScript regex execution
*/
private String[] 秘results;

private String[] results;

private String strString;

private int groupCount;

/**
* raw group count
*/
private int 秘groupCount;

private boolean 秘haveGroups;

/**
* No default constructor.
Expand Down Expand Up @@ -273,8 +286,8 @@ public MatchResult toMatchResult() {
Matcher result = new Matcher(pat, cs.toString());
result.first = first;
result.last = last;
result.groupCount = groupCount;
result.results = results.clone();
result.秘groupCount = 秘groupCount;
result.秘results = 秘results.clone();
return result;
}

Expand All @@ -297,9 +310,6 @@ public Matcher usePattern(Pattern newPattern) {
if (newPattern == null)
throw new IllegalArgumentException("Pattern cannot be null");
pat = newPattern;

// int parentGroupCount = Math.max(newPattern.capturingGroupCount, 10);
// groups = new int[parentGroupCount * 2];
clearGroups();
return this;
}
Expand Down Expand Up @@ -342,7 +352,7 @@ static RegExp clone(RegExp rg) {
public Matcher reset() {
first = -1;
last = 0;
groupCount = 0;
秘groupCount = 0;
oldLast = -1;
clearGroups();
appendPos = 0;
Expand Down Expand Up @@ -431,17 +441,15 @@ boolean search(int from, int anchor) {
RegExp rg = pat.regexp;
rg.lastIndex = from;
acceptMode = (anchor == UNSPECIFIED ? NOANCHOR : anchor);
results = execRE(rg, s);
boolean result = checkRE(results, s);
秘results = execRE(rg, s);
boolean result = checkRE(秘results, s);
this.oldLast = last;
return result;
}

private void clearGroups() {
// for (int i = 0; i < groups.length; i++)
// groups[i] = -1;
// for (int i = 0; i < locals.length; i++)
// locals[i] = -1;
秘haveGroups = false;
groups = null;
}

/**
Expand Down Expand Up @@ -607,6 +615,8 @@ private String processRepl(String replacement) {
null);
}
this.replacement = replacement;
if (replacement != null && replacement.indexOf("$") >= 0)
秘updateGroups();
// Process substitution string to replace group references with groups
int index = 0;
int replacementPos = 0;
Expand Down Expand Up @@ -940,6 +950,7 @@ public String group(int group) {
// this array is coming from JavaScript RegExp; some values may be "undefined"
// so we force them to be null.
return results[group] == null ? null : results[group];
// JavaScript does not need this
// if ((groups[group*2] == -1) || (groups[group*2+1] == -1))
// return null;
// return getSubSequence(groups[group * 2], groups[group * 2 + 1]).toString();
Expand Down Expand Up @@ -974,7 +985,7 @@ public String group(int group) {
public String group(String name) {
int group = getMatchedGroupIndex(name);
return group < 0 || group >= results.length ? null : group(group);
//
// JavaScript does not need this
// if ((groups[group*2] == -1) || (groups[group*2+1] == -1))
// return null;
// return getSubSequence(groups[group * 2], groups[group * 2 + 1]).toString();
Expand All @@ -996,9 +1007,35 @@ public String group(String name) {
*/
@Override
public int groupCount() {
秘updateGroups();
return groupCount;
}

private void 秘updateGroups() {
if (秘haveGroups || 秘groupCount <= 0 || 秘results == null)
return;
秘haveGroups = true;
pat.秘setNameGroups();
ArrayList<String> names = pat.秘groupNames;
int pt = start();
groupCount = -1;
groups = new int[names.size() * 2];
results = new String[秘results.length];
for (int i = 0, gpt = 0, n = names.size(); i < n; i++) {
String name = names.get(i);
String r = 秘results[i];
int len = (r == null ? 0 : r.length());
if (name == null || !name.startsWith("秘")) {
groups[gpt++] = pt;
groups[gpt++] = pt + len;
pat.namedGroups().put(name, groupCount);
results[++groupCount] = r;
} else {
pt += len;
}
}
}

/**
* Attempts to match the entire region against the pattern.
*
Expand Down Expand Up @@ -1033,8 +1070,8 @@ boolean match(int from, int anchor) {
return result;
}

private int indexRE(String[] results) {
return /** @j2sNative results.index || */
private int indexRE(String[] r) {
return /** @j2sNative r.index || */
0;
}

Expand All @@ -1050,7 +1087,7 @@ private boolean checkRE(String[] r, String s) {
first = -1;
return false;
}
groupCount = r.length - 1;
秘groupCount = r.length - 1;
int f0 = this.first;
first = indexRE(r);
last = first + r[0].length();
Expand All @@ -1060,7 +1097,7 @@ private boolean checkRE(String[] r, String s) {
// a longer string
return false;
}
if (groupCount < 0)
if (秘groupCount < 0)
return false;
switch (acceptMode) {
case STARTANCHOR:
Expand Down Expand Up @@ -1330,11 +1367,10 @@ int getMatchedGroupIndex(String name) {
Objects.requireNonNull(name, "Group name");
if (first < 0)
throw new IllegalStateException("No match found");
if (pat.namedGroups == null)
pat.秘setNameGroups();
秘updateGroups();
if (pat.namedGroups == null || !pat.namedGroups().containsKey(name))
throw new IllegalArgumentException("No group with name <" + name + ">");
return pat.namedGroups().get(name);
return pat.namedGroups().get(name).intValue() + 1;
}
/**
* Returns the start index of the previous match.
Expand Down Expand Up @@ -1380,9 +1416,9 @@ public int start(int group) {
throw new IllegalStateException("No match available");
if (group < 0 || group > groupCount())
throw new IndexOutOfBoundsException("No group " + group);
return NOT_AVAILABLE;
//
// return groups[group * 2];
if (group == 0)
return start();
return groups[group * 2];
}

/**
Expand All @@ -1404,9 +1440,8 @@ public int start(int group) {
* @since 1.8
*/
public int start(String name) {
getMatchedGroupIndex(name);
return NOT_AVAILABLE;
// return groups[getMatchedGroupIndex(name) * 2];
int g = getMatchedGroupIndex(name);
return groups[g * 2];
}

/**
Expand Down Expand Up @@ -1452,9 +1487,10 @@ public int end(int group) {
throw new IllegalStateException("No match available");
if (group < 0 || group > groupCount())
throw new IndexOutOfBoundsException("No group " + group);
return NOT_AVAILABLE;
if (group == 0)
return end();
return groups[group * 2 + 1];

// return groups[group * 2 + 1];
}

/**
Expand All @@ -1476,9 +1512,8 @@ public int end(int group) {
* @since 1.8
*/
public int end(String name) {
getMatchedGroupIndex(name);
return NOT_AVAILABLE;
// return groups[getMatchedGroupIndex(name) * 2 + 1];
int g = getMatchedGroupIndex(name);
return groups[g * 2 + 1];
}

}
Loading