improve tbase64.nim (#17964)

This commit is contained in:
Timothee Cour 2021-05-07 17:40:16 -07:00 • committed by GitHub
commit 9ca3218a6d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,14 +1,11 @@
discard """ discard """
output: '''YQ==''' targets: "c js"
nimout: '''YQ=='''
""" """
import base64
import base64 import std/base64
static: echo encode("a")
echo encode("a")
proc main() = template main() =
doAssert encode("a") == "YQ=="
doAssert encode("Hello World") == "SGVsbG8gV29ybGQ=" doAssert encode("Hello World") == "SGVsbG8gV29ybGQ="
doAssert encode("leasure.") == "bGVhc3VyZS4=" doAssert encode("leasure.") == "bGVhc3VyZS4="
doAssert encode("easure.") == "ZWFzdXJlLg==" doAssert encode("easure.") == "ZWFzdXJlLg=="
@ -38,11 +35,7 @@ proc main() =
doAssert decode(encodeMIME(t, lineLen=40)) == t doAssert decode(encodeMIME(t, lineLen=40)) == t
doAssert decode(encodeMIME(t, lineLen=76)) == t doAssert decode(encodeMIME(t, lineLen=76)) == t
const invalid = "SGVsbG\x008gV29ybGQ=" doAssertRaises(ValueError): discard decode("SGVsbG\x008gV29ybGQ=")
try:
doAssert decode(invalid) == "will throw error"
except ValueError:
discard
block base64urlSafe: block base64urlSafe:
doAssert encode("c\xf7>", safe = true) == "Y_c-" doAssert encode("c\xf7>", safe = true) == "Y_c-"
@ -59,4 +52,5 @@ proc main() =
doAssert encode("", safe = true) == "" doAssert encode("", safe = true) == ""
doAssert encode("the quick brown dog jumps over the lazy fox", safe = true) == "dGhlIHF1aWNrIGJyb3duIGRvZyBqdW1wcyBvdmVyIHRoZSBsYXp5IGZveA==" doAssert encode("the quick brown dog jumps over the lazy fox", safe = true) == "dGhlIHF1aWNrIGJyb3duIGRvZyBqdW1wcyBvdmVyIHRoZSBsYXp5IGZveA=="
static: main()
main() main()