Skip to content

Commit

Permalink
Reverse the parameters for the ~= String operator to not break the de…
Browse files Browse the repository at this point in the history
…fault implementation (#1113)
  • Loading branch information
guykogus committed Jun 20, 2023
1 parent 2db3c65 commit 9933b1f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ The changelog for **SwifterSwift**. Also see the [releases](https://github.com/S
- Renamed `EdgeInsets` typealias to `SFEdgeInsets` to fix namespace conflicts with swiftUI's `EdgeInsets` Type. [#1055](https://github.com/SwifterSwift/SwifterSwift/pull/1055) by [MussaCharles](https://github.com/MussaCharles)
- **Font**
- Renamed `Font` typealias to `SFFont` to fix namespace conflicts with swiftUI's `Font` Type. [#1055](https://github.com/SwifterSwift/SwifterSwift/pull/1055) by [MussaCharles](https://github.com/MussaCharles)
- **String**
- Reversed the parameters for the `~=` operator in order to not break the default implementation, which causes issues in `switch` statements, for example. [#1113](https://github.com/SwifterSwift/SwifterSwift/issues/1113) by [guykogus](https://github.com/guykogus)

### Deprecated
- **String**
Expand Down
8 changes: 4 additions & 4 deletions Sources/SwifterSwift/SwiftStdlib/StringExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ public extension String {
/// - rhs: Regex pattern to match against.
/// - Returns: true if string matches the pattern.
static func ~= (lhs: String, rhs: String) -> Bool {
return lhs.range(of: rhs, options: .regularExpression) != nil
return rhs.range(of: lhs, options: .regularExpression) != nil
}
#endif

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

Expand Down
31 changes: 21 additions & 10 deletions Tests/SwiftStdlibTests/StringExtensionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -605,21 +605,32 @@ final class StringExtensionsTests: XCTestCase {

#if canImport(Foundation)
func testPatternMatchOperator() {
XCTAssert("123" ~= "\\d{3}")
XCTAssertFalse("dasda" ~= "\\d{3}")
XCTAssertFalse("notanemail.com" ~= emailPattern)
XCTAssert("email@mail.com" ~= emailPattern)
XCTAssert("hat" ~= "[a-z]at")
XCTAssertFalse("" ~= "[a-z]at")
XCTAssert("" ~= "[a-z]*")
XCTAssertFalse("" ~= "[0-9]+")
XCTAssert("\\d{3}" ~= "123")
XCTAssertFalse("\\d{3}" ~= "dasda")
XCTAssertFalse(emailPattern ~= "notanemail.com")
XCTAssert(emailPattern ~= "email@mail.com")
XCTAssert("[a-z]at" ~= "hat")
XCTAssertFalse("[a-z]at" ~= "")
XCTAssert("[a-z]*" ~= "")
XCTAssertFalse("[0-9]+" ~= "")

// https://github.com/SwifterSwift/SwifterSwift/issues/1109
let codeString = "0"
switch codeString {
case "101":
XCTAssert(codeString == "101")
case "0":
XCTAssert(codeString == "0")
default:
XCTFail("Switch string value, not matching the correct result.")
}
}
#endif

func testRegexMatchOperator() throws {
let regex = try NSRegularExpression(pattern: "\\d{3}")
XCTAssert("123" ~= regex)
XCTAssertFalse("abc" ~= regex)
XCTAssert(regex ~= "123")
XCTAssertFalse(regex ~= "abc")
}

func testPadStart() {
Expand Down

0 comments on commit 9933b1f

Please sign in to comment.