apple / swift-experimental-string-processing

An early experimental general-purpose pattern matching engine for Swift.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Off by one error for `Repeat(...)`

hamishknight opened this issue · comments

From https://forums.swift.org/t/regexbuilder-repeat-range-off-by-one/58079:

import RegexBuilder

let s = "aaaaaaaaaa"
let rx = Regex {
    Repeat(0...1) { "a" }
}
s.firstMatch(of: rx) // ["aa"]

Looks like we're converting 0...1 to 0..<2 and then initializing a quantification .upToN without adjusting the upper bound down.

@milseman maybe a bug in the quantification algorithm in ByteCodeGen?

As Hamish said, 0...1 is converted to 0..<2 which is fine, but that is then converted to .upToN(2) which is not, because that means {,2}.

    case (0, _): // 0..<n or 0...n or ..<n or ...n
      return .quantification(.upToN(range.upperBound), kind, node)

Those can't all be the same notion of n.