-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add Base64 safe #13672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Base64 safe #13672
Conversation
No advantage over just replacing those chars yourself IMO. |
Can we get some benchmark numbers? It's some overhead. |
@Clyybber |
lib/pure/base64.nim
Outdated
## **See also:** | ||
## * `encode proc<#encode,string>`_ for encoding a string | ||
## * `decode proc<#decode,string>`_ for decoding a string | ||
runnableExamples: | ||
assert encode(['n', 'i', 'm']) == "bmlt" | ||
assert encode(@['n', 'i', 'm']) == "bmlt" | ||
assert encode([1, 2, 3, 4, 5]) == "AQIDBAU=" | ||
encodeInternal(s) | ||
encodeInternal(s, if safe: cb64safe else: cb64) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is if safe: cb64safe else: cb64
guaranteed to evaluate only once (here + in other overload) ?
the template will expand as:
while ...:
result[outputIndex] = (if safe: cb64safe else: cb64)[x and 63]
instead, I'd simply not make that assumption and pass a constant to it
EDIT: my suspicion was correct, see comment below
nim c -r -d:case2 -d:case_workaround -d:danger $timn_D/tests/nim/all/t10369.nim when defined case2:
import base64
import random
import times
import strutils
proc main()=
let n = 10000000
let m = 100
var s = newString(n)
for i in 0..<n:
s[i]=cast[char](rand(uint8.high.int))
let t = cpuTime()
for j in 0..<m:
when defined case_safe:
let s2 = encode(s, safe = true)
elif defined case_workaround:
let s2 = encode(s, safe = false).replace('+', '-').replace('/', '_')
doAssert s2[^1] != '\0' # prevent optimize out
else:
static: doAssert false
echo cpuTime()-t
# echo s2
main() [1]
but I'd argue that, even with that 4X speedup, this PR would still be worth it because it implements a standard and we shouldn't let users re-invent the wheel note:the patch needs to be applied to the other |
## Encodes `s` into base64 representation. | ||
## | ||
## This procedure encodes an openarray (array or sequence) of either integers | ||
## or characters. | ||
## | ||
## If ``safe`` is ``true`` then it will encode using the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit (doesn't have to be done in this PR): single ticks now works (simpler == better)
base64
adds URL-Safe Base64, implements RFC-4648 Section-7.