Changeset View
Changeset View
Standalone View
Standalone View
src/util/strencodings.h
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
/** | /** | ||||
* Utilities for converting data from/to strings. | * Utilities for converting data from/to strings. | ||||
*/ | */ | ||||
#ifndef BITCOIN_UTIL_STRENCODINGS_H | #ifndef BITCOIN_UTIL_STRENCODINGS_H | ||||
#define BITCOIN_UTIL_STRENCODINGS_H | #define BITCOIN_UTIL_STRENCODINGS_H | ||||
#include <attributes.h> | #include <attributes.h> | ||||
#include <span.h> | |||||
#include <cstdint> | #include <cstdint> | ||||
#include <iterator> | #include <iterator> | ||||
#include <string> | #include <string> | ||||
#include <vector> | #include <vector> | ||||
#define ARRAYLEN(array) (sizeof(array) / sizeof((array)[0])) | #define ARRAYLEN(array) (sizeof(array) / sizeof((array)[0])) | ||||
▲ Show 20 Lines • Show All 111 Lines • ▼ Show 20 Lines | |||||
/** | /** | ||||
* Convert string to double with strict parse error feedback. | * Convert string to double with strict parse error feedback. | ||||
* @returns true if the entire string could be parsed as valid double, false if | * @returns true if the entire string could be parsed as valid double, false if | ||||
* not the entire string could be parsed or when overflow or underflow occurred. | * not the entire string could be parsed or when overflow or underflow occurred. | ||||
*/ | */ | ||||
NODISCARD bool ParseDouble(const std::string &str, double *out); | NODISCARD bool ParseDouble(const std::string &str, double *out); | ||||
template <typename T> std::string HexStr(const T itbegin, const T itend) { | /** | ||||
std::string rv; | * Convert a span of bytes to a lower-case hexadecimal string. | ||||
static const char hexmap[16] = {'0', '1', '2', '3', '4', '5', '6', '7', | */ | ||||
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; | std::string HexStr(const Span<const uint8_t> s); | ||||
rv.reserve(std::distance(itbegin, itend) * 2); | inline std::string HexStr(const Span<const char> s) { | ||||
for (T it = itbegin; it < itend; ++it) { | return HexStr(MakeUCharSpan(s)); | ||||
uint8_t val = uint8_t(*it); | |||||
rv.push_back(hexmap[val >> 4]); | |||||
rv.push_back(hexmap[val & 15]); | |||||
} | |||||
return rv; | |||||
} | |||||
template <typename T> inline std::string HexStr(const T &vch) { | |||||
return HexStr(vch.begin(), vch.end()); | |||||
} | } | ||||
/** | /** | ||||
* Format a paragraph of text to a fixed width, adding spaces for indentation to | * Format a paragraph of text to a fixed width, adding spaces for indentation to | ||||
* any added line. | * any added line. | ||||
*/ | */ | ||||
std::string FormatParagraph(const std::string &in, size_t width = 79, | std::string FormatParagraph(const std::string &in, size_t width = 79, | ||||
size_t indent = 0); | size_t indent = 0); | ||||
▲ Show 20 Lines • Show All 118 Lines • Show Last 20 Lines |