File tree Expand file tree Collapse file tree 3 files changed +70
-14
lines changed
Sources/_StringProcessing/Regex Expand file tree Collapse file tree 3 files changed +70
-14
lines changed Original file line number Diff line number Diff line change @@ -61,6 +61,13 @@ public struct Regex<Output>: RegexComponent {
6161 }
6262}
6363
64+ @available ( SwiftStdlib 5 . 7 , * )
65+ extension Regex {
66+ public init ( quoting string: String ) {
67+ self . init ( node: . quotedLiteral( string) )
68+ }
69+ }
70+
6471@available ( SwiftStdlib 5 . 7 , * )
6572extension Regex {
6673 /// A program representation that caches any lowered representation for
Original file line number Diff line number Diff line change @@ -180,20 +180,12 @@ extension BidirectionalCollection where SubSequence == Substring {
180180}
181181
182182@available ( SwiftStdlib 5 . 7 , * )
183- extension Regex {
184- public init ( quoting string : String ) {
185- self . init ( node : . quotedLiteral ( string ) )
183+ extension RegexComponent {
184+ public static func ~= ( regex : Self , input : String ) -> Bool {
185+ input . wholeMatch ( of : regex ) != nil
186186 }
187- }
188187
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
193- }
194-
195- @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
188+ public static func ~= ( regex: Self , input: Substring ) -> Bool {
189+ input. wholeMatch ( of: regex) != nil
190+ }
199191}
Original file line number Diff line number Diff line change @@ -104,4 +104,61 @@ class RegexConsumerTests: XCTestCase {
104104 result: " 9+16, 3, 10, 99+1 " )
105105 )
106106 }
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+ break
131+ }
132+ }
133+ // Success cases
134+ do {
135+ let input = " abcde "
136+
137+ switch input {
138+ case Regex {
139+ " a "
140+ ZeroOrMore ( . any)
141+ " e "
142+ } :
143+ break
144+
145+ default :
146+ XCTFail ( )
147+ }
148+
149+ guard case Regex( {
150+ " a "
151+ ZeroOrMore ( . any)
152+ " e "
153+ } ) = input else {
154+ XCTFail ( )
155+ return
156+ }
157+
158+ guard case OneOrMore( . word) = input else {
159+ XCTFail ( )
160+ return
161+ }
162+ }
163+ }
107164}
You can’t perform that action at this time.
0 commit comments