Now store vFlagEnabled instead of each time searching for it.

Added `uFlagEnabled` for checking how should `\p{}` be treated. And small optimization.
This commit is contained in:
Napalys
2025-03-05 10:34:38 +01:00
parent 8086c25abe
commit 95d05ceab8

View File

@@ -71,7 +71,8 @@ public class RegExpParser {
private List<Error> errors; private List<Error> errors;
private List<BackReference> backrefs; private List<BackReference> backrefs;
private int maxbackref; private int maxbackref;
private String flags; private Boolean vFlagEnabled = false;
private Boolean uFlagEnabled = false;
/** Parse the given string as a regular expression. */ /** Parse the given string as a regular expression. */
public Result parse(String src) { public Result parse(String src) {
@@ -88,7 +89,8 @@ public class RegExpParser {
} }
public Result parse(String src, String flags) { public Result parse(String src, String flags) {
this.flags = flags; vFlagEnabled = flags != null && flags.contains("v");
uFlagEnabled = flags != null && flags.contains("u");
return parse(src); return parse(src);
} }
@@ -300,7 +302,7 @@ public class RegExpParser {
private RegExpTerm parseAlternativeInsideQuotedString() { private RegExpTerm parseAlternativeInsideQuotedString() {
SourceLocation loc = new SourceLocation(pos()); SourceLocation loc = new SourceLocation(pos());
StringBuilder sb = new StringBuilder(); int startPos = this.pos;
boolean escaped = false; boolean escaped = false;
while (true) { while (true) {
// If we're at the end of the string, something went wrong. // If we're at the end of the string, something went wrong.
@@ -316,10 +318,8 @@ public class RegExpParser {
char c = this.nextChar(); char c = this.nextChar();
// Track whether the character is an escape character. // Track whether the character is an escape character.
escaped = !escaped && (c == '\\'); escaped = !escaped && (c == '\\');
sb.append(c);
} }
String literal = src.substring(startPos, pos);
String literal = sb.toString();
loc.setEnd(pos()); loc.setEnd(pos());
loc.setSource(literal); loc.setSource(literal);
@@ -470,13 +470,13 @@ public class RegExpParser {
return this.finishTerm(new NamedBackReference(loc, name, "\\k<" + name + ">")); return this.finishTerm(new NamedBackReference(loc, name, "\\k<" + name + ">"));
} }
if (this.match("q{")) { if (vFlagEnabled && this.match("q{")) {
RegExpTerm term = parseDisjunctionInsideQuotedString(); RegExpTerm term = parseDisjunctionInsideQuotedString();
this.expectRBrace(); this.expectRBrace();
return this.finishTerm(new CharacterClassQuotedString(loc, term)); return this.finishTerm(new CharacterClassQuotedString(loc, term));
} }
if (this.match("p{", "P{")) { if ((vFlagEnabled || uFlagEnabled) && this.match("p{", "P{")) {
String name = this.readIdentifier(); String name = this.readIdentifier();
if (this.match("=")) { if (this.match("=")) {
value = this.readIdentifier(); value = this.readIdentifier();
@@ -548,7 +548,7 @@ public class RegExpParser {
} }
private RegExpTerm parseCharacterClass() { private RegExpTerm parseCharacterClass() {
if (flags != null && flags.contains("v")) return parseNestedCharacterClass(); if (vFlagEnabled) return parseNestedCharacterClass();
SourceLocation loc = new SourceLocation(pos()); SourceLocation loc = new SourceLocation(pos());
List<RegExpTerm> elements = new ArrayList<>(); List<RegExpTerm> elements = new ArrayList<>();
@@ -583,20 +583,10 @@ public class RegExpParser {
this.error(Error.EXPECTED_RBRACKET); this.error(Error.EXPECTED_RBRACKET);
break; break;
} }
if (lookahead("[")) { if (lookahead("[")) elements.add(parseNestedCharacterClass());
elements.add(parseNestedCharacterClass()); else if (this.match("&&")) classType = CharacterClassType.INTERSECTION;
} else if (this.match("--")) classType = CharacterClassType.SUBTRACTION;
else if (lookahead("&&")) { else elements.add(this.parseCharacterClassElement());
this.match("&&");
classType = CharacterClassType.INTERSECTION;
}
else if (lookahead("--")) {
this.match("--");
classType = CharacterClassType.SUBTRACTION;
}
else {
elements.add(this.parseCharacterClassElement());
}
} }
// Create appropriate RegExpTerm based on the detected class type // Create appropriate RegExpTerm based on the detected class type