Skip to content

Commit caad657

Browse files
committed
Update Sequence/Collection constraints
1 parent 59a34cb commit caad657

File tree

6 files changed

+53
-50
lines changed

6 files changed

+53
-50
lines changed

Sources/_StringProcessing/Algorithms/Algorithms/Contains.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ extension Collection where Element: Equatable {
2828
/// - Returns: `true` if the collection contains the specified sequence,
2929
/// otherwise `false`.
3030
@available(SwiftStdlib 5.7, *)
31-
public func contains<S: Sequence>(_ other: S) -> Bool
32-
where S.Element == Element
31+
public func contains<C: Collection>(_ other: C) -> Bool
32+
where C.Element == Element
3333
{
3434
firstRange(of: other) != nil
3535
}
3636
}
3737

3838
extension BidirectionalCollection where Element: Comparable {
39-
func contains<S: Sequence>(_ other: S) -> Bool
40-
where S.Element == Element
39+
func contains<C: Collection>(_ other: C) -> Bool
40+
where C.Element == Element
4141
{
4242
if #available(SwiftStdlib 5.7, *) {
4343
return firstRange(of: other) != nil

Sources/_StringProcessing/Algorithms/Algorithms/FirstRange.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,33 @@ 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.
35+
/// Finds and returns the range of the first occurrence of a given collection
36+
/// within this collection.
37+
///
38+
/// - Parameter other: The collection to search for.
3839
/// - Returns: A range in the collection of the first occurrence of `sequence`.
3940
/// Returns nil if `sequence` is not found.
4041
@available(SwiftStdlib 5.7, *)
41-
public func firstRange<S: Sequence>(
42-
of sequence: S
43-
) -> Range<Index>? where S.Element == Element {
42+
public func firstRange<C: Collection>(
43+
of other: C
44+
) -> Range<Index>? where C.Element == Element {
4445
// TODO: Use a more efficient search algorithm
45-
let searcher = ZSearcher<SubSequence>(pattern: Array(sequence), by: ==)
46+
let searcher = ZSearcher<SubSequence>(pattern: Array(other), by: ==)
4647
return searcher.search(self[...], in: startIndex..<endIndex)
4748
}
4849
}
4950

5051
extension BidirectionalCollection where Element: Comparable {
51-
/// Finds and returns the range of the first occurrence of a given sequence
52-
/// within the collection.
53-
/// - Parameter other: The sequence to search for.
52+
/// Finds and returns the range of the first occurrence of a given collection
53+
/// within this collection.
54+
///
55+
/// - Parameter other: The collection to search for.
5456
/// - Returns: A range in the collection of the first occurrence of `sequence`.
5557
/// Returns `nil` if `sequence` is not found.
5658
@available(SwiftStdlib 5.7, *)
57-
public func firstRange<S: Sequence>(
58-
of other: S
59-
) -> Range<Index>? where S.Element == Element {
59+
public func firstRange<C: Collection>(
60+
of other: C
61+
) -> Range<Index>? where C.Element == Element {
6062
let searcher = PatternOrEmpty(
6163
searcher: TwoWaySearcher<SubSequence>(pattern: Array(other)))
6264
let slice = self[...]

Sources/_StringProcessing/Algorithms/Algorithms/Ranges.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ extension BidirectionalCollection {
175175
// MARK: Fixed pattern algorithms
176176

177177
extension Collection where Element: Equatable {
178-
func ranges<S: Sequence>(
179-
of other: S
180-
) -> RangesCollection<ZSearcher<Self>> where S.Element == Element {
178+
func ranges<C: Collection>(
179+
of other: C
180+
) -> RangesCollection<ZSearcher<Self>> where C.Element == Element {
181181
ranges(of: ZSearcher(pattern: Array(other), by: ==))
182182
}
183183

@@ -188,9 +188,9 @@ extension Collection where Element: Equatable {
188188
/// - Returns: A collection of ranges of all occurrences of `other`. Returns
189189
/// an empty collection if `other` is not found.
190190
@available(SwiftStdlib 5.7, *)
191-
public func ranges<S: Sequence>(
192-
of other: S
193-
) -> [Range<Index>] where S.Element == Element {
191+
public func ranges<C: Collection>(
192+
of other: C
193+
) -> [Range<Index>] where C.Element == Element {
194194
ranges(of: ZSearcher(pattern: Array(other), by: ==)).map { $0 }
195195
}
196196
}
@@ -207,10 +207,10 @@ extension BidirectionalCollection where Element: Equatable {
207207
}
208208

209209
extension BidirectionalCollection where Element: Comparable {
210-
func ranges<S: Sequence>(
211-
of other: S
210+
func ranges<C: Collection>(
211+
of other: C
212212
) -> RangesCollection<PatternOrEmpty<TwoWaySearcher<Self>>>
213-
where S.Element == Element
213+
where C.Element == Element
214214
{
215215
ranges(of: PatternOrEmpty(searcher: TwoWaySearcher(pattern: Array(other))))
216216
}
@@ -247,6 +247,7 @@ extension BidirectionalCollection where SubSequence == Substring {
247247
// FIXME: Return `some Collection<Range<Index>>` for SE-0346
248248
/// Finds and returns the ranges of the all occurrences of a given sequence
249249
/// within the collection.
250+
///
250251
/// - Parameter regex: The regex to search for.
251252
/// - Returns: A collection or ranges in the receiver of all occurrences of
252253
/// `regex`. Returns an empty collection if `regex` is not found.

Sources/_StringProcessing/Algorithms/Algorithms/Replace.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ extension RangeReplaceableCollection where Element: Equatable {
7878
/// - Returns: A new collection in which all occurrences of `other` in
7979
/// `subrange` of the collection are replaced by `replacement`.
8080
@available(SwiftStdlib 5.7, *)
81-
public func replacing<S: Sequence, Replacement: Collection>(
82-
_ other: S,
81+
public func replacing<C: Collection, Replacement: Collection>(
82+
_ other: C,
8383
with replacement: Replacement,
8484
subrange: Range<Index>,
8585
maxReplacements: Int = .max
86-
) -> Self where S.Element == Element, Replacement.Element == Element {
86+
) -> Self where C.Element == Element, Replacement.Element == Element {
8787
replacing(
8888
ZSearcher(pattern: Array(other), by: ==),
8989
with: replacement,
@@ -101,11 +101,11 @@ extension RangeReplaceableCollection where Element: Equatable {
101101
/// - Returns: A new collection in which all occurrences of `other` in
102102
/// `subrange` of the collection are replaced by `replacement`.
103103
@available(SwiftStdlib 5.7, *)
104-
public func replacing<S: Sequence, Replacement: Collection>(
105-
_ other: S,
104+
public func replacing<C: Collection, Replacement: Collection>(
105+
_ other: C,
106106
with replacement: Replacement,
107107
maxReplacements: Int = .max
108-
) -> Self where S.Element == Element, Replacement.Element == Element {
108+
) -> Self where C.Element == Element, Replacement.Element == Element {
109109
replacing(
110110
other,
111111
with: replacement,
@@ -120,11 +120,11 @@ extension RangeReplaceableCollection where Element: Equatable {
120120
/// - maxReplacements: A number specifying how many occurrences of `other`
121121
/// to replace. Default is `Int.max`.
122122
@available(SwiftStdlib 5.7, *)
123-
public mutating func replace<S: Sequence, Replacement: Collection>(
124-
_ other: S,
123+
public mutating func replace<C: Collection, Replacement: Collection>(
124+
_ other: C,
125125
with replacement: Replacement,
126126
maxReplacements: Int = .max
127-
) where S.Element == Element, Replacement.Element == Element {
127+
) where C.Element == Element, Replacement.Element == Element {
128128
self = replacing(
129129
other,
130130
with: replacement,
@@ -136,36 +136,36 @@ extension RangeReplaceableCollection where Element: Equatable {
136136
extension RangeReplaceableCollection
137137
where Self: BidirectionalCollection, Element: Comparable
138138
{
139-
func replacing<S: Sequence, Replacement: Collection>(
140-
_ other: S,
139+
func replacing<C: Collection, Replacement: Collection>(
140+
_ other: C,
141141
with replacement: Replacement,
142142
subrange: Range<Index>,
143143
maxReplacements: Int = .max
144-
) -> Self where S.Element == Element, Replacement.Element == Element {
144+
) -> Self where C.Element == Element, Replacement.Element == Element {
145145
replacing(
146146
PatternOrEmpty(searcher: TwoWaySearcher(pattern: Array(other))),
147147
with: replacement,
148148
subrange: subrange,
149149
maxReplacements: maxReplacements)
150150
}
151151

152-
func replacing<S: Sequence, Replacement: Collection>(
153-
_ other: S,
152+
func replacing<C: Collection, Replacement: Collection>(
153+
_ other: C,
154154
with replacement: Replacement,
155155
maxReplacements: Int = .max
156-
) -> Self where S.Element == Element, Replacement.Element == Element {
156+
) -> Self where C.Element == Element, Replacement.Element == Element {
157157
replacing(
158158
other,
159159
with: replacement,
160160
subrange: startIndex..<endIndex,
161161
maxReplacements: maxReplacements)
162162
}
163163

164-
mutating func replace<S: Sequence, Replacement: Collection>(
165-
_ other: S,
164+
mutating func replace<C: Collection, Replacement: Collection>(
165+
_ other: C,
166166
with replacement: Replacement,
167167
maxReplacements: Int = .max
168-
) where S.Element == Element, Replacement.Element == Element {
168+
) where C.Element == Element, Replacement.Element == Element {
169169
self = replacing(
170170
other,
171171
with: replacement,

Sources/_StringProcessing/Algorithms/Algorithms/Split.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,10 @@ extension BidirectionalCollection where Element: Equatable {
266266
}
267267

268268
extension BidirectionalCollection where Element: Comparable {
269-
func split<S: Sequence>(
270-
by separator: S
269+
func split<C: Collection>(
270+
by separator: C
271271
) -> SplitCollection<PatternOrEmpty<TwoWaySearcher<Self>>>
272-
where S.Element == Element
272+
where C.Element == Element
273273
{
274274
split(
275275
by: PatternOrEmpty(searcher: TwoWaySearcher(pattern: Array(separator))))

Sources/_StringProcessing/Algorithms/Algorithms/Trim.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ extension Collection where Element: Equatable {
188188
/// - Returns: A collection containing the elements of the collection that are
189189
/// not removed by `predicate`.
190190
@available(SwiftStdlib 5.7, *)
191-
public func trimmingPrefix<Prefix: Collection>(
191+
public func trimmingPrefix<Prefix: Sequence>(
192192
_ prefix: Prefix
193193
) -> SubSequence where Prefix.Element == Element {
194194
trimmingPrefix(FixedPatternConsumer(pattern: prefix))
@@ -202,7 +202,7 @@ extension Collection where SubSequence == Self, Element: Equatable {
202202
/// as its argument and returns a Boolean value indicating whether the
203203
/// element should be removed from the collection.
204204
@available(SwiftStdlib 5.7, *)
205-
public mutating func trimPrefix<Prefix: Collection>(
205+
public mutating func trimPrefix<Prefix: Sequence>(
206206
_ prefix: Prefix
207207
) where Prefix.Element == Element {
208208
trimPrefix(FixedPatternConsumer<SubSequence, Prefix>(pattern: prefix))
@@ -217,7 +217,7 @@ extension RangeReplaceableCollection where Element: Equatable {
217217
/// as its argument and returns a Boolean value indicating whether the
218218
/// element should be removed from the collection.
219219
@available(SwiftStdlib 5.7, *)
220-
public mutating func trimPrefix<Prefix: Collection>(
220+
public mutating func trimPrefix<Prefix: Sequence>(
221221
_ prefix: Prefix
222222
) where Prefix.Element == Element {
223223
trimPrefix(FixedPatternConsumer(pattern: prefix))

0 commit comments

Comments
 (0)