diff options
author | Araq <rumpf_a@web.de> | 2013-09-24 09:22:29 +0200 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2013-09-24 09:22:29 +0200 |
commit | 05d12897a2cc8d8cc4231d8648d7dd954da0f758 (patch) | |
tree | dfcf008ca83a75be13be4cd031b6174d9fc949c9 /lib/pure | |
parent | f1e789bfc20a807365fc59f1a5f0c6fb96803ac7 (diff) | |
download | Nim-05d12897a2cc8d8cc4231d8648d7dd954da0f758.tar.gz |
changed parentDir to return the empty string on error
Diffstat (limited to 'lib/pure')
-rw-r--r-- | lib/pure/os.nim | 36 |
1 files changed, 20 insertions, 16 deletions
diff --git a/lib/pure/os.nim b/lib/pure/os.nim index 43bdb16ee..886823350 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -544,33 +544,31 @@ proc SplitPath*(path: string): tuple[head, tail: string] {. result.head = "" result.tail = path +proc parentDirPos(path: string): int = + var q = 1 + if path[len(path)-1] in {dirsep, altsep}: q = 2 + for i in countdown(len(path)-q, 0): + if path[i] in {dirsep, altsep}: return i + result = -1 + proc parentDir*(path: string): string {. noSideEffect, rtl, extern: "nos$1".} = ## Returns the parent directory of `path`. ## ## This is often the same as the ``head`` result of ``splitPath``. - ## If there is no parent, ``path`` is returned. + ## If there is no parent, "" is returned. ## | Example: ``parentDir("/usr/local/bin") == "/usr/local"``. ## | Example: ``parentDir("/usr/local/bin/") == "/usr/local"``. - var - sepPos = -1 - q = 1 - if path[len(path)-1] in {dirsep, altsep}: - q = 2 - for i in countdown(len(path)-q, 0): - if path[i] in {dirsep, altsep}: - sepPos = i - break + let sepPos = parentDirPos(path) if sepPos >= 0: result = substr(path, 0, sepPos-1) else: - result = path + result = "" proc isRootDir*(path: string): bool {. noSideEffect, rtl, extern: "nos$1".} = ## Checks whether a given `path` is a root directory - var p = parentDir(path) - result = p == path or p.len == 0 + result = parentDirPos(path) < 0 iterator parentDirs*(path: string, fromRoot=false, inclusive=true): string = ## Walks over all parent directories of a given `path` @@ -591,14 +589,20 @@ iterator parentDirs*(path: string, fromRoot=false, inclusive=true): string = else: for i in countup(0, path.len - 2): # ignore the last / # deal with non-normalized paths such as /foo//bar//baz - if path[i] in {dirsep, altsep} and (i == 0 or path[i-1] notin {dirsep, altsep}): + if path[i] in {dirsep, altsep} and + (i == 0 or path[i-1] notin {dirsep, altsep}): yield path.substr(0, i) if inclusive: yield path proc `/../` * (head, tail: string): string {.noSideEffect.} = - ## The same as ``parentDir(head) / tail`` - return parentDir(head) / tail + ## The same as ``parentDir(head) / tail`` unless there is no parent directory. + ## Then ``head / tail`` is performed instead. + let sepPos = parentDirPos(path) + if sepPos >= 0: + result = substr(path, 0, sepPos-1) / tail + else: + result = path / tail proc normExt(ext: string): string = if ext == "" or ext[0] == extSep: result = ext # no copy needed here |