Search code examples
swiftxcodeswitch-statementoverriding

How to find all places in a project, where overridden ~= is used?


extension String {
    static func ~= (lhs: String, rhs: String) -> Bool {
        guard let regex = try? NSRegularExpression(pattern: rhs) else { return false }
        let range = NSRange(location: 0, length: lhs.utf16.count)
        return regex.firstMatch(in: lhs, options: [], range: range) != nil
    }
}

I can’t figure out a way to find all places in a project, where this overridden function is used.
Cmd+Ctrl+Shift+H doesn’t work in this case for some reason.

You can say: “Just search for ~= symbols in the codebase”.
But ~= is also implicitly used for evaluation of switch-case operators.

Use case:
Let’s say I want to change the logic of the ~= function (and I really do). And I know for sure, that I also have to fix the code in places, where this function is used. So I need to find all the switch-cases where it is used. Otherwise these switch-cases will become work incorrectly (and the compiler will not show any warnings).

P. S.
As an example of a solution: to search for all the switch appearance in the codebase and among them look only for switch String/case String. That would be exactly what I need.
The only problem with this approach: it will take me hours (if not days) 🤣


Solution

  • What if you were to deprecate the override? Then you just have to look through your compiler warnings.

    extension String {
        @available(*, deprecated, message: "Found it")
        static func ~= (lhs: String, rhs: String) -> Bool {
            guard let regex = try? NSRegularExpression(pattern: rhs) else { return false }
            let range = NSRange(location: 0, length: lhs.utf16.count)
            return regex.firstMatch(in: lhs, options: [], range: range) != nil
        }
    }