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 { public var name: String {
// force unwrapping is fine here, regex is guaranteed to be valid guard let regex = try? NSRegularExpression(pattern: " \\(\\d+\\)$", options: .caseInsensitive) else {
let regex = try! NSRegularExpression(pattern: " \\(\\d+\\)$", options: .caseInsensitive) return localizedName
}
return regex.stringByReplacingMatches( return regex.stringByReplacingMatches(
in: localizedName, in: localizedName,
range: NSRange(location: 0, length: localizedName.count), range: NSRange(location: 0, length: localizedName.count),

View File

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