Skip to content

Commit 1c02a78

Browse files
committed
Update and sync documentation.
1 parent a1ae820 commit 1c02a78

File tree

11 files changed

+366
-261
lines changed

11 files changed

+366
-261
lines changed

Documentation/Evolution/StringProcessingAlgorithms.md

Lines changed: 171 additions & 130 deletions
Large diffs are not rendered by default.

Sources/_StringProcessing/Algorithms/Algorithms/Contains.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ extension Collection {
2323

2424
extension Collection where Element: Equatable {
2525
/// 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`.
26+
/// given collection.
27+
///
28+
/// - Parameter other: A collection to search for within this collection.
29+
/// - Returns: `true` if the collection contains `other`, otherwise `false`.
3030
@available(SwiftStdlib 5.7, *)
3131
public func contains<C: Collection>(_ other: C) -> Bool
3232
where C.Element == Element
@@ -63,11 +63,12 @@ extension StringProtocol {
6363
// MARK: Regex algorithms
6464

6565
extension BidirectionalCollection where SubSequence == Substring {
66-
/// Returns a Boolean value indicating whether the collection contains the
67-
/// given regex.
66+
/// Returns a Boolean value indicating whether this collection contains a
67+
/// match for the given regex.
68+
///
6869
/// - Parameter regex: A regex to search for within this collection.
69-
/// - Returns: `true` if the regex was found in the collection, otherwise
70-
/// `false`.
70+
/// - Returns: `true` if `regex` matched anywhere in this collection,
71+
/// otherwise `false`.
7172
@available(SwiftStdlib 5.7, *)
7273
public func contains<R: RegexComponent>(_ regex: R) -> Bool {
7374
contains(RegexConsumer(regex))

Sources/_StringProcessing/Algorithms/Algorithms/FirstRange.swift

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ 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 collection
36-
/// within this collection.
35+
/// Returns the range of the first occurrence of the given collection within
36+
/// this collection.
3737
///
3838
/// - Parameter other: The collection to search for.
39-
/// - Returns: A range in the collection of the first occurrence of `sequence`.
40-
/// Returns nil if `sequence` is not found.
39+
/// - Returns: A range in the collection of the first occurrence of `other`.
40+
/// Returns `nil` if `other` is not found.
4141
@available(SwiftStdlib 5.7, *)
4242
public func firstRange<C: Collection>(
4343
of other: C
@@ -49,12 +49,12 @@ extension Collection where Element: Equatable {
4949
}
5050

5151
extension BidirectionalCollection where Element: Comparable {
52-
/// Finds and returns the range of the first occurrence of a given collection
53-
/// within this collection.
52+
/// Returns the range of the first occurrence of the given collection within
53+
/// this collection.
5454
///
5555
/// - Parameter other: The collection to search for.
56-
/// - Returns: A range in the collection of the first occurrence of `sequence`.
57-
/// Returns `nil` if `sequence` is not found.
56+
/// - Returns: A range in the collection of the first occurrence of `other`.
57+
/// Returns `nil` if `other` is not found.
5858
@available(SwiftStdlib 5.7, *)
5959
public func firstRange<C: Collection>(
6060
of other: C
@@ -70,11 +70,12 @@ extension BidirectionalCollection where Element: Comparable {
7070
// MARK: Regex algorithms
7171

7272
extension BidirectionalCollection where SubSequence == Substring {
73-
/// Finds and returns the range of the first occurrence of a given regex
74-
/// within the collection.
73+
/// Returns the range of the first match for the given regex within this
74+
/// collection.
75+
///
7576
/// - Parameter regex: The regex to search for.
76-
/// - Returns: A range in the collection of the first occurrence of `regex`.
77-
/// Returns `nil` if `regex` is not found.
77+
/// - Returns: A range in the collection of the first occurrence of the first
78+
/// match of `regex`. Returns `nil` if no match for `regex` is found.
7879
@available(SwiftStdlib 5.7, *)
7980
public func firstRange<R: RegexComponent>(of regex: R) -> Range<Index>? {
8081
firstRange(of: RegexConsumer(regex))

Sources/_StringProcessing/Algorithms/Algorithms/Ranges.swift

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,12 @@ extension Collection where Element: Equatable {
182182
}
183183

184184
// FIXME: Return `some Collection<Range<Index>>` for SE-0346
185-
/// Finds and returns the ranges of the all occurrences of a given sequence
186-
/// within the collection.
187-
/// - Parameter other: The sequence to search for.
188-
/// - Returns: A collection of ranges of all occurrences of `other`. Returns
189-
/// an empty collection if `other` is not found.
185+
/// Returns the ranges of the all non-overlapping occurrences of the given
186+
/// collection within this collection.
187+
///
188+
/// - Parameter other: The collection to search for.
189+
/// - Returns: A collection of ranges of all non-overlapping occurrences of
190+
/// `other`. Returns an empty collection if `other` is not found.
190191
@available(SwiftStdlib 5.7, *)
191192
public func ranges<C: Collection>(
192193
of other: C
@@ -245,12 +246,12 @@ extension BidirectionalCollection where SubSequence == Substring {
245246
}
246247

247248
// FIXME: Return `some Collection<Range<Index>>` for SE-0346
248-
/// Finds and returns the ranges of the all occurrences of a given sequence
249-
/// within the collection.
250-
///
249+
/// Returns the ranges of the all non-overlapping matches for the given
250+
/// regex within this collection.
251+
///
251252
/// - Parameter regex: The regex to search for.
252-
/// - Returns: A collection or ranges in the receiver of all occurrences of
253-
/// `regex`. Returns an empty collection if `regex` is not found.
253+
/// - Returns: A collection of ranges of all matches for `regex`. Returns an
254+
/// empty collection if no match for `regex` is found.
254255
@available(SwiftStdlib 5.7, *)
255256
public func ranges<R: RegexComponent>(
256257
of regex: R

Sources/_StringProcessing/Algorithms/Algorithms/Replace.swift

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,18 @@ 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.
70+
/// Returns a new collection in which all occurrences of the given collection
71+
/// are replaced.
72+
///
7273
/// - Parameters:
73-
/// - other: The sequence to replace.
74-
/// - replacement: The new elements to add to the collection.
74+
/// - other: The collection to to search for and replace.
75+
/// - replacement: The new elements to add to the collection in place of
76+
/// `other`.
7577
/// - subrange: The range in the collection in which to search for `other`.
7678
/// - maxReplacements: A number specifying how many occurrences of `other`
77-
/// to replace. Default is `Int.max`.
79+
/// to replace.
7880
/// - Returns: A new collection in which all occurrences of `other` in
79-
/// `subrange` of the collection are replaced by `replacement`.
81+
/// `subrange` are replaced by `replacement`.
8082
@available(SwiftStdlib 5.7, *)
8183
public func replacing<C: Collection, Replacement: Collection>(
8284
_ other: C,
@@ -91,15 +93,17 @@ extension RangeReplaceableCollection where Element: Equatable {
9193
maxReplacements: maxReplacements)
9294
}
9395

94-
/// Returns a new collection in which all occurrences of a target sequence
95-
/// are replaced by another collection.
96+
/// Returns a new collection in which all occurrences of the given collection
97+
/// are replaced.
98+
///
9699
/// - Parameters:
97-
/// - other: The sequence to replace.
98-
/// - replacement: The new elements to add to the collection.
100+
/// - other: The collection to to search for and replace.
101+
/// - replacement: The new elements to add to the collection in place of
102+
/// `other`.
99103
/// - maxReplacements: A number specifying how many occurrences of `other`
100-
/// to replace. Default is `Int.max`.
101-
/// - Returns: A new collection in which all occurrences of `other` in
102-
/// `subrange` of the collection are replaced by `replacement`.
104+
/// to replace.
105+
/// - Returns: A new collection in which all occurrences of `other` are
106+
/// replaced by `replacement`.
103107
@available(SwiftStdlib 5.7, *)
104108
public func replacing<C: Collection, Replacement: Collection>(
105109
_ other: C,
@@ -113,12 +117,14 @@ extension RangeReplaceableCollection where Element: Equatable {
113117
maxReplacements: maxReplacements)
114118
}
115119

116-
/// Replaces all occurrences of a target sequence with a given collection
120+
/// Replaces all ocurrences of the given collection in this collection.
121+
///
117122
/// - Parameters:
118-
/// - other: The sequence to replace.
119-
/// - replacement: The new elements to add to the collection.
123+
/// - other: The collection to to search for and replace.
124+
/// - replacement: The new elements to add to the collection in place of
125+
/// `other`.
120126
/// - maxReplacements: A number specifying how many occurrences of `other`
121-
/// to replace. Default is `Int.max`.
127+
/// to replace.
122128
@available(SwiftStdlib 5.7, *)
123129
public mutating func replace<C: Collection, Replacement: Collection>(
124130
_ other: C,
@@ -177,16 +183,18 @@ extension RangeReplaceableCollection
177183
// MARK: Regex algorithms
178184

179185
extension RangeReplaceableCollection where SubSequence == Substring {
180-
/// Returns a new collection in which all occurrences of a sequence matching
181-
/// the given regex are replaced by another collection.
186+
/// Returns a new collection in which all matches for the given regex
187+
/// are replaced.
188+
///
182189
/// - Parameters:
183-
/// - regex: A regex describing the sequence to replace.
184-
/// - replacement: The new elements to add to the collection.
190+
/// - regex: The collection to to search for and replace.
191+
/// - replacement: The new elements to add to the collection in place of
192+
/// each match for `regex`.
185193
/// - subrange: The range in the collection in which to search for `regex`.
186-
/// - maxReplacements: A number specifying how many occurrences of the
187-
/// sequence matching `regex` to replace. Default is `Int.max`.
188-
/// - Returns: A new collection in which all occurrences of subsequence
189-
/// matching `regex` in `subrange` are replaced by `replacement`.
194+
/// - maxReplacements: A number specifying how many occurrences of `regex`
195+
/// to replace.
196+
/// - Returns: A new collection in which all matches for `regex` in
197+
/// `subrange` are replaced by `replacement`.
190198
@available(SwiftStdlib 5.7, *)
191199
public func replacing<R: RegexComponent, Replacement: Collection>(
192200
_ regex: R,
@@ -201,15 +209,17 @@ extension RangeReplaceableCollection where SubSequence == Substring {
201209
maxReplacements: maxReplacements)
202210
}
203211

204-
/// Returns a new collection in which all occurrences of a sequence matching
205-
/// the given regex are replaced by another collection.
212+
/// Returns a new collection in which all matches for the given regex
213+
/// are replaced.
214+
///
206215
/// - Parameters:
207-
/// - regex: A regex describing the sequence to replace.
208-
/// - replacement: The new elements to add to the collection.
209-
/// - maxReplacements: A number specifying how many occurrences of the
210-
/// sequence matching `regex` to replace. Default is `Int.max`.
211-
/// - Returns: A new collection in which all occurrences of subsequence
212-
/// matching `regex` are replaced by `replacement`.
216+
/// - regex: The collection to to search for and replace.
217+
/// - replacement: The new elements to add to the collection in place of
218+
/// each match for `regex`.
219+
/// - maxReplacements: A number specifying how many occurrences of `regex`
220+
/// to replace.
221+
/// - Returns: A new collection in which all matches for `regex` are replaced
222+
/// by `replacement`.
213223
@available(SwiftStdlib 5.7, *)
214224
public func replacing<R: RegexComponent, Replacement: Collection>(
215225
_ regex: R,
@@ -223,13 +233,14 @@ extension RangeReplaceableCollection where SubSequence == Substring {
223233
maxReplacements: maxReplacements)
224234
}
225235

226-
/// Replaces all occurrences of the sequence matching the given regex with
227-
/// a given collection.
236+
/// Replaces all matches for the given regex in this collection.
237+
///
228238
/// - Parameters:
229-
/// - regex: A regex describing the sequence to replace.
230-
/// - replacement: The new elements to add to the collection.
231-
/// - maxReplacements: A number specifying how many occurrences of the
232-
/// sequence matching `regex` to replace. Default is `Int.max`.
239+
/// - regex: The collection to to search for and replace.
240+
/// - replacement: The new elements to add to the collection in place of
241+
/// each match for `regex`.
242+
/// - maxReplacements: A number specifying how many occurrences of `regex`
243+
/// to replace.
233244
@available(SwiftStdlib 5.7, *)
234245
public mutating func replace<R: RegexComponent, Replacement: Collection>(
235246
_ regex: R,

Sources/_StringProcessing/Algorithms/Algorithms/Split.swift

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,19 @@ extension Collection where Element: Equatable {
298298

299299
// FIXME: Return `some Collection<SubSequence>` for SE-0346
300300
/// Returns the longest possible subsequences of the collection, in order,
301-
/// around elements equal to the given separator.
302-
/// - Parameter separator: The element to be split upon.
301+
/// around elements equal to the given separator collection.
302+
///
303+
/// - Parameters:
304+
/// - separator: A collection of elements to be split upon.
305+
/// - maxSplits: The maximum number of times to split the collection,
306+
/// or one less than the number of subsequences to return.
307+
/// - omittingEmptySubsequences: If `false`, an empty subsequence is
308+
/// returned in the result for each consecutive pair of separator
309+
/// sequences in the collection and for each instance of separator
310+
/// sequences at the start or end of the collection. If `true`, only
311+
/// nonempty subsequences are returned.
303312
/// - Returns: A collection of subsequences, split from this collection's
304-
/// elements.
313+
/// elements.
305314
@available(SwiftStdlib 5.7, *)
306315
public func split<C: Collection>(
307316
separator: C,
@@ -372,10 +381,18 @@ extension BidirectionalCollection where SubSequence == Substring {
372381

373382
// FIXME: Return `some Collection<Subsequence>` for SE-0346
374383
/// Returns the longest possible subsequences of the collection, in order,
375-
/// around elements equal to the given separator.
376-
/// - Parameter separator: A regex describing elements to be split upon.
384+
/// around subsequence that match the given separator regex.
385+
///
386+
/// - Parameters:
387+
/// - separator: A regex to be split upon.
388+
/// - maxSplits: The maximum number of times to split the collection,
389+
/// or one less than the number of subsequences to return.
390+
/// - omittingEmptySubsequences: If `false`, an empty subsequence is
391+
/// returned in the result for each consecutive pair of matches
392+
/// and for each match at the start or end of the collection. If
393+
/// `true`, only nonempty subsequences are returned.
377394
/// - Returns: A collection of substrings, split from this collection's
378-
/// elements.
395+
/// elements.
379396
@_disfavoredOverload
380397
public func split<R: RegexComponent>(
381398
separator: R,

Sources/_StringProcessing/Algorithms/Algorithms/StartsWith.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ extension BidirectionalCollection where Element: Equatable {
4949

5050
@available(SwiftStdlib 5.7, *)
5151
extension BidirectionalCollection where SubSequence == Substring {
52-
/// Returns a Boolean value indicating whether the initial elements of the
53-
/// sequence are the same as the elements in the specified regex.
54-
/// - Parameter regex: A regex to compare to this sequence.
55-
/// - Returns: `true` if the initial elements of the sequence matches the
56-
/// beginning of `regex`; otherwise, `false`.
52+
/// Returns a Boolean value indicating whether the initial elements of this
53+
/// collection are a match for the given regex.
54+
///
55+
/// - Parameter regex: A regex to match at the beginning of this collection.
56+
/// - Returns: `true` if the initial elements of this collection match
57+
/// `regex`; otherwise, `false`.
5758
public func starts<R: RegexComponent>(with regex: R) -> Bool {
5859
starts(with: RegexConsumer(regex))
5960
}

0 commit comments

Comments
 (0)