small improvements

This commit is contained in:
zvonand 2022-07-20 13:36:47 +02:00
parent 30eb2cbcb3
commit 11b8d788ca
2 changed files with 20 additions and 22 deletions

View File

@ -5,7 +5,7 @@
namespace DB
{
inline bool encodeBase58(const char8_t * src, char8_t * dst)
inline size_t encodeBase58(const char8_t * src, char8_t * dst)
{
const char * base58_encoding_alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
@ -38,10 +38,10 @@ inline bool encodeBase58(const char8_t * src, char8_t * dst)
dst[c_idx] = base58_encoding_alphabet[static_cast<unsigned char>(dst[c_idx])];
}
dst[idx] = '\0';
return true;
return idx + 1;
}
inline bool decodeBase58(const char8_t * src, char8_t * dst)
inline size_t decodeBase58(const char8_t * src, char8_t * dst)
{
const char map_digits[128]
= {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
@ -53,12 +53,12 @@ inline bool decodeBase58(const char8_t * src, char8_t * dst)
for (; *src; ++src)
{
unsigned int carry = static_cast<unsigned int>(map_digits[static_cast<unsigned char>(*src)]);
if (carry == UINT_MAX || *src < '1' || map_digits[static_cast<unsigned char>(*src)] == map_digits[0])
unsigned int carry = map_digits[*src];
if (unlikely(carry == UINT_MAX))
{
return false;
return 0;
}
for (size_t j = 0; j < idx; j++)
for (size_t j = 0; j < idx; ++j)
{
carry += static_cast<unsigned char>(dst[j]) * 58;
dst[j] = static_cast<unsigned char>(carry & 0xff);
@ -79,7 +79,7 @@ inline bool decodeBase58(const char8_t * src, char8_t * dst)
dst[idx - (i + 1)] = s;
}
dst[idx] = '\0';
return true;
return idx + 1;
}
}

View File

@ -39,7 +39,7 @@ struct Base58Encode
const ColumnString::Offsets & src_offsets = src_column.getOffsets();
const auto * source = src_column.getChars().data();
const auto * src = src_column.getChars().data();
auto * dst = dst_data.data();
auto * dst_pos = dst;
@ -47,13 +47,11 @@ struct Base58Encode
for (size_t row = 0; row < input_rows_count; ++row)
{
size_t srclen = src_offsets[row] - src_offset_prev - 1;
encodeBase58(source, dst_pos);
size_t srclen = src_offsets[row] - src_offset_prev;
auto encoded_size = encodeBase58(src, dst_pos);
size_t encoded_length = strlen(reinterpret_cast<const char *>(dst_pos));
source += srclen + 1;
dst_pos += encoded_length + 1;
src += srclen;
dst_pos += encoded_size;
dst_offsets[row] = dst_pos - dst;
src_offset_prev = src_offsets[row];
@ -82,7 +80,7 @@ struct Base58Decode
const ColumnString::Offsets & src_offsets = src_column.getOffsets();
const auto * source = src_column.getChars().data();
const auto * src = src_column.getChars().data();
auto * dst = dst_data.data();
auto * dst_pos = dst;
@ -90,14 +88,14 @@ struct Base58Decode
for (size_t row = 0; row < input_rows_count; ++row)
{
size_t srclen = src_offsets[row] - src_offset_prev - 1;
if (!decodeBase58(source, dst_pos))
size_t srclen = src_offsets[row] - src_offset_prev;
auto decoded_size = decodeBase58(src, dst_pos);
if (!decoded_size)
throw Exception("Invalid Base58 value, cannot be decoded", ErrorCodes::BAD_ARGUMENTS);
size_t encoded_length = strlen(reinterpret_cast<const char *>(dst_pos));
source += srclen + 1;
dst_pos += encoded_length + 1;
src += srclen;
dst_pos += decoded_size;
dst_offsets[row] = dst_pos - dst;
src_offset_prev = src_offsets[row];