summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorTimothee Cour <timothee.cour2@gmail.com>2020-06-15 01:57:34 -0700
committerGitHub <noreply@github.com>2020-06-15 10:57:34 +0200
commitbf604c6829f87a551dc3b5ad836679be4ab53789 (patch)
tree614f36dd394bc455557bd8fa2e294f435ba8b2e9 /lib
parent3cbf59336d6b9838dbf534f8e55fdfc8e7ab6954 (diff)
downloadNim-bf604c6829f87a551dc3b5ad836679be4ab53789.tar.gz
normalizeExe (#14668)
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/os.nim16
1 files changed, 14 insertions, 2 deletions
diff --git a/lib/pure/os.nim b/lib/pure/os.nim
index 97e414d44..79cd27e10 100644
--- a/lib/pure/os.nim
+++ b/lib/pure/os.nim
@@ -1407,6 +1407,18 @@ proc absolutePath*(path: string, root = getCurrentDir()): string =
 proc absolutePathInternal(path: string): string =
   absolutePath(path, getCurrentDir())
 
+proc normalizeExe*(file: var string) {.since: (1, 3, 5).} =
+  ## on posix, prepends `./` if `file` doesn't contain `/` and is not `"", ".", ".."`.
+  runnableExamples:
+    import sugar
+    when defined(posix):
+      doAssert "foo".dup(normalizeExe) == "./foo"
+      doAssert "foo/../bar".dup(normalizeExe) == "foo/../bar"
+    doAssert "".dup(normalizeExe) == ""
+  when defined(posix):
+    if file.len > 0 and DirSep notin file and file != "." and file != "..":
+      file = "./" & file
+
 proc normalizePath*(path: var string) {.rtl, extern: "nos$1", tags: [].} =
   ## Normalize a path.
   ##
@@ -1420,8 +1432,8 @@ proc normalizePath*(path: var string) {.rtl, extern: "nos$1", tags: [].} =
   ##
   ## See also:
   ## * `absolutePath proc <#absolutePath,string>`_
-  ## * `normalizedPath proc <#normalizedPath,string>`_ for a version which returns
-  ##   a new string
+  ## * `normalizedPath proc <#normalizedPath,string>`_ for outplace version
+  ## * `normalizeExe proc <#normalizeExe,string>`_
   runnableExamples:
     when defined(posix):
       var a = "a///b//..//c///d"