8197462: Inconsistent exception messages for invalid capturing group names

8179608: Error in comment in Pattern.java

Reviewed-by: sherman
This commit is contained in:
Ivan Gerasimov 2018-02-12 21:06:06 -08:00
parent bc690b263b
commit 7d7c653abf
2 changed files with 49 additions and 18 deletions

View file

@ -782,12 +782,9 @@ public final class Pattern
* arguments, they can also be passed as inline modifiers. * arguments, they can also be passed as inline modifiers.
* For example, the following statements have the same effect. * For example, the following statements have the same effect.
* <pre> * <pre>
* RegExp r1 = RegExp.compile("abc", Pattern.I|Pattern.M); * Pattern p1 = Pattern.compile("abc", Pattern.CASE_INSENSITIVE|Pattern.MULTILINE);
* RegExp r2 = RegExp.compile("(?im)abc", 0); * Pattern p2 = Pattern.compile("(?im)abc", 0);
* </pre> * </pre>
*
* The flags are duplicated so that the familiar Perl match flag
* names are available.
*/ */
/** /**
@ -2527,7 +2524,7 @@ loop: for(int x=0, offset=0; x<nCodePoints; x++, offset+=len) {
throw error("\\k is not followed by '<' for named capturing group"); throw error("\\k is not followed by '<' for named capturing group");
String name = groupname(read()); String name = groupname(read());
if (!namedGroups().containsKey(name)) if (!namedGroups().containsKey(name))
throw error("(named capturing group <"+ name+"> does not exit"); throw error("named capturing group <" + name + "> does not exist");
if (create) { if (create) {
hasGroupRef = true; hasGroupRef = true;
if (has(CASE_INSENSITIVE)) if (has(CASE_INSENSITIVE))
@ -2922,13 +2919,11 @@ loop: for(int x=0, offset=0; x<nCodePoints; x++, offset+=len) {
*/ */
private String groupname(int ch) { private String groupname(int ch) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(Character.toChars(ch)); if (!ASCII.isAlpha(ch))
while (ASCII.isLower(ch=read()) || ASCII.isUpper(ch) || throw error("capturing group name does not start with a Latin letter");
ASCII.isDigit(ch)) { do {
sb.append(Character.toChars(ch)); sb.append((char) ch);
} } while (ASCII.isAlnum(ch=read()));
if (sb.length() == 0)
throw error("named capturing group has 0 length name");
if (ch != '>') if (ch != '>')
throw error("named capturing group is missing trailing '>'"); throw error("named capturing group is missing trailing '>'");
return sb.toString(); return sb.toString();
@ -2974,7 +2969,7 @@ loop: for(int x=0, offset=0; x<nCodePoints; x++, offset+=len) {
break; break;
case '<': // (?<xxx) look behind case '<': // (?<xxx) look behind
ch = read(); ch = read();
if (ASCII.isLower(ch) || ASCII.isUpper(ch)) { if (ch != '=' && ch != '!') {
// named captured group // named captured group
String name = groupname(ch); String name = groupname(ch);
if (namedGroups().containsKey(name)) if (namedGroups().containsKey(name))
@ -3005,14 +3000,12 @@ loop: for(int x=0, offset=0; x<nCodePoints; x++, offset+=len) {
info.minLength) : info.minLength) :
new Behind(head, info.maxLength, new Behind(head, info.maxLength,
info.minLength)); info.minLength));
} else if (ch == '!') { } else { // if (ch == '!')
head = tail = (hasSupplementary ? head = tail = (hasSupplementary ?
new NotBehindS(head, info.maxLength, new NotBehindS(head, info.maxLength,
info.minLength) : info.minLength) :
new NotBehind(head, info.maxLength, new NotBehind(head, info.maxLength,
info.minLength)); info.minLength));
} else {
throw error("Unknown look-behind group");
} }
// clear all top-closure-nodes inside lookbehind // clear all top-closure-nodes inside lookbehind
if (saveTCNCount < topClosureNodes.size()) if (saveTCNCount < topClosureNodes.size())

View file

@ -35,7 +35,7 @@
* 8027645 8035076 8039124 8035975 8074678 6854417 8143854 8147531 7071819 * 8027645 8035076 8039124 8035975 8074678 6854417 8143854 8147531 7071819
* 8151481 4867170 7080302 6728861 6995635 6736245 4916384 6328855 6192895 * 8151481 4867170 7080302 6728861 6995635 6736245 4916384 6328855 6192895
* 6345469 6988218 6693451 7006761 8140212 8143282 8158482 8176029 8184706 * 6345469 6988218 6693451 7006761 8140212 8143282 8158482 8176029 8184706
* 8194667 * 8194667 8197462
* *
* @library /test/lib * @library /test/lib
* @build jdk.test.lib.RandomFactory * @build jdk.test.lib.RandomFactory
@ -168,6 +168,7 @@ public class RegExTest {
embeddedFlags(); embeddedFlags();
grapheme(); grapheme();
expoBacktracking(); expoBacktracking();
invalidGroupName();
if (failure) { if (failure) {
throw new throw new
@ -4870,4 +4871,41 @@ public class RegExTest {
} }
} }
} }
private static void invalidGroupName() {
// Invalid start of a group name
for (String groupName : List.of("", ".", "0", "\u0040", "\u005b",
"\u0060", "\u007b", "\u0416")) {
for (String pat : List.of("(?<" + groupName + ">)",
"\\k<" + groupName + ">")) {
try {
Pattern.compile(pat);
failCount++;
} catch (PatternSyntaxException e) {
if (!e.getMessage().startsWith(
"capturing group name does not start with a"
+ " Latin letter")) {
failCount++;
}
}
}
}
// Invalid char in a group name
for (String groupName : List.of("a.", "b\u0040", "c\u005b",
"d\u0060", "e\u007b", "f\u0416")) {
for (String pat : List.of("(?<" + groupName + ">)",
"\\k<" + groupName + ">")) {
try {
Pattern.compile(pat);
failCount++;
} catch (PatternSyntaxException e) {
if (!e.getMessage().startsWith(
"named capturing group is missing trailing '>'")) {
failCount++;
}
}
}
}
report("Invalid capturing group names");
}
} }