fight the code bloat in base64.nim
This commit is contained in:
parent
12ab5059a0
commit
dae9865b14
1 changed files with 16 additions and 7 deletions
|
|
@ -61,12 +61,21 @@
|
||||||
## * `md5 module<md5.html>`_ implements the MD5 checksum algorithm
|
## * `md5 module<md5.html>`_ implements the MD5 checksum algorithm
|
||||||
## * `sha1 module<sha1.html>`_ implements a sha1 encoder and decoder
|
## * `sha1 module<sha1.html>`_ implements a sha1 encoder and decoder
|
||||||
|
|
||||||
|
template cbBase(a, b): untyped = [
|
||||||
|
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
|
||||||
|
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
|
||||||
|
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
|
||||||
|
'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
|
||||||
|
cb64 = cbBase('+', '/')
|
||||||
|
cb64safe = cbBase('-', '_')
|
||||||
|
|
||||||
const
|
const
|
||||||
cb64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
|
||||||
cb64safe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
|
|
||||||
invalidChar = 255
|
invalidChar = 255
|
||||||
|
|
||||||
template encodeInternal(s: typed, alphabet: string): untyped =
|
template encodeInternal(s: typed, alphabet: ptr array[64, char]): untyped =
|
||||||
## encodes `s` into base64 representation.
|
## encodes `s` into base64 representation.
|
||||||
proc encodeSize(size: int): int =
|
proc encodeSize(size: int): int =
|
||||||
return (size * 4 div 3) + 6
|
return (size * 4 div 3) + 6
|
||||||
|
|
@ -139,8 +148,8 @@ 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(@['n', 'i', 'm']) == "bmlt"
|
assert encode(@['n', 'i', 'm']) == "bmlt"
|
||||||
assert encode([1, 2, 3, 4, 5]) == "AQIDBAU="
|
assert encode([1, 2, 3, 4, 5]) == "AQIDBAU="
|
||||||
if safe: encodeInternal(s, cb64safe)
|
let lookupTable = if safe: unsafeAddr(cb64safe) else: unsafeAddr(cb64)
|
||||||
else: encodeInternal(s, cb64)
|
encodeInternal(s, lookupTable)
|
||||||
|
|
||||||
proc encode*(s: string, safe = false): string =
|
proc encode*(s: string, safe = false): string =
|
||||||
## Encodes ``s`` into base64 representation.
|
## Encodes ``s`` into base64 representation.
|
||||||
|
|
@ -158,8 +167,8 @@ proc encode*(s: string, safe = false): string =
|
||||||
## * `decode proc<#decode,string>`_ for decoding a string
|
## * `decode proc<#decode,string>`_ for decoding a string
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
assert encode("Hello World") == "SGVsbG8gV29ybGQ="
|
assert encode("Hello World") == "SGVsbG8gV29ybGQ="
|
||||||
if safe: encodeInternal(s, cb64safe)
|
let lookupTable = if safe: unsafeAddr(cb64safe) else: unsafeAddr(cb64)
|
||||||
else: encodeInternal(s, cb64)
|
encodeInternal(s, lookupTable)
|
||||||
|
|
||||||
proc encodeMIME*(s: string, lineLen = 75, newLine = "\r\n"): string =
|
proc encodeMIME*(s: string, lineLen = 75, newLine = "\r\n"): string =
|
||||||
## Encodes ``s`` into base64 representation as lines.
|
## Encodes ``s`` into base64 representation as lines.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue