Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get relative path? #31

Open
devSC opened this issue Oct 23, 2016 · 3 comments
Open

How to get relative path? #31

devSC opened this issue Oct 23, 2016 · 3 comments

Comments

@devSC
Copy link

devSC commented Oct 23, 2016

It's have a easy method to get relative path?

@phimage
Copy link
Collaborator

phimage commented Jan 16, 2017

relative path from what?
from current path?

there is no cocoa functions I think so we must implement it

I could provide a non tested code

  • maybe we must standardize the path before
  • then some code maybe are not swift3 . compliant
  • return a Path will be more elegant
extension Path {
 
    func stringWithPathRelative(to anchorPath: Path) -> String {
        let pathComponents = self. components
        let anchorComponents = anchorPath. components

        var componentsInCommon = 0
        for (c1, c2) in zip(pathComponents, anchorComponents) {
            if c1 != c2 {
                break
            }
            componentsInCommon += 1
        }

        let numberOfParentComponents = anchorComponents.count - componentsInCommon
        let numberOfPathComponents = pathComponents.count - componentsInCommon

        var relativeComponents = [String]()
        relativeComponents.reserveCapacity(numberOfParentComponents + numberOfPathComponents)
        for _ in 0..<numberOfParentComponents {
            relativeComponents.append("..")
        }
        relativeComponents.appendContentsOf(pathComponents[componentsInCommon..<pathComponents.count])

        return String.pathWithComponents(relativeComponents)
    }
}

@ivankolesnik
Copy link

I have something like this in my extension

    func relativeTo(path: Path) -> Path? {
        let root = path.components
        let this = self.components
        
        guard this.starts(with: root) else {
            return nil
        }
        
        let rel = this.suffix(from: root.endIndex)
        let path = rel.map({ $0.rawValue }).joined(separator: "/")
        
        return Path(path)
    }

And I use it like that

let filePath = file.relativeTo(path: Path.userDocuments)

This function does not handle some edge cases, but it suits my needs.

@neoneye
Copy link

neoneye commented Jan 21, 2018

I have made separate repo, SwiftyRelativePath, for just this purpose with tests for edge cases.

let url0 = URL(fileURLWithPath: "/computer/qubit/17")
let url1 = URL(fileURLWithPath: "/computer/lab")
url0.relativePath(from: url1) // -> "../qubit/17"

Feel free to import it into FileKit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants