Skip to content

Commit 2df0f24

Browse files
authored
Add ~= overloads (#335)
1 parent 571e259 commit 2df0f24

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

Sources/_StringProcessing/Regex/Match.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,15 @@ extension Regex {
185185
self.init(node: .quotedLiteral(string))
186186
}
187187
}
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
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
199+
}

Tests/RegexTests/AlgorithmsTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,19 @@ class RegexConsumerTests: XCTestCase {
172172
s2.matches(of: regex).map(\.0),
173173
["aa"])
174174
}
175+
176+
func testSwitches() {
177+
switch "abcde" {
178+
case try! Regex("a.*f"):
179+
XCTFail()
180+
case try! Regex("abc"):
181+
XCTFail()
182+
183+
case try! Regex("a.*e"):
184+
break // success
185+
186+
default:
187+
XCTFail()
188+
}
189+
}
175190
}

0 commit comments

Comments
 (0)