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 16:11:09 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2023-08-16 19:27:31 +02:00
|
|
|
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
2014-11-17 04:04:01 +01: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 16:11:09 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utilities for converting data from/to strings.
|
|
|
|
*/
|
2021-06-27 08:33:13 +02: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 16:11:09 +02:00
|
|
|
|
2021-03-22 18:10:27 +01:00
|
|
|
#include <attributes.h>
|
2020-06-24 17:26:47 +02:00
|
|
|
#include <span.h>
|
2022-12-20 17:18:10 +01:00
|
|
|
#include <util/string.h>
|
2021-03-22 18:10:27 +01:00
|
|
|
|
2021-09-18 06:30:30 +02:00
|
|
|
#include <charconv>
|
2021-03-22 18:10:27 +01:00
|
|
|
#include <cstdint>
|
2021-09-18 06:30:30 +02: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 16:11:09 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2015-09-09 14:24:56 +02:00
|
|
|
/** Used by SanitizeString() */
|
|
|
|
enum SafeChars
|
|
|
|
{
|
|
|
|
SAFE_CHARS_DEFAULT, //!< The full set of allowed chars
|
2017-03-03 13:37:19 +01:00
|
|
|
SAFE_CHARS_UA_COMMENT, //!< BIP-0014 subset
|
|
|
|
SAFE_CHARS_FILENAME, //!< Chars allowed in filenames
|
2018-11-05 13:40:45 +01:00
|
|
|
SAFE_CHARS_URI, //!< Chars allowed in URIs (RFC 3986)
|
2015-09-09 14:24:56 +02: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
|
|
|
|
*/
|
|
|
|
std::string SanitizeString(const std::string& str, int rule = SAFE_CHARS_DEFAULT);
|
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 16:11:09 +02:00
|
|
|
std::vector<unsigned char> ParseHex(const char* psz);
|
|
|
|
std::vector<unsigned char> ParseHex(const std::string& str);
|
|
|
|
signed char HexDigit(char c);
|
2017-09-06 18:49:43 +02:00
|
|
|
/* Returns true if each character in str is a hex character, and has an even
|
|
|
|
* number of hex digits.*/
|
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 16:11:09 +02:00
|
|
|
bool IsHex(const std::string& str);
|
2017-09-06 18:49:43 +02:00
|
|
|
/**
|
|
|
|
* Return true if the string is a hex number, optionally prefixed with "0x"
|
|
|
|
*/
|
|
|
|
bool IsHexNumber(const std::string& str);
|
2019-01-29 07:44:11 +01:00
|
|
|
std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid = nullptr);
|
|
|
|
std::string DecodeBase64(const std::string& str, bool* pf_invalid = nullptr);
|
2020-08-07 19:55:51 +02:00
|
|
|
std::string EncodeBase64(Span<const unsigned char> input);
|
2021-11-04 09:17:14 +01:00
|
|
|
inline std::string EncodeBase64(Span<const std::byte> input) { return EncodeBase64(MakeUCharSpan(input)); }
|
|
|
|
inline std::string EncodeBase64(const std::string& str) { return EncodeBase64(MakeUCharSpan(str)); }
|
2019-01-29 07:44:11 +01:00
|
|
|
std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid = nullptr);
|
|
|
|
std::string DecodeBase32(const std::string& str, bool* pf_invalid = nullptr);
|
2021-05-25 15:43:13 +02: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.
|
|
|
|
*/
|
|
|
|
std::string EncodeBase32(const std::string& 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 16:11:09 +02:00
|
|
|
|
2021-03-01 21:35:28 +01:00
|
|
|
void SplitHostPort(std::string in, uint16_t &portOut, std::string &hostOut);
|
2022-12-20 17:18:10 +01:00
|
|
|
|
|
|
|
// LocaleIndependentAtoi is provided for backwards compatibility reasons.
|
|
|
|
//
|
|
|
|
// New code should use the ParseInt64/ParseUInt64/ParseInt32/ParseUInt32 functions
|
|
|
|
// which provide parse error feedback.
|
|
|
|
//
|
|
|
|
// The goal of LocaleIndependentAtoi is to replicate the exact defined behaviour
|
|
|
|
// of atoi and atoi64 as they behave under the "C" locale.
|
|
|
|
template <typename T>
|
|
|
|
T LocaleIndependentAtoi(const std::string& str)
|
|
|
|
{
|
|
|
|
static_assert(std::is_integral<T>::value);
|
|
|
|
T result;
|
|
|
|
// Emulate atoi(...) handling of white space and leading +/-.
|
|
|
|
std::string s = TrimString(str);
|
|
|
|
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);
|
|
|
|
if (error_condition != std::errc{}) {
|
|
|
|
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 16:11:09 +02:00
|
|
|
|
2018-07-24 20:49:54 +02: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-28 11:48:46 +01: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 06:30:30 +02:00
|
|
|
/**
|
|
|
|
* Convert string to integral type T.
|
|
|
|
*
|
|
|
|
* @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>
|
|
|
|
std::optional<T> ToIntegral(const std::string& str)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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 16:11:09 +02: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 04:04:01 +01: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 16:11:09 +02:00
|
|
|
*/
|
2021-06-16 10:01:16 +02:00
|
|
|
[[nodiscard]] bool ParseInt32(const std::string& 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 16:11:09 +02:00
|
|
|
|
2015-06-04 12:03:09 +02: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.
|
|
|
|
*/
|
2021-06-16 10:01:16 +02:00
|
|
|
[[nodiscard]] bool ParseInt64(const std::string& str, int64_t *out);
|
2015-06-04 12:03:09 +02:00
|
|
|
|
2020-08-24 21:03:31 +02: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.
|
|
|
|
*/
|
2021-06-16 10:01:16 +02:00
|
|
|
[[nodiscard]] bool ParseUInt8(const std::string& str, uint8_t *out);
|
2020-08-24 21:03:31 +02:00
|
|
|
|
2021-03-01 21:35:28 +01: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.
|
|
|
|
*/
|
|
|
|
[[nodiscard]] bool ParseUInt16(const std::string& str, uint16_t* out);
|
|
|
|
|
2016-06-09 07:37:01 +02: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.
|
|
|
|
*/
|
2021-06-16 10:01:16 +02:00
|
|
|
[[nodiscard]] bool ParseUInt32(const std::string& str, uint32_t *out);
|
2016-06-09 07:37:01 +02: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.
|
|
|
|
*/
|
2021-06-16 10:01:16 +02:00
|
|
|
[[nodiscard]] bool ParseUInt64(const std::string& str, uint64_t *out);
|
2016-06-09 07:37:01 +02:00
|
|
|
|
2015-06-04 12:03:09 +02:00
|
|
|
/**
|
|
|
|
* Convert string to double with strict parse error feedback.
|
|
|
|
* @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.
|
|
|
|
*/
|
2021-06-16 10:01:16 +02:00
|
|
|
[[nodiscard]] bool ParseDouble(const std::string& str, double *out);
|
2015-06-04 12:03:09 +02:00
|
|
|
|
2020-06-24 17:26:47 +02: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 09:17:14 +01: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 16:11:09 +02:00
|
|
|
|
2015-05-31 15:36:44 +02:00
|
|
|
/**
|
2014-11-17 04:04:01 +01: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 16:11:09 +02:00
|
|
|
* indentation to any added line.
|
|
|
|
*/
|
2015-05-31 15:36:44 +02:00
|
|
|
std::string FormatParagraph(const std::string& 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 16:11:09 +02: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++)
|
2023-08-23 21:07:49 +02: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 16:11:09 +02:00
|
|
|
return accumulator == 0;
|
|
|
|
}
|
|
|
|
|
2015-07-06 10:49:24 +02:00
|
|
|
/** Parse number as fixed point according to JSON number syntax.
|
|
|
|
* See http://json.org/number.gif
|
|
|
|
* @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.
|
|
|
|
*/
|
2021-06-16 10:01:16 +02:00
|
|
|
[[nodiscard]] bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out);
|
2015-07-06 10:49:24 +02:00
|
|
|
|
2020-12-25 05:33:37 +01:00
|
|
|
/** Convert from one power-of-2 number base to another. */
|
|
|
|
template<int frombits, int tobits, bool pad, typename O, typename I>
|
2018-03-07 16:28:45 +01:00
|
|
|
bool ConvertBits(const O& outfn, I it, I end) {
|
2020-12-25 05:33:37 +01: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) {
|
|
|
|
acc = ((acc << frombits) | *it) & max_acc;
|
|
|
|
bits += frombits;
|
|
|
|
while (bits >= tobits) {
|
|
|
|
bits -= tobits;
|
2018-03-07 16:28:45 +01:00
|
|
|
outfn((acc >> bits) & maxv);
|
2020-12-25 05:33:37 +01:00
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
if (pad) {
|
2018-03-07 16:28:45 +01:00
|
|
|
if (bits) outfn((acc << (tobits - bits)) & maxv);
|
2020-12-25 05:33:37 +01:00
|
|
|
} else if (bits >= frombits || ((acc << (tobits - bits)) & maxv)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-15 20:35:06 +02: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 06:42:54 +02:00
|
|
|
* This is a feature, not a limitation.
|
|
|
|
*
|
2021-07-15 20:35:06 +02: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-10 18:09:20 +01:00
|
|
|
constexpr char ToLower(char c)
|
2021-07-15 20:35:06 +02:00
|
|
|
{
|
|
|
|
return (c >= 'A' && c <= 'Z' ? (c - 'A') + 'a' : c);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-07 06:42:54 +02:00
|
|
|
* Returns the lowercase equivalent of the given string.
|
2021-07-15 20:35:06 +02:00
|
|
|
* This function is locale independent. It only converts uppercase
|
|
|
|
* characters in the standard 7-bit ASCII range.
|
2019-08-07 06:42:54 +02:00
|
|
|
* This is a feature, not a limitation.
|
|
|
|
*
|
|
|
|
* @param[in] str the string to convert to lowercase.
|
|
|
|
* @returns lowercased equivalent of str
|
2021-07-15 20:35:06 +02:00
|
|
|
*/
|
2019-08-07 06:42:54 +02:00
|
|
|
std::string ToLower(const std::string& str);
|
2021-07-15 20:35:06 +02: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 06:42:54 +02:00
|
|
|
* This is a feature, not a limitation.
|
|
|
|
*
|
2021-07-15 20:35:06 +02: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-10 18:09:20 +01:00
|
|
|
constexpr char ToUpper(char c)
|
2021-07-15 20:35:06 +02:00
|
|
|
{
|
|
|
|
return (c >= 'a' && c <= 'z' ? (c - 'a') + 'A' : c);
|
|
|
|
}
|
|
|
|
|
2019-08-07 06:42:54 +02: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
|
|
|
|
*/
|
|
|
|
std::string ToUpper(const std::string& str);
|
|
|
|
|
2021-07-15 20:35:06 +02:00
|
|
|
/**
|
|
|
|
* Capitalizes the first character of the given string.
|
2019-08-07 06:42:54 +02: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.
|
|
|
|
*
|
2021-07-15 20:35:06 +02:00
|
|
|
* @param[in] str the string to capitalize.
|
2019-08-07 06:42:54 +02:00
|
|
|
* @returns string with the first letter capitalized.
|
2021-07-15 20:35:06 +02:00
|
|
|
*/
|
|
|
|
std::string Capitalize(std::string str);
|
|
|
|
|
2018-08-08 23:27:20 +02:00
|
|
|
/** Parse an HD keypaths like "m/7/0'/2000". */
|
2023-01-31 11:03:42 +01:00
|
|
|
[[nodiscard]] bool ParseHDKeypath(const std::string& keypath_str, std::vector<uint32_t>& keypath);
|
2018-08-08 23:27:20 +02:00
|
|
|
|
2021-06-27 08:33:13 +02:00
|
|
|
#endif // BITCOIN_UTIL_STRENCODINGS_H
|