Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 10:11:09 -04:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2021-12-30 14:36:57 -03:00
|
|
|
// Copyright (c) 2009-2021 The Bitcoin Core developers
|
2014-11-17 00:04:01 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 10:11:09 -04:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utilities for converting data from/to strings.
|
|
|
|
*/
|
2018-10-22 19:51:11 -03:00
|
|
|
#ifndef BITCOIN_UTIL_STRENCODINGS_H
|
|
|
|
#define BITCOIN_UTIL_STRENCODINGS_H
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 10:11:09 -04:00
|
|
|
|
2018-09-25 02:00:36 -03:00
|
|
|
#include <attributes.h>
|
2020-06-24 11:26:47 -04:00
|
|
|
#include <span.h>
|
2021-09-30 11:18:50 -03:00
|
|
|
#include <util/string.h>
|
2018-09-25 02:00:36 -03:00
|
|
|
|
2021-09-18 01:30:30 -03:00
|
|
|
#include <charconv>
|
2018-09-25 02:00:36 -03:00
|
|
|
#include <cstdint>
|
2019-03-10 14:37:05 -03:00
|
|
|
#include <iterator>
|
2021-12-22 14:11:13 -03:00
|
|
|
#include <limits>
|
2021-09-18 01:30:30 -03:00
|
|
|
#include <optional>
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 10:11:09 -04:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-09-09 09:24:56 -03:00
|
|
|
/** Used by SanitizeString() */
|
|
|
|
enum SafeChars
|
|
|
|
{
|
|
|
|
SAFE_CHARS_DEFAULT, //!< The full set of allowed chars
|
2017-01-08 17:41:30 -03:00
|
|
|
SAFE_CHARS_UA_COMMENT, //!< BIP-0014 subset
|
|
|
|
SAFE_CHARS_FILENAME, //!< Chars allowed in filenames
|
2018-11-01 13:03:32 -03:00
|
|
|
SAFE_CHARS_URI, //!< Chars allowed in URIs (RFC 3986)
|
2015-09-09 09:24:56 -03:00
|
|
|
};
|
|
|
|
|
2021-11-17 07:47:30 -03:00
|
|
|
/**
|
|
|
|
* Used by ParseByteUnits()
|
|
|
|
* Lowercase base 1000
|
|
|
|
* Uppercase base 1024
|
|
|
|
*/
|
|
|
|
enum class ByteUnit : uint64_t {
|
|
|
|
NOOP = 1ULL,
|
|
|
|
k = 1000ULL,
|
|
|
|
K = 1024ULL,
|
|
|
|
m = 1'000'000ULL,
|
|
|
|
M = 1ULL << 20,
|
|
|
|
g = 1'000'000'000ULL,
|
|
|
|
G = 1ULL << 30,
|
|
|
|
t = 1'000'000'000'000ULL,
|
|
|
|
T = 1ULL << 40,
|
|
|
|
};
|
|
|
|
|
2015-09-09 09:24:56 -03:00
|
|
|
/**
|
|
|
|
* Remove unsafe chars. Safe chars chosen to allow simple messages/URLs/email
|
|
|
|
* addresses, but avoid anything even possibly remotely dangerous like & or >
|
|
|
|
* @param[in] str The string to sanitize
|
|
|
|
* @param[in] rule The set of safe chars to choose (default: least restrictive)
|
|
|
|
* @return A new string without unsafe chars
|
|
|
|
*/
|
2022-04-04 11:23:03 -04:00
|
|
|
std::string SanitizeString(std::string_view str, int rule = SAFE_CHARS_DEFAULT);
|
2022-04-04 11:08:08 -04:00
|
|
|
std::vector<unsigned char> ParseHex(std::string_view str);
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 10:11:09 -04:00
|
|
|
signed char HexDigit(char c);
|
2017-05-07 15:10:19 -03:00
|
|
|
/* Returns true if each character in str is a hex character, and has an even
|
|
|
|
* number of hex digits.*/
|
2022-04-04 11:12:04 -04:00
|
|
|
bool IsHex(std::string_view str);
|
2017-05-07 15:10:19 -03:00
|
|
|
/**
|
|
|
|
* Return true if the string is a hex number, optionally prefixed with "0x"
|
|
|
|
*/
|
2022-04-04 11:20:10 -04:00
|
|
|
bool IsHexNumber(std::string_view str);
|
2022-04-04 14:10:10 -04:00
|
|
|
std::optional<std::vector<unsigned char>> DecodeBase64(std::string_view str);
|
2020-08-07 14:25:42 -04:00
|
|
|
std::string EncodeBase64(Span<const unsigned char> input);
|
2021-11-04 05:03:04 -03:00
|
|
|
inline std::string EncodeBase64(Span<const std::byte> input) { return EncodeBase64(MakeUCharSpan(input)); }
|
2022-04-04 15:05:47 -04:00
|
|
|
inline std::string EncodeBase64(std::string_view str) { return EncodeBase64(MakeUCharSpan(str)); }
|
2022-04-04 14:10:10 -04:00
|
|
|
std::optional<std::vector<unsigned char>> DecodeBase32(std::string_view str);
|
2020-08-27 05:03:21 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base32 encode.
|
|
|
|
* If `pad` is true, then the output will be padded with '=' so that its length
|
|
|
|
* is a multiple of 8.
|
|
|
|
*/
|
|
|
|
std::string EncodeBase32(Span<const unsigned char> input, bool pad = true);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base32 encode.
|
|
|
|
* If `pad` is true, then the output will be padded with '=' so that its length
|
|
|
|
* is a multiple of 8.
|
|
|
|
*/
|
2022-04-04 15:05:47 -04:00
|
|
|
std::string EncodeBase32(std::string_view str, bool pad = true);
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 10:11:09 -04:00
|
|
|
|
2022-04-04 15:05:47 -04:00
|
|
|
void SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut);
|
2021-09-30 11:18:50 -03:00
|
|
|
|
|
|
|
// LocaleIndependentAtoi is provided for backwards compatibility reasons.
|
|
|
|
//
|
2021-10-05 08:34:24 -03:00
|
|
|
// New code should use ToIntegral or the ParseInt* functions
|
2021-09-30 11:18:50 -03:00
|
|
|
// which provide parse error feedback.
|
|
|
|
//
|
2021-12-22 14:11:13 -03:00
|
|
|
// The goal of LocaleIndependentAtoi is to replicate the defined behaviour of
|
|
|
|
// std::atoi as it behaves under the "C" locale, and remove some undefined
|
|
|
|
// behavior. If the parsed value is bigger than the integer type's maximum
|
|
|
|
// value, or smaller than the integer type's minimum value, std::atoi has
|
|
|
|
// undefined behavior, while this function returns the maximum or minimum
|
|
|
|
// values, respectively.
|
2021-09-30 11:18:50 -03:00
|
|
|
template <typename T>
|
2022-04-04 15:05:47 -04:00
|
|
|
T LocaleIndependentAtoi(std::string_view str)
|
2021-09-30 11:18:50 -03:00
|
|
|
{
|
|
|
|
static_assert(std::is_integral<T>::value);
|
|
|
|
T result;
|
|
|
|
// Emulate atoi(...) handling of white space and leading +/-.
|
2022-04-04 15:05:47 -04:00
|
|
|
std::string_view s = TrimStringView(str);
|
2021-09-30 11:18:50 -03:00
|
|
|
if (!s.empty() && s[0] == '+') {
|
|
|
|
if (s.length() >= 2 && s[1] == '-') {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
s = s.substr(1);
|
|
|
|
}
|
|
|
|
auto [_, error_condition] = std::from_chars(s.data(), s.data() + s.size(), result);
|
2021-12-22 14:11:13 -03:00
|
|
|
if (error_condition == std::errc::result_out_of_range) {
|
|
|
|
if (s.length() >= 1 && s[0] == '-') {
|
|
|
|
// Saturate underflow, per strtoll's behavior.
|
|
|
|
return std::numeric_limits<T>::min();
|
|
|
|
} else {
|
|
|
|
// Saturate overflow, per strtoll's behavior.
|
|
|
|
return std::numeric_limits<T>::max();
|
|
|
|
}
|
|
|
|
} else if (error_condition != std::errc{}) {
|
2021-09-30 11:18:50 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 10:11:09 -04:00
|
|
|
|
2018-07-22 15:34:45 -04:00
|
|
|
/**
|
|
|
|
* Tests if the given character is a decimal digit.
|
|
|
|
* @param[in] c character to test
|
|
|
|
* @return true if the argument is a decimal digit; otherwise false.
|
|
|
|
*/
|
|
|
|
constexpr bool IsDigit(char c)
|
|
|
|
{
|
|
|
|
return c >= '0' && c <= '9';
|
|
|
|
}
|
|
|
|
|
2018-10-26 13:54:30 -03:00
|
|
|
/**
|
|
|
|
* Tests if the given character is a whitespace character. The whitespace characters
|
|
|
|
* are: space, form-feed ('\f'), newline ('\n'), carriage return ('\r'), horizontal
|
|
|
|
* tab ('\t'), and vertical tab ('\v').
|
|
|
|
*
|
|
|
|
* This function is locale independent. Under the C locale this function gives the
|
|
|
|
* same result as std::isspace.
|
|
|
|
*
|
|
|
|
* @param[in] c character to test
|
|
|
|
* @return true if the argument is a whitespace character; otherwise false
|
|
|
|
*/
|
|
|
|
constexpr inline bool IsSpace(char c) noexcept {
|
|
|
|
return c == ' ' || c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v';
|
|
|
|
}
|
|
|
|
|
2021-09-18 01:30:30 -03:00
|
|
|
/**
|
2021-10-01 12:33:35 -03:00
|
|
|
* Convert string to integral type T. Leading whitespace, a leading +, or any
|
|
|
|
* trailing character fail the parsing. The required format expressed as regex
|
2021-10-05 08:34:24 -03:00
|
|
|
* is `-?[0-9]+`. The minus sign is only permitted for signed integer types.
|
2021-09-18 01:30:30 -03:00
|
|
|
*
|
|
|
|
* @returns std::nullopt if the entire string could not be parsed, or if the
|
|
|
|
* parsed value is not in the range representable by the type T.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
2022-04-04 15:05:47 -04:00
|
|
|
std::optional<T> ToIntegral(std::string_view str)
|
2021-09-18 01:30:30 -03:00
|
|
|
{
|
|
|
|
static_assert(std::is_integral<T>::value);
|
|
|
|
T result;
|
|
|
|
const auto [first_nonmatching, error_condition] = std::from_chars(str.data(), str.data() + str.size(), result);
|
|
|
|
if (first_nonmatching != str.data() + str.size() || error_condition != std::errc{}) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2021-10-01 12:33:35 -03:00
|
|
|
return result;
|
2021-09-18 01:30:30 -03:00
|
|
|
}
|
|
|
|
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 10:11:09 -04:00
|
|
|
/**
|
|
|
|
* Convert string to signed 32-bit integer with strict parse error feedback.
|
|
|
|
* @returns true if the entire string could be parsed as valid integer,
|
2014-11-17 00:04:01 -03:00
|
|
|
* false if not the entire string could be parsed or when overflow or underflow occurred.
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 10:11:09 -04:00
|
|
|
*/
|
2022-04-04 15:05:47 -04:00
|
|
|
[[nodiscard]] bool ParseInt32(std::string_view str, int32_t *out);
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 10:11:09 -04:00
|
|
|
|
2015-06-04 07:03:09 -03:00
|
|
|
/**
|
|
|
|
* 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 not the entire string could be parsed or when overflow or underflow occurred.
|
|
|
|
*/
|
2022-04-04 15:05:47 -04:00
|
|
|
[[nodiscard]] bool ParseInt64(std::string_view str, int64_t *out);
|
2015-06-04 07:03:09 -03:00
|
|
|
|
2020-08-24 15:34:26 -04:00
|
|
|
/**
|
|
|
|
* Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
|
|
|
|
* @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.
|
|
|
|
*/
|
2022-04-04 15:05:47 -04:00
|
|
|
[[nodiscard]] bool ParseUInt8(std::string_view str, uint8_t *out);
|
2020-08-24 15:34:26 -04:00
|
|
|
|
2021-03-02 18:37:50 -03:00
|
|
|
/**
|
|
|
|
* Convert decimal string to unsigned 16-bit integer with strict parse error feedback.
|
|
|
|
* @returns true if the entire string could be parsed as valid integer,
|
|
|
|
* false if the entire string could not be parsed or if overflow or underflow occurred.
|
|
|
|
*/
|
2022-04-04 15:05:47 -04:00
|
|
|
[[nodiscard]] bool ParseUInt16(std::string_view str, uint16_t* out);
|
2021-03-02 18:37:50 -03:00
|
|
|
|
2016-06-08 04:23:25 -04:00
|
|
|
/**
|
|
|
|
* Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
|
|
|
|
* @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.
|
|
|
|
*/
|
2022-04-04 15:05:47 -04:00
|
|
|
[[nodiscard]] bool ParseUInt32(std::string_view str, uint32_t *out);
|
2016-06-08 04:23:25 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
|
|
|
|
* @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.
|
|
|
|
*/
|
2022-04-04 15:05:47 -04:00
|
|
|
[[nodiscard]] bool ParseUInt64(std::string_view str, uint64_t *out);
|
2016-06-08 04:23:25 -04:00
|
|
|
|
2020-06-24 11:26:47 -04:00
|
|
|
/**
|
|
|
|
* Convert a span of bytes to a lower-case hexadecimal string.
|
|
|
|
*/
|
|
|
|
std::string HexStr(const Span<const uint8_t> s);
|
|
|
|
inline std::string HexStr(const Span<const char> s) { return HexStr(MakeUCharSpan(s)); }
|
2021-11-04 05:03:04 -03:00
|
|
|
inline std::string HexStr(const Span<const std::byte> s) { return HexStr(MakeUCharSpan(s)); }
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 10:11:09 -04:00
|
|
|
|
2015-05-31 10:36:44 -03:00
|
|
|
/**
|
2014-11-17 00:04:01 -03:00
|
|
|
* Format a paragraph of text to a fixed width, adding spaces for
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 10:11:09 -04:00
|
|
|
* indentation to any added line.
|
|
|
|
*/
|
2022-04-04 15:05:47 -04:00
|
|
|
std::string FormatParagraph(std::string_view in, size_t width = 79, size_t indent = 0);
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 10:11:09 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Timing-attack-resistant comparison.
|
|
|
|
* Takes time proportional to length
|
|
|
|
* of first argument.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
bool TimingResistantEqual(const T& a, const T& b)
|
|
|
|
{
|
|
|
|
if (b.size() == 0) return a.size() == 0;
|
|
|
|
size_t accumulator = a.size() ^ b.size();
|
|
|
|
for (size_t i = 0; i < a.size(); i++)
|
2021-12-13 06:04:50 -03:00
|
|
|
accumulator |= size_t(a[i] ^ b[i%b.size()]);
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 10:11:09 -04:00
|
|
|
return accumulator == 0;
|
|
|
|
}
|
|
|
|
|
2015-07-06 05:49:24 -03:00
|
|
|
/** Parse number as fixed point according to JSON number syntax.
|
2020-12-30 23:49:12 -03:00
|
|
|
* See https://json.org/number.gif
|
2015-07-06 05:49:24 -03:00
|
|
|
* @returns true on success, false on error.
|
|
|
|
* @note The result must be in the range (-10^18,10^18), otherwise an overflow error will trigger.
|
|
|
|
*/
|
2022-04-04 15:05:47 -04:00
|
|
|
[[nodiscard]] bool ParseFixedPoint(std::string_view, int decimals, int64_t *amount_out);
|
2015-07-06 05:49:24 -03:00
|
|
|
|
2022-04-04 11:58:54 -04:00
|
|
|
namespace {
|
|
|
|
/** Helper class for the default infn argument to ConvertBits (just returns the input). */
|
|
|
|
struct IntIdentity
|
|
|
|
{
|
|
|
|
[[maybe_unused]] int operator()(int x) const { return x; }
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-08-25 23:55:52 -03:00
|
|
|
/** Convert from one power-of-2 number base to another. */
|
2022-04-04 11:58:54 -04:00
|
|
|
template<int frombits, int tobits, bool pad, typename O, typename It, typename I = IntIdentity>
|
|
|
|
bool ConvertBits(O outfn, It it, It end, I infn = {}) {
|
2017-08-25 23:55:52 -03:00
|
|
|
size_t acc = 0;
|
|
|
|
size_t bits = 0;
|
|
|
|
constexpr size_t maxv = (1 << tobits) - 1;
|
|
|
|
constexpr size_t max_acc = (1 << (frombits + tobits - 1)) - 1;
|
|
|
|
while (it != end) {
|
2022-04-04 11:58:54 -04:00
|
|
|
int v = infn(*it);
|
|
|
|
if (v < 0) return false;
|
|
|
|
acc = ((acc << frombits) | v) & max_acc;
|
2017-08-25 23:55:52 -03:00
|
|
|
bits += frombits;
|
|
|
|
while (bits >= tobits) {
|
|
|
|
bits -= tobits;
|
2017-11-22 22:04:48 -03:00
|
|
|
outfn((acc >> bits) & maxv);
|
2017-08-25 23:55:52 -03:00
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
if (pad) {
|
2017-11-22 22:04:48 -03:00
|
|
|
if (bits) outfn((acc << (tobits - bits)) & maxv);
|
2017-08-25 23:55:52 -03:00
|
|
|
} else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-28 13:42:27 -03:00
|
|
|
/**
|
|
|
|
* Converts the given character to its lowercase equivalent.
|
|
|
|
* This function is locale independent. It only converts uppercase
|
|
|
|
* characters in the standard 7-bit ASCII range.
|
2019-08-07 00:42:54 -04:00
|
|
|
* This is a feature, not a limitation.
|
|
|
|
*
|
2018-08-28 13:42:27 -03:00
|
|
|
* @param[in] c the character to convert to lowercase.
|
|
|
|
* @return the lowercase equivalent of c; or the argument
|
|
|
|
* if no conversion is possible.
|
|
|
|
*/
|
2019-01-09 21:46:32 -03:00
|
|
|
constexpr char ToLower(char c)
|
2018-08-28 13:42:27 -03:00
|
|
|
{
|
|
|
|
return (c >= 'A' && c <= 'Z' ? (c - 'A') + 'a' : c);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-07 00:42:54 -04:00
|
|
|
* Returns the lowercase equivalent of the given string.
|
2018-08-28 13:42:27 -03:00
|
|
|
* This function is locale independent. It only converts uppercase
|
|
|
|
* characters in the standard 7-bit ASCII range.
|
2019-08-07 00:42:54 -04:00
|
|
|
* This is a feature, not a limitation.
|
|
|
|
*
|
|
|
|
* @param[in] str the string to convert to lowercase.
|
|
|
|
* @returns lowercased equivalent of str
|
2018-08-28 13:42:27 -03:00
|
|
|
*/
|
2022-04-04 15:05:47 -04:00
|
|
|
std::string ToLower(std::string_view str);
|
2018-08-28 13:42:27 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts the given character to its uppercase equivalent.
|
|
|
|
* This function is locale independent. It only converts lowercase
|
|
|
|
* characters in the standard 7-bit ASCII range.
|
2019-08-07 00:42:54 -04:00
|
|
|
* This is a feature, not a limitation.
|
|
|
|
*
|
2018-08-28 13:42:27 -03:00
|
|
|
* @param[in] c the character to convert to uppercase.
|
|
|
|
* @return the uppercase equivalent of c; or the argument
|
|
|
|
* if no conversion is possible.
|
|
|
|
*/
|
2019-01-09 21:46:32 -03:00
|
|
|
constexpr char ToUpper(char c)
|
2018-08-28 13:42:27 -03:00
|
|
|
{
|
|
|
|
return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c);
|
|
|
|
}
|
|
|
|
|
2019-08-07 00:42:54 -04:00
|
|
|
/**
|
|
|
|
* Returns the uppercase equivalent of the given string.
|
|
|
|
* This function is locale independent. It only converts lowercase
|
|
|
|
* characters in the standard 7-bit ASCII range.
|
|
|
|
* This is a feature, not a limitation.
|
|
|
|
*
|
|
|
|
* @param[in] str the string to convert to uppercase.
|
|
|
|
* @returns UPPERCASED EQUIVALENT OF str
|
|
|
|
*/
|
2022-04-04 15:05:47 -04:00
|
|
|
std::string ToUpper(std::string_view str);
|
2019-08-07 00:42:54 -04:00
|
|
|
|
2018-08-28 13:42:27 -03:00
|
|
|
/**
|
|
|
|
* Capitalizes the first character of the given string.
|
2019-08-07 00:42:54 -04:00
|
|
|
* This function is locale independent. It only converts lowercase
|
|
|
|
* characters in the standard 7-bit ASCII range.
|
|
|
|
* This is a feature, not a limitation.
|
|
|
|
*
|
2018-08-28 13:42:27 -03:00
|
|
|
* @param[in] str the string to capitalize.
|
2019-08-07 00:42:54 -04:00
|
|
|
* @returns string with the first letter capitalized.
|
2018-08-28 13:42:27 -03:00
|
|
|
*/
|
|
|
|
std::string Capitalize(std::string str);
|
|
|
|
|
2021-11-17 07:47:30 -03:00
|
|
|
/**
|
|
|
|
* Parse a string with suffix unit [k|K|m|M|g|G|t|T].
|
|
|
|
* Must be a whole integer, fractions not allowed (0.5t), no whitespace or +-
|
|
|
|
* Lowercase units are 1000 base. Uppercase units are 1024 base.
|
|
|
|
* Examples: 2m,27M,19g,41T
|
|
|
|
*
|
|
|
|
* @param[in] str the string to convert into bytes
|
|
|
|
* @param[in] default_multiplier if no unit is found in str use this unit
|
|
|
|
* @returns optional uint64_t bytes from str or nullopt
|
|
|
|
* if ToIntegral is false, str is empty, trailing whitespace or overflow
|
|
|
|
*/
|
2022-04-04 15:05:47 -04:00
|
|
|
std::optional<uint64_t> ParseByteUnits(std::string_view str, ByteUnit default_multiplier);
|
2021-11-17 07:47:30 -03:00
|
|
|
|
2018-10-22 19:51:11 -03:00
|
|
|
#endif // BITCOIN_UTIL_STRENCODINGS_H
|