Skip to content

Fix firstRange(of:) search #656

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 2 commits into from
Apr 14, 2023
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 @@ -50,11 +50,8 @@ extension BidirectionalCollection where Element: Comparable {
public func firstRange<C: Collection>(
of other: C
) -> Range<Index>? where C.Element == Element {
let searcher = PatternOrEmpty(
searcher: TwoWaySearcher<SubSequence>(pattern: Array(other)))
let slice = self[...]
var state = searcher.state(for: slice, in: startIndex..<endIndex)
return searcher.search(slice, &state)
let searcher = ZSearcher<SubSequence>(pattern: Array(other), by: ==)
return searcher.search(self[...], in: startIndex..<endIndex)
}
}

Expand Down
60 changes: 0 additions & 60 deletions Sources/_StringProcessing/Algorithms/Algorithms/Split.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,31 +122,6 @@ extension Collection {
}
}

// MARK: Predicate algorithms

extension Collection {
// TODO: Non-escaping and throwing
func split(
whereSeparator predicate: @escaping (Element) -> Bool,
maxSplits: Int,
omittingEmptySubsequences: Bool
) -> SplitCollection<PredicateConsumer<Self>> {
split(by: PredicateConsumer(predicate: predicate), maxSplits: maxSplits, omittingEmptySubsequences: omittingEmptySubsequences)
}
}

// MARK: Single element algorithms

extension Collection where Element: Equatable {
func split(
by separator: Element,
maxSplits: Int,
omittingEmptySubsequences: Bool
) -> SplitCollection<PredicateConsumer<Self>> {
split(whereSeparator: { $0 == separator }, maxSplits: maxSplits, omittingEmptySubsequences: omittingEmptySubsequences)
}
}

// MARK: Fixed pattern algorithms

extension Collection where Element: Equatable {
Expand Down Expand Up @@ -180,41 +155,6 @@ extension Collection where Element: Equatable {
}
}

extension BidirectionalCollection where Element: Equatable {
// FIXME
// public func splitFromBack<S: Sequence>(
// separator: S
// ) -> ReversedSplitCollection<ZSearcher<SubSequence>>
// where S.Element == Element
// {
// splitFromBack(separator: ZSearcher(pattern: Array(separator), by: ==))
// }
}

extension BidirectionalCollection where Element: Comparable {
func split<C: Collection>(
by separator: C,
maxSplits: Int,
omittingEmptySubsequences: Bool
) -> SplitCollection<PatternOrEmpty<TwoWaySearcher<Self>>>
where C.Element == Element
{
split(
by: PatternOrEmpty(searcher: TwoWaySearcher(pattern: Array(separator))),
maxSplits: maxSplits, omittingEmptySubsequences: omittingEmptySubsequences)
}

// FIXME
// public func splitFromBack<S: Sequence>(
// separator: S
// ) -> ReversedSplitCollection<PatternOrEmpty<TwoWaySearcher<SubSequence>>>
// where S.Element == Element
// {
// splitFromBack(separator: PatternOrEmpty(
// searcher: TwoWaySearcher(pattern: Array(separator))))
// }
}

// String split overload breakers
//
// These are underscored and marked as SPI so that the *actual* public overloads
Expand Down
197 changes: 0 additions & 197 deletions Sources/_StringProcessing/Algorithms/Searchers/TwoWaySearcher.swift

This file was deleted.

1 change: 0 additions & 1 deletion Sources/_StringProcessing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ add_library(_StringProcessing
Algorithms/Searchers/NaivePatternSearcher.swift
Algorithms/Searchers/PatternOrEmpty.swift
Algorithms/Searchers/PredicateSearcher.swift
Algorithms/Searchers/TwoWaySearcher.swift
Algorithms/Searchers/ZSearcher.swift
Engine/Backtracking.swift
Engine/Consume.swift
Expand Down
11 changes: 9 additions & 2 deletions Tests/RegexTests/AlgorithmsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,12 @@ class AlgorithmTests: XCTestCase {
let actualCol: [Range<Int>] = input.ranges(of: pattern)[...].map(input.offsets(of:))
XCTAssertEqual(actualCol, expected, file: file, line: line)

let firstRange = input.firstRange(of: pattern).map(input.offsets(of:))
XCTAssertEqual(firstRange, expected.first, file: file, line: line)
let firstRange = input.firstRange(of: pattern)
XCTAssertEqual(firstRange.map(input.offsets(of:)), expected.first, file: file, line: line)
if let upperBound = firstRange?.upperBound, !pattern.isEmpty {
let secondRange = input[upperBound...].firstRange(of: pattern).map(input.offsets(of:))
XCTAssertEqual(secondRange, expected.dropFirst().first, file: file, line: line)
}
}

expectRanges("", "", [0..<0])
Expand All @@ -176,6 +180,9 @@ class AlgorithmTests: XCTestCase {
expectRanges("abcde", "bcd", [1..<4])
expectRanges("ababacabababa", "abababa", [6..<13])
expectRanges("ababacabababa", "aba", [0..<3, 6..<9, 10..<13])

// Test for rdar://92794248
expectRanges("ADACBADADACBADACB", "ADACB", [0..<5, 7..<12, 12..<17])
}

// rdar://105154010
Expand Down