minor code cleanups (#21215)

This commit is contained in:
Andreas Rumpf 2023-01-02 10:39:17 +01:00 • committed by GitHub
commit cf1b16ef8b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 32 additions and 36 deletions

View file

@ -94,8 +94,10 @@ when not defined(nimscript):
assert getEnv("unknownEnv", "doesn't exist") == "doesn't exist"
let env = getEnvImpl(key)
if env == nil: return default
result = $env
if env == nil:
result = default
else:
result = $env
proc existsEnv*(key: string): bool {.tags: [ReadEnvEffect].} =
## Checks whether the environment variable named `key` exists.
@ -109,7 +111,7 @@ when not defined(nimscript):
runnableExamples:
assert not existsEnv("unknownEnv")
return getEnvImpl(key) != nil
result = getEnvImpl(key) != nil
proc putEnv*(key, val: string) {.tags: [WriteEnvEffect].} =
## Sets the value of the `environment variable`:idx: named `key` to `val`.
@ -140,7 +142,7 @@ when not defined(nimscript):
## * `envPairs iterator`_
template bail = raiseOSError(osLastError(), key)
when defined(windows):
#[
#[
# https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/putenv-s-wputenv-s?view=msvc-160
> You can remove a variable from the environment by specifying an empty string (that is, "") for value_string
note that nil is not legal
@ -177,20 +179,17 @@ when not defined(nimscript):
iterator envPairsImpl(): tuple[key, value: string] {.tags: [ReadEnvEffect].} =
when defined(windows):
block implBlock:
template impl(get_fun, typ, size, zero, free_fun) =
let env = get_fun()
var e = env
if e == nil: break implBlock
while true:
let eend = strEnd(e)
let kv = $e
let p = find(kv, '=')
yield (substr(kv, 0, p-1), substr(kv, p+1))
e = cast[typ](cast[int](eend)+size)
if typeof(zero)(eend[1]) == zero: break
discard free_fun(env)
impl(getEnvironmentStringsW, WideCString, 2, 0, freeEnvironmentStringsW)
let env = getEnvironmentStringsW()
var e = env
if e != nil:
while true:
let eend = strEnd(e)
let kv = $e
let p = find(kv, '=')
yield (substr(kv, 0, p-1), substr(kv, p+1))
e = cast[WideCString](cast[ByteAddress](eend)+2)
if int(eend[1]) == 0: break
discard freeEnvironmentStringsW(env)
else:
var i = 0
when defined(macosx) and not defined(ios) and not defined(emscripten):