summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2020-03-20 10:46:58 +0100
committerAraq <rumpf_a@web.de>2020-03-20 10:46:58 +0100
commitdae9865b14fffd98ca0ef20517500831169861e7 (patch)
tree1432bf4bcbceb142e67e1fa19549026baf539e0f
parent12ab5059a0930bd642db43a96bcaa64f2a3c5327 (diff)
downloadNim-dae9865b14fffd98ca0ef20517500831169861e7.tar.gz
fight the code bloat in base64.nim
-rw-r--r--lib/pure/base64.nim23
1 files changed, 16 insertions, 7 deletions
diff --git a/lib/pure/base64.nim b/lib/pure/base64.nim
index f418a9d44..aaad26f96 100644
--- a/lib/pure/base64.nim
+++ b/lib/pure/base64.nim
@@ -61,12 +61,21 @@
 ## * `md5 module<md5.html>`_ implements the MD5 checksum algorithm
 ## * `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
-  cb64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
-  cb64safe = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
   invalidChar = 255
 
-template encodeInternal(s: typed, alphabet: string): untyped =
+template encodeInternal(s: typed, alphabet: ptr array[64, char]): untyped =
   ## encodes `s` into base64 representation.
   proc encodeSize(size: int): int =
     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([1, 2, 3, 4, 5]) == "AQIDBAU="
-  if safe: encodeInternal(s, cb64safe)
-  else: encodeInternal(s, cb64)
+  let lookupTable = if safe: unsafeAddr(cb64safe) else: unsafeAddr(cb64)
+  encodeInternal(s, lookupTable)
 
 proc encode*(s: string, safe = false): string =
   ## Encodes ``s`` into base64 representation.
@@ -158,8 +167,8 @@ proc encode*(s: string, safe = false): string =
   ## * `decode proc<#decode,string>`_ for decoding a string
   runnableExamples:
     assert encode("Hello World") == "SGVsbG8gV29ybGQ="
-  if safe: encodeInternal(s, cb64safe)
-  else: encodeInternal(s, cb64)
+  let lookupTable = if safe: unsafeAddr(cb64safe) else: unsafeAddr(cb64)
+  encodeInternal(s, lookupTable)
 
 proc encodeMIME*(s: string, lineLen = 75, newLine = "\r\n"): string =
   ## Encodes ``s`` into base64 representation as lines.