8305785: Avoid redundant HashMap.containsKey call in java.util.regex

Reviewed-by: stsypanov, jpai
This commit is contained in:
Andrey Turbanov 2023-05-22 10:27:13 +00:00
parent 8011ba74a2
commit 6b65e5754c
2 changed files with 10 additions and 7 deletions

View file

@ -1070,10 +1070,11 @@ public final class Matcher implements MatchResult {
throw new IllegalArgumentException(
"capturing group name {" + gname +
"} starts with digit character");
if (!namedGroups().containsKey(gname))
Integer number = namedGroups().get(gname);
if (number == null)
throw new IllegalArgumentException(
"No group with name {" + gname + "}");
refNum = namedGroups().get(gname);
refNum = number;
cursor++;
} else {
// The first number is always a group
@ -1805,9 +1806,10 @@ public final class Matcher implements MatchResult {
int getMatchedGroupIndex(String name) {
Objects.requireNonNull(name, "Group name");
checkMatch();
if (!namedGroups().containsKey(name))
Integer number = namedGroups().get(name);
if (number == null)
throw new IllegalArgumentException("No group with name <" + name + ">");
return namedGroups().get(name);
return number;
}
private void checkGroup(int group) {

View file

@ -2712,14 +2712,15 @@ loop: for(int x=0, offset=0; x<nCodePoints; x++, offset+=len) {
if (read() != '<')
throw error("\\k is not followed by '<' for named capturing group");
String name = groupname(read());
if (!namedGroupsMap().containsKey(name))
Integer number = namedGroupsMap().get(name);
if (number == null)
throw error("named capturing group <" + name + "> does not exist");
if (create) {
hasGroupRef = true;
if (has(CASE_INSENSITIVE))
root = new CIBackRef(namedGroupsMap().get(name), has(UNICODE_CASE));
root = new CIBackRef(number, has(UNICODE_CASE));
else
root = new BackRef(namedGroupsMap().get(name));
root = new BackRef(number);
}
return -1;
case 'l':