veriblock-pop-cpp
C++11 Libraries for leveraging VeriBlock Proof-Of-Proof blockchain technology.
strutil.hpp
1// Copyright (c) 2019-2022 Xenios SEZC
2// https://www.veriblock.org
3// Distributed under the MIT software license, see the accompanying
4// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef ALT_INTEGRATION_VERIBLOCK_STRUTIL_HPP
7#define ALT_INTEGRATION_VERIBLOCK_STRUTIL_HPP
8
9#include <string>
10#include <vector>
11
12#include "base58.hpp"
13#include "base59.hpp"
14
15namespace altintegration {
16
18bool IsHex(const std::string& str);
19
21std::vector<uint8_t> ParseHex(const char* psz);
23std::vector<uint8_t> ParseHex(const std::string& hex);
24
37constexpr inline bool IsSpace(char c) noexcept {
38 return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' ||
39 c == '\v';
40}
41
43template <typename T>
44std::string HexStr(const T itbegin, const T itend) {
45 std::string rv;
46 // clang-format off
47 static const char hexmap[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
48 // clang-format on
49 rv.reserve(std::distance(itbegin, itend) * 2u);
50 for (T it = itbegin; it < itend; ++it) {
51 auto val = (uint8_t)(*it);
52 rv.push_back(hexmap[val >> 4u]);
53 rv.push_back(hexmap[val & 15u]);
54 }
55 return rv;
56}
57
59template <typename T>
60inline std::string HexStr(const T& vch, bool reverseHex = false) {
61 if (reverseHex) {
62 return HexStr(vch.rbegin(), vch.rend());
63 } else {
64 return HexStr(vch.begin(), vch.end());
65 }
66}
67
69std::vector<uint8_t> toBytes(const std::string& input);
70
71} // namespace altintegration
72#endif // ALT_INTEGRATION_INCLUDE_VERIBLOCK_STRUTIL_HPP_
Defines logging helpers.
Definition: block.hpp:14
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
Definition: strutil.hpp:37
std::string HexStr(const T itbegin, const T itend)
Convert bytes to hex.
Definition: strutil.hpp:44
std::vector< uint8_t > ParseHex(const char *psz)
Parse bytes from hex.