Skip to content

Commit

Permalink
Optimize base64.urlencode
Browse files Browse the repository at this point in the history
  • Loading branch information
Sainan committed Oct 27, 2024
1 parent 879e42b commit 756de3c
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
9 changes: 7 additions & 2 deletions src/lbase64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ static int decode(lua_State* L) {

static int urlEncode(lua_State* L) {
size_t len;
auto str = luaL_checklstring(L, 1, &len);
pluto_pushstring(L, soup::base64::urlEncode(str, len, (bool)lua_toboolean(L, 2)));
const char *str = luaL_checklstring(L, 1, &len);
const bool pad = lua_toboolean(L, 2);
size_t out_len = soup::base64::getEncodedSize(len, pad);
char shrtbuf[LUAI_MAXSHORTLEN];
char *enc = plutoS_prealloc(L, shrtbuf, out_len);
soup::base64::urlEncode(enc, str, len, pad);
plutoS_commit(L, enc, out_len);
return 1;
}

Expand Down
5 changes: 5 additions & 0 deletions src/vendor/Soup/soup/base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ NAMESPACE_SOUP
return encode(data, size, pad, table_encode_base64url);
}

void base64::urlEncode(char* out, const char* const data, const size_t size, const bool pad) noexcept
{
return encode(out, data, size, pad, table_encode_base64url);
}

std::string base64::encode(const char* const data, const size_t size, const bool pad, const char* table) SOUP_EXCAL
{
std::string enc(getEncodedSize(size, pad), '\0');
Expand Down
1 change: 1 addition & 0 deletions src/vendor/Soup/soup/base64.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ NAMESPACE_SOUP
[[nodiscard]] static std::string urlEncode(const char* data, const bool pad = false) SOUP_EXCAL;
[[nodiscard]] static std::string urlEncode(const std::string& data, const bool pad = false) SOUP_EXCAL;
[[nodiscard]] static std::string urlEncode(const char* const data, const size_t size, const bool pad = false) SOUP_EXCAL;
static void urlEncode(char* out, const char* const data, const size_t size, const bool pad) noexcept;

[[nodiscard]] static std::string encode(const char* const data, const size_t size, const bool pad, const char* table) SOUP_EXCAL;
static void encode(char* out, const char* data, const size_t size, const bool pad, const char* table) noexcept;
Expand Down
21 changes: 17 additions & 4 deletions testes/bench/_stdlib.pluto
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,32 @@ end

local { base64, crypto } = require "*"

bench("base64 encode, with padding, short string", function()
bench("base64.encode, with padding, short string", function()
base64.encode("abc", true)
end)
bench("base64 encode, without padding, short string", function()
bench("base64.encode, without padding, short string", function()
base64.encode("abc", false)
end)
bench("base64 encode, with padding, long string", function()
bench("base64.encode, with padding, long string", function()
base64.encode("The quick brown fox jumps over the lazy dog.", true)
end)
bench("base64 encode, without padding, long string", function()
bench("base64.encode, without padding, long string", function()
base64.encode("The quick brown fox jumps over the lazy dog.", false)
end)

bench("base64.urlencode, with padding, short string", function()
base64.urlencode("abc", true)
end)
bench("base64.urlencode, without padding, short string", function()
base64.urlencode("abc", false)
end)
bench("base64.urlencode, with padding, long string", function()
base64.urlencode("The quick brown fox jumps over the lazy dog.", true)
end)
bench("base64.urlencode, without padding, long string", function()
base64.urlencode("The quick brown fox jumps over the lazy dog.", false)
end)

bench("sha1, hex digest", function()
crypto.sha1("The quick brown fox jumps over the lazy dog.")
end)
Expand Down
2 changes: 2 additions & 0 deletions testes/pluto/basic.pluto
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,8 @@ do

assert(base64.urlencode("Hello") == "SGVsbG8")
assert(base64.urldecode("SGVsbG8") == "Hello")
assert(base64.urlencode("The quick brown fox jumps over the lazy dog.", true) == "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4=")
assert(base64.urlencode("The quick brown fox jumps over the lazy dog.", false) == "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4")

-- Binary safe
assert(base64.encode("") == "")
Expand Down

0 comments on commit 756de3c

Please sign in to comment.