lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


* On 2014-11-04 18:21:41 +0100, Patrick Donnelly wrote:
 
> You can avoid allocating 2 copies of s doing:
> 
>  local function base64(s)
>     local byte = string.byte
>     return s:gsub("..?.?", function(cs)
>        local a, b, c = byte(cs, 1, 3)
>        if not b then
>          return bs[a>>2] .. bs[(a&3)<<4] .. "=="
>        elseif not c then
>          return bs[a>>2] .. bs[(a&3)<<4|b>>4] .. bs[(b&15)<<2] .. "="
>        else
>          return bs[a>>2] .. bs[(a&3)<<4|b>>4] .. bs[(b&15)<<2|c>>6] .. bs[c&63]
>        end
>     end)
>  end

I tried something like this as well, but for some reason the performance
is about 20% worse for my workload (+4MB size input).