Skip to content

Commit 52bc932

Browse files
itingliurxwei
andauthored
Clean up based on the String Processing Algorithms proposal (#247)
* Clean up based on the String Processing Algorithms proposal - Move functions and types that have not been proposed from public to internal - Add doc comments for public API - Add FIXME for API awaiting SE-0346 - Replace `_MatchResult` with `Regex<Output>.Match` and update tests Co-authored-by: Richard Wei <[email protected]>
1 parent 5d4d136 commit 52bc932

19 files changed

+387
-154
lines changed

Sources/_StringProcessing/Algorithms/Algorithms/Contains.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// MARK: `CollectionSearcher` algorithms
1313

1414
extension Collection {
15-
public func contains<Searcher: CollectionSearcher>(
15+
func contains<Searcher: CollectionSearcher>(
1616
_ searcher: Searcher
1717
) -> Bool where Searcher.Searched == Self {
1818
firstRange(of: searcher) != nil
@@ -22,6 +22,11 @@ extension Collection {
2222
// MARK: Fixed pattern algorithms
2323

2424
extension Collection where Element: Equatable {
25+
/// Returns a Boolean value indicating whether the collection contains the
26+
/// given sequence.
27+
/// - Parameter other: A sequence to search for within this collection.
28+
/// - Returns: `true` if the collection contains the specified sequence,
29+
/// otherwise `false`.
2530
public func contains<S: Sequence>(_ other: S) -> Bool
2631
where S.Element == Element
2732
{
@@ -30,7 +35,7 @@ extension Collection where Element: Equatable {
3035
}
3136

3237
extension BidirectionalCollection where Element: Comparable {
33-
public func contains<S: Sequence>(_ other: S) -> Bool
38+
func contains<S: Sequence>(_ other: S) -> Bool
3439
where S.Element == Element
3540
{
3641
firstRange(of: other) != nil
@@ -40,6 +45,11 @@ extension BidirectionalCollection where Element: Comparable {
4045
// MARK: Regex algorithms
4146

4247
extension BidirectionalCollection where SubSequence == Substring {
48+
/// Returns a Boolean value indicating whether the collection contains the
49+
/// given regex.
50+
/// - Parameter regex: A regex to search for within this collection.
51+
/// - Returns: `true` if the regex was found in the collection, otherwise
52+
/// `false`.
4353
public func contains<R: RegexComponent>(_ regex: R) -> Bool {
4454
contains(RegexConsumer(regex))
4555
}

Sources/_StringProcessing/Algorithms/Algorithms/FirstRange.swift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// MARK: `CollectionSearcher` algorithms
1313

1414
extension Collection {
15-
public func firstRange<S: CollectionSearcher>(
15+
func firstRange<S: CollectionSearcher>(
1616
of searcher: S
1717
) -> Range<Index>? where S.Searched == Self {
1818
var state = searcher.state(for: self, in: startIndex..<endIndex)
@@ -21,7 +21,7 @@ extension Collection {
2121
}
2222

2323
extension BidirectionalCollection {
24-
public func lastRange<S: BackwardCollectionSearcher>(
24+
func lastRange<S: BackwardCollectionSearcher>(
2525
of searcher: S
2626
) -> Range<Index>? where S.BackwardSearched == Self {
2727
var state = searcher.backwardState(for: self, in: startIndex..<endIndex)
@@ -32,6 +32,11 @@ extension BidirectionalCollection {
3232
// MARK: Fixed pattern algorithms
3333

3434
extension Collection where Element: Equatable {
35+
/// Finds and returns the range of the first occurrence of a given sequence
36+
/// within the collection.
37+
/// - Parameter sequence: The sequence to search for.
38+
/// - Returns: A range in the collection of the first occurrence of `sequence`.
39+
/// Returns nil if `sequence` is not found.
3540
public func firstRange<S: Sequence>(
3641
of sequence: S
3742
) -> Range<Index>? where S.Element == Element {
@@ -42,6 +47,11 @@ extension Collection where Element: Equatable {
4247
}
4348

4449
extension BidirectionalCollection where Element: Comparable {
50+
/// Finds and returns the range of the first occurrence of a given sequence
51+
/// within the collection.
52+
/// - Parameter other: The sequence to search for.
53+
/// - Returns: A range in the collection of the first occurrence of `sequence`.
54+
/// Returns `nil` if `sequence` is not found.
4555
public func firstRange<S: Sequence>(
4656
of other: S
4757
) -> Range<Index>? where S.Element == Element {
@@ -56,11 +66,16 @@ extension BidirectionalCollection where Element: Comparable {
5666
// MARK: Regex algorithms
5767

5868
extension BidirectionalCollection where SubSequence == Substring {
69+
/// Finds and returns the range of the first occurrence of a given regex
70+
/// within the collection.
71+
/// - Parameter regex: The regex to search for.
72+
/// - Returns: A range in the collection of the first occurrence of `regex`.
73+
/// Returns `nil` if `regex` is not found.
5974
public func firstRange<R: RegexComponent>(of regex: R) -> Range<Index>? {
6075
firstRange(of: RegexConsumer(regex))
6176
}
6277

63-
public func lastRange<R: RegexComponent>(of regex: R) -> Range<Index>? {
78+
func lastRange<R: RegexComponent>(of regex: R) -> Range<Index>? {
6479
lastRange(of: RegexConsumer(regex))
6580
}
6681
}

Sources/_StringProcessing/Algorithms/Algorithms/Ranges.swift

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
// MARK: `RangesCollection`
1313

14-
public struct RangesCollection<Searcher: CollectionSearcher> {
14+
struct RangesCollection<Searcher: CollectionSearcher> {
1515
public typealias Base = Searcher.Searched
1616

1717
let base: Base
@@ -33,7 +33,7 @@ public struct RangesCollection<Searcher: CollectionSearcher> {
3333
}
3434
}
3535

36-
public struct RangesIterator<Searcher: CollectionSearcher>: IteratorProtocol {
36+
struct RangesIterator<Searcher: CollectionSearcher>: IteratorProtocol {
3737
public typealias Base = Searcher.Searched
3838

3939
let base: Base
@@ -92,7 +92,7 @@ extension RangesCollection: Collection {
9292
}
9393

9494
extension RangesCollection.Index: Comparable {
95-
public static func == (lhs: Self, rhs: Self) -> Bool {
95+
static func == (lhs: Self, rhs: Self) -> Bool {
9696
switch (lhs.range, rhs.range) {
9797
case (nil, nil):
9898
return true
@@ -103,7 +103,7 @@ extension RangesCollection.Index: Comparable {
103103
}
104104
}
105105

106-
public static func < (lhs: Self, rhs: Self) -> Bool {
106+
static func < (lhs: Self, rhs: Self) -> Bool {
107107
switch (lhs.range, rhs.range) {
108108
case (nil, _):
109109
return false
@@ -117,8 +117,8 @@ extension RangesCollection.Index: Comparable {
117117

118118
// MARK: `ReversedRangesCollection`
119119

120-
public struct ReversedRangesCollection<Searcher: BackwardCollectionSearcher> {
121-
public typealias Base = Searcher.BackwardSearched
120+
struct ReversedRangesCollection<Searcher: BackwardCollectionSearcher> {
121+
typealias Base = Searcher.BackwardSearched
122122

123123
let base: Base
124124
let searcher: Searcher
@@ -157,15 +157,15 @@ extension ReversedRangesCollection: Sequence {
157157
// MARK: `CollectionSearcher` algorithms
158158

159159
extension Collection {
160-
public func ranges<S: CollectionSearcher>(
160+
func ranges<S: CollectionSearcher>(
161161
of searcher: S
162162
) -> RangesCollection<S> where S.Searched == Self {
163163
RangesCollection(base: self, searcher: searcher)
164164
}
165165
}
166166

167167
extension BidirectionalCollection {
168-
public func rangesFromBack<S: BackwardCollectionSearcher>(
168+
func rangesFromBack<S: BackwardCollectionSearcher>(
169169
of searcher: S
170170
) -> ReversedRangesCollection<S> where S.BackwardSearched == Self {
171171
ReversedRangesCollection(base: self, searcher: searcher)
@@ -175,7 +175,8 @@ extension BidirectionalCollection {
175175
// MARK: Fixed pattern algorithms
176176

177177
extension Collection where Element: Equatable {
178-
public func ranges<S: Sequence>(
178+
// FIXME: Replace `RangesCollection` when SE-0346 is enabled
179+
func ranges<S: Sequence>(
179180
of other: S
180181
) -> RangesCollection<ZSearcher<Self>> where S.Element == Element {
181182
ranges(of: ZSearcher(pattern: Array(other), by: ==))
@@ -194,7 +195,7 @@ extension BidirectionalCollection where Element: Equatable {
194195
}
195196

196197
extension BidirectionalCollection where Element: Comparable {
197-
public func ranges<S: Sequence>(
198+
func ranges<S: Sequence>(
198199
of other: S
199200
) -> RangesCollection<PatternOrEmpty<TwoWaySearcher<Self>>>
200201
where S.Element == Element
@@ -216,13 +217,14 @@ extension BidirectionalCollection where Element: Comparable {
216217
// MARK: Regex algorithms
217218

218219
extension BidirectionalCollection where SubSequence == Substring {
219-
public func ranges<R: RegexComponent>(
220+
// FIXME: Replace `RangesCollection` when SE-0346 is enabled
221+
func ranges<R: RegexComponent>(
220222
of regex: R
221223
) -> RangesCollection<RegexConsumer<R, Self>> {
222224
ranges(of: RegexConsumer(regex))
223225
}
224226

225-
public func rangesFromBack<R: RegexComponent>(
227+
func rangesFromBack<R: RegexComponent>(
226228
of regex: R
227229
) -> ReversedRangesCollection<RegexConsumer<R, Self>> {
228230
rangesFromBack(of: RegexConsumer(regex))

Sources/_StringProcessing/Algorithms/Algorithms/Replace.swift

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// MARK: `CollectionSearcher` algorithms
1313

1414
extension RangeReplaceableCollection {
15-
public func replacing<Searcher: CollectionSearcher, Replacement: Collection>(
15+
func replacing<Searcher: CollectionSearcher, Replacement: Collection>(
1616
_ searcher: Searcher,
1717
with replacement: Replacement,
1818
subrange: Range<Index>,
@@ -36,7 +36,7 @@ extension RangeReplaceableCollection {
3636
return result
3737
}
3838

39-
public func replacing<Searcher: CollectionSearcher, Replacement: Collection>(
39+
func replacing<Searcher: CollectionSearcher, Replacement: Collection>(
4040
_ searcher: Searcher,
4141
with replacement: Replacement,
4242
maxReplacements: Int = .max
@@ -50,7 +50,7 @@ extension RangeReplaceableCollection {
5050
maxReplacements: maxReplacements)
5151
}
5252

53-
public mutating func replace<
53+
mutating func replace<
5454
Searcher: CollectionSearcher, Replacement: Collection
5555
>(
5656
_ searcher: Searcher,
@@ -67,6 +67,16 @@ extension RangeReplaceableCollection {
6767
// MARK: Fixed pattern algorithms
6868

6969
extension RangeReplaceableCollection where Element: Equatable {
70+
/// Returns a new collection in which all occurrences of a target sequence
71+
/// are replaced by another collection.
72+
/// - Parameters:
73+
/// - other: The sequence to replace.
74+
/// - replacement: The new elements to add to the collection.
75+
/// - subrange: The range in the collection in which to search for `other`.
76+
/// - maxReplacements: A number specifying how many occurrences of `other`
77+
/// to replace. Default is `Int.max`.
78+
/// - Returns: A new collection in which all occurrences of `other` in
79+
/// `subrange` of the collection are replaced by `replacement`.
7080
public func replacing<S: Sequence, Replacement: Collection>(
7181
_ other: S,
7282
with replacement: Replacement,
@@ -79,7 +89,16 @@ extension RangeReplaceableCollection where Element: Equatable {
7989
subrange: subrange,
8090
maxReplacements: maxReplacements)
8191
}
82-
92+
93+
/// Returns a new collection in which all occurrences of a target sequence
94+
/// are replaced by another collection.
95+
/// - Parameters:
96+
/// - other: The sequence to replace.
97+
/// - replacement: The new elements to add to the collection.
98+
/// - maxReplacements: A number specifying how many occurrences of `other`
99+
/// to replace. Default is `Int.max`.
100+
/// - Returns: A new collection in which all occurrences of `other` in
101+
/// `subrange` of the collection are replaced by `replacement`.
83102
public func replacing<S: Sequence, Replacement: Collection>(
84103
_ other: S,
85104
with replacement: Replacement,
@@ -91,7 +110,13 @@ extension RangeReplaceableCollection where Element: Equatable {
91110
subrange: startIndex..<endIndex,
92111
maxReplacements: maxReplacements)
93112
}
94-
113+
114+
/// Replaces all occurrences of a target sequence with a given collection
115+
/// - Parameters:
116+
/// - other: The sequence to replace.
117+
/// - replacement: The new elements to add to the collection.
118+
/// - maxReplacements: A number specifying how many occurrences of `other`
119+
/// to replace. Default is `Int.max`.
95120
public mutating func replace<S: Sequence, Replacement: Collection>(
96121
_ other: S,
97122
with replacement: Replacement,
@@ -108,7 +133,7 @@ extension RangeReplaceableCollection where Element: Equatable {
108133
extension RangeReplaceableCollection
109134
where Self: BidirectionalCollection, Element: Comparable
110135
{
111-
public func replacing<S: Sequence, Replacement: Collection>(
136+
func replacing<S: Sequence, Replacement: Collection>(
112137
_ other: S,
113138
with replacement: Replacement,
114139
subrange: Range<Index>,
@@ -121,7 +146,7 @@ extension RangeReplaceableCollection
121146
maxReplacements: maxReplacements)
122147
}
123148

124-
public func replacing<S: Sequence, Replacement: Collection>(
149+
func replacing<S: Sequence, Replacement: Collection>(
125150
_ other: S,
126151
with replacement: Replacement,
127152
maxReplacements: Int = .max
@@ -133,7 +158,7 @@ extension RangeReplaceableCollection
133158
maxReplacements: maxReplacements)
134159
}
135160

136-
public mutating func replace<S: Sequence, Replacement: Collection>(
161+
mutating func replace<S: Sequence, Replacement: Collection>(
137162
_ other: S,
138163
with replacement: Replacement,
139164
maxReplacements: Int = .max
@@ -149,6 +174,16 @@ extension RangeReplaceableCollection
149174
// MARK: Regex algorithms
150175

151176
extension RangeReplaceableCollection where SubSequence == Substring {
177+
/// Returns a new collection in which all occurrences of a sequence matching
178+
/// the given regex are replaced by another collection.
179+
/// - Parameters:
180+
/// - regex: A regex describing the sequence to replace.
181+
/// - replacement: The new elements to add to the collection.
182+
/// - subrange: The range in the collection in which to search for `regex`.
183+
/// - maxReplacements: A number specifying how many occurrences of the
184+
/// sequence matching `regex` to replace. Default is `Int.max`.
185+
/// - Returns: A new collection in which all occurrences of subsequence
186+
/// matching `regex` in `subrange` are replaced by `replacement`.
152187
public func replacing<R: RegexComponent, Replacement: Collection>(
153188
_ regex: R,
154189
with replacement: Replacement,
@@ -161,7 +196,16 @@ extension RangeReplaceableCollection where SubSequence == Substring {
161196
subrange: subrange,
162197
maxReplacements: maxReplacements)
163198
}
164-
199+
200+
/// Returns a new collection in which all occurrences of a sequence matching
201+
/// the given regex are replaced by another collection.
202+
/// - Parameters:
203+
/// - regex: A regex describing the sequence to replace.
204+
/// - replacement: The new elements to add to the collection.
205+
/// - maxReplacements: A number specifying how many occurrences of the
206+
/// sequence matching `regex` to replace. Default is `Int.max`.
207+
/// - Returns: A new collection in which all occurrences of subsequence
208+
/// matching `regex` are replaced by `replacement`.
165209
public func replacing<R: RegexComponent, Replacement: Collection>(
166210
_ regex: R,
167211
with replacement: Replacement,
@@ -173,7 +217,14 @@ extension RangeReplaceableCollection where SubSequence == Substring {
173217
subrange: startIndex..<endIndex,
174218
maxReplacements: maxReplacements)
175219
}
176-
220+
221+
/// Replaces all occurrences of the sequence matching the given regex with
222+
/// a given collection.
223+
/// - Parameters:
224+
/// - regex: A regex describing the sequence to replace.
225+
/// - replacement: The new elements to add to the collection.
226+
/// - maxReplacements: A number specifying how many occurrences of the
227+
/// sequence matching `regex` to replace. Default is `Int.max`.
177228
public mutating func replace<R: RegexComponent, Replacement: Collection>(
178229
_ regex: R,
179230
with replacement: Replacement,

0 commit comments

Comments
 (0)