Skip to content

Commit

Permalink
do it different
Browse files Browse the repository at this point in the history
  • Loading branch information
disruptek committed Jan 10, 2020
1 parent 57b7e0b commit 56e3780
Showing 1 changed file with 53 additions and 25 deletions.
78 changes: 53 additions & 25 deletions lib/pure/os.nim
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,7 @@ type

include "includes/osseps"

proc normalizePathEnd(path: var string, trailingSep = false) =
## Ensures ``path`` has exactly 0 or 1 trailing `DirSep`, depending on
## ``trailingSep``, and taking care of edge cases: it preserves whether
## a path is absolute or relative, and makes sure trailing sep is `DirSep`,
## not `AltSep`.
## See also:
## * `normalizePathEnd proc <#normalizePathEnd,string,bool>`_ for a version
## which returns a new string.
template normalizePathEndImpl(path: var string; trailingSep: bool) =
if path.len == 0: return
var i = path.len
while i >= 1 and path[i-1] in {DirSep, AltSep}: dec(i)
Expand All @@ -105,24 +98,59 @@ proc normalizePathEnd(path: var string, trailingSep = false) =
# // => / (empty case was already taken care of)
path = $DirSep

proc normalizePathEnd(path: string, trailingSep = false): string =
## A version of ``normalizePathEnd()``
## See also:
## * `normalizePathEnd proc <#normalizePathEnd,string,bool>`_ for the
## in-place version.
runnableExamples:
when defined(posix):
assert normalizePathEnd("/lib/", trailingSep = true) == "/lib/"
assert normalizePathEnd("", trailingSep = true) == "/"
assert normalizePathEnd("/lib", trailingSep = false) == "/lib"
assert normalizePathEnd("lib//", trailingSep = false) == "lib"
assert normalizePathEnd("lib", trailingSep = false) == "lib"
assert normalizePathEnd("/lib//", trailingSep = true) == "/lib/"
result = path
result.normalizePathEnd(trailingSep)

when (NimMajor, NimMinor) >= (1, 1):
export normalizePathEnd
proc normalizePathEnd*(path: var string, trailingSep = false) =
## Ensures ``path`` has exactly 0 or 1 trailing `DirSep`, depending on
## ``trailingSep``, and taking care of edge cases: it preserves whether
## a path is absolute or relative, and makes sure trailing sep is `DirSep`,
## not `AltSep`.
## See also:
## * `normalizePathEnd proc <#normalizePathEnd,string,bool>`_ for a version
## which returns a new string.
normalizePathEndImpl(path, trailingSep = trailingSep)

proc normalizePathEnd*(path: string, trailingSep = false): string =
## A version of ``normalizePathEnd()``
## See also:
## * `normalizePathEnd proc <#normalizePathEnd,string,bool>`_ for the
## in-place version.
runnableExamples:
when defined(posix):
assert normalizePathEnd("/lib/", trailingSep = true) == "/lib/"
assert normalizePathEnd("", trailingSep = true) == "/"
assert normalizePathEnd("/lib", trailingSep = false) == "/lib"
assert normalizePathEnd("lib//", trailingSep = false) == "lib"
assert normalizePathEnd("lib", trailingSep = false) == "lib"
assert normalizePathEnd("/lib//", trailingSep = true) == "/lib/"
result = path
result.normalizePathEnd(trailingSep)

else:
proc normalizePathEnd(path: var string, trailingSep = false) =
## Ensures ``path`` has exactly 0 or 1 trailing `DirSep`, depending on
## ``trailingSep``, and taking care of edge cases: it preserves whether
## a path is absolute or relative, and makes sure trailing sep is `DirSep`,
## not `AltSep`.
## See also:
## * `normalizePathEnd proc <#normalizePathEnd,string,bool>`_ for a version
## which returns a new string.
normalizePathEndImpl(path, trailingSep = trailingSep)

proc normalizePathEnd(path: string, trailingSep = false): string =
## A version of ``normalizePathEnd()``
## See also:
## * `normalizePathEnd proc <#normalizePathEnd,string,bool>`_ for the
## in-place version.
runnableExamples:
when defined(posix):
assert normalizePathEnd("/lib/", trailingSep = true) == "/lib/"
assert normalizePathEnd("", trailingSep = true) == "/"
assert normalizePathEnd("/lib", trailingSep = false) == "/lib"
assert normalizePathEnd("lib//", trailingSep = false) == "lib"
assert normalizePathEnd("lib", trailingSep = false) == "lib"
assert normalizePathEnd("/lib//", trailingSep = true) == "/lib/"
result = path
result.normalizePathEnd(trailingSep)

proc joinPath*(head, tail: string): string {.
noSideEffect, rtl, extern: "nos$1".} =
Expand Down

0 comments on commit 56e3780

Please sign in to comment.