mac/swift: move isUrl check to String extension

This commit is contained in:
der richter
2025-02-15 16:22:52 +00:00
parent 6b0e4f7b91
commit c75779d3b4
2 changed files with 14 additions and 12 deletions
+13
View File
@@ -52,6 +52,19 @@ extension NSEvent.ModifierFlags {
public static let optionRight: NSEvent.ModifierFlags = .init(rawValue: UInt(NX_DEVICERALTKEYMASK))
}
extension String {
func isUrl() -> Bool {
guard let regex = try? NSRegularExpression(pattern: "^(https?|ftp)://[^\\s/$.?#].[^\\s]*$",
options: .caseInsensitive) else {
return false
}
let isUrl = regex.numberOfMatches(in: self,
options: [],
range: NSRange(location: 0, length: self.count))
return isUrl > 0
}
}
extension mp_keymap {
init(_ f: Int, _ t: Int32) {
self.init(from: Int32(f), to: t)
+1 -12
View File
@@ -65,17 +65,6 @@ class View: NSView, CALayerDelegate {
return []
}
func isURL(_ str: String) -> Bool {
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))
return isURL > 0
}
override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
let pb = sender.draggingPasteboard
guard let types = pb.types else { return false }
@@ -89,7 +78,7 @@ class View: NSView, CALayerDelegate {
files = str.components(separatedBy: "\n").compactMap {
let url = $0.trimmingCharacters(in: .whitespacesAndNewlines)
let path = (url as NSString).expandingTildeInPath
if isURL(url) { return url }
if url.isUrl() { return url }
if path.starts(with: "/") { return path }
return nil
}