Verified Commit 28401140 authored by Camil Staps's avatar Camil Staps 🚀

base64(URL)EncodeLen: avoid string concatenation for performance

parent 5a4908b6
...@@ -59,15 +59,25 @@ encodeString s a ...@@ -59,15 +59,25 @@ encodeString s a
| otherwise = encodeLastOctet (oct >> 6) (off - 1) p {s & [off + dest_o] = a.[oct bitand 63]} | otherwise = encodeLastOctet (oct >> 6) (off - 1) p {s & [off + dest_o] = a.[oct bitand 63]}
srcSize = size s srcSize = size s
addLineBreaks :: !.String Length -> .String addLineBreaks :: !u:String !Length -> u:String
addLineBreaks s l addLineBreaks s l
| l > 0 = addLineBreaks` s "" l | l <= 0
| otherwise = abort "Length cannot be 0 or less." = abort "Length cannot be 0 or less."
# sz = size s
| sz <= l
= s
# required = case sz rem l of
0 -> (sz/l) * (l+1) - 1
r -> (sz/l) * (l+1) + r
= copy s 0 l (createArray required '\0') 0
where where
addLineBreaks` :: !.String !.String !Length -> .String copy :: !.String !Int !Int !*String !Int -> .String
addLineBreaks` src dest len copy src src_o remaining dest dest_o
| len >= (size src) = dest +++. src | src_o >= size src
| otherwise = addLineBreaks` (src % (len,(size src))) (dest+++(src % (0,len-1))+++"\n") len = dest
| remaining == 0
= copy src src_o l {dest & [dest_o]='\n'} (dest_o+1)
= copy src (src_o+1) (remaining-1) {dest & [dest_o]=src.[src_o]} (dest_o+1)
base64Decode :: !.String -> .String base64Decode :: !.String -> .String
base64Decode s = decodeString s decodeWithStdAlphabet base64Decode s = decodeString s decodeWithStdAlphabet
...@@ -141,13 +151,14 @@ where ...@@ -141,13 +151,14 @@ where
decodeString` :: !.String !Int !Alphabet !*{#Char} !Int !Int -> *{#Char} decodeString` :: !.String !Int !Alphabet !*{#Char} !Int !Int -> *{#Char}
decodeString` s sz a dest src_o dest_o decodeString` s sz a dest src_o dest_o
| src_o >= sz = dest
#! (c1,src_o,s) = nextChar s src_o sz #! (c1,src_o,s) = nextChar s src_o sz
#! (c2,src_o,s) = nextChar s src_o sz #! (c2,src_o,s) = nextChar s src_o sz
#! (c3,src_o,s) = nextChar s src_o sz #! (c3,src_o,s) = nextChar s src_o sz
#! (c4,src_o,s) = nextChar s src_o sz #! (c4,src_o,s) = nextChar s src_o sz
| c4 == '\0' | c4 == '\0'
= abort "invalid base64 input: not a multiple of 4\n" | c1 == '\0'
= dest
= abort "invalid base64 input: not a multiple of 4\n"
| c3 == '=' // lose the last four padding bits | c3 == '=' // lose the last four padding bits
# oct = # oct =
(toInt a.[toInt c1] << 2) + (toInt a.[toInt c1] << 2) +
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment