/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include <sys/types.h> #include <algorithm> #include <array> #include <cstring> #include <string> #include <type_traits> #include <glog/logging.h> #include <fmt/core.h> #include <folly/detail/IPAddress.h> // BSDish platforms don't provide standard access to s6_addr16 #ifndef s6_addr16 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || \ defined(__OpenBSD__) #define s6_addr16 … #endif #endif namespace folly { namespace detail { /** * Helper for working with unsigned char* or uint8_t* ByteArray values */ struct Bytes { … }; // // Write a maximum amount of base-converted character digits, of a // given base, from an unsigned integral type into a byte buffer of // sufficient size. // // This function does not append null terminators. // // Output buffer size must be guaranteed by caller (indirectly // controlled by DigitCount template parameter). // // Having these parameters at compile time allows compiler to // precompute several of the values, use smaller instructions, and // better optimize surrounding code. // // IntegralType: // - Something like uint8_t, uint16_t, etc // // DigitCount is the maximum number of digits to be printed // - This is tied to IntegralType and Base. For example: // - uint8_t in base 10 will print at most 3 digits ("255") // - uint16_t in base 16 will print at most 4 hex digits ("FFFF") // // Base is the desired output base of the string // - Base 10 will print [0-9], base 16 will print [0-9a-f] // // PrintAllDigits: // - Whether or not leading zeros should be printed // template < class IntegralType, IntegralType DigitCount, IntegralType Base = IntegralType(10), bool PrintAllDigits = false, class = typename std::enable_if< std::is_integral<IntegralType>::value && std::is_unsigned<IntegralType>::value, bool>::type> inline void writeIntegerString(IntegralType val, char** buffer) { … } inline size_t fastIpV4ToBufferUnsafe(const in_addr& inAddr, char* str) { … } inline std::string fastIpv4ToString(const in_addr& inAddr) { … } inline void fastIpv4AppendToString(const in_addr& inAddr, std::string& out) { … } inline size_t fastIpv6ToBufferUnsafe(const in6_addr& in6Addr, char* str) { … } inline std::string fastIpv6ToString(const in6_addr& in6Addr) { … } inline void fastIpv6AppendToString(const in6_addr& in6Addr, std::string& out) { … } } // namespace detail } // namespace folly