Skip to content

Commit ac4aff4

Browse files
authored
Replace regex operator ~= with =~ (#1239)
1 parent 50bd4da commit ac4aff4

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
The changelog for **SwifterSwift**. Also see the [releases](https://github.com/SwifterSwift/SwifterSwift/releases) on GitHub.
44

55
## Upcoming Release
6+
### Breaking Change
7+
- **String**
8+
- Replaced `~=` operator overload with `=~` to fix breaking `switch` statements and align with other languages. [#1239](https://github.com/SwifterSwift/SwifterSwift/pull/1239) by [guykogus](https://github.com/guykogus)
9+
610
### Added
711
- **UIView**
812
- Added `addBottomSeparator(color:height:spacing:)` method to easily insert a bottom line to a view. 🚀 [#1234](https://github.com/SwifterSwift/SwifterSwift/pull/1234) by [sangjin](https://github.com/SsangG77)

Sources/SwifterSwift/SwiftStdlib/StringExtensions.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,8 +1080,8 @@ public extension String {
10801080
/// - lhs: String to check on regex pattern.
10811081
/// - rhs: Regex pattern to match against.
10821082
/// - Returns: true if string matches the pattern.
1083-
static func ~= (lhs: String, rhs: String) -> Bool {
1084-
return rhs.range(of: lhs, options: .regularExpression) != nil
1083+
static func =~ (lhs: String, rhs: String) -> Bool {
1084+
return lhs.range(of: rhs, options: .regularExpression) != nil
10851085
}
10861086
#endif
10871087

@@ -1092,9 +1092,9 @@ public extension String {
10921092
/// - lhs: String to check on regex.
10931093
/// - rhs: Regex to match against.
10941094
/// - Returns: `true` if there is at least one match for the regex in the string.
1095-
static func ~= (lhs: NSRegularExpression, rhs: String) -> Bool {
1096-
let range = NSRange(rhs.startIndex..<rhs.endIndex, in: rhs)
1097-
return lhs.firstMatch(in: rhs, range: range) != nil
1095+
static func =~ (lhs: String, rhs: NSRegularExpression) -> Bool {
1096+
let range = NSRange(lhs.startIndex..<lhs.endIndex, in: lhs)
1097+
return rhs.firstMatch(in: lhs, range: range) != nil
10981098
}
10991099
#endif
11001100

@@ -1411,3 +1411,9 @@ public extension String {
14111411
}
14121412

14131413
#endif
1414+
1415+
// MARK: Operators
1416+
1417+
#if canImport(Foundation)
1418+
infix operator =~: ComparisonPrecedence
1419+
#endif

Tests/SwiftStdlibTests/StringExtensionsTests.swift

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -620,32 +620,22 @@ final class StringExtensionsTests: XCTestCase {
620620

621621
#if canImport(Foundation)
622622
func testPatternMatchOperator() {
623-
XCTAssert("\\d{3}" ~= "123")
624-
XCTAssertFalse("\\d{3}" ~= "dasda")
625-
XCTAssertFalse(emailPattern ~= "notanemail.com")
626-
XCTAssert(emailPattern ~= "email@mail.com")
627-
XCTAssert("[a-z]at" ~= "hat")
628-
XCTAssertFalse("[a-z]at" ~= "")
629-
XCTAssert("[a-z]*" ~= "")
630-
XCTAssertFalse("[0-9]+" ~= "")
631-
632-
// https://github.com/SwifterSwift/SwifterSwift/issues/1109
633-
let codeString = "0"
634-
switch codeString {
635-
case "101":
636-
XCTAssert(codeString == "101")
637-
case "0":
638-
XCTAssert(codeString == "0")
639-
default:
640-
XCTFail("Switch string value, not matching the correct result.")
641-
}
623+
XCTAssert("123" =~ "\\d{3}")
624+
XCTAssertFalse("dasda" =~ "\\d{3}")
625+
XCTAssertFalse("notanemail.com" =~ emailPattern)
626+
XCTAssert("email@mail.com" =~ emailPattern)
627+
XCTAssert("hat" =~ "[a-z]at")
628+
XCTAssertFalse("" =~ "[a-z]at")
629+
XCTAssert("" =~ "[a-z]*")
630+
XCTAssert("" =~ "[a-z]*")
631+
XCTAssertFalse("" =~ "[0-9]+")
642632
}
643633
#endif
644634

645635
func testRegexMatchOperator() throws {
646636
let regex = try NSRegularExpression(pattern: "\\d{3}")
647-
XCTAssert(regex ~= "123")
648-
XCTAssertFalse(regex ~= "abc")
637+
XCTAssert("123" =~ regex)
638+
XCTAssertFalse("abc" =~ regex)
649639
}
650640

651641
func testPadStart() {

0 commit comments

Comments
 (0)