Skip to content

fix(regex): accept an escaped endpoint in a character-class range - #139

Merged
BuddhiLW merged 3 commits into
stagingfrom
fix/regex-class-escaped-range
Dec 1, 2026
Merged

BuddhiLW merged 3 commits into
stagingfrom
fix/regex-class-escaped-range

Conversation

@BuddhiLW

@BuddhiLW BuddhiLW commented Dec 1, 2026 •

Copy link
Copy Markdown
Owner

Fixes #135.

The defect

  • A range that starts with an escaped character, such as [\]-~], compiled and silently never matched. The parser added the escaped literal as a lone member and went on, so the class became the three members ], - and ~.
  • A range that ends with an escaped character, such as [&-\[], did not compile: it returned NotImplemented, which surfaces as the uncatchable unsupported-feature error (see The regex "not supported" error cannot be caught by try and ends the process #136).

The fix

parseClassSet in src/runtime/regex/compile.zig reads every member through one function, parseClassMember. It yields either one character (a plain byte, a UTF-8 codepoint, \uXXXX or an escaped literal) or a class escape such as \d. Any character may bound a range. addClassRange splits the range between the byte bitmap and the codepoint set.

That also fixes, as in java.util.regex:

  • a range that crosses from ASCII into Unicode ([a-é]), which used to set a byte range up to the first UTF-8 byte;
  • a - before a nested class ([a-[b]]), which is a literal;
  • a class escape as a range end ([a-\d]), now the catchable invalid-pattern error.

compile.zig gets 6 lines longer overall (104 changed: 55 added, 49 removed).

Checks

Built with zig build -Dwasm -Doptimize=ReleaseSafe (Zig 0.16.0):

  • test/clj/run_suites.clj: 171 suites loaded, 1004 tests, 5410 assertions, 0 failures, 0 errors
  • scripts/corpus_regression.clj: 5529 of 5529 golden outputs reproduced, including the new regex_class_escaped_range.txt
  • test/e2e/regex_parity_gaps.sh 3 of 3, test/e2e/phase14_re_matcher.sh passes
  • zig build test and zig fmt --check pass

Every expected value in the new suite cases and the corpus file was read from java.util.regex (through babashka).

Not in this PR

Please do not merge before release PR #138 lands: a merge into staging moves its head and restarts both gate jobs.

A range that started with an escaped character (`[\]-~]`) was parsed as
three separate members, so it compiled and silently never matched. A range
that ended with an escaped character (`[&-\[]`) returned NotImplemented,
which surfaces as the uncatchable unsupported-feature error.

parseClassSet now reads every member through parseClassMember, which
yields either one character (a plain byte, a UTF-8 codepoint, `\uXXXX` or
an escaped literal) or a class escape. Any character may bound a range;
addClassRange splits it between the byte bitmap and the codepoint set, so
a range may also cross from ASCII into Unicode (`[a-é]`). A `-` before a
nested class is a literal, and a class escape as a range end is the
catchable invalid-pattern error, both as in java.util.regex.

Expected values in the suite and the corpus were read from java.util.regex.

Fixes #135
@BuddhiLW
BuddhiLW merged commit f528181 into staging Dec 1, 2026
3 checks passed
@BuddhiLW
BuddhiLW deleted the fix/regex-class-escaped-range branch December 1, 2026 18:58
BuddhiLW added a commit that referenced this pull request Dec 1, 2026
No textual conflicts. build.zig.zon takes staging's version 1.15.9 and
keeps this branch's minimum_zig_version, zlinter and zwasm pins.
CHANGELOG keeps staging's [1.15.9] section unchanged and its Unreleased
entries; the auto-merge had folded this branch's four Zig 0.17.1 /
zwasm v2.12.1 entries into [1.15.9], so they are moved back under
Unreleased, since they did not ship in 1.15.9.

The incoming regex (#139, #142) and require (#141) code uses no API
0.17.1 removed or deprecated, so it builds unchanged: the 0.17 rename
pass over src touches nothing, `zig fmt --check` is clean, and the
-Dwasm ReleaseSafe build is green. All 171 clj suites pass (1006 tests,
5420 assertions).

Smell-audited: 0: merge only; build.zig.zon takes staging's version, no Zig source changed by the resolution
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant