mac: properly handle regular expressions without force unwrap

This commit is contained in:
der richter 2024-04-27 22:36:02 +02:00
parent 8f1189341f
commit 4a686dac6f
2 changed files with 7 additions and 5 deletions

View File

@ -32,8 +32,9 @@ extension NSScreen {
}
public var name: String {
// force unwrapping is fine here, regex is guaranteed to be valid
let regex = try! NSRegularExpression(pattern: " \\(\\d+\\)$", options: .caseInsensitive)
guard let regex = try? NSRegularExpression(pattern: " \\(\\d+\\)$", options: .caseInsensitive) else {
return localizedName
}
return regex.stringByReplacingMatches(
in: localizedName,
range: NSRange(location: 0, length: localizedName.count),

View File

@ -64,9 +64,10 @@ class View: NSView, CALayerDelegate {
}
func isURL(_ str: String) -> Bool {
// force unwrapping is fine here, regex is guaranteed to be valid
let regex = try! NSRegularExpression(pattern: "^(https?|ftp)://[^\\s/$.?#].[^\\s]*$",
options: .caseInsensitive)
guard let regex = try? NSRegularExpression(pattern: "^(https?|ftp)://[^\\s/$.?#].[^\\s]*$",
options: .caseInsensitive) else {
return false
}
let isURL = regex.numberOfMatches(in: str,
options: [],
range: NSRange(location: 0, length: str.count))