You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Auto-possessification loses its place after an optional sibling: (?i)(?:a*\+*)A{2} misses "aA" #898
letregex=tryRegex(#"(?:a*\+*)A{2}"#).ignoresCase()print("aA".matches(of:regex).count) // 0, expected 1 (the whole "aA")
It is the same with inline (?i), and with a capture group: (a*\+*)A{2}. Without the group (a*\+*A{2}) it matches, and so does (?:a*)A{2}. Swift on macOS (the OS's own _StringProcessing) matches all of them. I reproduced the miss with _StringProcessing built from the swift-6.4.0-RELEASE tag. It is still present on main.
Cause
In Sources/_StringProcessing/Optimizations/AutoPossessification.swift, the walk loses its place in the flattened list:
In _requiredAtomImpl, the .quantification case returns .some(nil) for an optional quantification (\+*) without moving position past the quantification's child.
The .concatenation case of autoPossessifyNextQuantification then returns the quantification it found (a*) to the enclosing concatenation. That concatenation does position += 1, which lands on the + atom inside \+* rather than on A{2}.
a excludes +, so a* is made possessive, even though a and A are the same when ignoring case. The possessive a* takes all of "aA", and A{2} fails.
More generally, the functions in this file seem to disagree about where position is left on return (on the node, on its last descendant, or past it).
Suggested fix
Make each child step explicit: probe with a copy of the cursor, then skipNode past the child's whole subtree, as in the forward-progress fix. As a stop-gap, not calling root.autoPossessify() in emitRoot restores correct results. It is only an optimization, and Swift on Apple's platforms doesn't have it.
Activity
Fix cursor bookkeeping in auto-possessification