userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
hex.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/utils/encoding/hex.hpp
4
/// @brief Encoders, decoders and helpers for hexadecimal representations
5
/// @ingroup userver_universal
6
7
#
include
<
cstdint
>
8
#
include
<
string
>
9
10
#
include
<
userver
/
utils
/
span
.
hpp
>
11
12
USERVER_NAMESPACE_BEGIN
13
14
/// @brief Encoders/decoders: TSKV, hex, and related parsers.
15
namespace
utils
::
encoding
{
16
17
/// @brief Converts number to hex character; number must be within range [0,16)
18
/// @throws out_of_bounds exception if \p num is out of range
19
char
ToHexChar
(
int
num);
20
21
/// @brief Calculate expected length of input after being hex encoded
22
/// @{
23
24
constexpr
size_t
LengthInHexForm
(size_t size)
noexcept
{
return
size * 2; }
25
26
constexpr
size_t LengthInHexForm(std::string_view data)
noexcept
{
return
LengthInHexForm
(
data.size()
)
; }
27
/// @}
28
29
/// @brief Return upper limit on number of characters required
30
/// to unhex input of given size.
31
///
32
/// For example:
33
/// - FromHexUpperBound(1) = 0, because you can't really unhex one char,
34
/// it is only half byte. Where is the second half?
35
/// - FromHexUpperBound(2) = 1. Two chars will be converted into one byte
36
/// - FromHexUpperBound(5) = 2. First 4 chars will be unhexed into 2 bytes,
37
/// and there will be one left.
38
constexpr
size_t
FromHexUpperBound
(size_t size)
noexcept
{
39
// Although only even-sized input is valid, we have to support an odd
40
// number is well. Luckily for us, size / 2 will get us correct value
41
// anyway
42
return
size / 2;
43
}
44
45
/// @brief Converts input to hex and writes data to output \p out
46
/// @param input bytes to convert
47
/// @param out string to write data. out will be cleared
48
void
ToHex
(std::string_view input, std::string& out)
noexcept
;
49
50
/// @brief Converts input to hex and writes data to output @p out.
51
/// @warning `out` must be pre-allocated to at least @ref utils::encoding::LengthInHexForm bytes.
52
/// @param input bytes to convert
53
/// @param out buffer to write data
54
void
ToHexBuffer
(std::string_view input,
utils
::
span
<
char
> out)
noexcept
;
55
56
/// @brief Allocates std::string, converts input and writes into said string
57
/// @param data range of input bytes
58
inline
std::string
ToHex
(std::string_view data)
noexcept
{
59
std::string result;
60
ToHex
(
data
,
result
)
;
61
return
result;
62
}
63
64
/// @brief Allocates std::string, converts input and writes into said string
65
/// @param encoded start of continuous range in memory
66
/// @param len size of that range
67
inline
std::string
ToHex
(
const
void
* encoded, size_t len)
noexcept
{
68
const
auto
* chars =
reinterpret_cast
<
const
char
*>(encoded);
69
return
ToHex
(
std::string_view{chars, len}
)
;
70
}
71
72
/// @brief Converts as much of input from hex as possible and writes data
73
/// into \p out.
74
///
75
/// To convert some range from hex, range
76
/// must have even number of elements and every element must be hex character.
77
/// To avoid throwing, algorithms consumes as much data as possible and returns
78
/// how much it was able to process
79
///
80
/// @param encoded input range to convert
81
/// @param out Result will be written into out. Previous value will be cleared.
82
/// @returns Number of characters successfully parsed.
83
size_t
FromHex
(std::string_view encoded, std::string& out)
noexcept
;
84
85
/// @brief This FromHex overload allocates string and calls FromHex; if data
86
/// is not fully a hex string, then it will be only partially processed.
87
inline
std::string
FromHex
(std::string_view encoded)
noexcept
{
88
std::string result;
89
FromHex
(
encoded
,
result
)
;
90
return
result;
91
}
92
93
/// Returns range that constitutes hex string - e.g. sub-view of encoded
94
/// that could be fully interpreted as hex string.
95
/// Basically, if you have string_view \p a and c = GetHexPart(a), then
96
/// range FromHex(c) will be fully parsed.
97
/// @param encoded input array of bytes.
98
std::string_view
GetHexPart
(std::string_view encoded)
noexcept
;
99
100
/// @brief Checks that given range is fully a hex string. That is, if passed to
101
/// FromHex, it will be fully processed
102
bool
IsHexData
(std::string_view encoded)
noexcept
;
103
104
/// @brief Interprets uint64_t value as array of bytes and applies ToHex to it
105
inline
std::string
ToHexString
(uint64_t value) {
return
ToHex
(
&value
,
sizeof
(value)
)
; }
106
107
}
// namespace utils::encoding
108
109
USERVER_NAMESPACE_END
userver
utils
encoding
hex.hpp
Generated on
for userver by
Doxygen
1.17.0