如何找到项目中使用 overridden ~= 的所有地方?

huangapple go评论59阅读模式
英文:

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
}
}

我找不到一种方法来查找项目中使用了这个重写函数的所有位置。
在这种情况下,Cmd+Ctrl+Shift+H 不起作用。
你可以说:“只需搜索代码库中的~=符号”。
~=隐式用于评估switch-case操作符。
用例:
假设我想更改~=函数的逻辑(我真的想这样做)。
我确切地知道,我还必须修复使用这个函数的地方的代码。
否则,这些switch-case将不正确地工作(编译器不会显示任何警告)。
附言:
作为解决方案的示例:搜索代码库中的所有switch出现,并在其中仅查找switch String/case String
这正是我所需要的。
这种方法的唯一问题是:这将花费我几个小时(如果不是几天)🤣

英文:
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) 🤣

答案1

得分: 1

如果您要弃用覆盖,那么您只需查看编译器警告。

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
    }
}
英文:

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
    }
}

huangapple
  • 本文由 发表于 2023年4月11日 09:21:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定