diff options
author | bptato <nincsnevem662@gmail.com> | 2023-11-29 22:24:12 +0100 |
---|---|---|
committer | bptato <nincsnevem662@gmail.com> | 2023-11-29 22:25:12 +0100 |
commit | a969b10663e923ff903ea7c77c479ef0ec71f66a (patch) | |
tree | f8fc9c28ff4eb51075a9db077fadc3e3e91f60ed /src/utils/twtstr.nim | |
parent | 91b5ed05c485e689eeff3de404fd93c3b792839b (diff) | |
download | chawan-a969b10663e923ff903ea7c77c479ef0ec71f66a.tar.gz |
twtstr: simplify expandPath
Diffstat (limited to 'src/utils/twtstr.nim')
-rw-r--r-- | src/utils/twtstr.nim | 31 |
1 files changed, 12 insertions, 19 deletions
diff --git a/src/utils/twtstr.nim b/src/utils/twtstr.nim index 78c92883..2756f2de 100644 --- a/src/utils/twtstr.nim +++ b/src/utils/twtstr.nim @@ -779,26 +779,19 @@ func utf16Len*(s: string): int = result += 2 proc expandPath*(path: string): string = - if path.len == 0: + if path.len == 0 or path[0] != '~': + return path + if path.len == 1: + return getHomeDir() + elif path[1] == '/': + return getHomeDir() / path.substr(2) + else: + when defined(posix): + let usr = path.until({'/'}, 1) + let p = getpwnam(cstring(usr)) + if p != nil: + return $p.pw_dir / path.substr(usr.len) return path - result = path - var i = 0 - if path[0] == '~': - if path.len == 1: - result = getHomeDir() - elif path[1] == '/': - result = getHomeDir() / path.substr(2) - inc i - else: - when defined(posix): - i = 1 - var usr = "" - while path[i] != '/': - usr &= path[i] - inc i - let p = getpwnam(cstring(usr)) - if p != nil: - result = $p.pw_dir / path.substr(i) # Combining chars from https://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c # |