File tree Expand file tree Collapse file tree 3 files changed +47
-13
lines changed
Sources/_StringProcessing/Regex Expand file tree Collapse file tree 3 files changed +47
-13
lines changed Original file line number Diff line number Diff line change @@ -61,6 +61,13 @@ public struct Regex<Output>: RegexComponent {
61
61
}
62
62
}
63
63
64
+ @available ( SwiftStdlib 5 . 7 , * )
65
+ extension Regex {
66
+ public init ( quoting string: String ) {
67
+ self . init ( node: . quotedLiteral( string) )
68
+ }
69
+ }
70
+
64
71
@available ( SwiftStdlib 5 . 7 , * )
65
72
extension Regex {
66
73
/// A program representation that caches any lowered representation for
Original file line number Diff line number Diff line change @@ -180,20 +180,13 @@ extension BidirectionalCollection where SubSequence == Substring {
180
180
}
181
181
182
182
@available ( SwiftStdlib 5 . 7 , * )
183
- extension Regex {
184
- public init ( quoting string: String ) {
185
- self . init ( node: . quotedLiteral( string) )
186
- }
187
- }
188
-
189
- @available ( SwiftStdlib 5 . 7 , * )
190
- public func ~= < Output> ( regex: Regex < Output > , input: String ) -> Bool {
191
- guard let _ = try ? regex. wholeMatch ( in: input) else { return false }
192
- return true
183
+ @_disfavoredOverload // disambiguate from equatable patterns
184
+ public func ~= < R: RegexComponent > ( regex: R , input: String ) -> Bool {
185
+ input. wholeMatch ( of: regex) != nil
193
186
}
194
187
195
188
@available ( SwiftStdlib 5 . 7 , * )
196
- public func ~= < Output > ( regex : Regex < Output > , input : Substring ) -> Bool {
197
- guard let _ = try ? regex . wholeMatch ( in : input) else { return false }
198
- return true
189
+ @ _disfavoredOverload // disambiguate from equatable patterns
190
+ public func ~= < R : RegexComponent > ( regex : R , input: Substring ) -> Bool {
191
+ input . wholeMatch ( of : regex ) != nil
199
192
}
Original file line number Diff line number Diff line change @@ -104,4 +104,38 @@ class RegexConsumerTests: XCTestCase {
104
104
result: " 9+16, 3, 10, 99+1 " )
105
105
)
106
106
}
107
+
108
+ func testSwitches( ) {
109
+ switch " abcde " {
110
+ case Regex {
111
+ " a "
112
+ ZeroOrMore ( . any)
113
+ " f "
114
+ } :
115
+ XCTFail ( )
116
+
117
+ case " abc " :
118
+ XCTFail ( )
119
+
120
+ case Regex {
121
+ " a "
122
+ " b "
123
+ " c "
124
+ } :
125
+ XCTFail ( )
126
+
127
+ case Regex {
128
+ " a "
129
+ ZeroOrMore ( . any)
130
+ " e "
131
+ } :
132
+ break // success
133
+
134
+ case OneOrMore ( . word) :
135
+ break // success
136
+
137
+ default :
138
+ XCTFail ( )
139
+ }
140
+ }
107
141
}
You can’t perform that action at this time.
0 commit comments