Skip to content

Remove RegexConsumer and fix its dependencies #617

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 4 commits into from
Dec 14, 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
17 changes: 2 additions & 15 deletions Sources/_StringProcessing/Algorithms/Algorithms/FirstRange.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,6 @@ extension Collection {
}
}

extension BidirectionalCollection {
func _lastRange<S: BackwardCollectionSearcher>(
of searcher: S
) -> Range<Index>? where S.BackwardSearched == Self {
var state = searcher.backwardState(for: self, in: startIndex..<endIndex)
return searcher.searchBack(self, &state)
}
}

// MARK: Fixed pattern algorithms

extension Collection where Element: Equatable {
Expand Down Expand Up @@ -78,11 +69,7 @@ extension BidirectionalCollection where SubSequence == Substring {
@_disfavoredOverload
@available(SwiftStdlib 5.7, *)
public func firstRange(of regex: some RegexComponent) -> Range<Index>? {
_firstRange(of: RegexConsumer(regex))
}

@available(SwiftStdlib 5.7, *)
func _lastRange<R: RegexComponent>(of regex: R) -> Range<Index>? {
_lastRange(of: RegexConsumer(regex))
let s = self[...]
return try? regex.regex.firstMatch(in: s)?.range
}
}
83 changes: 0 additions & 83 deletions Sources/_StringProcessing/Algorithms/Algorithms/Ranges.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,43 +115,6 @@ extension RangesCollection.Index: Comparable {
}
}

// MARK: `ReversedRangesCollection`

struct ReversedRangesCollection<Searcher: BackwardCollectionSearcher> {
typealias Base = Searcher.BackwardSearched

let base: Base
let searcher: Searcher

init(base: Base, searcher: Searcher) {
self.base = base
self.searcher = searcher
}
}

extension ReversedRangesCollection: Sequence {
public struct Iterator: IteratorProtocol {
let base: Base
let searcher: Searcher
var state: Searcher.BackwardState

init(base: Base, searcher: Searcher) {
self.base = base
self.searcher = searcher
self.state = searcher.backwardState(
for: base, in: base.startIndex..<base.endIndex)
}

public mutating func next() -> Range<Base.Index>? {
searcher.searchBack(base, &state)
}
}

public func makeIterator() -> Iterator {
Iterator(base: base, searcher: searcher)
}
}

// TODO: `Collection` conformance

// MARK: `CollectionSearcher` algorithms
Expand All @@ -164,14 +127,6 @@ extension Collection {
}
}

extension BidirectionalCollection {
func _rangesFromBack<S: BackwardCollectionSearcher>(
of searcher: S
) -> ReversedRangesCollection<S> where S.BackwardSearched == Self {
ReversedRangesCollection(base: self, searcher: searcher)
}
}

// MARK: Fixed pattern algorithms

extension Collection where Element: Equatable {
Expand All @@ -195,37 +150,6 @@ extension Collection where Element: Equatable {
}
}

extension BidirectionalCollection where Element: Equatable {
// FIXME
// public func rangesFromBack<S: Sequence>(
// of other: S
// ) -> ReversedRangesCollection<ZSearcher<SubSequence>>
// where S.Element == Element
// {
// fatalError()
// }
}

extension BidirectionalCollection where Element: Comparable {
func _ranges<C: Collection>(
of other: C
) -> RangesCollection<PatternOrEmpty<TwoWaySearcher<Self>>>
where C.Element == Element
{
_ranges(of: PatternOrEmpty(searcher: TwoWaySearcher(pattern: Array(other))))
}

// FIXME
// public func rangesFromBack<S: Sequence>(
// of other: S
// ) -> ReversedRangesCollection<PatternOrEmpty<TwoWaySearcher<SubSequence>>>
// where S.Element == Element
// {
// rangesFromBack(
// of: PatternOrEmpty(searcher: TwoWaySearcher(pattern: Array(other))))
// }
}

@available(SwiftStdlib 5.7, *)
struct RegexRangesCollection<Output> {
let base: RegexMatchesCollection<Output>
Expand Down Expand Up @@ -299,13 +223,6 @@ extension Collection where SubSequence == Substring {
}

extension BidirectionalCollection where SubSequence == Substring {
@available(SwiftStdlib 5.7, *)
func _rangesFromBack<R: RegexComponent>(
of regex: R
) -> ReversedRangesCollection<RegexConsumer<R, Self>> {
_rangesFromBack(of: RegexConsumer(regex))
}

// FIXME: Return `some Collection<Range<Index>>` for SE-0346
/// Finds and returns the ranges of the all occurrences of a given sequence
/// within the collection.
Expand Down
189 changes: 29 additions & 160 deletions Sources/_StringProcessing/Algorithms/Algorithms/Split.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,121 +106,6 @@ extension SplitCollection: Sequence {
}
}

//extension SplitCollection: Collection {
// public struct Index {
// var start: Base.Index
// var base: RangesCollection<Searcher>.Index
// var isEndIndex: Bool
// }
//
// public var startIndex: Index {
// let base = ranges.startIndex
// return Index(start: ranges.base.startIndex, base: base, isEndIndex: false)
// }
//
// public var endIndex: Index {
// Index(start: ranges.base.endIndex, base: ranges.endIndex, isEndIndex: true)
// }
//
// public func formIndex(after index: inout Index) {
// guard !index.isEndIndex else { fatalError("Cannot advance past endIndex") }
//
// if let range = index.base.range {
// let newStart = range.upperBound
// ranges.formIndex(after: &index.base)
// index.start = newStart
// } else {
// index.isEndIndex = true
// }
// }
//
// public func index(after index: Index) -> Index {
// var index = index
// formIndex(after: &index)
// return index
// }
//
// public subscript(index: Index) -> Base.SubSequence {
// guard !index.isEndIndex else {
// fatalError("Cannot subscript using endIndex")
// }
// let end = index.base.range?.lowerBound ?? ranges.base.endIndex
// return ranges.base[index.start..<end]
// }
//}
//
//extension SplitCollection.Index: Comparable {
// static func == (lhs: Self, rhs: Self) -> Bool {
// switch (lhs.isEndIndex, rhs.isEndIndex) {
// case (false, false):
// return lhs.start == rhs.start
// case (let lhs, let rhs):
// return lhs == rhs
// }
// }
//
// static func < (lhs: Self, rhs: Self) -> Bool {
// switch (lhs.isEndIndex, rhs.isEndIndex) {
// case (true, _):
// return false
// case (_, true):
// return true
// case (false, false):
// return lhs.start < rhs.start
// }
// }
//}

// MARK: `ReversedSplitCollection`

struct ReversedSplitCollection<Searcher: BackwardCollectionSearcher> {
public typealias Base = Searcher.BackwardSearched

let ranges: ReversedRangesCollection<Searcher>

init(ranges: ReversedRangesCollection<Searcher>) {
self.ranges = ranges
}

init(base: Base, searcher: Searcher) {
self.ranges = base._rangesFromBack(of: searcher)
}
}

extension ReversedSplitCollection: Sequence {
public struct Iterator: IteratorProtocol {
let base: Base
var index: Base.Index
var ranges: ReversedRangesCollection<Searcher>.Iterator
var isDone: Bool

init(ranges: ReversedRangesCollection<Searcher>) {
self.base = ranges.base
self.index = base.endIndex
self.ranges = ranges.makeIterator()
self.isDone = false
}

public mutating func next() -> Base.SubSequence? {
guard !isDone else { return nil }

guard let range = ranges.next() else {
isDone = true
return base[..<index]
}

defer { index = range.lowerBound }
return base[range.upperBound..<index]
}
}

public func makeIterator() -> Iterator {
Iterator(ranges: ranges)
}
}

// TODO: `Collection` conformance

// MARK: `CollectionSearcher` algorithms

extension Collection {
Expand All @@ -237,16 +122,6 @@ extension Collection {
}
}

extension BidirectionalCollection {
func splitFromBack<Searcher: BackwardCollectionSearcher>(
by separator: Searcher
) -> ReversedSplitCollection<Searcher>
where Searcher.BackwardSearched == Self
{
ReversedSplitCollection(base: self, searcher: separator)
}
}

// MARK: Predicate algorithms

extension Collection {
Expand All @@ -260,14 +135,6 @@ extension Collection {
}
}

extension BidirectionalCollection where Element: Equatable {
func splitFromBack(
whereSeparator predicate: @escaping (Element) -> Bool
) -> ReversedSplitCollection<PredicateConsumer<Self>> {
splitFromBack(by: PredicateConsumer(predicate: predicate))
}
}

// MARK: Single element algorithms

extension Collection where Element: Equatable {
Expand All @@ -280,14 +147,6 @@ extension Collection where Element: Equatable {
}
}

extension BidirectionalCollection where Element: Equatable {
func splitFromBack(
by separator: Element
) -> ReversedSplitCollection<PredicateConsumer<Self>> {
splitFromBack(whereSeparator: { $0 == separator })
}
}

// MARK: Fixed pattern algorithms

extension Collection where Element: Equatable {
Expand Down Expand Up @@ -399,21 +258,6 @@ extension StringProtocol where SubSequence == Substring {

@available(SwiftStdlib 5.7, *)
extension BidirectionalCollection where SubSequence == Substring {
@_disfavoredOverload
func split<R: RegexComponent>(
by separator: R,
maxSplits: Int,
omittingEmptySubsequences: Bool
) -> SplitCollection<RegexConsumer<R, Self>> {
split(by: RegexConsumer(separator), maxSplits: maxSplits, omittingEmptySubsequences: omittingEmptySubsequences)
}

func splitFromBack<R: RegexComponent>(
by separator: R
) -> ReversedSplitCollection<RegexConsumer<R, Self>> {
splitFromBack(by: RegexConsumer(separator))
}

// TODO: Is this @_disfavoredOverload necessary?
// It prevents split(separator: String) from choosing this overload instead
// of the collection-based version when String has RegexComponent conformance
Expand All @@ -431,9 +275,34 @@ extension BidirectionalCollection where SubSequence == Substring {
maxSplits: Int = .max,
omittingEmptySubsequences: Bool = true
) -> [SubSequence] {
Array(split(
by: RegexConsumer(separator),
maxSplits: maxSplits,
omittingEmptySubsequences: omittingEmptySubsequences))
var result: [SubSequence] = []
var subSequenceStart = startIndex

func appendSubsequence(end: Index) -> Bool {
if subSequenceStart == end && omittingEmptySubsequences {
return false
}
result.append(self[subSequenceStart..<end])
return true
}

guard maxSplits > 0 && !isEmpty else {
_ = appendSubsequence(end: endIndex)
return result
}

for match in _matches(of: separator) {
defer { subSequenceStart = match.range.upperBound }
let didAppend = appendSubsequence(end: match.range.lowerBound)
if didAppend && result.count == maxSplits {
break
}
}

if subSequenceStart != endIndex || !omittingEmptySubsequences {
result.append(self[subSequenceStart..<endIndex])
}

return result
}
}
Loading