Skip to content

Commit dcf7dd8

Browse files
authored
Add primary associated type to RegexComponent (#454)
* Add a primary associated type to RegexComponent * Switch algorithms methods to using opaque params * Use primary assoc type in RegexBuilder algorithms
1 parent 3463d35 commit dcf7dd8

File tree

12 files changed

+73
-73
lines changed

12 files changed

+73
-73
lines changed

Sources/RegexBuilder/Algorithms.swift

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ extension BidirectionalCollection where SubSequence == Substring {
2323
/// - Parameter content: A closure that returns a regex to match against.
2424
/// - Returns: The match if there is one, or `nil` if none.
2525
@available(SwiftStdlib 5.7, *)
26-
public func wholeMatch<R: RegexComponent>(
27-
@RegexComponentBuilder of content: () -> R
28-
) -> Regex<R.RegexOutput>.Match? {
26+
public func wholeMatch<Output>(
27+
@RegexComponentBuilder of content: () -> some RegexComponent<Output>
28+
) -> Regex<Output>.Match? {
2929
wholeMatch(of: content())
3030
}
3131

@@ -35,9 +35,9 @@ extension BidirectionalCollection where SubSequence == Substring {
3535
/// - Parameter content: A closure that returns a regex to match against.
3636
/// - Returns: The match if there is one, or `nil` if none.
3737
@available(SwiftStdlib 5.7, *)
38-
public func prefixMatch<R: RegexComponent>(
39-
@RegexComponentBuilder of content: () -> R
40-
) -> Regex<R.RegexOutput>.Match? {
38+
public func prefixMatch<Output>(
39+
@RegexComponentBuilder of content: () -> some RegexComponent<Output>
40+
) -> Regex<Output>.Match? {
4141
prefixMatch(of: content())
4242
}
4343

@@ -49,8 +49,8 @@ extension BidirectionalCollection where SubSequence == Substring {
4949
/// - Returns: `true` if the regex returned by `content` matched anywhere in
5050
/// this collection, otherwise `false`.
5151
@available(SwiftStdlib 5.7, *)
52-
public func contains<R: RegexComponent>(
53-
@RegexComponentBuilder _ content: () -> R
52+
public func contains(
53+
@RegexComponentBuilder _ content: () -> some RegexComponent
5454
) -> Bool {
5555
contains(content())
5656
}
@@ -63,8 +63,8 @@ extension BidirectionalCollection where SubSequence == Substring {
6363
/// match of if the regex returned by `content`. Returns `nil` if no match
6464
/// for the regex is found.
6565
@available(SwiftStdlib 5.7, *)
66-
public func firstRange<R: RegexComponent>(
67-
@RegexComponentBuilder of content: () -> R
66+
public func firstRange(
67+
@RegexComponentBuilder of content: () -> some RegexComponent
6868
) -> Range<Index>? {
6969
firstRange(of: content())
7070
}
@@ -78,8 +78,8 @@ extension BidirectionalCollection where SubSequence == Substring {
7878
/// `content`. Returns an empty collection if no match for the regex
7979
/// is found.
8080
@available(SwiftStdlib 5.7, *)
81-
public func ranges<R: RegexComponent>(
82-
@RegexComponentBuilder of content: () -> R
81+
public func ranges(
82+
@RegexComponentBuilder of content: () -> some RegexComponent
8383
) -> [Range<Index>] {
8484
ranges(of: content())
8585
}
@@ -99,10 +99,10 @@ extension BidirectionalCollection where SubSequence == Substring {
9999
/// - Returns: A collection of substrings, split from this collection's
100100
/// elements.
101101
@available(SwiftStdlib 5.7, *)
102-
public func split<R: RegexComponent>(
102+
public func split(
103103
maxSplits: Int = Int.max,
104104
omittingEmptySubsequences: Bool = true,
105-
@RegexComponentBuilder separator: () -> R
105+
@RegexComponentBuilder separator: () -> some RegexComponent
106106
) -> [SubSequence] {
107107
split(separator: separator(), maxSplits: maxSplits, omittingEmptySubsequences: omittingEmptySubsequences)
108108
}
@@ -115,8 +115,8 @@ extension BidirectionalCollection where SubSequence == Substring {
115115
/// - Returns: `true` if the initial elements of this collection match
116116
/// regex returned by `content`; otherwise, `false`.
117117
@available(SwiftStdlib 5.7, *)
118-
public func starts<R: RegexComponent>(
119-
@RegexComponentBuilder with content: () -> R
118+
public func starts(
119+
@RegexComponentBuilder with content: () -> some RegexComponent
120120
) -> Bool {
121121
starts(with: content())
122122
}
@@ -132,8 +132,8 @@ extension BidirectionalCollection where SubSequence == Substring {
132132
/// the start of the collection, the entire contents of this collection
133133
/// are returned.
134134
@available(SwiftStdlib 5.7, *)
135-
public func trimmingPrefix<R: RegexComponent>(
136-
@RegexComponentBuilder _ content: () -> R
135+
public func trimmingPrefix(
136+
@RegexComponentBuilder _ content: () -> some RegexComponent
137137
) -> SubSequence {
138138
trimmingPrefix(content())
139139
}
@@ -145,9 +145,9 @@ extension BidirectionalCollection where SubSequence == Substring {
145145
/// - Returns: The first match for the regex created by `content` in this
146146
/// collection, or `nil` if no match is found.
147147
@available(SwiftStdlib 5.7, *)
148-
public func firstMatch<R: RegexComponent>(
149-
@RegexComponentBuilder of content: () -> R
150-
) -> Regex<R.RegexOutput>.Match? {
148+
public func firstMatch<Output>(
149+
@RegexComponentBuilder of content: () -> some RegexComponent<Output>
150+
) -> Regex<Output>.Match? {
151151
firstMatch(of: content())
152152
}
153153

@@ -159,9 +159,9 @@ extension BidirectionalCollection where SubSequence == Substring {
159159
/// - Returns: A collection of matches for the regex returned by `content`.
160160
/// If no matches are found, the returned collection is empty.
161161
@available(SwiftStdlib 5.7, *)
162-
public func matches<R: RegexComponent>(
163-
@RegexComponentBuilder of content: () -> R
164-
) -> [Regex<R.RegexOutput>.Match] {
162+
public func matches<Output>(
163+
@RegexComponentBuilder of content: () -> some RegexComponent<Output>
164+
) -> [Regex<Output>.Match] {
165165
matches(of: content())
166166
}
167167
}
@@ -175,8 +175,8 @@ where Self: BidirectionalCollection, SubSequence == Substring {
175175
/// - Parameter content: A closure that returns the regex to search for
176176
/// at the start of this collection.
177177
@available(SwiftStdlib 5.7, *)
178-
public mutating func trimPrefix<R: RegexComponent>(
179-
@RegexComponentBuilder _ content: () -> R
178+
public mutating func trimPrefix(
179+
@RegexComponentBuilder _ content: () -> some RegexComponent
180180
) {
181181
trimPrefix(content())
182182
}
@@ -196,11 +196,11 @@ where Self: BidirectionalCollection, SubSequence == Substring {
196196
/// - Returns: A new collection in which all matches for regex in `subrange`
197197
/// are replaced by `replacement`, using `content` to create the regex.
198198
@available(SwiftStdlib 5.7, *)
199-
public func replacing<R: RegexComponent, Replacement: Collection>(
199+
public func replacing<Replacement: Collection>(
200200
with replacement: Replacement,
201201
subrange: Range<Index>,
202202
maxReplacements: Int = .max,
203-
@RegexComponentBuilder content: () -> R
203+
@RegexComponentBuilder content: () -> some RegexComponent
204204
) -> Self where Replacement.Element == Element {
205205
replacing(content(), with: replacement, subrange: subrange, maxReplacements: maxReplacements)
206206
}
@@ -218,10 +218,10 @@ where Self: BidirectionalCollection, SubSequence == Substring {
218218
/// - Returns: A new collection in which all matches for regex in `subrange`
219219
/// are replaced by `replacement`, using `content` to create the regex.
220220
@available(SwiftStdlib 5.7, *)
221-
public func replacing<R: RegexComponent, Replacement: Collection>(
221+
public func replacing<Replacement: Collection>(
222222
with replacement: Replacement,
223223
maxReplacements: Int = .max,
224-
@RegexComponentBuilder content: () -> R
224+
@RegexComponentBuilder content: () -> some RegexComponent
225225
) -> Self where Replacement.Element == Element {
226226
replacing(content(), with: replacement, maxReplacements: maxReplacements)
227227
}
@@ -237,10 +237,10 @@ where Self: BidirectionalCollection, SubSequence == Substring {
237237
/// - content: A closure that returns the collection to search for
238238
/// and replace.
239239
@available(SwiftStdlib 5.7, *)
240-
public mutating func replace<R: RegexComponent, Replacement: Collection>(
240+
public mutating func replace<Replacement: Collection>(
241241
with replacement: Replacement,
242242
maxReplacements: Int = .max,
243-
@RegexComponentBuilder content: () -> R
243+
@RegexComponentBuilder content: () -> some RegexComponent
244244
) where Replacement.Element == Element {
245245
replace(content(), with: replacement, maxReplacements: maxReplacements)
246246
}
@@ -262,11 +262,11 @@ where Self: BidirectionalCollection, SubSequence == Substring {
262262
/// are replaced by the result of calling `replacement`, where regex
263263
/// is the result of calling `content`.
264264
@available(SwiftStdlib 5.7, *)
265-
public func replacing<R: RegexComponent, Replacement: Collection>(
265+
public func replacing<Output, Replacement: Collection>(
266266
subrange: Range<Index>,
267267
maxReplacements: Int = .max,
268-
@RegexComponentBuilder content: () -> R,
269-
with replacement: (Regex<R.RegexOutput>.Match) throws -> Replacement
268+
@RegexComponentBuilder content: () -> some RegexComponent<Output>,
269+
with replacement: (Regex<Output>.Match) throws -> Replacement
270270
) rethrows -> Self where Replacement.Element == Element {
271271
try replacing(content(), subrange: subrange, maxReplacements: maxReplacements, with: replacement)
272272
}
@@ -286,10 +286,10 @@ where Self: BidirectionalCollection, SubSequence == Substring {
286286
/// are replaced by the result of calling `replacement`, where regex is
287287
/// the result of calling `content`.
288288
@available(SwiftStdlib 5.7, *)
289-
public func replacing<R: RegexComponent, Replacement: Collection>(
289+
public func replacing<Output, Replacement: Collection>(
290290
maxReplacements: Int = .max,
291-
@RegexComponentBuilder content: () -> R,
292-
with replacement: (Regex<R.RegexOutput>.Match) throws -> Replacement
291+
@RegexComponentBuilder content: () -> some RegexComponent<Output>,
292+
with replacement: (Regex<Output>.Match) throws -> Replacement
293293
) rethrows -> Self where Replacement.Element == Element {
294294
try replacing(content(), maxReplacements: maxReplacements, with: replacement)
295295
}
@@ -305,10 +305,10 @@ where Self: BidirectionalCollection, SubSequence == Substring {
305305
/// - replacement: A closure that receives the full match information,
306306
/// including captures, and returns a replacement collection.
307307
@available(SwiftStdlib 5.7, *)
308-
public mutating func replace<R: RegexComponent, Replacement: Collection>(
308+
public mutating func replace<Output, Replacement: Collection>(
309309
maxReplacements: Int = .max,
310-
@RegexComponentBuilder content: () -> R,
311-
with replacement: (Regex<R.RegexOutput>.Match) throws -> Replacement
310+
@RegexComponentBuilder content: () -> some RegexComponent<Output>,
311+
with replacement: (Regex<Output>.Match) throws -> Replacement
312312
) rethrows where Replacement.Element == Element {
313313
try replace(content(), maxReplacements: maxReplacements, with: replacement)
314314
}

Sources/_StringProcessing/Algorithms/Algorithms/Contains.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ extension BidirectionalCollection where SubSequence == Substring {
7373
/// `false`.
7474
@_disfavoredOverload
7575
@available(SwiftStdlib 5.7, *)
76-
public func contains<R: RegexComponent>(_ regex: R) -> Bool {
76+
public func contains(_ regex: some RegexComponent) -> Bool {
7777
_contains(RegexConsumer(regex))
7878
}
7979
}

Sources/_StringProcessing/Algorithms/Algorithms/FirstRange.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ extension BidirectionalCollection where SubSequence == Substring {
7777
/// Returns `nil` if `regex` is not found.
7878
@_disfavoredOverload
7979
@available(SwiftStdlib 5.7, *)
80-
public func firstRange<R: RegexComponent>(of regex: R) -> Range<Index>? {
80+
public func firstRange(of regex: some RegexComponent) -> Range<Index>? {
8181
_firstRange(of: RegexConsumer(regex))
8282
}
8383

Sources/_StringProcessing/Algorithms/Algorithms/Ranges.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ extension BidirectionalCollection where SubSequence == Substring {
253253
/// `regex`. Returns an empty collection if `regex` is not found.
254254
@_disfavoredOverload
255255
@available(SwiftStdlib 5.7, *)
256-
public func ranges<R: RegexComponent>(
257-
of regex: R
256+
public func ranges(
257+
of regex: some RegexComponent
258258
) -> [Range<Index>] {
259259
Array(_ranges(of: regex))
260260
}

Sources/_StringProcessing/Algorithms/Algorithms/Replace.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ extension RangeReplaceableCollection where SubSequence == Substring {
188188
/// - Returns: A new collection in which all occurrences of subsequence
189189
/// matching `regex` in `subrange` are replaced by `replacement`.
190190
@available(SwiftStdlib 5.7, *)
191-
public func replacing<R: RegexComponent, Replacement: Collection>(
192-
_ regex: R,
191+
public func replacing<Replacement: Collection>(
192+
_ regex: some RegexComponent,
193193
with replacement: Replacement,
194194
subrange: Range<Index>,
195195
maxReplacements: Int = .max
@@ -212,8 +212,8 @@ extension RangeReplaceableCollection where SubSequence == Substring {
212212
/// matching `regex` are replaced by `replacement`.
213213
@_disfavoredOverload
214214
@available(SwiftStdlib 5.7, *)
215-
public func replacing<R: RegexComponent, Replacement: Collection>(
216-
_ regex: R,
215+
public func replacing<Replacement: Collection>(
216+
_ regex: some RegexComponent,
217217
with replacement: Replacement,
218218
maxReplacements: Int = .max
219219
) -> Self where Replacement.Element == Element {
@@ -232,8 +232,8 @@ extension RangeReplaceableCollection where SubSequence == Substring {
232232
/// - maxReplacements: A number specifying how many occurrences of the
233233
/// sequence matching `regex` to replace. Default is `Int.max`.
234234
@available(SwiftStdlib 5.7, *)
235-
public mutating func replace<R: RegexComponent, Replacement: Collection>(
236-
_ regex: R,
235+
public mutating func replace<Replacement: Collection>(
236+
_ regex: some RegexComponent,
237237
with replacement: Replacement,
238238
maxReplacements: Int = .max
239239
) where Replacement.Element == Element {

Sources/_StringProcessing/Algorithms/Algorithms/Split.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,8 @@ extension BidirectionalCollection where SubSequence == Substring {
426426
/// - Returns: A collection of substrings, split from this collection's
427427
/// elements.
428428
@_disfavoredOverload
429-
public func split<R: RegexComponent>(
430-
separator: R,
429+
public func split(
430+
separator: some RegexComponent,
431431
maxSplits: Int = .max,
432432
omittingEmptySubsequences: Bool = true
433433
) -> [SubSequence] {

Sources/_StringProcessing/Algorithms/Algorithms/StartsWith.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ extension BidirectionalCollection where SubSequence == Substring {
5555
/// - Parameter regex: A regex to compare to this sequence.
5656
/// - Returns: `true` if the initial elements of the sequence matches the
5757
/// beginning of `regex`; otherwise, `false`.
58-
public func starts<R: RegexComponent>(with regex: R) -> Bool {
58+
public func starts(with regex: some RegexComponent) -> Bool {
5959
_starts(with: RegexConsumer(regex))
6060
}
6161

Sources/_StringProcessing/Algorithms/Algorithms/Trim.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ extension BidirectionalCollection where SubSequence == Substring {
291291
/// `prefix` from the start.
292292
@_disfavoredOverload
293293
@available(SwiftStdlib 5.7, *)
294-
public func trimmingPrefix<R: RegexComponent>(_ regex: R) -> SubSequence {
294+
public func trimmingPrefix(_ regex: some RegexComponent) -> SubSequence {
295295
_trimmingPrefix(RegexConsumer(regex))
296296
}
297297

@@ -313,7 +313,7 @@ extension RangeReplaceableCollection
313313
/// - Parameter regex: The regex to remove from this collection.
314314
@_disfavoredOverload
315315
@available(SwiftStdlib 5.7, *)
316-
public mutating func trimPrefix<R: RegexComponent>(_ regex: R) {
316+
public mutating func trimPrefix(_ regex: some RegexComponent) {
317317
_trimPrefix(RegexConsumer(regex))
318318
}
319319

Sources/_StringProcessing/Algorithms/Matching/FirstMatch.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ extension BidirectionalCollection where SubSequence == Substring {
5858
/// - Returns: The first match of `regex` in the collection, or `nil` if
5959
/// there isn't a match.
6060
@available(SwiftStdlib 5.7, *)
61-
public func firstMatch<R: RegexComponent>(
62-
of r: R
63-
) -> Regex<R.RegexOutput>.Match? {
61+
public func firstMatch<Output>(
62+
of r: some RegexComponent<Output>
63+
) -> Regex<Output>.Match? {
6464
let slice = self[...]
6565
return try? r.regex.firstMatch(in: slice)
6666
}

Sources/_StringProcessing/Algorithms/Matching/MatchReplace.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,11 @@ extension RangeReplaceableCollection where SubSequence == Substring {
126126
/// - Returns: A new collection in which all occurrences of subsequence
127127
/// matching `regex` are replaced by `replacement`.
128128
@available(SwiftStdlib 5.7, *)
129-
public func replacing<R: RegexComponent, Replacement: Collection>(
130-
_ regex: R,
129+
public func replacing<Output, Replacement: Collection>(
130+
_ regex: some RegexComponent<Output>,
131131
subrange: Range<Index>,
132132
maxReplacements: Int = .max,
133-
with replacement: (Regex<R.RegexOutput>.Match) throws -> Replacement
133+
with replacement: (Regex<Output>.Match) throws -> Replacement
134134
) rethrows -> Self where Replacement.Element == Element {
135135

136136
precondition(maxReplacements >= 0)
@@ -162,10 +162,10 @@ extension RangeReplaceableCollection where SubSequence == Substring {
162162
/// - Returns: A new collection in which all occurrences of subsequence
163163
/// matching `regex` are replaced by `replacement`.
164164
@available(SwiftStdlib 5.7, *)
165-
public func replacing<R: RegexComponent, Replacement: Collection>(
166-
_ regex: R,
165+
public func replacing<Output, Replacement: Collection>(
166+
_ regex: some RegexComponent<Output>,
167167
maxReplacements: Int = .max,
168-
with replacement: (Regex<R.RegexOutput>.Match) throws -> Replacement
168+
with replacement: (Regex<Output>.Match) throws -> Replacement
169169
) rethrows -> Self where Replacement.Element == Element {
170170
try replacing(
171171
regex,
@@ -183,10 +183,10 @@ extension RangeReplaceableCollection where SubSequence == Substring {
183183
/// - replacement: A closure that receives the full match information,
184184
/// including captures, and returns a replacement collection.
185185
@available(SwiftStdlib 5.7, *)
186-
public mutating func replace<R: RegexComponent, Replacement: Collection>(
187-
_ regex: R,
186+
public mutating func replace<Output, Replacement: Collection>(
187+
_ regex: some RegexComponent<Output>,
188188
maxReplacements: Int = .max,
189-
with replacement: (Regex<R.RegexOutput>.Match) throws -> Replacement
189+
with replacement: (Regex<Output>.Match) throws -> Replacement
190190
) rethrows where Replacement.Element == Element {
191191
self = try replacing(
192192
regex,

Sources/_StringProcessing/Algorithms/Matching/Matches.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,15 +204,15 @@ extension BidirectionalCollection where SubSequence == Substring {
204204
/// - Parameter regex: The regex to search for.
205205
/// - Returns: A collection of matches of `regex`.
206206
@available(SwiftStdlib 5.7, *)
207-
public func matches<R: RegexComponent>(
208-
of r: R
209-
) -> [Regex<R.RegexOutput>.Match] {
207+
public func matches<Output>(
208+
of r: some RegexComponent<Output>
209+
) -> [Regex<Output>.Match] {
210210
let slice = self[...]
211211
var start = self.startIndex
212212
let end = self.endIndex
213213
let regex = r.regex
214214

215-
var result = [Regex<R.RegexOutput>.Match]()
215+
var result = [Regex<Output>.Match]()
216216
while start <= end {
217217
guard let match = try? regex._firstMatch(
218218
slice.base, in: start..<end

Sources/_StringProcessing/Regex/Core.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
/// A type that represents a regular expression.
1616
@available(SwiftStdlib 5.7, *)
17-
public protocol RegexComponent {
17+
public protocol RegexComponent<RegexOutput> {
1818
associatedtype RegexOutput
1919
var regex: Regex<RegexOutput> { get }
2020
}

0 commit comments

Comments
 (0)