19 #include <glog/logging.h> 20 #include <sys/types.h> 25 #include <type_traits> 32 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || \ 34 #define s6_addr16 __u6_addr.__u6_addr16 46 template <std::
size_t N>
47 static std::array<uint8_t, N>
mask(
48 const std::array<uint8_t, N>&
a,
49 const std::array<uint8_t, N>&
b) {
50 static_assert(N > 0,
"Can't mask an empty ByteArray");
51 std::size_t asize = a.size();
52 std::array<uint8_t, N> ba{{0}};
53 for (std::size_t
i = 0;
i < asize;
i++) {
59 template <std::
size_t N>
61 const std::array<uint8_t, N>& one,
63 const std::array<uint8_t, N>& two,
65 static constexpr
auto kBitCount = N * 8;
66 static constexpr std::array<uint8_t, 8> kMasks{{
76 if (oneMask > kBitCount || twoMask > kBitCount) {
77 throw std::invalid_argument(
sformat(
78 "Invalid mask length: {}. Mask length must be <= {}",
85 std::array<uint8_t, N> ba{{0}};
89 while (byteIndex * 8 <
mask && one[byteIndex] == two[byteIndex]) {
90 ba[byteIndex] = one[byteIndex];
102 while (bitIndex <
mask &&
103 ((one[bI] & kMasks[bM]) == (two[bI] & kMasks[bM]))) {
104 ba[bI] =
uint8_t(one[bI] & kMasks[bM]);
109 return {ba, bitIndex};
118 std::memset(&
addr, 0, 4);
119 std::memcpy(
addr.bytes, src, 4);
126 std::memset(&addr, 0, 16);
127 std::memcpy(addr.s6_addr, src, 16);
133 static const char*
const lut =
"0123456789abcdef";
135 for (std::size_t
i = 0;
i < len;
i++) {
136 const unsigned char c = src[
i];
137 out[
i * 2 + 0] = lut[c >> 4];
138 out[
i * 2 + 1] = lut[c & 15];
178 IntegralType DigitCount,
179 IntegralType
Base = IntegralType(10),
180 bool PrintAllDigits =
false,
181 class =
typename std::enable_if<
188 if (!PrintAllDigits && val == 0) {
194 IntegralType powerToPrint = 1;
195 for (IntegralType
i = 1;
i < DigitCount; ++
i) {
196 powerToPrint *=
Base;
199 bool found = PrintAllDigits;
200 while (powerToPrint) {
201 if (found || powerToPrint <= val) {
202 IntegralType
value = IntegralType(val / powerToPrint);
203 if (Base == 10 || value < 10) {
208 *(buf++) =
char(value);
213 powerToPrint /=
Base;
220 const uint8_t* octets =
reinterpret_cast<const uint8_t*
>(&inAddr.s_addr);
223 writeIntegerString<uint8_t, 3>(octets[0], &buf);
225 writeIntegerString<uint8_t, 3>(octets[1], &buf);
227 writeIntegerString<uint8_t, 3>(octets[2], &buf);
229 writeIntegerString<uint8_t, 3>(octets[3], &buf);
235 char str[
sizeof(
"255.255.255.255")];
240 char str[
sizeof(
"255.255.255.255")];
248 const uint16_t* bytes =
reinterpret_cast<const uint16_t*
>(&in6Addr.s6_addr16);
252 for (
int i = 0;
i < 8; ++
i) {
257 true>(htons(bytes[
i]), &buf);
268 char str[
sizeof(
"2001:0db8:0000:0000:0000:ff00:0042:8329")];
273 char str[
sizeof(
"2001:0db8:0000:0000:0000:ff00:0042:8329")];
std::vector< uint8_t > buffer(kBufferSize+16)
void writeIntegerString(IntegralType val, char **buffer)
std::string sformat(StringPiece fmt, Args &&...args)
static std::array< uint8_t, N > mask(const std::array< uint8_t, N > &a, const std::array< uint8_t, N > &b)
—— Concurrent Priority Queue Implementation ——
static in6_addr mkAddress6(const uint8_t *src)
AtomicCounter< T, DeterministicAtomic > Base
static std::pair< std::array< uint8_t, N >, uint8_t > longestCommonPrefix(const std::array< uint8_t, N > &one, uint8_t oneMask, const std::array< uint8_t, N > &two, uint8_t twoMask)
void fastIpv6AppendToString(const in6_addr &in6Addr, std::string &out)
static const char *const value
size_t fastIpv6ToBufferUnsafe(const in6_addr &in6Addr, char *str)
static in_addr mkAddress4(const uint8_t *src)
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
std::string fastIpv4ToString(const in_addr &inAddr)
void fastIpv4AppendToString(const in_addr &inAddr, std::string &out)
static std::string toHex(const uint8_t *src, std::size_t len)
std::string fastIpv6ToString(const in6_addr &in6Addr)
ThreadPoolListHook * addr
size_t fastIpV4ToBufferUnsafe(const in_addr &inAddr, char *str)