Skip to content

Add regex-specific Matches and Ranges collections #460

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 14 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
27 changes: 24 additions & 3 deletions Sources/_StringProcessing/Algorithms/Algorithms/Ranges.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,38 @@ extension BidirectionalCollection where Element: Comparable {
// }
}

@available(SwiftStdlib 5.7, *)
struct RegexRangesCollection<Output> {
let base: RegexMatchesCollection<Output>

init(string: Substring, regex: Regex<Output>) {
self.base = RegexMatchesCollection(base: string, regex: regex)
}
}

@available(SwiftStdlib 5.7, *)
extension RegexRangesCollection: Collection {
typealias Index = RegexMatchesCollection<Output>.Index

var startIndex: Index { base.startIndex }
var endIndex: Index { base.endIndex }
func index(after i: Index) -> Index { base.index(after: i) }
subscript(position: Index) -> Range<String.Index> { base[position].range }
}

// MARK: Regex algorithms

extension BidirectionalCollection where SubSequence == Substring {
extension Collection where SubSequence == Substring {
@available(SwiftStdlib 5.7, *)
@_disfavoredOverload
func _ranges<R: RegexComponent>(
of regex: R
) -> RangesCollection<RegexConsumer<R, Self>> {
_ranges(of: RegexConsumer(regex))
) -> RegexRangesCollection<R.RegexOutput> {
RegexRangesCollection(string: self[...], regex: regex.regex)
}
}

extension BidirectionalCollection where SubSequence == Substring {
@available(SwiftStdlib 5.7, *)
func _rangesFromBack<R: RegexComponent>(
of regex: R
Expand Down
43 changes: 12 additions & 31 deletions Sources/_StringProcessing/Algorithms/Algorithms/Replace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,19 @@
// MARK: `CollectionSearcher` algorithms

extension RangeReplaceableCollection {
func _replacing<Searcher: CollectionSearcher, Replacement: Collection>(
_ searcher: Searcher,
func _replacing<Ranges: Collection, Replacement: Collection>(
_ ranges: Ranges,
with replacement: Replacement,
subrange: Range<Index>,
maxReplacements: Int = .max
) -> Self where Searcher.Searched == SubSequence,
) -> Self where Ranges.Element == Range<Index>,
Replacement.Element == Element
{
precondition(maxReplacements >= 0)

var index = subrange.lowerBound
var result = Self()
result.append(contentsOf: self[..<index])
var index = startIndex

for range in self[subrange]._ranges(of: searcher).prefix(maxReplacements) {
for range in ranges.prefix(maxReplacements) {
result.append(contentsOf: self[index..<range.lowerBound])
result.append(contentsOf: replacement)
index = range.upperBound
Expand All @@ -36,29 +34,15 @@ extension RangeReplaceableCollection {
return result
}

func _replacing<Searcher: CollectionSearcher, Replacement: Collection>(
_ searcher: Searcher,
with replacement: Replacement,
maxReplacements: Int = .max
) -> Self where Searcher.Searched == SubSequence,
Replacement.Element == Element
{
_replacing(
searcher,
with: replacement,
subrange: startIndex..<endIndex,
maxReplacements: maxReplacements)
}

mutating func _replace<
Searcher: CollectionSearcher, Replacement: Collection
Ranges: Collection, Replacement: Collection
>(
_ searcher: Searcher,
_ ranges: Ranges,
with replacement: Replacement,
maxReplacements: Int = .max
) where Searcher.Searched == SubSequence, Replacement.Element == Element {
) where Ranges.Element == Range<Index>, Replacement.Element == Element {
self = _replacing(
searcher,
ranges,
with: replacement,
maxReplacements: maxReplacements)
}
Expand All @@ -85,9 +69,8 @@ extension RangeReplaceableCollection where Element: Equatable {
maxReplacements: Int = .max
) -> Self where C.Element == Element, Replacement.Element == Element {
_replacing(
ZSearcher(pattern: Array(other), by: ==),
self[subrange]._ranges(of: other),
with: replacement,
subrange: subrange,
maxReplacements: maxReplacements)
}

Expand Down Expand Up @@ -143,9 +126,8 @@ extension RangeReplaceableCollection
maxReplacements: Int = .max
) -> Self where C.Element == Element, Replacement.Element == Element {
_replacing(
PatternOrEmpty(searcher: TwoWaySearcher(pattern: Array(other))),
self[subrange]._ranges(of: other),
with: replacement,
subrange: subrange,
maxReplacements: maxReplacements)
}

Expand Down Expand Up @@ -195,9 +177,8 @@ extension RangeReplaceableCollection where SubSequence == Substring {
maxReplacements: Int = .max
) -> Self where Replacement.Element == Element {
_replacing(
RegexConsumer(regex),
self[subrange]._ranges(of: regex),
with: replacement,
subrange: subrange,
maxReplacements: maxReplacements)
}

Expand Down
123 changes: 96 additions & 27 deletions Sources/_StringProcessing/Algorithms/Matching/Matches.swift
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,106 @@ extension BidirectionalCollection {

// MARK: Regex algorithms

@available(SwiftStdlib 5.7, *)
struct RegexMatchesCollection<Output> {
let base: Substring
let regex: Regex<Output>
let startIndex: Index

init(base: Substring, regex: Regex<Output>) {
self.base = base
self.regex = regex
self.startIndex = base.firstMatch(of: regex).map(Index.match) ?? .end
}
}

@available(SwiftStdlib 5.7, *)
extension RegexMatchesCollection: Collection {
enum Index: Comparable {
case match(Regex<Output>.Match)
case end

static func == (lhs: Self, rhs: Self) -> Bool {
switch (lhs, rhs) {
case (.match(let lhs), .match(let rhs)):
return lhs.range == rhs.range
case (.end, .end):
return true
case (.end, .match), (.match, .end):
return false
}
}

static func < (lhs: Self, rhs: Self) -> Bool {
switch (lhs, rhs) {
case (.match(let lhs), .match(let rhs)):
// This implementation uses a tuple comparison so that an empty
// range `i..<i` will be ordered before a non-empty range at that
// same starting point `i..<j`. As of 2022-05-30, `Regex` does not
// return matches of this kind, but that is one behavior under
// discussion for regexes like /a*|b/ when matched against "b".
return (lhs.range.lowerBound, lhs.range.upperBound)
< (rhs.range.lowerBound, rhs.range.upperBound)
case (.match, .end):
return true
case (.end, .match), (.end, .end):
return false
}
}
}

var endIndex: Index {
Index.end
}

func index(after i: Index) -> Index {
let currentMatch: Element
switch i {
case .match(let match):
currentMatch = match
case .end:
fatalError("Can't advance past the 'endIndex' of a match collection.")
}

let start: String.Index
if currentMatch.range.isEmpty {
if currentMatch.range.lowerBound == base.endIndex {
return .end
}

switch regex.initialOptions.semanticLevel {
case .graphemeCluster:
start = base.index(after: currentMatch.range.upperBound)
case .unicodeScalar:
start = base.unicodeScalars.index(after: currentMatch.range.upperBound)
}
} else {
start = currentMatch.range.upperBound
}

guard let nextMatch = try? regex.firstMatch(in: base[start...]) else {
return .end
}
return Index.match(nextMatch)
}

subscript(position: Index) -> Regex<Output>.Match {
switch position {
case .match(let match):
return match
case .end:
fatalError("Can't subscript the 'endIndex' of a match collection.")
}
}
}

extension BidirectionalCollection where SubSequence == Substring {
@available(SwiftStdlib 5.7, *)
@_disfavoredOverload
func _matches<R: RegexComponent>(
of regex: R
) -> MatchesCollection<RegexConsumer<R, Self>> {
_matches(of: RegexConsumer(regex))
) -> RegexMatchesCollection<R.RegexOutput> {
RegexMatchesCollection(base: self[...], regex: regex.regex)
}

@available(SwiftStdlib 5.7, *)
Expand All @@ -207,30 +300,6 @@ extension BidirectionalCollection where SubSequence == Substring {
public func matches<Output>(
of r: some RegexComponent<Output>
) -> [Regex<Output>.Match] {
let slice = self[...]
var start = self.startIndex
let end = self.endIndex
let regex = r.regex

var result = [Regex<Output>.Match]()
while start <= end {
guard let match = try? regex._firstMatch(
slice.base, in: start..<end
) else {
break
}
result.append(match)
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
Array(_matches(of: r))
}

}
19 changes: 19 additions & 0 deletions Tests/RegexTests/AlgorithmsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,25 @@ class AlgorithmTests: XCTestCase {
s2.ranges(of: try Regex("a*?")).map(s2.offsets(of:)), [0..<0, 1..<1, 2..<2])
}

func testUnicodeScalarSemantics() throws {
let regex = try Regex(#"(?u)."#, as: Substring.self)
let emptyRegex = try Regex(#"(?u)z?"#, as: Substring.self)

XCTAssertEqual("".matches(of: regex).map(\.output), [])
XCTAssertEqual("Café".matches(of: regex).map(\.output), ["C", "a", "f", "é"])
XCTAssertEqual("Cafe\u{301}".matches(of: regex).map(\.output), ["C", "a", "f", "e", "\u{301}"])
XCTAssertEqual("Cafe\u{301}".matches(of: emptyRegex).count, 6)

XCTAssertEqual("Café".ranges(of: regex).count, 4)
XCTAssertEqual("Cafe\u{301}".ranges(of: regex).count, 5)
XCTAssertEqual("Cafe\u{301}".ranges(of: emptyRegex).count, 6)

XCTAssertEqual("Café".replacing(regex, with: "-"), "----")
XCTAssertEqual("Cafe\u{301}".replacing(regex, with: "-"), "-----")
XCTAssertEqual("Café".replacing(emptyRegex, with: "-"), "-C-a-f-é-")
XCTAssertEqual("Cafe\u{301}".replacing(emptyRegex, with: "-"), "-C-a-f-e-\u{301}-")
}

func testSwitches() {
switch "abcde" {
case try! Regex("a.*f"):
Expand Down