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 <cstdint> | #include <cstdint> | ||||
#include <string> | #include <string> | ||||
#include <vector> | #include <vector> | ||||
#define BEGIN(a) ((char *)&(a)) | #define BEGIN(a) ((char *)&(a)) | ||||
#define END(a) ((char *)&((&(a))[1])) | #define END(a) ((char *)&((&(a))[1])) | ||||
#define UBEGIN(a) ((uint8_t *)&(a)) | #define UBEGIN(a) ((uint8_t *)&(a)) | ||||
#define UEND(a) ((uint8_t *)&((&(a))[1])) | #define UEND(a) ((uint8_t *)&((&(a))[1])) | ||||
▲ Show 20 Lines • Show All 64 Lines • ▼ Show 20 Lines | return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || | ||||
c == '\v'; | c == '\v'; | ||||
} | } | ||||
/** | /** | ||||
* Convert string to signed 32-bit integer with strict parse error feedback. | * Convert string to signed 32-bit integer with strict parse error feedback. | ||||
* @returns true if the entire string could be parsed as valid integer, false if | * @returns true if the entire string could be parsed as valid integer, 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. | ||||
*/ | */ | ||||
bool ParseInt32(const std::string &str, int32_t *out); | NODISCARD bool ParseInt32(const std::string &str, int32_t *out); | ||||
/** | /** | ||||
* Convert string to signed 64-bit integer with strict parse error feedback. | * Convert string to signed 64-bit integer with strict parse error feedback. | ||||
* @returns true if the entire string could be parsed as valid integer, false if | * @returns true if the entire string could be parsed as valid integer, 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. | ||||
*/ | */ | ||||
bool ParseInt64(const std::string &str, int64_t *out); | NODISCARD bool ParseInt64(const std::string &str, int64_t *out); | ||||
/** | /** | ||||
* Convert decimal string to unsigned 32-bit integer with strict parse error | * Convert decimal string to unsigned 32-bit integer with strict parse error | ||||
* feedback. | * feedback. | ||||
* @returns true if the entire string could be parsed as valid integer, false if | * @returns true if the entire string could be parsed as valid integer, 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. | ||||
*/ | */ | ||||
bool ParseUInt32(const std::string &str, uint32_t *out); | NODISCARD bool ParseUInt32(const std::string &str, uint32_t *out); | ||||
/** | /** | ||||
* Convert decimal string to unsigned 64-bit integer with strict parse error | * Convert decimal string to unsigned 64-bit integer with strict parse error | ||||
* feedback. | * feedback. | ||||
* @returns true if the entire string could be parsed as valid integer, false if | * @returns true if the entire string could be parsed as valid integer, 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. | ||||
*/ | */ | ||||
bool ParseUInt64(const std::string &str, uint64_t *out); | NODISCARD bool ParseUInt64(const std::string &str, uint64_t *out); | ||||
/** | /** | ||||
* 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. | ||||
*/ | */ | ||||
bool ParseDouble(const std::string &str, double *out); | NODISCARD bool ParseDouble(const std::string &str, double *out); | ||||
template <typename T> | template <typename T> | ||||
std::string HexStr(const T itbegin, const T itend, bool fSpaces = false) { | std::string HexStr(const T itbegin, const T itend, bool fSpaces = false) { | ||||
std::string rv; | std::string rv; | ||||
static const char hexmap[16] = {'0', '1', '2', '3', '4', '5', '6', '7', | static const char hexmap[16] = {'0', '1', '2', '3', '4', '5', '6', '7', | ||||
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; | '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; | ||||
rv.reserve((itend - itbegin) * 3); | rv.reserve((itend - itbegin) * 3); | ||||
for (T it = itbegin; it < itend; ++it) { | for (T it = itbegin; it < itend; ++it) { | ||||
Show All 32 Lines | |||||
/** | /** | ||||
* Parse number as fixed point according to JSON number syntax. | * Parse number as fixed point according to JSON number syntax. | ||||
* See http://json.org/number.gif | * See http://json.org/number.gif | ||||
* @returns true on success, false on error. | * @returns true on success, false on error. | ||||
* @note The result must be in the range (-10^18,10^18), otherwise an overflow | * @note The result must be in the range (-10^18,10^18), otherwise an overflow | ||||
* error will trigger. | * error will trigger. | ||||
*/ | */ | ||||
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out); | NODISCARD bool ParseFixedPoint(const std::string &val, int decimals, | ||||
int64_t *amount_out); | |||||
/** | /** | ||||
* Convert from one power-of-2 number base to another. | * Convert from one power-of-2 number base to another. | ||||
* | * | ||||
* If padding is enabled, this always return true. If not, then it returns true | * If padding is enabled, this always return true. If not, then it returns true | ||||
* of all the bits of the input are encoded in the output. | * of all the bits of the input are encoded in the output. | ||||
*/ | */ | ||||
template <int frombits, int tobits, bool pad, typename O, typename I> | template <int frombits, int tobits, bool pad, typename O, typename I> | ||||
Show All 21 Lines | bool ConvertBits(O &out, I it, I end) { | ||||
if (pad && bits) { | if (pad && bits) { | ||||
out.push_back((acc << (tobits - bits)) & maxv); | out.push_back((acc << (tobits - bits)) & maxv); | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
/** Parse an HD keypaths like "m/7/0'/2000". */ | /** Parse an HD keypaths like "m/7/0'/2000". */ | ||||
bool ParseHDKeypath(const std::string &keypath_str, | NODISCARD bool ParseHDKeypath(const std::string &keypath_str, | ||||
std::vector<uint32_t> &keypath); | std::vector<uint32_t> &keypath); | ||||
/** | /** | ||||
* Converts the given character to its lowercase equivalent. | * Converts the given character to its lowercase equivalent. | ||||
* This function is locale independent. It only converts uppercase | * This function is locale independent. It only converts uppercase | ||||
* characters in the standard 7-bit ASCII range. | * characters in the standard 7-bit ASCII range. | ||||
* @param[in] c the character to convert to lowercase. | * @param[in] c the character to convert to lowercase. | ||||
* @return the lowercase equivalent of c; or the argument | * @return the lowercase equivalent of c; or the argument | ||||
* if no conversion is possible. | * if no conversion is possible. | ||||
Show All 36 Lines |