Skip to content

Commit 1a72e37

Browse files
committed
Rename 'RegexProtocol' to 'RegexComponent'.
With this change, should `String.match(_:)` take a `Regex` instead?
1 parent a4d89ec commit 1a72e37

25 files changed

+575
-575
lines changed

Sources/Exercises/Participants/RegexParticipant.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private func extractFromCaptures(
5858
}
5959

6060
@inline(__always) // get rid of generic please
61-
private func graphemeBreakPropertyData<RP: RegexProtocol>(
61+
private func graphemeBreakPropertyData<RP: RegexComponent>(
6262
forLine line: String,
6363
using regex: RP
6464
) -> GraphemeBreakEntry? where RP.Match == (Substring, Substring, Substring?, Substring) {

Sources/Prototypes/TourOfTypes/Literal.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ enum SemanticsLevel {
5555
}
5656

5757
/// Conformers can be ran as a regex / pattern
58-
protocol RegexProtocol {
58+
protocol RegexComponent {
5959
var level: SemanticsLevel? { get }
6060
}
6161

6262
/// Provide the option to encode semantic level statically
6363
protocol RegexLiteralProtocol: ExpressibleByRegexLiteral {
64-
associatedtype ScalarSemanticRegex: RegexProtocol
65-
associatedtype GraphemeSemanticRegex: RegexProtocol
66-
associatedtype POSIXSemanticRegex: RegexProtocol
67-
associatedtype UnspecifiedSemanticRegex: RegexProtocol = RegexLiteral
64+
associatedtype ScalarSemanticRegex: RegexComponent
65+
associatedtype GraphemeSemanticRegex: RegexComponent
66+
associatedtype POSIXSemanticRegex: RegexComponent
67+
associatedtype UnspecifiedSemanticRegex: RegexComponent = RegexLiteral
6868

6969
var scalarSemantic: ScalarSemanticRegex { get }
7070
var graphemeSemantic: GraphemeSemanticRegex { get }
@@ -84,16 +84,16 @@ struct StaticSemanticRegexLiteral: RegexLiteralProtocol {
8484
*/
8585

8686
/// A regex that has statically bound its semantic level
87-
struct ScalarSemanticRegex: RegexProtocol {
87+
struct ScalarSemanticRegex: RegexComponent {
8888
var level: SemanticsLevel? { .scalar }
8989
}
90-
struct GraphemeSemanticRegex: RegexProtocol {
90+
struct GraphemeSemanticRegex: RegexComponent {
9191
var level: SemanticsLevel? { .graphemeCluster }
9292
}
93-
struct POSIXSemanticRegex: RegexProtocol {
93+
struct POSIXSemanticRegex: RegexComponent {
9494
var level: SemanticsLevel? { .posix }
9595
}
96-
struct UnspecifiedSemanticRegex: RegexProtocol {
96+
struct UnspecifiedSemanticRegex: RegexComponent {
9797
var level: SemanticsLevel? { nil }
9898
}
9999

@@ -132,9 +132,9 @@ struct RegexLiteral: ExpressibleByRegexLiteral {
132132
}
133133
}
134134

135-
extension RegexLiteral: RegexProtocol, RegexLiteralProtocol {
135+
extension RegexLiteral: RegexComponent, RegexLiteralProtocol {
136136
/// A regex that has finally bound its semantic level (dynamically)
137-
struct BoundSemantic: RegexProtocol {
137+
struct BoundSemantic: RegexComponent {
138138
var _level: SemanticsLevel // Bound semantic level
139139
var level: SemanticsLevel? { _level }
140140
}

Sources/VariadicsGenerator/VariadicsGenerator.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ struct StandardErrorStream: TextOutputStream {
8989
var standardError = StandardErrorStream()
9090

9191
typealias Counter = Int64
92-
let regexProtocolName = "RegexProtocol"
92+
let regexComponentProtocolName = "RegexComponent"
9393
let matchAssociatedTypeName = "Match"
9494
let patternBuilderTypeName = "RegexBuilder"
9595
let patternProtocolRequirementName = "regex"
@@ -194,7 +194,7 @@ struct VariadicsGenerator: ParsableCommand {
194194
result += (0..<leftArity+rightArity).map {
195195
", C\($0)"
196196
}.joined()
197-
result += ", R0: \(regexProtocolName), R1: \(regexProtocolName)"
197+
result += ", R0: \(regexComponentProtocolName), R1: \(regexComponentProtocolName)"
198198
return result
199199
}()
200200

@@ -253,7 +253,7 @@ struct VariadicsGenerator: ParsableCommand {
253253
", C\($0)"
254254
}
255255
output("""
256-
, R0: \(regexProtocolName), R1: \(regexProtocolName)>(
256+
, R0: \(regexComponentProtocolName), R1: \(regexComponentProtocolName)>(
257257
combining next: R1, into combined: R0
258258
) -> \(regexTypeName)<
259259
""")
@@ -329,7 +329,7 @@ struct VariadicsGenerator: ParsableCommand {
329329
result += (0..<arity).map { ", C\($0)" }.joined()
330330
result += ", "
331331
}
332-
result += "Component: \(regexProtocolName)"
332+
result += "Component: \(regexComponentProtocolName)"
333333
return result
334334
}()
335335

@@ -456,7 +456,7 @@ struct VariadicsGenerator: ParsableCommand {
456456
}()
457457
let genericParams = leftGenParams + ", " + rightGenParams
458458
let whereClause: String = {
459-
var result = "where R0: \(regexProtocolName), R1: \(regexProtocolName)"
459+
var result = "where R0: \(regexComponentProtocolName), R1: \(regexComponentProtocolName)"
460460
if leftArity > 0 {
461461
result += ", R0.\(matchAssociatedTypeName) == (W0, \((0..<leftArity).map { "C\($0)" }.joined(separator: ", ")))"
462462
}
@@ -505,7 +505,7 @@ struct VariadicsGenerator: ParsableCommand {
505505
return "R, W, " + captures
506506
}()
507507
let whereClause: String = """
508-
where R: \(regexProtocolName), \
508+
where R: \(regexComponentProtocolName), \
509509
R.\(matchAssociatedTypeName) == (W, \(captures))
510510
"""
511511
let resultCaptures = (0..<arity).map { "C\($0)?" }.joined(separator: ", ")
@@ -521,8 +521,8 @@ struct VariadicsGenerator: ParsableCommand {
521521

522522
func emitCapture(arity: Int) {
523523
let genericParams = arity == 0
524-
? "R: \(regexProtocolName), W"
525-
: "R: \(regexProtocolName), W, " + (0..<arity).map { "C\($0)" }.joined(separator: ", ")
524+
? "R: \(regexComponentProtocolName), W"
525+
: "R: \(regexComponentProtocolName), W, " + (0..<arity).map { "C\($0)" }.joined(separator: ", ")
526526
let matchType = arity == 0
527527
? "W"
528528
: "(W, " + (0..<arity).map { "C\($0)" }.joined(separator: ", ") + ")"

Sources/_StringProcessing/Algorithms/Algorithms/Contains.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extension BidirectionalCollection where Element: Comparable {
4040
// MARK: Regex algorithms
4141

4242
extension BidirectionalCollection where SubSequence == Substring {
43-
public func contains<R: RegexProtocol>(_ regex: R) -> Bool {
43+
public func contains<R: RegexComponent>(_ regex: R) -> Bool {
4444
contains(RegexConsumer(regex))
4545
}
4646
}

Sources/_StringProcessing/Algorithms/Algorithms/FirstRange.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ extension BidirectionalCollection where Element: Comparable {
5656
// MARK: Regex algorithms
5757

5858
extension BidirectionalCollection where SubSequence == Substring {
59-
public func firstRange<R: RegexProtocol>(of regex: R) -> Range<Index>? {
59+
public func firstRange<R: RegexComponent>(of regex: R) -> Range<Index>? {
6060
firstRange(of: RegexConsumer(regex))
6161
}
6262

63-
public func lastRange<R: RegexProtocol>(of regex: R) -> Range<Index>? {
63+
public func lastRange<R: RegexComponent>(of regex: R) -> Range<Index>? {
6464
lastRange(of: RegexConsumer(regex))
6565
}
6666
}

Sources/_StringProcessing/Algorithms/Algorithms/Ranges.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,13 +216,13 @@ extension BidirectionalCollection where Element: Comparable {
216216
// MARK: Regex algorithms
217217

218218
extension BidirectionalCollection where SubSequence == Substring {
219-
public func ranges<R: RegexProtocol>(
219+
public func ranges<R: RegexComponent>(
220220
of regex: R
221221
) -> RangesCollection<RegexConsumer<R, Self>> {
222222
ranges(of: RegexConsumer(regex))
223223
}
224224

225-
public func rangesFromBack<R: RegexProtocol>(
225+
public func rangesFromBack<R: RegexComponent>(
226226
of regex: R
227227
) -> ReversedRangesCollection<RegexConsumer<R, Self>> {
228228
rangesFromBack(of: RegexConsumer(regex))

Sources/_StringProcessing/Algorithms/Algorithms/Replace.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ extension RangeReplaceableCollection
149149
// MARK: Regex algorithms
150150

151151
extension RangeReplaceableCollection where SubSequence == Substring {
152-
public func replacing<R: RegexProtocol, Replacement: Collection>(
152+
public func replacing<R: RegexComponent, Replacement: Collection>(
153153
_ regex: R,
154154
with replacement: Replacement,
155155
subrange: Range<Index>,
@@ -162,7 +162,7 @@ extension RangeReplaceableCollection where SubSequence == Substring {
162162
maxReplacements: maxReplacements)
163163
}
164164

165-
public func replacing<R: RegexProtocol, Replacement: Collection>(
165+
public func replacing<R: RegexComponent, Replacement: Collection>(
166166
_ regex: R,
167167
with replacement: Replacement,
168168
maxReplacements: Int = .max
@@ -174,7 +174,7 @@ extension RangeReplaceableCollection where SubSequence == Substring {
174174
maxReplacements: maxReplacements)
175175
}
176176

177-
public mutating func replace<R: RegexProtocol, Replacement: Collection>(
177+
public mutating func replace<R: RegexComponent, Replacement: Collection>(
178178
_ regex: R,
179179
with replacement: Replacement,
180180
maxReplacements: Int = .max

Sources/_StringProcessing/Algorithms/Algorithms/Split.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,13 @@ extension BidirectionalCollection where Element: Comparable {
275275
// MARK: Regex algorithms
276276

277277
extension BidirectionalCollection where SubSequence == Substring {
278-
public func split<R: RegexProtocol>(
278+
public func split<R: RegexComponent>(
279279
by separator: R
280280
) -> SplitCollection<RegexConsumer<R, Self>> {
281281
split(by: RegexConsumer(separator))
282282
}
283283

284-
public func splitFromBack<R: RegexProtocol>(
284+
public func splitFromBack<R: RegexComponent>(
285285
by separator: R
286286
) -> ReversedSplitCollection<RegexConsumer<R, Self>> {
287287
splitFromBack(by: RegexConsumer(separator))

Sources/_StringProcessing/Algorithms/Algorithms/StartsWith.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ extension BidirectionalCollection where Element: Equatable {
4848
// MARK: Regex algorithms
4949

5050
extension BidirectionalCollection where SubSequence == Substring {
51-
public func starts<R: RegexProtocol>(with regex: R) -> Bool {
51+
public func starts<R: RegexComponent>(with regex: R) -> Bool {
5252
starts(with: RegexConsumer(regex))
5353
}
5454

55-
public func ends<R: RegexProtocol>(with regex: R) -> Bool {
55+
public func ends<R: RegexComponent>(with regex: R) -> Bool {
5656
ends(with: RegexConsumer(regex))
5757
}
5858
}

Sources/_StringProcessing/Algorithms/Algorithms/Trim.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -257,47 +257,47 @@ extension RangeReplaceableCollection
257257
// MARK: Regex algorithms
258258

259259
extension BidirectionalCollection where SubSequence == Substring {
260-
public func trimmingPrefix<R: RegexProtocol>(_ regex: R) -> SubSequence {
260+
public func trimmingPrefix<R: RegexComponent>(_ regex: R) -> SubSequence {
261261
trimmingPrefix(RegexConsumer(regex))
262262
}
263263

264-
public func trimmingSuffix<R: RegexProtocol>(_ regex: R) -> SubSequence {
264+
public func trimmingSuffix<R: RegexComponent>(_ regex: R) -> SubSequence {
265265
trimmingSuffix(RegexConsumer(regex))
266266
}
267267

268-
public func trimming<R: RegexProtocol>(_ regex: R) -> SubSequence {
268+
public func trimming<R: RegexComponent>(_ regex: R) -> SubSequence {
269269
trimming(RegexConsumer(regex))
270270
}
271271
}
272272

273273
extension RangeReplaceableCollection
274274
where Self: BidirectionalCollection, SubSequence == Substring
275275
{
276-
public mutating func trimPrefix<R: RegexProtocol>(_ regex: R) {
276+
public mutating func trimPrefix<R: RegexComponent>(_ regex: R) {
277277
trimPrefix(RegexConsumer(regex))
278278
}
279279

280-
public mutating func trimSuffix<R: RegexProtocol>(_ regex: R) {
280+
public mutating func trimSuffix<R: RegexComponent>(_ regex: R) {
281281
trimSuffix(RegexConsumer(regex))
282282
}
283283

284-
public mutating func trim<R: RegexProtocol>(_ regex: R) {
284+
public mutating func trim<R: RegexComponent>(_ regex: R) {
285285
let consumer = RegexConsumer<R, Self>(regex)
286286
trimPrefix(consumer)
287287
trimSuffix(consumer)
288288
}
289289
}
290290

291291
extension Substring {
292-
public mutating func trimPrefix<R: RegexProtocol>(_ regex: R) {
292+
public mutating func trimPrefix<R: RegexComponent>(_ regex: R) {
293293
trimPrefix(RegexConsumer(regex))
294294
}
295295

296-
public mutating func trimSuffix<R: RegexProtocol>(_ regex: R) {
296+
public mutating func trimSuffix<R: RegexComponent>(_ regex: R) {
297297
trimSuffix(RegexConsumer(regex))
298298
}
299299

300-
public mutating func trim<R: RegexProtocol>(_ regex: R) {
300+
public mutating func trim<R: RegexComponent>(_ regex: R) {
301301
let consumer = RegexConsumer<R, Self>(regex)
302302
trimPrefix(consumer)
303303
trimSuffix(consumer)

Sources/_StringProcessing/Algorithms/Consumers/RegexConsumer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//===----------------------------------------------------------------------===//
1111

1212
public struct RegexConsumer<
13-
R: RegexProtocol, Consumed: BidirectionalCollection
13+
R: RegexComponent, Consumed: BidirectionalCollection
1414
> where Consumed.SubSequence == Substring {
1515
// TODO: Should `Regex` itself implement these protocols?
1616
let regex: R

Sources/_StringProcessing/Algorithms/Matching/FirstMatch.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ extension BidirectionalCollection {
3838
// MARK: Regex algorithms
3939

4040
extension BidirectionalCollection where SubSequence == Substring {
41-
public func firstMatch<R: RegexProtocol>(
41+
public func firstMatch<R: RegexComponent>(
4242
of regex: R
4343
) -> _MatchResult<RegexConsumer<R, Self>>? {
4444
firstMatch(of: RegexConsumer(regex))
4545
}
4646

47-
public func lastMatch<R: RegexProtocol>(
47+
public func lastMatch<R: RegexComponent>(
4848
of regex: R
4949
) -> _BackwardMatchResult<RegexConsumer<R, Self>>? {
5050
lastMatch(of: RegexConsumer(regex))

Sources/_StringProcessing/Algorithms/Matching/MatchReplace.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ extension RangeReplaceableCollection {
7575
// MARK: Regex algorithms
7676

7777
extension RangeReplaceableCollection where SubSequence == Substring {
78-
public func replacing<R: RegexProtocol, Replacement: Collection>(
78+
public func replacing<R: RegexComponent, Replacement: Collection>(
7979
_ regex: R,
8080
with replacement: (_MatchResult<RegexConsumer<R, Substring>>) throws -> Replacement,
8181
subrange: Range<Index>,
@@ -88,7 +88,7 @@ extension RangeReplaceableCollection where SubSequence == Substring {
8888
maxReplacements: maxReplacements)
8989
}
9090

91-
public func replacing<R: RegexProtocol, Replacement: Collection>(
91+
public func replacing<R: RegexComponent, Replacement: Collection>(
9292
_ regex: R,
9393
with replacement: (_MatchResult<RegexConsumer<R, Substring>>) throws -> Replacement,
9494
maxReplacements: Int = .max
@@ -100,7 +100,7 @@ extension RangeReplaceableCollection where SubSequence == Substring {
100100
maxReplacements: maxReplacements)
101101
}
102102

103-
public mutating func replace<R: RegexProtocol, Replacement: Collection>(
103+
public mutating func replace<R: RegexComponent, Replacement: Collection>(
104104
_ regex: R,
105105
with replacement: (_MatchResult<RegexConsumer<R, Substring>>) throws -> Replacement,
106106
maxReplacements: Int = .max

Sources/_StringProcessing/Algorithms/Matching/Matches.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,13 @@ extension BidirectionalCollection {
184184
// MARK: Regex algorithms
185185

186186
extension BidirectionalCollection where SubSequence == Substring {
187-
public func matches<R: RegexProtocol>(
187+
public func matches<R: RegexComponent>(
188188
of regex: R
189189
) -> MatchesCollection<RegexConsumer<R, Self>> {
190190
matches(of: RegexConsumer(regex))
191191
}
192192

193-
public func matchesFromBack<R: RegexProtocol>(
193+
public func matchesFromBack<R: RegexComponent>(
194194
of regex: R
195195
) -> ReversedMatchesCollection<RegexConsumer<R, Self>> {
196196
matchesFromBack(of: RegexConsumer(regex))

Sources/_StringProcessing/CharacterClass.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public struct CharacterClass: Hashable {
178178
}
179179
}
180180

181-
extension RegexProtocol where Self == CharacterClass {
181+
extension RegexComponent where Self == CharacterClass {
182182
public static var any: CharacterClass {
183183
.init(cc: .any, matchLevel: .graphemeCluster)
184184
}

Sources/_StringProcessing/RegexDSL/Anchor.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public struct Anchor {
2727
var isInverted: Bool = false
2828
}
2929

30-
extension Anchor: RegexProtocol {
30+
extension Anchor: RegexComponent {
3131
var astAssertion: AST.Atom.AssertionKind {
3232
if !isInverted {
3333
switch kind {
@@ -106,14 +106,14 @@ extension Anchor {
106106
}
107107
}
108108

109-
public func lookahead<R: RegexProtocol>(
109+
public func lookahead<R: RegexComponent>(
110110
negative: Bool = false,
111111
@RegexBuilder _ content: () -> R
112112
) -> Regex<R.Match> {
113113
Regex(node: .group(negative ? .negativeLookahead : .lookahead, content().regex.root))
114114
}
115115

116-
public func lookahead<R: RegexProtocol>(
116+
public func lookahead<R: RegexComponent>(
117117
_ component: R,
118118
negative: Bool = false
119119
) -> Regex<R.Match> {

0 commit comments

Comments
 (0)