Ruby的
Pathname.relative_path_from文档.
在objc中已经有KSFileUtilities的ks_stringRelativeToURL方法,非常接近.我正在寻找可以在Linux上运行的纯粹快速解决方案.
我更喜欢使用file:// URL的解决方案,但String也没问题.
文件系统可以区分大小写/不区分大小写.确定相对路径可能很棘手.
输入和预期输出的示例:
| Long Path | Relative to Path | Return Value | |--------------------------------|------------------|-------------------| | /usr/X11/agent/47.gz | /usr/X11 | agent/47.gz | | /usr/share/man/meltdown.1 | /usr/share/cups | ../man/meltdown.1 | | file:///var/logs/x/y/z/log.txt | file:///var/logs | x/y/z/log.txt |
Swift已经拥有FileManager.getRelationship(_:of:in:toItemAt:),但它没有返回相对路径.
解决方法
Swift标准库或者没有这样的方法
据我所知,基金会框架.
据我所知,基金会框架.
以下是作为URL的扩展方法的可能实现:
extension URL { func relativePath(from base: URL) -> String? { // Ensure that both URLs represent files: guard self.isFileURL && base.isFileURL else { return nil } // Remove/replace "." and "..",make paths absolute: let destComponents = self.standardized.pathComponents let baseComponents = base.standardized.pathComponents // Find number of common path components: var i = 0 while i < destComponents.count && i < baseComponents.count && destComponents[i] == baseComponents[i] { i += 1 } // Build relative path: var relComponents = Array(repeating: "..",count: baseComponents.count - i) relComponents.append(contentsOf: destComponents[i...]) return relComponents.joined(separator: "/") } }
我的测试代码:
func test(_ p1: String,_ p2: String) { let u1 = URL(fileURLWithPath: p1) let u2 = URL(fileURLWithPath: p2) print(u1.relativePath(from: u2) ?? "<ERROR>") } test("/usr/X11/agent/47.gz","/usr/X11") // "agent/47.gz" test("/usr/share/man/meltdown.1","/usr/share/cups") // "../man/meltdown.1" test("/var/logs/x/y/z/log.txt","/var/logs") // "x/y/z/log.txt"
备注:
>“.”并删除给定URL中的“..”和相对文件URL
是绝对的(都使用URL的标准化方法).
>不处理案例(in)敏感度.
>假设基本URL表示目录.
附录:@neoneye将其包装成Swift包:
SwiftyRelativePath.