Skip to content

Commit

Permalink
Merge pull request #2542 from cloudflare/yagiz/improve-buffer-tostring
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell authored Aug 16, 2024
2 parents a61bb30 + bfdf13a commit 299094f
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/workerd/api/node/buffer.c++
Original file line number Diff line number Diff line change
Expand Up @@ -459,10 +459,17 @@ jsg::JsString toStringImpl(
return js.str(data);
}
case Encoding::BASE64: {
return js.str(kj::encodeBase64(slice));
size_t length = simdutf::base64_length_from_binary(slice.size());
auto out = kj::heapArray<kj::byte>(length);
simdutf::binary_to_base64(reinterpret_cast<const char*>(slice.begin()), slice.size(), reinterpret_cast<char*>(out.begin()));
return js.str(out);
}
case Encoding::BASE64URL: {
return js.str(kj::encodeBase64Url(slice));
auto options = simdutf::base64_url;
size_t length = simdutf::base64_length_from_binary(slice.size(), options);
auto out = kj::heapArray<kj::byte>(length);
simdutf::binary_to_base64(reinterpret_cast<const char*>(slice.begin()), slice.size(), reinterpret_cast<char*>(out.begin()), options);
return js.str(out);
}
case Encoding::HEX: {
return js.str(kj::encodeHex(slice));
Expand Down

0 comments on commit 299094f

Please sign in to comment.