Skip to content

Underscore internal algorithms methods #414

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 1 commit into from
May 15, 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 @@ -12,10 +12,10 @@
// MARK: `CollectionSearcher` algorithms

extension Collection {
func contains<Searcher: CollectionSearcher>(
func _contains<Searcher: CollectionSearcher>(
_ searcher: Searcher
) -> Bool where Searcher.Searched == Self {
firstRange(of: searcher) != nil
_firstRange(of: searcher) != nil
}
}

Expand All @@ -36,7 +36,7 @@ extension Collection where Element: Equatable {
}

extension BidirectionalCollection where Element: Comparable {
func contains<C: Collection>(_ other: C) -> Bool
func _contains<C: Collection>(_ other: C) -> Bool
where C.Element == Element
{
if #available(SwiftStdlib 5.7, *) {
Expand Down Expand Up @@ -70,6 +70,6 @@ extension BidirectionalCollection where SubSequence == Substring {
/// `false`.
@available(SwiftStdlib 5.7, *)
public func contains<R: RegexComponent>(_ regex: R) -> Bool {
contains(RegexConsumer(regex))
_contains(RegexConsumer(regex))
}
}
10 changes: 5 additions & 5 deletions Sources/_StringProcessing/Algorithms/Algorithms/FirstRange.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// MARK: `CollectionSearcher` algorithms

extension Collection {
func firstRange<S: CollectionSearcher>(
func _firstRange<S: CollectionSearcher>(
of searcher: S
) -> Range<Index>? where S.Searched == Self {
var state = searcher.state(for: self, in: startIndex..<endIndex)
Expand All @@ -21,7 +21,7 @@ extension Collection {
}

extension BidirectionalCollection {
func lastRange<S: BackwardCollectionSearcher>(
func _lastRange<S: BackwardCollectionSearcher>(
of searcher: S
) -> Range<Index>? where S.BackwardSearched == Self {
var state = searcher.backwardState(for: self, in: startIndex..<endIndex)
Expand Down Expand Up @@ -77,11 +77,11 @@ extension BidirectionalCollection where SubSequence == Substring {
/// Returns `nil` if `regex` is not found.
@available(SwiftStdlib 5.7, *)
public func firstRange<R: RegexComponent>(of regex: R) -> Range<Index>? {
firstRange(of: RegexConsumer(regex))
_firstRange(of: RegexConsumer(regex))
}

@available(SwiftStdlib 5.7, *)
func lastRange<R: RegexComponent>(of regex: R) -> Range<Index>? {
lastRange(of: RegexConsumer(regex))
func _lastRange<R: RegexComponent>(of regex: R) -> Range<Index>? {
_lastRange(of: RegexConsumer(regex))
}
}
24 changes: 12 additions & 12 deletions Sources/_StringProcessing/Algorithms/Algorithms/Ranges.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,15 @@ extension ReversedRangesCollection: Sequence {
// MARK: `CollectionSearcher` algorithms

extension Collection {
func ranges<S: CollectionSearcher>(
func _ranges<S: CollectionSearcher>(
of searcher: S
) -> RangesCollection<S> where S.Searched == Self {
RangesCollection(base: self, searcher: searcher)
}
}

extension BidirectionalCollection {
func rangesFromBack<S: BackwardCollectionSearcher>(
func _rangesFromBack<S: BackwardCollectionSearcher>(
of searcher: S
) -> ReversedRangesCollection<S> where S.BackwardSearched == Self {
ReversedRangesCollection(base: self, searcher: searcher)
Expand All @@ -175,10 +175,10 @@ extension BidirectionalCollection {
// MARK: Fixed pattern algorithms

extension Collection where Element: Equatable {
func ranges<C: Collection>(
func _ranges<C: Collection>(
of other: C
) -> RangesCollection<ZSearcher<Self>> where C.Element == Element {
ranges(of: ZSearcher(pattern: Array(other), by: ==))
_ranges(of: ZSearcher(pattern: Array(other), by: ==))
}

// FIXME: Return `some Collection<Range<Index>>` for SE-0346
Expand All @@ -191,7 +191,7 @@ extension Collection where Element: Equatable {
public func ranges<C: Collection>(
of other: C
) -> [Range<Index>] where C.Element == Element {
ranges(of: ZSearcher(pattern: Array(other), by: ==)).map { $0 }
Array(_ranges(of: other))
}
}

Expand All @@ -207,12 +207,12 @@ extension BidirectionalCollection where Element: Equatable {
}

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

// FIXME
Expand All @@ -231,17 +231,17 @@ extension BidirectionalCollection where Element: Comparable {
extension BidirectionalCollection where SubSequence == Substring {
@available(SwiftStdlib 5.7, *)
@_disfavoredOverload
func ranges<R: RegexComponent>(
func _ranges<R: RegexComponent>(
of regex: R
) -> RangesCollection<RegexConsumer<R, Self>> {
ranges(of: RegexConsumer(regex))
_ranges(of: RegexConsumer(regex))
}

@available(SwiftStdlib 5.7, *)
func rangesFromBack<R: RegexComponent>(
func _rangesFromBack<R: RegexComponent>(
of regex: R
) -> ReversedRangesCollection<RegexConsumer<R, Self>> {
rangesFromBack(of: RegexConsumer(regex))
_rangesFromBack(of: RegexConsumer(regex))
}

// FIXME: Return `some Collection<Range<Index>>` for SE-0346
Expand All @@ -255,6 +255,6 @@ extension BidirectionalCollection where SubSequence == Substring {
public func ranges<R: RegexComponent>(
of regex: R
) -> [Range<Index>] {
Array(ranges(of: RegexConsumer(regex)))
Array(_ranges(of: regex))
}
}
28 changes: 14 additions & 14 deletions Sources/_StringProcessing/Algorithms/Algorithms/Replace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// MARK: `CollectionSearcher` algorithms

extension RangeReplaceableCollection {
func replacing<Searcher: CollectionSearcher, Replacement: Collection>(
func _replacing<Searcher: CollectionSearcher, Replacement: Collection>(
_ searcher: Searcher,
with replacement: Replacement,
subrange: Range<Index>,
Expand All @@ -26,7 +26,7 @@ extension RangeReplaceableCollection {
var result = Self()
result.append(contentsOf: self[..<index])

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

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

mutating func replace<
mutating func _replace<
Searcher: CollectionSearcher, Replacement: Collection
>(
_ searcher: Searcher,
with replacement: Replacement,
maxReplacements: Int = .max
) where Searcher.Searched == SubSequence, Replacement.Element == Element {
self = replacing(
self = _replacing(
searcher,
with: replacement,
maxReplacements: maxReplacements)
Expand All @@ -84,7 +84,7 @@ extension RangeReplaceableCollection where Element: Equatable {
subrange: Range<Index>,
maxReplacements: Int = .max
) -> Self where C.Element == Element, Replacement.Element == Element {
replacing(
_replacing(
ZSearcher(pattern: Array(other), by: ==),
with: replacement,
subrange: subrange,
Expand Down Expand Up @@ -136,37 +136,37 @@ extension RangeReplaceableCollection where Element: Equatable {
extension RangeReplaceableCollection
where Self: BidirectionalCollection, Element: Comparable
{
func replacing<C: Collection, Replacement: Collection>(
func _replacing<C: Collection, Replacement: Collection>(
_ other: C,
with replacement: Replacement,
subrange: Range<Index>,
maxReplacements: Int = .max
) -> Self where C.Element == Element, Replacement.Element == Element {
replacing(
_replacing(
PatternOrEmpty(searcher: TwoWaySearcher(pattern: Array(other))),
with: replacement,
subrange: subrange,
maxReplacements: maxReplacements)
}

func replacing<C: Collection, Replacement: Collection>(
func _replacing<C: Collection, Replacement: Collection>(
_ other: C,
with replacement: Replacement,
maxReplacements: Int = .max
) -> Self where C.Element == Element, Replacement.Element == Element {
replacing(
_replacing(
other,
with: replacement,
subrange: startIndex..<endIndex,
maxReplacements: maxReplacements)
}

mutating func replace<C: Collection, Replacement: Collection>(
mutating func _replace<C: Collection, Replacement: Collection>(
_ other: C,
with replacement: Replacement,
maxReplacements: Int = .max
) where C.Element == Element, Replacement.Element == Element {
self = replacing(
self = _replacing(
other,
with: replacement,
subrange: startIndex..<endIndex,
Expand Down Expand Up @@ -194,7 +194,7 @@ extension RangeReplaceableCollection where SubSequence == Substring {
subrange: Range<Index>,
maxReplacements: Int = .max
) -> Self where Replacement.Element == Element {
replacing(
_replacing(
RegexConsumer(regex),
with: replacement,
subrange: subrange,
Expand Down
4 changes: 2 additions & 2 deletions Sources/_StringProcessing/Algorithms/Algorithms/Split.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct SplitCollection<Searcher: CollectionSearcher> {
maxSplits: Int,
omittingEmptySubsequences: Bool)
{
self.ranges = base.ranges(of: searcher)
self.ranges = base._ranges(of: searcher)
self.maxSplits = maxSplits
self.omittingEmptySubsequences = omittingEmptySubsequences
}
Expand Down Expand Up @@ -183,7 +183,7 @@ struct ReversedSplitCollection<Searcher: BackwardCollectionSearcher> {
}

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

Expand Down
18 changes: 9 additions & 9 deletions Sources/_StringProcessing/Algorithms/Algorithms/StartsWith.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
// MARK: `CollectionConsumer` algorithms

extension Collection {
func starts<C: CollectionConsumer>(with consumer: C) -> Bool
func _starts<C: CollectionConsumer>(with consumer: C) -> Bool
where C.Consumed == SubSequence
{
consumer.consuming(self[...]) != nil
}
}

extension BidirectionalCollection {
func ends<C: BidirectionalCollectionConsumer>(with consumer: C) -> Bool
func _ends<C: BidirectionalCollectionConsumer>(with consumer: C) -> Bool
where C.Consumed == SubSequence
{
consumer.consumingBack(self[...]) != nil
Expand All @@ -30,18 +30,18 @@ extension BidirectionalCollection {
// MARK: Fixed pattern algorithms

extension Collection where Element: Equatable {
func starts<C: Collection>(with prefix: C) -> Bool
func _starts<C: Collection>(with prefix: C) -> Bool
where C.Element == Element
{
starts(with: FixedPatternConsumer(pattern: prefix))
_starts(with: FixedPatternConsumer(pattern: prefix))
}
}

extension BidirectionalCollection where Element: Equatable {
func ends<C: BidirectionalCollection>(with suffix: C) -> Bool
func _ends<C: BidirectionalCollection>(with suffix: C) -> Bool
where C.Element == Element
{
ends(with: FixedPatternConsumer(pattern: suffix))
_ends(with: FixedPatternConsumer(pattern: suffix))
}
}

Expand All @@ -56,10 +56,10 @@ extension BidirectionalCollection where SubSequence == Substring {
/// - Returns: `true` if the initial elements of the sequence matches the
/// beginning of `regex`; otherwise, `false`.
public func starts<R: RegexComponent>(with regex: R) -> Bool {
starts(with: RegexConsumer(regex))
_starts(with: RegexConsumer(regex))
}

func ends<R: RegexComponent>(with regex: R) -> Bool {
ends(with: RegexConsumer(regex))
func _ends<R: RegexComponent>(with regex: R) -> Bool {
_ends(with: RegexConsumer(regex))
}
}
Loading