Skip to content

Fix bug in word boundary caching #769

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 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 5 additions & 5 deletions Sources/_StringProcessing/Unicode/WordBreaking.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,18 @@ extension String {
}

if #available(SwiftStdlib 5.7, *) {
var indices: Set<String.Index> = []
if cache == nil {
cache = []
}
var j = maxIndex ?? range.lowerBound

while j < range.upperBound, j <= i {
indices.insert(j)
cache!.insert(j)
j = _wordIndex(after: j)
}

cache = indices
maxIndex = j

return indices.contains(i)
return cache!.contains(i)
} else {
return false
}
Expand Down
13 changes: 12 additions & 1 deletion Tests/RegexTests/MatchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2381,7 +2381,18 @@ extension RegexTests {
XCTAssertTrue("cafe".contains(caseInsensitiveRegex))
XCTAssertTrue("CaFe".contains(caseInsensitiveRegex))
}


// https://github.com/swiftlang/swift-experimental-string-processing/issues/768
func testWordBoundaryCaching() throws {
// This will first find word boundaries up til the middle before failing,
// then it will find word boundaries til late in the string, then fail,
// and finally should succeed on a word boundary cached from the first
// attempt.
let input = "first second third fourth"
let regex = try Regex(#".*second\bX|.*third\bX|.*first\b"#)
XCTAssertTrue(input.contains(regex))
}

// MARK: Character Semantics

var eComposed: String { "é" }
Expand Down