Skip to content

Commit

Permalink
update to string_view
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe-Abraham committed Aug 16, 2024
1 parent 37aac8a commit 69ff5a1
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 74 deletions.
68 changes: 31 additions & 37 deletions velox/common/encode/Base64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,13 @@ size_t Base64::calculateEncodedSize(size_t size, bool withPadding) {
}

// static
void Base64::encode(const char* data, size_t len, char* output) {
encodeImpl(folly::StringPiece(data, len), kBase64Charset, true, output);
void Base64::encode(std::string_view data, char* output) {
encodeImpl(data, kBase64Charset, true, output);
}

// static
void Base64::encodeUrl(const char* data, size_t len, char* output) {
encodeImpl(folly::StringPiece(data, len), kBase64UrlCharset, true, output);
void Base64::encodeUrl(std::string_view data, char* output) {
encodeImpl(data, kBase64UrlCharset, true, output);
}

template <class T>
Expand Down Expand Up @@ -244,13 +244,13 @@ template <class T>
}

// static
std::string Base64::encode(folly::StringPiece text) {
std::string Base64::encode(std::string_view text) {
return encodeImpl(text, kBase64Charset, true);
}

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

namespace {
Expand Down Expand Up @@ -305,23 +305,21 @@ std::string Base64::encode(const folly::IOBuf* data) {
}

// static
std::string Base64::decode(folly::StringPiece encoded) {
std::string Base64::decode(std::string_view encoded) {
std::string output;
Base64::decode(std::make_pair(encoded.data(), encoded.size()), output);
Base64::decode(encoded, output);
return output;
}

// static
void Base64::decode(
const std::pair<const char*, int32_t>& payload,
std::string& output) {
size_t inputSize = payload.second;
output.resize(calculateDecodedSize(payload.first, inputSize));
decode(payload.first, inputSize, output.data(), output.size());
void Base64::decode(std::string_view payload, std::string& output) {
size_t inputSize = payload.size();
output.resize(calculateDecodedSize(payload, inputSize));
decode(payload.data(), inputSize, output.data(), output.size());
}

// static
void Base64::decode(const char* data, size_t size, char* output) {
void Base64::decode(std::string_view data, size_t size, char* output) {
size_t out_len = size / 4 * 3;
Base64::decode(data, size, output, out_len);
}
Expand All @@ -338,13 +336,16 @@ uint8_t Base64::base64ReverseLookup(
}

// static
size_t
Base64::decode(const char* src, size_t src_len, char* dst, size_t dst_len) {
size_t Base64::decode(
std::string_view src,
size_t src_len,
char* dst,
size_t dst_len) {
return decodeImpl(src, src_len, dst, dst_len, kBase64ReverseIndexTable);
}

// static
size_t Base64::calculateDecodedSize(const char* data, size_t& size) {
size_t Base64::calculateDecodedSize(std::string_view data, size_t& size) {
if (size == 0) {
return 0;
}
Expand Down Expand Up @@ -388,7 +389,7 @@ size_t Base64::calculateDecodedSize(const char* data, size_t& size) {

// static
size_t Base64::decodeImpl(
const char* src,
std::string_view src,
size_t src_len,
char* dst,
size_t dst_len,
Expand All @@ -405,7 +406,7 @@ size_t Base64::decodeImpl(
}

// Handle full groups of 4 characters
for (; src_len > 4; src_len -= 4, src += 4, dst += 3) {
for (; src_len > 4; src_len -= 4, src.remove_prefix(4), dst += 3) {
// Each character of the 4 encode 6 bits of the original, grab each with
// the appropriate shifts to rebuild the original and then split that back
// into the original 8 bit bytes.
Expand Down Expand Up @@ -437,13 +438,8 @@ size_t Base64::decodeImpl(
}

// static
std::string Base64::encodeUrl(folly::StringPiece text) {
return encodeImpl(text, kBase64UrlCharset, false);
}

// static
std::string Base64::encodeUrl(const char* data, size_t len) {
return encodeUrl(folly::StringPiece(data, len));
std::string Base64::encodeUrl(std::string_view data) {
return encodeImpl(data, kBase64UrlCharset, false);
}

// static
Expand All @@ -453,29 +449,27 @@ std::string Base64::encodeUrl(const folly::IOBuf* data) {

// static
void Base64::decodeUrl(
const char* src,
std::string_view src,
size_t src_len,
char* dst,
size_t dst_len) {
decodeImpl(src, src_len, dst, dst_len, kBase64UrlReverseIndexTable);
}

// static
std::string Base64::decodeUrl(folly::StringPiece encoded) {
std::string Base64::decodeUrl(std::string_view encoded) {
std::string output;
Base64::decodeUrl(std::make_pair(encoded.data(), encoded.size()), output);
Base64::decodeUrl(encoded, output);
return output;
}

// static
void Base64::decodeUrl(
const std::pair<const char*, int32_t>& payload,
std::string& output) {
size_t out_len = (payload.second + 3) / 4 * 3;
void Base64::decodeUrl(std::string_view payload, std::string& output) {
size_t out_len = (payload.size() + 3) / 4 * 3;
output.resize(out_len, '\0');
out_len = Base64::decodeImpl(
payload.first,
payload.second,
payload.data(),
payload.size(),
&output[0],
out_len,
kBase64UrlReverseIndexTable);
Expand Down
62 changes: 27 additions & 35 deletions velox/common/encode/Base64.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,11 @@ class Base64 {
/// within the encoding base.
using ReverseIndex = std::array<uint8_t, kReverseIndexSize>;

/// Padding character used in encoding.
static const char kPadding = '=';

/// Encodes the specified number of characters from the 'data'.
static std::string encode(const char* data, size_t len);
static std::string encode(std::string_view data, size_t len);

/// Encodes the specified text.
static std::string encode(folly::StringPiece text);
static std::string encode(std::string_view text);

/// Encodes the specified IOBuf data.
static std::string encode(const folly::IOBuf* text);
Expand All @@ -59,94 +56,89 @@ 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(const char* data, size_t size, char* output);
static void encode(std::string_view data, char* output);

/// Decodes the specified encoded text.
static std::string decode(folly::StringPiece encoded);
static std::string decode(std::string_view encoded);

/// Returns the actual size of the decoded data. Will also remove the padding
/// length from the input data 'size'.
static size_t calculateDecodedSize(const char* data, size_t& size);
static size_t calculateDecodedSize(std::string_view data, size_t& size);

/// Decodes 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 calculateDecodedSize().
static void decode(const char* data, size_t size, char* output);
static void decode(std::string_view data, size_t size, char* output);

static void decode(
const std::pair<const char*, int32_t>& payload,
std::string& output);
static void decode(std::string_view payload, std::string& output);

/// 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(const char* data, size_t size, char* output);

/// Encodes the specified number of characters from the 'data' using URL
/// encoding.
static std::string encodeUrl(const char* data, size_t len);
static void encodeUrl(std::string_view data, char* output);

/// Encodes the specified IOBuf data using URL encoding.
static std::string encodeUrl(const folly::IOBuf* data);

/// Encodes the specified text using URL encoding.
static std::string encodeUrl(folly::StringPiece text);
static std::string encodeUrl(std::string_view text);

/// Decodes the specified URL encoded payload and writes the result to the
/// 'output'.
static void decodeUrl(
const std::pair<const char*, int32_t>& payload,
std::string& output);
static void decodeUrl(std::string_view payload, std::string& output);

/// Decodes the specified URL encoded text.
static std::string decodeUrl(folly::StringPiece text);
static std::string decodeUrl(std::string_view text);

/// Decodes the specified number of characters from the 'src' and writes the
/// result to the 'dst'.
static size_t
decode(const char* src, size_t src_len, char* dst, size_t dst_len);
decode(std::string_view src, size_t src_len, char* dst, size_t dst_len);

/// Decodes the specified number of characters from the 'src' using URL
/// encoding and writes the result to the 'dst'.
static void
decodeUrl(const char* src, size_t src_len, char* dst, size_t dst_len);
decodeUrl(std::string_view src, size_t src_len, char* dst, size_t dst_len);

private:
/// Checks if there is padding in encoded data.
static inline bool isPadded(const char* data, size_t len) {
// Padding character used in encoding.
static const char kPadding = '=';

// Checks if there is padding in encoded data.
static inline bool isPadded(std::string_view data, size_t len) {
return (len > 0 && data[len - 1] == kPadding);
}

/// Counts the number of padding characters in encoded data.
static inline size_t numPadding(const char* src, size_t len) {
// Counts the number of padding characters in encoded data.
static inline size_t numPadding(std::string_view data, size_t len) {
size_t numPadding{0};
while (len > 0 && src[len - 1] == kPadding) {
while (len > 0 && data[len - 1] == kPadding) {
numPadding++;
len--;
}
return numPadding;
}

/// Performs a reverse lookup in the reverse index to retrieve the original
/// index of a character in the base.
// Performs a reverse lookup in the reverse index to retrieve the original
// index of a character in the base.
static uint8_t base64ReverseLookup(char p, const ReverseIndex& reverseIndex);

/// Encodes the specified data using the provided charset.
// Encodes the specified data using the provided charset.
template <class T>
static std::string
encodeImpl(const T& data, const Charset& charset, bool include_pad);

/// Encodes the specified data using the provided charset.
// Encodes the specified data using the provided charset.
template <class T>
static void encodeImpl(
const T& data,
const Charset& charset,
bool include_pad,
char* out);

/// Decodes the specified data using the provided reverse lookup table.
// Decodes the specified data using the provided reverse lookup table.
static size_t decodeImpl(
const char* src,
std::string_view src,
size_t src_len,
char* dst,
size_t dst_len,
Expand Down
8 changes: 6 additions & 2 deletions velox/functions/prestosql/BinaryFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,10 @@ struct ToBase64Function {
FOLLY_ALWAYS_INLINE void 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(input.data(), input.size(), result.data());
encoding::Base64::encode(inputString, result.data());
}
};

Expand Down Expand Up @@ -319,8 +321,10 @@ struct ToBase64UrlFunction {
FOLLY_ALWAYS_INLINE void 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(input.data(), input.size(), result.data());
encoding::Base64::encodeUrl(inputString, result.data());
}
};

Expand Down

0 comments on commit 69ff5a1

Please sign in to comment.