remove unnecessary side-effects from base64.encode(mime) (#22986)

Fixes https://github.com/nim-lang/Nim/issues/22985
This commit is contained in:
tersec
2023-11-25 19:52:42 +00:00
committed by GitHub
parent 379299a5ac
commit 26f2ea149c
2 changed files with 8 additions and 12 deletions

View File

@@ -66,14 +66,10 @@ template cbBase(a, b): untyped = [
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', a, b]
let
const
cb64 = cbBase('+', '/')
cb64safe = cbBase('-', '_')
const
cb64VM = cbBase('+', '/')
cb64safeVM = cbBase('-', '_')
const
invalidChar = 255
@@ -134,14 +130,10 @@ template encodeInternal(s, alphabet: typed): untyped =
result.setLen(outputIndex)
template encodeImpl() {.dirty.} =
when nimvm:
block:
let lookupTableVM = if safe: cb64safeVM else: cb64VM
encodeInternal(s, lookupTableVM)
if safe:
encodeInternal(s, cb64safe)
else:
block:
let lookupTable = if safe: unsafeAddr(cb64safe) else: unsafeAddr(cb64)
encodeInternal(s, lookupTable)
encodeInternal(s, cb64)
proc encode*[T: byte|char](s: openArray[T], safe = false): string =
## Encodes `s` into base64 representation.

View File

@@ -53,5 +53,9 @@ template main() =
doAssert encode("", safe = true) == ""
doAssert encode("the quick brown dog jumps over the lazy fox", safe = true) == "dGhlIHF1aWNrIGJyb3duIGRvZyBqdW1wcyBvdmVyIHRoZSBsYXp5IGZveA=="
func mainNoSideEffects() = main()
static: main()
main()
static: mainNoSideEffects()
mainNoSideEffects()