summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDominik Picheta <dominikpicheta@googlemail.com>2018-08-05 16:30:17 +0100
committerGitHub <noreply@github.com>2018-08-05 16:30:17 +0100
commit6fffadb7fdd60174891fba6de302bb748cd7ca60 (patch)
treee8aaa2984dd7f6268f1f9b18c0c09c8bf3a50ce4
parentbaa77387d7b9ecda84a58af2fed515cb32cb2a5b (diff)
parent20591845df1e2b44ed365dd3aa62199371f0d273 (diff)
downloadNim-6fffadb7fdd60174891fba6de302bb748cd7ca60.tar.gz
Merge pull request #8364 from timotheecour/pr_expandTilde
ospaths.expandTilde: handle ~ correctly; refactor to use DirSep, AltSep
-rw-r--r--lib/pure/ospaths.nim26
1 files changed, 12 insertions, 14 deletions
diff --git a/lib/pure/ospaths.nim b/lib/pure/ospaths.nim
index a7ebd9d15..d0c5ef03f 100644
--- a/lib/pure/ospaths.nim
+++ b/lib/pure/ospaths.nim
@@ -553,23 +553,21 @@ proc getTempDir*(): string {.rtl, extern: "nos$1",
 
 proc expandTilde*(path: string): string {.
   tags: [ReadEnvEffect, ReadIOEffect].} =
-  ## Expands a path starting with ``~/`` to a full path.
+  ## Expands ``~`` or a path starting with ``~/`` to a full path, replacing
+  ## ``~`` with ``getHomeDir()`` (otherwise returns ``path`` unmodified).
   ##
-  ## If `path` starts with the tilde character and is followed by `/` or `\\`
-  ## this proc will return the reminder of the path appended to the result of
-  ## the getHomeDir() proc, otherwise the input path will be returned without
-  ## modification.
-  ##
-  ## The behaviour of this proc is the same on the Windows platform despite
-  ## not having this convention. Example:
-  ##
-  ## .. code-block:: nim
-  ##   let configFile = expandTilde("~" / "appname.cfg")
-  ##   echo configFile
-  ##   # --> C:\Users\amber\appname.cfg
-  if len(path) > 1 and path[0] == '~' and (path[1] == '/' or path[1] == '\\'):
+  ## Windows: this is still supported despite Windows platform not having this
+  ## convention; also, both ``~/`` and ``~\`` are handled.
+  runnableExamples:
+    doAssert expandTilde("~" / "appname.cfg") == getHomeDir() / "appname.cfg"
+  if len(path) == 0 or path[0] != '~':
+    result = path
+  elif len(path) == 1:
+    result = getHomeDir()
+  elif (path[1] in {DirSep, AltSep}):
     result = getHomeDir() / path.substr(2)
   else:
+    # TODO: handle `~bob` and `~bob/` which means home of bob
     result = path
 
 proc quoteShellWindows*(s: string): string {.noSideEffect, rtl, extern: "nosp$1".} =