Skip to content
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
12 changes: 10 additions & 2 deletions Sources/_StringProcessing/Algorithms/Matching/Matches.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,22 @@ extension BidirectionalCollection where SubSequence == Substring {
let regex = r.regex

var result = [Regex<R.RegexOutput>.Match]()
while start < end {
while start <= end {
guard let match = try? regex._firstMatch(
slice.base, in: start..<end
) else {
break
}
result.append(match)
start = match.range.upperBound
if match.range.isEmpty {
if match.range.upperBound == end {
break
}
// FIXME: semantic level
start = slice.index(after: match.range.upperBound)
} else {
start = match.range.upperBound
}
}
return result
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/_StringProcessing/Regex/Match.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,13 @@ extension Regex {

var low = inputRange.lowerBound
let high = inputRange.upperBound
while low < high {
while true {
if let m = try _match(input, in: low..<high, mode: .partialFromFront) {
return m
}
if low == high { return nil }
input.formIndex(after: &low)
}
return nil
}
}

Expand Down
8 changes: 8 additions & 0 deletions Tests/RegexTests/AlgorithmsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ class AlgorithmTests: XCTestCase {
let actualCol: [Range<Int>] = string[...].ranges(of: regex)[...].map(string.offsets(of:))
XCTAssertEqual(actualCol, expected, file: file, line: line)

let matchRanges = string.matches(of: regex).map { string.offsets(of: $0.range) }
XCTAssertEqual(matchRanges, expected, file: file, line: line)

let firstRange = string.firstRange(of: regex).map(string.offsets(of:))
XCTAssertEqual(firstRange, expected.first, file: file, line: line)
}
Expand Down Expand Up @@ -332,6 +335,11 @@ class AlgorithmTests: XCTestCase {
XCTAssertEqual(
s2.matches(of: regex).map(\.0),
["aa"])

XCTAssertEqual(
s2.matches(of: try Regex("a*?")).map { s2.offsets(of: $0.range) }, [0..<0, 1..<1, 2..<2])
XCTAssertEqual(
s2.ranges(of: try Regex("a*?")).map(s2.offsets(of:)), [0..<0, 1..<1, 2..<2])
}

func testSwitches() {
Expand Down