userver: userver/utils/encoding/hex.hpp Source File
Loading...
Searching...
No Matches
hex.hpp
Go to the documentation of this file.
1#pragma once
2
6
7#include <cstdint>
8#include <string>
9
11
12USERVER_NAMESPACE_BEGIN
13
15namespace utils::encoding {
16
19char ToHexChar(int num);
20
23
24constexpr size_t LengthInHexForm(size_t size) noexcept { return size * 2; }
25
26constexpr size_t LengthInHexForm(std::string_view data) noexcept {
27 return USERVER_NAMESPACE::utils::encoding::LengthInHexForm(data.size());
28}
30
40constexpr size_t FromHexUpperBound(size_t size) noexcept {
41 // Although only even-sized input is valid, we have to support an odd
42 // number is well. Luckily for us, size / 2 will get us correct value
43 // anyway
44 return size / 2;
45}
46
50void ToHex(std::string_view input, std::string& out) noexcept;
51
56void ToHexBuffer(std::string_view input, utils::span<char> out) noexcept;
57
60inline std::string ToHex(std::string_view data) noexcept {
61 std::string result;
62 USERVER_NAMESPACE::utils::encoding::ToHex(data, result);
63 return result;
64}
65
69inline std::string ToHex(const void* encoded, size_t len) noexcept {
70 const auto* chars = reinterpret_cast<const char*>(encoded);
71 return USERVER_NAMESPACE::utils::encoding::ToHex(std::string_view{chars, len});
72}
73
85size_t FromHex(std::string_view encoded, std::string& out) noexcept;
86
89inline std::string FromHex(std::string_view encoded) noexcept {
90 std::string result;
91 USERVER_NAMESPACE::utils::encoding::FromHex(encoded, result);
92 return result;
93}
94
100std::string_view GetHexPart(std::string_view encoded) noexcept;
101
104bool IsHexData(std::string_view encoded) noexcept;
105
107inline std::string ToHexString(uint64_t value) {
108 return USERVER_NAMESPACE::utils::encoding::ToHex(&value, sizeof(value));
109}
110
111} // namespace utils::encoding
112
113USERVER_NAMESPACE_END