Repository navigation
feat(std): regex captures, replacement, split and literal-prefix prefilter - #440
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)
Regex::captures(text)returnsOption<Captures>, andcapturesAll(text)returns them all.Capturesoffersgroup(index), where 0 is the whole match and a group that didn't take part isNONE, plusname(name),whole()andgroupCount().Regexalso gainsgroupCount(),groupName(index)andgroupIndex(name).replaceandreplaceAll(text, replacement)expand$n,$12,${n}and${name}, and$$is a literal$. Any other$is literal, including$namewithout 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.findAll,capturesAll,replaceAllandsplituse one loop, so their rules for non-overlapping and empty matches agree.Literal-prefix prefilter and the reference doc (second commit)
\w \d \sand(?i)are ASCII-only in v1. The example in it was built and run.Evidence
ignis test-std regex: 46/46.(a|b)+on"ab"gives group 1 =1..2.$form;x*on"abc"with"-"gives"-a-b-c-".split: separators, empty matches, matches at either end, no match.aabin"aaab",é, andfoo(bar|baz)).a{2,3}?on"aaaa"gives0..2;(?i)straßematches"STRAßE"but not"STRASSE"(only ASCII folds).test-std642/642;check-stdandfmt --checkare clean; the doc baseline is regenerated.