/root/doris/be/src/vec/common/hex.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // This file is copied from |
18 | | // https://github.com/ClickHouse/ClickHouse/blob/master/src/Common/hex.h |
19 | | // and modified by Doris |
20 | | |
21 | | #pragma once |
22 | | #include <cstring> |
23 | | #include <string> |
24 | | |
25 | | #include "vec/core/types.h" |
26 | | |
27 | | namespace doris::vectorized { |
28 | | |
29 | | /// Maps 0..15 to 0..9A..F or 0..9a..f correspondingly. |
30 | | |
31 | | extern const char* const hex_digit_to_char_uppercase_table; |
32 | | extern const char* const hex_digit_to_char_lowercase_table; |
33 | | |
34 | 24 | inline char hex_digit_uppercase(unsigned char c) { |
35 | 24 | return hex_digit_to_char_uppercase_table[c]; |
36 | 24 | } |
37 | | |
38 | 1.18k | inline char hex_digit_lowercase(unsigned char c) { |
39 | 1.18k | return hex_digit_to_char_lowercase_table[c]; |
40 | 1.18k | } |
41 | | |
42 | | /// Maps 0..255 to 00..FF or 00..ff correspondingly |
43 | | |
44 | | extern const char* const hex_byte_to_char_uppercase_table; |
45 | | extern const char* const hex_byte_to_char_lowercase_table; |
46 | | |
47 | 0 | inline void write_hex_byte_uppercase(UInt8 byte, void* out) { |
48 | 0 | memcpy(out, &hex_byte_to_char_uppercase_table[static_cast<size_t>(byte) * 2], 2); |
49 | 0 | } |
50 | | |
51 | 104 | inline void write_hex_byte_lowercase(UInt8 byte, void* out) { |
52 | 104 | memcpy(out, &hex_byte_to_char_lowercase_table[static_cast<size_t>(byte) * 2], 2); |
53 | 104 | } |
54 | | |
55 | | extern const char* const bin_byte_to_char_table; |
56 | | |
57 | 0 | inline void write_bin_byte(UInt8 byte, void* out) { |
58 | 0 | memcpy(out, &bin_byte_to_char_table[static_cast<size_t>(byte) * 8], 8); |
59 | 0 | } |
60 | | |
61 | | /// Produces hex representation of an unsigned int with leading zeros (for checksums) |
62 | | template <typename TUInt> |
63 | 15.2k | void write_hex_uint_impl(TUInt uint_, char* out, const char* const table) { |
64 | 15.2k | union { |
65 | 15.2k | TUInt value; |
66 | 15.2k | UInt8 uint8[sizeof(TUInt)]; |
67 | 15.2k | }; |
68 | | |
69 | 15.2k | value = uint_; |
70 | | |
71 | | /// Use little endian |
72 | 258k | for (size_t i = 0; i < sizeof(TUInt); ++i) { |
73 | 243k | memcpy(out + i * 2, &table[static_cast<size_t>(uint8[sizeof(TUInt) - 1 - i]) * 2], 2); |
74 | 243k | } |
75 | 15.2k | } |
76 | | |
77 | | template <typename TUInt> |
78 | | void write_hex_uint_uppercase(TUInt uint_, char* out) { |
79 | | write_hex_uint_impl(uint_, out, hex_byte_to_char_uppercase_table); |
80 | | } |
81 | | |
82 | | template <typename TUInt> |
83 | 15.2k | void write_hex_uint_lowercase(TUInt uint_, char* out) { |
84 | 15.2k | write_hex_uint_impl(uint_, out, hex_byte_to_char_lowercase_table); |
85 | 15.2k | } |
86 | | |
87 | | template <typename TUInt> |
88 | | std::string get_hex_uint_uppercase(TUInt uint_) { |
89 | | std::string res(sizeof(TUInt) * 2, '\0'); |
90 | | write_hex_uint_uppercase(uint_, res.data()); |
91 | | return res; |
92 | | } |
93 | | |
94 | | template <typename TUInt> |
95 | 15.2k | std::string get_hex_uint_lowercase(TUInt uint_) { |
96 | 15.2k | std::string res(sizeof(TUInt) * 2, '\0'); |
97 | 15.2k | write_hex_uint_lowercase(uint_, res.data()); |
98 | 15.2k | return res; |
99 | 15.2k | } |
100 | | |
101 | | /// Maps 0..9, A..F, a..f to 0..15. Other chars are mapped to implementation specific value. |
102 | | |
103 | | extern const char* const hex_char_to_digit_table; |
104 | | |
105 | 1.84k | inline UInt8 unhex(char c) { |
106 | 1.84k | return hex_char_to_digit_table[static_cast<UInt8>(c)]; |
107 | 1.84k | } |
108 | | |
109 | 0 | inline UInt8 unhex2(const char* data) { |
110 | 0 | return static_cast<UInt8>(unhex(data[0])) * 0x10 + static_cast<UInt8>(unhex(data[1])); |
111 | 0 | } |
112 | | |
113 | 0 | inline UInt16 unhex4(const char* data) { |
114 | 0 | return static_cast<UInt16>(unhex(data[0])) * 0x1000 + |
115 | 0 | static_cast<UInt16>(unhex(data[1])) * 0x100 + |
116 | 0 | static_cast<UInt16>(unhex(data[2])) * 0x10 + static_cast<UInt16>(unhex(data[3])); |
117 | 0 | } |
118 | | |
119 | | template <typename TUInt> |
120 | 87 | TUInt unhex_uint(const char* data) { |
121 | 87 | TUInt res = TUInt(0); |
122 | 87 | if constexpr ((sizeof(TUInt) <= 8) || ((sizeof(TUInt) % 8) != 0)) { |
123 | 986 | for (size_t i = 0; i < sizeof(TUInt) * 2; ++i, ++data) { |
124 | 928 | res <<= 4; |
125 | 928 | res += unhex(*data); |
126 | 928 | } |
127 | 58 | } else { |
128 | 87 | for (size_t i = 0; i < sizeof(TUInt) / 8; ++i, data += 16) { |
129 | 58 | res <<= TUInt(64); |
130 | 58 | res += TUInt(unhex_uint<UInt64>(data)); |
131 | 58 | } |
132 | 29 | } |
133 | 87 | return res; |
134 | 87 | } _ZN5doris10vectorized10unhex_uintIN4wide7integerILm128EjEEEET_PKc Line | Count | Source | 120 | 29 | TUInt unhex_uint(const char* data) { | 121 | 29 | TUInt res = TUInt(0); | 122 | 29 | if constexpr ((sizeof(TUInt) <= 8) || ((sizeof(TUInt) % 8) != 0)) { | 123 | 29 | for (size_t i = 0; i < sizeof(TUInt) * 2; ++i, ++data) { | 124 | 29 | res <<= 4; | 125 | 29 | res += unhex(*data); | 126 | 29 | } | 127 | 29 | } else { | 128 | 87 | for (size_t i = 0; i < sizeof(TUInt) / 8; ++i, data += 16) { | 129 | 58 | res <<= TUInt(64); | 130 | 58 | res += TUInt(unhex_uint<UInt64>(data)); | 131 | 58 | } | 132 | 29 | } | 133 | 29 | return res; | 134 | 29 | } |
_ZN5doris10vectorized10unhex_uintImEET_PKc Line | Count | Source | 120 | 58 | TUInt unhex_uint(const char* data) { | 121 | 58 | TUInt res = TUInt(0); | 122 | 58 | if constexpr ((sizeof(TUInt) <= 8) || ((sizeof(TUInt) % 8) != 0)) { | 123 | 986 | for (size_t i = 0; i < sizeof(TUInt) * 2; ++i, ++data) { | 124 | 928 | res <<= 4; | 125 | 928 | res += unhex(*data); | 126 | 928 | } | 127 | 58 | } else { | 128 | 58 | for (size_t i = 0; i < sizeof(TUInt) / 8; ++i, data += 16) { | 129 | 58 | res <<= TUInt(64); | 130 | 58 | res += TUInt(unhex_uint<UInt64>(data)); | 131 | 58 | } | 132 | 58 | } | 133 | 58 | return res; | 134 | 58 | } |
|
135 | | } // namespace doris::vectorized |