minor code cleanups (#21215)
This commit is contained in:
parent
e9ed090c33
commit
cf1b16ef8b
6 changed files with 32 additions and 36 deletions
|
|
@ -21,7 +21,7 @@ macro enumerate*(x: ForLoopStmt): untyped {.since: (1, 3).} =
|
|||
## The default starting count `0` can be manually overridden if needed.
|
||||
runnableExamples:
|
||||
let a = [10, 20, 30]
|
||||
var b: seq[(int, int)]
|
||||
var b: seq[(int, int)] = @[]
|
||||
for i, x in enumerate(a):
|
||||
b.add((i, x))
|
||||
assert b == @[(0, 10), (1, 20), (2, 30)]
|
||||
|
|
|
|||
|
|
@ -112,9 +112,9 @@ const invalidSlot = uint8.high
|
|||
|
||||
proc genLookup[T: typedesc[HoleyEnum]](_: T): auto =
|
||||
const n = span(T)
|
||||
var ret: array[n, uint8]
|
||||
var i = 0
|
||||
assert n <= invalidSlot.int
|
||||
var ret {.noinit.}: array[n, uint8]
|
||||
for ai in mitems(ret): ai = invalidSlot
|
||||
for ai in items(T):
|
||||
ret[ai.ord - T.low.ord] = uint8(i)
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -75,17 +75,14 @@ proc newOSError*(
|
|||
## See also:
|
||||
## * `osErrorMsg proc`_
|
||||
## * `osLastError proc`_
|
||||
var e: owned(ref OSError); new(e)
|
||||
e.errorCode = errorCode.int32
|
||||
e.msg = osErrorMsg(errorCode)
|
||||
result = (ref OSError)(errorCode: errorCode.int32, msg: osErrorMsg(errorCode))
|
||||
if additionalInfo.len > 0:
|
||||
if e.msg.len > 0 and e.msg[^1] != '\n': e.msg.add '\n'
|
||||
e.msg.add "Additional info: "
|
||||
e.msg.add additionalInfo
|
||||
if result.msg.len > 0 and result.msg[^1] != '\n': result.msg.add '\n'
|
||||
result.msg.add "Additional info: "
|
||||
result.msg.add additionalInfo
|
||||
# don't add trailing `.` etc, which negatively impacts "jump to file" in IDEs.
|
||||
if e.msg == "":
|
||||
e.msg = "unknown OS error"
|
||||
return e
|
||||
if result.msg == "":
|
||||
result.msg = "unknown OS error"
|
||||
|
||||
proc raiseOSError*(errorCode: OSErrorCode, additionalInfo = "") {.noinline.} =
|
||||
## Raises an `OSError exception <system.html#OSError>`_.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue