Skip to content

Commit

Permalink
TRegex: minor cleanups and more assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
djoooooe committed May 31, 2024
1 parent f5b8bd7 commit a19a609
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public static void caseClosure(CaseFoldData.CaseFoldAlgorithm algorithm, CodePoi
CodePointSet allowedCodePoints, boolean transitiveEquivalence) {
tmp.clear();
caseFoldCharClass(algorithm, charClass, (from, to) -> {
if ((transitiveEquivalence || hasNoCaseFolding(algorithm, to[0]))) {
if (transitiveEquivalence || hasNoCaseFolding(algorithm, to[0])) {
if (to.length == 1) {
// Add the case-folded version to the character class...
if (filter.test(from, to[0])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,12 @@ public void setEndOfString() {
}

public void clear() {
assert codepoints == null && !isEndOfString : "clear can only be called on the root node";
children.clear();
}

public void generateAST(RegexASTBuilder astBuilder, boolean negate) {
if (needsGroupWrapper(negate)) {
if (rootNeedsGroupWrapper(negate)) {
astBuilder.pushGroup();
}
if (negate) {
Expand Down Expand Up @@ -217,7 +218,7 @@ public void generateAST(RegexASTBuilder astBuilder, boolean negate) {
}
}
}
if (needsGroupWrapper(negate)) {
if (rootNeedsGroupWrapper(negate)) {
astBuilder.popGroup();
}
}
Expand Down Expand Up @@ -265,4 +266,8 @@ private void generateASTInner(RegexASTBuilder astBuilder, boolean negate) {
private boolean needsGroupWrapper(boolean negate) {
return children.size() > 1 || !negate && isEndOfString() && !children.isEmpty();
}

private boolean rootNeedsGroupWrapper(boolean negate) {
return needsGroupWrapper(negate) || negate && children.size() == 1 && !children.get(0).isEndOfString;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -253,16 +253,16 @@ private void addCCAtomIgnoreCase(ClassSetContents contents) {
private void addCCAtomMultiCharExpansion(ClassSetContents contents, CaseFoldAlgorithm algorithm) {
caseClosure(algorithm, contents.getCodePointSet());
addCCAtomCodePointSet(charClassTmpCaseClosure.toCodePointSet());
assert contents.isCharacter() || contents.isPosixCollationElement() || contents.isPosixCollationEquivalenceClass();
assert contents.getCodePointSet().matchesSingleChar();
// No transitive closure
for (Range range : contents.getCodePointSet()) {
CaseFoldData.getTable(algorithm).caseFold(range, (codepoint, caseFolded) -> {
if (caseFolded.length > 1) {
CodePointSet encodingRange = Encodings.UTF_8.getFullSet();
CompilationBuffer compilationBuffer = lexer.getCompilationBuffer();
MultiCharacterCaseFolding.caseFoldUnfoldString(algorithm, caseFolded, encodingRange, false, false, null, curCharClass, compilationBuffer);
}
});
}
CaseFoldData.getTable(algorithm).caseFold(contents.getCodePointSet().iterator().next(), (codepoint, caseFolded) -> {
if (caseFolded.length > 1) {
CodePointSet encodingRange = Encodings.UTF_8.getFullSet();
CompilationBuffer compilationBuffer = lexer.getCompilationBuffer();
MultiCharacterCaseFolding.caseFoldUnfoldString(algorithm, caseFolded, encodingRange, false, false, null, curCharClass, compilationBuffer);
}
});
}

private void caseClosure(CaseFoldAlgorithm algorithm, CodePointSet codePointSet) {
Expand Down

0 comments on commit a19a609

Please sign in to comment.