diff options
author | Timothee Cour <timothee.cour2@gmail.com> | 2020-01-22 15:45:16 -0800 |
---|---|---|
committer | Andreas Rumpf <rumpf_a@web.de> | 2020-01-23 00:45:16 +0100 |
commit | 7356bc29b7c8e6525121491141698c899e93b2c8 (patch) | |
tree | 6b90cf609e83742804a9cc3fdb059cd7b6eccee4 | |
parent | 3e843ab335817cd4a111b56574575c2c841b40ed (diff) | |
download | Nim-7356bc29b7c8e6525121491141698c899e93b2c8.tar.gz |
new os.isRelativeTo (#13212)
-rw-r--r-- | changelog.md | 2 | ||||
-rw-r--r-- | lib/pure/os.nim | 12 | ||||
-rw-r--r-- | tests/stdlib/tos.nim | 12 |
3 files changed, 26 insertions, 0 deletions
diff --git a/changelog.md b/changelog.md index 7e08133a0..8fac8dab6 100644 --- a/changelog.md +++ b/changelog.md @@ -53,6 +53,8 @@ Eg: `echo ?.n.typ.kind` - Added `minIndex` and `maxIndex` to the `sequtils` module +- Added `os.isRelativeTo` to tell whether a path is relative to another + ## Library changes - `asyncdispatch.drain` now properly takes into account `selector.hasPendingOperations` diff --git a/lib/pure/os.nim b/lib/pure/os.nim index 3b1421cb5..12170aa40 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -406,6 +406,18 @@ proc relativePath*(path, base: string; sep = DirSep): string {. if not f.hasNext(path): break ff = f.next(path) +proc isRelativeTo*(path: string, base: string): bool {.since: (1, 1).} = + ## Returns true if `path` is relative to `base`. + runnableExamples: + doAssert isRelativeTo("./foo//bar", "foo") + doAssert isRelativeTo("foo/bar", ".") + doAssert isRelativeTo("/foo/bar.nim", "/foo/bar.nim") + doAssert not isRelativeTo("foo/bar.nims", "foo/bar.nim") + let path = path.normalizePath + let base = base.normalizePath + let ret = relativePath(path, base) + result = path.len > 0 and not ret.startsWith ".." + proc parentDirPos(path: string): int = var q = 1 if len(path) >= 1 and path[len(path)-1] in {DirSep, AltSep}: q = 2 diff --git a/tests/stdlib/tos.nim b/tests/stdlib/tos.nim index 48202157a..94f0d657e 100644 --- a/tests/stdlib/tos.nim +++ b/tests/stdlib/tos.nim @@ -382,3 +382,15 @@ block osenv: doAssert existsEnv(dummyEnvVar) == false delEnv(dummyEnvVar) # deleting an already deleted env var doAssert existsEnv(dummyEnvVar) == false + +block isRelativeTo: + doAssert isRelativeTo("/foo", "/") + doAssert isRelativeTo("/foo/bar", "/foo") + doAssert isRelativeTo("foo/bar", "foo") + doAssert isRelativeTo("/foo/bar.nim", "/foo/bar.nim") + doAssert isRelativeTo("./foo/", "foo") + doAssert isRelativeTo("foo", "./foo/") + doAssert isRelativeTo(".", ".") + doAssert isRelativeTo("foo/bar", ".") + doAssert not isRelativeTo("foo/bar.nims", "foo/bar.nim") + doAssert not isRelativeTo("/foo2", "/foo") |