-
Notifications
You must be signed in to change notification settings - Fork 34
/
hexutil.cpp
48 lines (40 loc) · 1.29 KB
/
hexutil.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Copyright Quadrivium LLC
* All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*/
#include "common/hexutil.hpp"
#include <qtils/hex.hpp>
#include <qtils/unhex.hpp>
#include "common/buffer_view.hpp"
OUTCOME_CPP_DEFINE_CATEGORY(kagome::common, UnhexError, e) {
using kagome::common::UnhexError;
switch (e) {
case UnhexError::NON_HEX_INPUT:
return "Input contains non-hex characters";
case UnhexError::NOT_ENOUGH_INPUT:
return "Input contains odd number of characters";
case UnhexError::VALUE_OUT_OF_RANGE:
return "Decoded value is out of range of requested type";
case UnhexError::MISSING_0X_PREFIX:
return "Missing expected 0x prefix";
case UnhexError::UNKNOWN:
return "Unknown error";
}
return "Unknown error (error id not listed)";
}
namespace kagome::common {
std::string hex_lower(BufferView bytes) {
return fmt::format("{:x}", std::span{bytes});
}
std::string hex_lower_0x(BufferView bytes) {
return fmt::format("{:0x}", std::span{bytes});
}
outcome::result<std::vector<uint8_t>> unhex(std::string_view hex) {
return qtils::unhex(hex);
}
outcome::result<std::vector<uint8_t>> unhexWith0x(
std::string_view hex_with_prefix) {
return qtils::unhex0x(hex_with_prefix);
}
} // namespace kagome::common