Add Base64 safe (#13672)

* Implement RFC-4648 Section-7
* https://github.com/nim-lang/Nim/pull/13672#issuecomment-600993466
This commit is contained in:
Juan Carlos
2020-03-20 06:21:42 -03:00
committed by GitHub
parent dd362ab4c0
commit 70d93636cb
3 changed files with 45 additions and 9 deletions

View File

@@ -39,6 +39,21 @@ proc main() =
except ValueError:
discard
block base64urlSafe:
doAssert encode("c\xf7>", safe = true) == "Y_c-"
doAssert encode("c\xf7>", safe = false) == "Y/c+" # Not a nice URL :(
doAssert decode("Y/c+") == decode("Y_c-")
# Output must not change with safe=true
doAssert encode("Hello World", safe = true) == "SGVsbG8gV29ybGQ="
doAssert encode("leasure.", safe = true) == "bGVhc3VyZS4="
doAssert encode("easure.", safe = true) == "ZWFzdXJlLg=="
doAssert encode("asure.", safe = true) == "YXN1cmUu"
doAssert encode("sure.", safe = true) == "c3VyZS4="
doAssert encode([1,2,3], safe = true) == "AQID"
doAssert encode(['h','e','y'], safe = true) == "aGV5"
doAssert encode("", safe = true) == ""
doAssert encode("the quick brown dog jumps over the lazy fox", safe = true) == "dGhlIHF1aWNrIGJyb3duIGRvZyBqdW1wcyBvdmVyIHRoZSBsYXp5IGZveA=="
echo "OK"
main()