Skip to content

[5.7] Fix anchor bugs, de-genericize processor, add ranges collection #531

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 7 commits into from
Jul 1, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ extension BidirectionalCollection where SubSequence == Substring {
@_disfavoredOverload
@available(SwiftStdlib 5.7, *)
public func contains(_ regex: some RegexComponent) -> Bool {
_contains(RegexConsumer(regex))
(try? regex.regex.firstMatch(in: self[...])) != nil
}
}
68 changes: 65 additions & 3 deletions Sources/_StringProcessing/Algorithms/Algorithms/Ranges.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,79 @@ extension BidirectionalCollection where Element: Comparable {
// }
}

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

init(
input: String,
subjectBounds: Range<String.Index>,
searchBounds: Range<String.Index>,
regex: Regex<Output>
) {
self.base = .init(
input: input,
subjectBounds: subjectBounds,
searchBounds: searchBounds,
regex: regex)
}
}

@available(SwiftStdlib 5.7, *)
extension RegexRangesCollection: Sequence {
struct Iterator: IteratorProtocol {
var matchesBase: RegexMatchesCollection<Output>.Iterator

mutating func next() -> Range<String.Index>? {
matchesBase.next().map(\.range)
}
}

func makeIterator() -> Iterator {
Iterator(matchesBase: base.makeIterator())
}
}

@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,
subjectBounds: Range<String.Index>,
searchBounds: Range<String.Index>
) -> RegexRangesCollection<R.RegexOutput> {
RegexRangesCollection(
input: self[...].base,
subjectBounds: subjectBounds,
searchBounds: searchBounds,
regex: regex.regex)
}

@available(SwiftStdlib 5.7, *)
@_disfavoredOverload
func _ranges<R: RegexComponent>(
of regex: R
) -> RangesCollection<RegexConsumer<R, Self>> {
_ranges(of: RegexConsumer(regex))
) -> RegexRangesCollection<R.RegexOutput> {
_ranges(
of: regex,
subjectBounds: startIndex..<endIndex,
searchBounds: startIndex..<endIndex)
}
}

extension BidirectionalCollection where SubSequence == Substring {
@available(SwiftStdlib 5.7, *)
func _rangesFromBack<R: RegexComponent>(
of regex: R
Expand Down
48 changes: 17 additions & 31 deletions Sources/_StringProcessing/Algorithms/Algorithms/Replace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
// 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) {
// `maxRanges` is a workaround for https://github.com/apple/swift/issues/59522
let maxRanges = ranges.prefix(maxReplacements)
for range in maxRanges {
result.append(contentsOf: self[index..<range.lowerBound])
result.append(contentsOf: replacement)
index = range.upperBound
Expand All @@ -36,29 +36,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 +71,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 +128,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 +179,11 @@ extension RangeReplaceableCollection where SubSequence == Substring {
maxReplacements: Int = .max
) -> Self where Replacement.Element == Element {
_replacing(
RegexConsumer(regex),
self._ranges(
of: regex,
subjectBounds: startIndex..<endIndex,
searchBounds: subrange),
with: replacement,
subrange: subrange,
maxReplacements: maxReplacements)
}

Expand Down
Loading