diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2020-03-23 15:47:10 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-23 15:47:10 +0100 |
commit | fc5dd11b3da555a3617c1163dc3105973d101b97 (patch) | |
tree | e0322a3d5c5bcba7b58f2d314eabd99d4d637217 /lib | |
parent | e05fd1d00fb940aa89fdd7bd9f98e091abfd378b (diff) | |
download | Nim-fc5dd11b3da555a3617c1163dc3105973d101b97.tar.gz |
fixes #13722 (#13729)
* fixes #13722 * better fix
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pure/base64.nim | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/lib/pure/base64.nim b/lib/pure/base64.nim index aaad26f96..b715e749e 100644 --- a/lib/pure/base64.nim +++ b/lib/pure/base64.nim @@ -73,12 +73,16 @@ let cb64safe = cbBase('-', '_') const + cb64VM = cbBase('+', '/') + cb64safeVM = cbBase('-', '_') + +const invalidChar = 255 -template encodeInternal(s: typed, alphabet: ptr array[64, char]): untyped = +template encodeSize(size: int): int = (size * 4 div 3) + 6 + +template encodeInternal(s, alphabet: typed): untyped = ## encodes `s` into base64 representation. - proc encodeSize(size: int): int = - return (size * 4 div 3) + 6 result.setLen(encodeSize(s.len)) @@ -94,7 +98,7 @@ template encodeInternal(s: typed, alphabet: ptr array[64, char]): untyped = n = exp inc inputIndex - template outputChar(x: untyped) = + template outputChar(x: typed) = result[outputIndex] = alphabet[x and 63] inc outputIndex @@ -129,6 +133,16 @@ template encodeInternal(s: typed, alphabet: ptr array[64, char]): untyped = result.setLen(outputIndex) +template encodeImpl() {.dirty.} = + when nimVM: + block: + let lookupTableVM = if safe: cb64safeVM else: cb64VM + encodeInternal(s, lookupTableVM) + else: + block: + let lookupTable = if safe: unsafeAddr(cb64safe) else: unsafeAddr(cb64) + encodeInternal(s, lookupTable) + proc encode*[T: SomeInteger|char](s: openArray[T], safe = false): string = ## Encodes `s` into base64 representation. ## @@ -148,8 +162,7 @@ proc encode*[T: SomeInteger|char](s: openArray[T], safe = false): string = assert encode(['n', 'i', 'm']) == "bmlt" assert encode(@['n', 'i', 'm']) == "bmlt" assert encode([1, 2, 3, 4, 5]) == "AQIDBAU=" - let lookupTable = if safe: unsafeAddr(cb64safe) else: unsafeAddr(cb64) - encodeInternal(s, lookupTable) + encodeImpl() proc encode*(s: string, safe = false): string = ## Encodes ``s`` into base64 representation. @@ -167,8 +180,7 @@ proc encode*(s: string, safe = false): string = ## * `decode proc<#decode,string>`_ for decoding a string runnableExamples: assert encode("Hello World") == "SGVsbG8gV29ybGQ=" - let lookupTable = if safe: unsafeAddr(cb64safe) else: unsafeAddr(cb64) - encodeInternal(s, lookupTable) + encodeImpl() proc encodeMIME*(s: string, lineLen = 75, newLine = "\r\n"): string = ## Encodes ``s`` into base64 representation as lines. |