-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor Base64 class to match coding style #10371
Conversation
✅ Deploy Preview for meta-velox canceled.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Joe-Abraham Some high-level comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made the changes as per your comments
72f2e1e
to
7b31e43
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Joe-Abraham Can you clarify the changes to the API?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated the comments
7b31e43
to
a28984a
Compare
e659d21
to
4ade5dc
Compare
velox/common/encode/Base64.cpp
Outdated
constexpr bool constCharsetContains( | ||
|
||
/// Searches for a character within a charset up to a certain index. | ||
static constexpr bool findCharacterInCharSet( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove constexpr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is indirectly used by static assertion, so couldn't be removed
94a53ba
to
570bb3e
Compare
@majetideepak I have made the following changes
|
ccc856e
to
551b286
Compare
9780b07
to
f2727c7
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Joe-Abraham One comment. Thanks!
f2727c7
to
2de0472
Compare
@bikramSingh91, @mbasmanova do you have any feedback on this? Thanks! |
@@ -86,4 +89,14 @@ TEST_F(Base64Test, calculateDecodedSizeProperSize) { | |||
EXPECT_EQ(14, encoded_size); | |||
} | |||
|
|||
TEST_F(Base64Test, checksPadding) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: consider renaming these test to match the methods they are testing: isPadded and numPadding.
@@ -59,9 +62,9 @@ TEST_F(Base64Test, calculateDecodedSizeProperSize) { | |||
EXPECT_EQ(18, encoded_size); | |||
|
|||
encoded_size = 21; | |||
EXPECT_THROW( | |||
VELOX_ASSERT_THROW( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Joe-Abraham Thank you for the cleanup.
template <class T> | ||
static std::string | ||
encodeImpl(const T& data, const Charset& charset, bool include_pad); | ||
|
||
/// Encodes the specified data using the provided charset. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume here the caller is expected to allocate 'out' of sufficient length. It would be nice to clarify this in the comment.
static uint8_t Base64ReverseLookup(char p, const ReverseIndex& table); | ||
/// 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a private method? If so, comments should use //
} | ||
|
||
static inline size_t countPadding(const char* src, size_t len) { | ||
/// Counts the number of padding characters in encoded data. | ||
static inline size_t numPadding(const char* src, size_t len) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: using std::string_view for the argument would be nicer
private: | ||
/// Checks if there is padding in encoded data. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// for private methods
static size_t | ||
decode(const char* src, size_t src_len, char* dst, size_t dst_len); | ||
|
||
/// Decodes the specified number of characters from the 'src' using URL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to add some links to material that explains the difference between 'encoding' and 'URL encoding'.
Would you clarify what happens if 'src' cannot be decoded?
static void decodeUrl( | ||
const std::pair<const char*, int32_t>& payload, | ||
std::string& output); | ||
|
||
/// Decodes the specified URL encoded text. | ||
static std::string decodeUrl(folly::StringPiece text); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to replace folly::StringPiece with std::string_view
static std::string encodeUrl(folly::StringPiece 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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use std::string_view here?
/// Reverse lookup table for decoding purposes. | ||
/// Maps each possible encoded character to its corresponding numeric value | ||
/// within the encoding base. | ||
using ReverseIndex = std::array<uint8_t, kReverseIndexSize>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be public?
if (!src_len) { | ||
return 0; | ||
} | ||
|
||
auto needed = calculateDecodedSize(src, src_len); | ||
if (dst_len < needed) { | ||
throw Base64Exception( | ||
VELOX_USER_FAIL( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to make this API non-throwing: https://velox-lib.io/blog/optimize-try-more
template <class T> | ||
/* static */ std::string | ||
Base64::encodeImpl(const T& data, const Charset& charset, bool include_pad) { | ||
/* static */ std::string Base64::encodeImpl( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// static
@xiaoxmeng has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
@xiaoxmeng merged this pull request in a4389f9. |
Conbench analyzed the 1 benchmark run on commit There were no benchmark performance regressions. 🎉 The full Conbench report has more details. |
@majetideepak I will add a new PR for |
Rewrite to match the Velox coding guidelines.
Rename constCharsetContains to findCharacterInCharset for clarity.
Add comments.