Skip to content

Commit

Permalink
encode to nonthrowing API
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe-Abraham committed Aug 16, 2024
1 parent 69ff5a1 commit 798aa1f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
19 changes: 11 additions & 8 deletions velox/common/encode/Base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ static_assert(
// "kBase64UrlReverseIndexTable has incorrect entries.");

// Implementation of Base64 encoding and decoding functions.
// static
template <class T>
/* static */ std::string Base64::encodeImpl(
std::string Base64::encodeImpl(
const T& data,
const Base64::Charset& charset,
bool include_pad) {
Expand All @@ -184,24 +185,25 @@ size_t Base64::calculateEncodedSize(size_t size, bool withPadding) {
}

// static
void Base64::encode(std::string_view data, char* output) {
encodeImpl(data, kBase64Charset, true, output);
Status Base64::encode(std::string_view data, char* output) {
return encodeImpl(data, kBase64Charset, true, output);
}

// static
void Base64::encodeUrl(std::string_view data, char* output) {
encodeImpl(data, kBase64UrlCharset, true, output);
Status Base64::encodeUrl(std::string_view data, char* output) {
return encodeImpl(data, kBase64UrlCharset, true, output);
}

// static
template <class T>
/* static */ void Base64::encodeImpl(
Status Base64::encodeImpl(
const T& data,
const Base64::Charset& charset,
bool include_pad,
char* out) {
auto len = data.size();
if (len == 0) {
return;
return Status::OK();
}

auto wp = out;
Expand Down Expand Up @@ -241,6 +243,7 @@ template <class T>
}
}
}
return Status::OK();
}

// static
Expand All @@ -249,7 +252,7 @@ std::string Base64::encode(std::string_view text) {
}

// static
std::string Base64::encode(std::string_view data, size_t len) {
std::string Base64::encode(std::string_view data, size_t /*len*/) {
return encodeImpl(data, kBase64Charset, true);
}

Expand Down
7 changes: 4 additions & 3 deletions velox/common/encode/Base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <folly/io/IOBuf.h>

#include "velox/common/base/GTestMacros.h"
#include "velox/common/base/Status.h"

namespace facebook::velox::encoding {

Expand Down Expand Up @@ -56,7 +57,7 @@ class Base64 {
/// Encodes the specified number of characters from the 'data' and writes the
/// result to the 'output'. The output must have enough space, e.g. as
/// returned by the calculateEncodedSize().
static void encode(std::string_view data, char* output);
static Status encode(std::string_view data, char* output);

/// Decodes the specified encoded text.
static std::string decode(std::string_view encoded);
Expand All @@ -75,7 +76,7 @@ class Base64 {
/// Encodes the specified number of characters from the 'data' and writes the
/// result to the 'output' using URL encoding. The output must have enough
/// space as returned by the calculateEncodedSize().
static void encodeUrl(std::string_view data, char* output);
static Status encodeUrl(std::string_view data, char* output);

/// Encodes the specified IOBuf data using URL encoding.
static std::string encodeUrl(const folly::IOBuf* data);
Expand Down Expand Up @@ -130,7 +131,7 @@ class Base64 {

// Encodes the specified data using the provided charset.
template <class T>
static void encodeImpl(
static Status encodeImpl(
const T& data,
const Charset& charset,
bool include_pad,
Expand Down
14 changes: 6 additions & 8 deletions velox/functions/prestosql/BinaryFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,13 +274,12 @@ template <typename T>
struct ToBase64Function {
VELOX_DEFINE_FUNCTION_TYPES(T);

FOLLY_ALWAYS_INLINE void call(
out_type<Varchar>& result,
const arg_type<Varbinary>& input) {
FOLLY_ALWAYS_INLINE Status
call(out_type<Varchar>& result, const arg_type<Varbinary>& input) {
std::string_view inputString(
reinterpret_cast<const char*>(input.data()), input.size());
result.resize(encoding::Base64::calculateEncodedSize(input.size()));
encoding::Base64::encode(inputString, result.data());
return encoding::Base64::encode(inputString, result.data());
}
};

Expand Down Expand Up @@ -318,13 +317,12 @@ template <typename T>
struct ToBase64UrlFunction {
VELOX_DEFINE_FUNCTION_TYPES(T);

FOLLY_ALWAYS_INLINE void call(
out_type<Varchar>& result,
const arg_type<Varbinary>& input) {
FOLLY_ALWAYS_INLINE Status
call(out_type<Varchar>& result, const arg_type<Varbinary>& input) {
std::string_view inputString(
reinterpret_cast<const char*>(input.data()), input.size());
result.resize(encoding::Base64::calculateEncodedSize(input.size()));
encoding::Base64::encodeUrl(inputString, result.data());
return encoding::Base64::encodeUrl(inputString, result.data());
}
};

Expand Down

0 comments on commit 798aa1f

Please sign in to comment.