|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +#pragma once |
| 5 | + |
| 6 | +#include <cstdint> |
| 7 | + |
| 8 | +#include "opentelemetry/version.h" |
| 9 | + |
| 10 | +OPENTELEMETRY_BEGIN_NAMESPACE |
| 11 | +namespace common |
| 12 | +{ |
| 13 | +namespace detail |
| 14 | +{ |
| 15 | + |
| 16 | +// Character classes for protocol and specification text, which define them over |
| 17 | +// ASCII. The <cctype> functions depend on the global locale, and passing them a |
| 18 | +// negative char is undefined behavior. |
| 19 | + |
| 20 | +constexpr bool IsAsciiDigit(char c) noexcept |
| 21 | +{ |
| 22 | + return c >= '0' && c <= '9'; |
| 23 | +} |
| 24 | + |
| 25 | +constexpr bool IsAsciiLower(char c) noexcept |
| 26 | +{ |
| 27 | + return c >= 'a' && c <= 'z'; |
| 28 | +} |
| 29 | + |
| 30 | +constexpr bool IsAsciiUpper(char c) noexcept |
| 31 | +{ |
| 32 | + return c >= 'A' && c <= 'Z'; |
| 33 | +} |
| 34 | + |
| 35 | +constexpr bool IsAsciiAlpha(char c) noexcept |
| 36 | +{ |
| 37 | + return IsAsciiLower(c) || IsAsciiUpper(c); |
| 38 | +} |
| 39 | + |
| 40 | +constexpr bool IsAsciiAlnum(char c) noexcept |
| 41 | +{ |
| 42 | + return IsAsciiAlpha(c) || IsAsciiDigit(c); |
| 43 | +} |
| 44 | + |
| 45 | +// Indexed by byte value: hexadecimal digits are decoded on the propagation hot |
| 46 | +// path, where a lookup is faster than range comparisons. |
| 47 | +constexpr int8_t kHexDigitValues[256] = { |
| 48 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 49 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 50 | + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, |
| 51 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 52 | + -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 53 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 54 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 55 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 56 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 57 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 58 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; |
| 59 | + |
| 60 | +// Returns the value of a hexadecimal digit of either case, or -1 if c is not one. |
| 61 | +constexpr int HexDigitValue(char c) noexcept |
| 62 | +{ |
| 63 | + return kHexDigitValues[static_cast<unsigned char>(c)]; |
| 64 | +} |
| 65 | + |
| 66 | +constexpr bool IsAsciiHexDigit(char c) noexcept |
| 67 | +{ |
| 68 | + return HexDigitValue(c) >= 0; |
| 69 | +} |
| 70 | + |
| 71 | +// Returns the hexadecimal digit for the low four bits of value. |
| 72 | +constexpr char LowerHexDigit(int value) noexcept |
| 73 | +{ |
| 74 | + return "0123456789abcdef"[value & 0xF]; |
| 75 | +} |
| 76 | + |
| 77 | +constexpr char UpperHexDigit(int value) noexcept |
| 78 | +{ |
| 79 | + return "0123456789ABCDEF"[value & 0xF]; |
| 80 | +} |
| 81 | + |
| 82 | +// 0x00 to 0x1F and 0x7F: what std::iscntrl accepts in the "C" locale. |
| 83 | +constexpr bool IsAsciiControl(char c) noexcept |
| 84 | +{ |
| 85 | + return static_cast<unsigned char>(c) < 0x20 || c == '\x7f'; |
| 86 | +} |
| 87 | + |
| 88 | +// Space, \t, \n, \v, \f and \r: what std::isspace accepts in the "C" locale. |
| 89 | +constexpr bool IsAsciiSpace(char c) noexcept |
| 90 | +{ |
| 91 | + return c == ' ' || (c >= '\t' && c <= '\r'); |
| 92 | +} |
| 93 | + |
| 94 | +constexpr char ToAsciiLower(char c) noexcept |
| 95 | +{ |
| 96 | + return IsAsciiUpper(c) ? static_cast<char>(c - 'A' + 'a') : c; |
| 97 | +} |
| 98 | + |
| 99 | +constexpr char ToAsciiUpper(char c) noexcept |
| 100 | +{ |
| 101 | + return IsAsciiLower(c) ? static_cast<char>(c - 'a' + 'A') : c; |
| 102 | +} |
| 103 | + |
| 104 | +} // namespace detail |
| 105 | +} // namespace common |
| 106 | +OPENTELEMETRY_END_NAMESPACE |
0 commit comments