mirror of
https://github.com/mpv-player/mpv
synced 2025-02-19 22:36:55 +00:00
mac/swift: move isUrl check to String extension
This commit is contained in:
parent
6b0e4f7b91
commit
c75779d3b4
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user