File tree Expand file tree Collapse file tree 3 files changed +58
-13
lines changed
Sources/_StringProcessing/Regex Expand file tree Collapse file tree 3 files changed +58
-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,49 @@ class RegexConsumerTests: XCTestCase {
104
104
result: " 9+16, 3, 10, 99+1 " )
105
105
)
106
106
}
107
+
108
+ func testSwitches( ) {
109
+ // Failure cases
110
+ do {
111
+ switch " abcde " {
112
+ case Regex {
113
+ " a "
114
+ ZeroOrMore ( . any)
115
+ " f "
116
+ } :
117
+ XCTFail ( )
118
+
119
+ case " abc " :
120
+ XCTFail ( )
121
+
122
+ case Regex {
123
+ " a "
124
+ " b "
125
+ " c "
126
+ } :
127
+ XCTFail ( )
128
+
129
+ default :
130
+ return
131
+ }
132
+ }
133
+ // Success cases
134
+ do {
135
+ let input = " abcde "
136
+
137
+ guard case Regex( {
138
+ " a "
139
+ ZeroOrMore ( . any)
140
+ " e "
141
+ } ) = input else {
142
+ XCTFail ( )
143
+ return
144
+ }
145
+
146
+ guard case OneOrMore( . word) = input else {
147
+ XCTFail ( )
148
+ return
149
+ }
150
+ }
151
+ }
107
152
}
You can’t perform that action at this time.
0 commit comments