History

mac/swift: move isUrl check to String extension

der richter committed 1 year ago

Commit c75779d1 parent 6b0e4f72 files · +14 −12Browse files

Unified · Split

2 files
  1. osdep/mac/swift_extensions.swift +13 −0
  2. video/out/mac/view.swift +1 −12
osdep/mac/swift_extensions.swift+13−0
@@ -52,6 +52,19 @@ extension NSEvent.ModifierFlags { · 51 unmodified lines
5252 public static let optionRight: NSEvent.ModifierFlags = .init(rawValue: UInt(NX_DEVICERALTKEYMASK))
5353 }
5454
55+extension String {
56+ func isUrl() -> Bool {
57+ guard let regex = try? NSRegularExpression(pattern: "^(https?|ftp)://[^\\s/$.?#].[^\\s]*$",
58+ options: .caseInsensitive) else {
59+ return false
60+ }
61+ let isUrl = regex.numberOfMatches(in: self,
62+ options: [],
63+ range: NSRange(location: 0, length: self.count))
64+ return isUrl > 0
65+ }
66+}
67+
5568 extension mp_keymap {
5669 init(_ f: Int, _ t: Int32) {
5770 self.init(from: Int32(f), to: t)
video/out/mac/view.swift+1−12
@@ -65,17 +65,6 @@ class View: NSView, CALayerDelegate { · 64 unmodified lines
6565 return []
6666 }
6767
68− func isURL(_ str: String) -> Bool {
69− guard let regex = try? NSRegularExpression(pattern: "^(https?|ftp)://[^\\s/$.?#].[^\\s]*$",
70− options: .caseInsensitive) else {
71− return false
72− }
73− let isURL = regex.numberOfMatches(in: str,
74− options: [],
75− range: NSRange(location: 0, length: str.count))
76− return isURL > 0
77− }
78−
7968 override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
8069 let pb = sender.draggingPasteboard
8170 guard let types = pb.types else { return false }
@@ -89,7 +78,7 @@ class View: NSView, CALayerDelegate { · 7 unmodified lines
8978 files = str.components(separatedBy: "\n").compactMap {
9079 let url = $0.trimmingCharacters(in: .whitespacesAndNewlines)
9180 let path = (url as NSString).expandingTildeInPath
92− if isURL(url) { return url }
81+ if url.isUrl() { return url }
9382 if path.starts(with: "/") { return path }
9483 return nil
9584 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t