Skip to content

Fix range-based quantification fast path #653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions Sources/_StringProcessing/Engine/MEQuantify.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@ extension Processor {
}
}
let next = _doQuantifyMatch(payload)
guard let idx = next else { break }
guard let idx = next else {
if !savePoint.rangeIsEmpty {
// The last save point has saved the current, non-matching position,
// so it's unneeded.
savePoint.shrinkRange(input)
}
break
}
Comment on lines +43 to +50
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the shrinkRange from below to this guard failure — the other break is up on line 36, the shrink shouldn't happen in that case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Latter code has an .eager check, is that needed or unneeded here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unneeded — save points are only ever added when the quantification is eager, so the check is redundant.

currentPosition = idx
trips += 1
}
Expand All @@ -50,12 +57,8 @@ extension Processor {
return false
}

if payload.quantKind == .eager && !savePoint.rangeIsEmpty {
// The last save point has saved the current position, so it's unneeded
savePoint.shrinkRange(input)
if !savePoint.rangeIsEmpty {
savePoints.append(savePoint)
}
if !savePoint.rangeIsEmpty {
savePoints.append(savePoint)
}
return true
}
Expand Down
22 changes: 22 additions & 0 deletions Tests/RegexTests/MatchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2574,4 +2574,26 @@ extension RegexTests {
func testFuzzerArtifacts() throws {
expectCompletion(regex: #"(b?)\1*"#, in: "a")
}

func testIssue640() throws {
// Original report from https://github.com/apple/swift-experimental-string-processing/issues/640
let r = try Regex("[1-9][0-9]{0,2}(?:,?[0-9]{3})*")
XCTAssertNotNil("36,769".wholeMatch(of: r))
XCTAssertNotNil("36769".wholeMatch(of: r))

for max in 1...8 {
let pattern = "a{0,\(max)}a"
let regex = try Regex(pattern)
for length in 1...(max + 1) {
let str = String(repeating: "a", count: length)
if str.wholeMatch(of: regex) == nil {
XCTFail("Didn't match '\(pattern)' in '\(str)' (\(max),\(length)).")
}
}

let possessiveRegex = try Regex("a{0,\(max)}+a")
let str = String(repeating: "a", count: max + 1)
XCTAssertNotNil(str.wholeMatch(of: possessiveRegex))
}
}
}