Skip to content

Commit dd8c522

Browse files
committed
Add Regex<Match, Captures> alternative to regex proposal
1 parent 5ec5001 commit dd8c522

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

Documentation/Evolution/RegexTypeOverview.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,12 @@ We're also looking for more community discussion on what the default type system
482482

483483
The actual `Match` struct just stores ranges: the `Substrings` are lazily created on demand. This avoids unnecessary ARC traffic and memory usage.
484484

485+
### `Regex<Match, Captures>` instead of `Regex<Output>`
486+
487+
The generic parameter `Output` is proposed to contain both the whole match (the `.0` element if `Output` is a tuple) and captures. One alternative we have considered is separating `Output` into the entire match and the captures, i.e. `Regex<Match, Captures>`, and using `Void` for for `Captures` when there are no captures.
488+
489+
The biggest issue with this alternative design is that it misaligns with capturing numbering in textual regexes, where backreference `\0` refers to the entire match and captures start at `\1`. This design would sacrifice familarity and have the pitfall of introducing off-by-one errors.
490+
485491
### Future work: static optimization and compilation
486492

487493
Swift's support for static compilation is still developing, and future work here is leveraging that to compile regex when profitable. Many regex describe simple [DFAs](https://en.wikipedia.org/wiki/Deterministic_finite_automaton) and can be statically compiled into very efficient programs. Full static compilation needs to be balanced with code size concerns, as a matching-specific bytecode is typically far smaller than a corresponding program (especially since the bytecode interpreter is shared).

0 commit comments

Comments
 (0)