Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverse the parameters for the ~= String operator to not break the default implementation #1113

Merged
merged 3 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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]+" ~= "")
}
#endif

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

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

func testPadStart() {
Expand Down