Skip to content

feat(std): regex captures, replacement, split and literal-prefix prefilter - #440

Merged
0xErwin1 merged 2 commits into
mainfrom
feat/regex-captures-prefilter
Oct 28, 2026
Merged

0xErwin1 merged 2 commits into
mainfrom
feat/regex-captures-prefilter

Conversation

@0xErwin1

Copy link
Copy Markdown
Collaborator

The last two slices of std::regex, after the parser (#438) and the Pike VM (#439). The PR has two commits, one per slice; review them separately.

Captures, replacement and split (first commit)

  • Captures:
    • Regex::captures(text) returns Option<Captures>, and capturesAll(text) returns them all.
    • Captures offers group(index), where 0 is the whole match and a group that didn't take part is NONE, plus name(name), whole() and groupCount().
    • A group inside a repetition keeps its last iteration.
    • Regex also gains groupCount(), groupName(index) and groupIndex(name).
    • The VM didn't change: slice 2 already saved every group's slots.
  • Replacement: replace and replaceAll(text, replacement) expand $n, $12, ${n} and ${name}, and $$ is a literal $. Any other $ is literal, including $name without braces. A missing or non-participating group inserts nothing.
  • split: returns the pieces between matches. An empty match splits between scalars, a leading or trailing match leaves an empty piece, and a text with no match comes back whole.
  • Shared search loop: findAll, capturesAll, replaceAll and split use one loop, so their rules for non-overlapping and empty matches agree.

Literal-prefix prefilter and the reference doc (second commit)

  • Prefilter: when every match must start with a literal prefix (literals, through groups and repetitions with a minimum of one), the search jumps to the next occurrence of it whenever no thread is alive, instead of starting a thread at every position. Results are unchanged. The scan is a plain byte search, so the doc's "linear" wording assumes a short prefix.
  • Doc: the module doc is now the reference. It covers the syntax table, the API, leftmost-first semantics, byte offsets, the replacement syntax, the limits (repetition count 1000, program size 100 000), performance, and what is unsupported and why: backreferences and lookaround break linear time, and \w \d \s and (?i) are ASCII-only in v1. The example in it was built and run.

Evidence

  • ignis test-std regex: 46/46.
    • Groups: numbered, named, nested, non-participating; (a|b)+ on "ab" gives group 1 = 1..2.
    • Replacement: every $ form; x* on "abc" with "-" gives "-a-b-c-".
    • split: separators, empty matches, matches at either end, no match.
    • Prefilter: which prefix is extracted, and skipping to it (including the overlap aab in "aaab", é, and foo(bar|baz)).
    • Lazy and case folding: a{2,3}? on "aaaa" gives 0..2; (?i)straße matches "STRAßE" but not "STRASSE" (only ASCII folds).
  • Full test-std 642/642; check-std and fmt --check are clean; the doc baseline is regenerated.

Regex::captures and capturesAll return Captures with numbered and named
groups (a non-participating group is NONE; in a repetition the last
iteration wins). replace and replaceAll expand $n, ${n}, ${name} and $$
(any other $ is literal; a missing group inserts nothing). split returns the
pieces between matches, with empty matches splitting between scalars.
findAll, capturesAll, replaceAll and split share one search loop, so their
non-overlapping and empty-match rules agree.
When every match must start with a literal prefix (literal scalars, through
groups and repetitions with a minimum of one), the search jumps to the next
occurrence of it whenever no thread is alive, instead of starting a thread at
every position. Results are unchanged; the VM still runs in linear time.

The module doc is now the reference: syntax, API, leftmost-first semantics,
byte offsets, replacement syntax, limits, performance, and what is
unsupported and why (backreferences and lookaround break linear time; classes
and (?i) are ASCII in v1). Tests cover lazy counted repetition and the
ASCII-only case folding.
@0xErwin1
0xErwin1 merged commit 892faf8 into main Oct 28, 2026
9 checks passed
@0xErwin1
0xErwin1 deleted the feat/regex-captures-prefilter branch October 28, 2026 20:23
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