0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-03-06 14:19:59 -05:00

Use std::string_view throughout util strencodings/string

This commit is contained in:
Pieter Wuille 2022-04-04 15:05:47 -04:00 committed by MacroFake
parent 8ffbd1412d
commit e7d2fbda63
9 changed files with 69 additions and 56 deletions

View file

@ -240,7 +240,7 @@ static void MutateTxRBFOptIn(CMutableTransaction& tx, const std::string& strInId
template <typename T>
static T TrimAndParse(const std::string& int_str, const std::string& err)
{
const auto parsed{ToIntegral<T>(TrimString(int_str))};
const auto parsed{ToIntegral<T>(TrimStringView(int_str))};
if (!parsed.has_value()) {
throw std::runtime_error(err + " '" + int_str + "'");
}

View file

@ -131,7 +131,7 @@ static bool RPCAuthorized(const std::string& strAuth, std::string& strAuthUserna
return false;
if (strAuth.substr(0, 6) != "Basic ")
return false;
std::string strUserPass64 = TrimString(strAuth.substr(6));
std::string_view strUserPass64 = TrimStringView(std::string_view{strAuth}.substr(6));
auto userpass_data = DecodeBase64(strUserPass64);
std::string strUserPass;
if (!userpass_data) return false;

View file

@ -26,7 +26,7 @@ FUZZ_TARGET_INIT(base_encode_decode, initialize_base_encode_decode)
std::vector<unsigned char> decoded;
if (DecodeBase58(random_encoded_string, decoded, 100)) {
const std::string encoded_string = EncodeBase58(decoded);
assert(encoded_string == TrimString(encoded_string));
assert(encoded_string == TrimStringView(encoded_string));
assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
}
@ -39,7 +39,7 @@ FUZZ_TARGET_INIT(base_encode_decode, initialize_base_encode_decode)
auto result = DecodeBase32(random_encoded_string);
if (result) {
const std::string encoded_string = EncodeBase32(*result);
assert(encoded_string == TrimString(encoded_string));
assert(encoded_string == TrimStringView(encoded_string));
assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
}

View file

@ -39,7 +39,7 @@ FUZZ_TARGET(http_request)
// and is a consequence of our hacky but necessary use of the internal function evhttp_parse_firstline_ in
// this fuzzing harness. The workaround is not aesthetically pleasing, but it successfully avoids the troublesome
// code path. " http:// HTTP/1.1\n" was a crashing input prior to this workaround.
const std::string http_buffer_str = ToLower({http_buffer.begin(), http_buffer.end()});
const std::string http_buffer_str = ToLower(std::string{http_buffer.begin(), http_buffer.end()});
if (http_buffer_str.find(" http://") != std::string::npos || http_buffer_str.find(" https://") != std::string::npos ||
evhttp_parse_firstline_(evreq, evbuf) != 1 || evhttp_parse_headers_(evreq, evbuf) != 1) {
evbuffer_free(evbuf);

View file

@ -226,17 +226,17 @@ BOOST_AUTO_TEST_CASE(util_Join)
BOOST_AUTO_TEST_CASE(util_TrimString)
{
BOOST_CHECK_EQUAL(TrimString(" foo bar "), "foo bar");
BOOST_CHECK_EQUAL(TrimString("\t \n \n \f\n\r\t\v\tfoo \n \f\n\r\t\v\tbar\t \n \f\n\r\t\v\t\n "), "foo \n \f\n\r\t\v\tbar");
BOOST_CHECK_EQUAL(TrimStringView("\t \n \n \f\n\r\t\v\tfoo \n \f\n\r\t\v\tbar\t \n \f\n\r\t\v\t\n "), "foo \n \f\n\r\t\v\tbar");
BOOST_CHECK_EQUAL(TrimString("\t \n foo \n\tbar\t \n "), "foo \n\tbar");
BOOST_CHECK_EQUAL(TrimString("\t \n foo \n\tbar\t \n ", "fobar"), "\t \n foo \n\tbar\t \n ");
BOOST_CHECK_EQUAL(TrimStringView("\t \n foo \n\tbar\t \n ", "fobar"), "\t \n foo \n\tbar\t \n ");
BOOST_CHECK_EQUAL(TrimString("foo bar"), "foo bar");
BOOST_CHECK_EQUAL(TrimString("foo bar", "fobar"), " ");
BOOST_CHECK_EQUAL(TrimStringView("foo bar", "fobar"), " ");
BOOST_CHECK_EQUAL(TrimString(std::string("\0 foo \0 ", 8)), std::string("\0 foo \0", 7));
BOOST_CHECK_EQUAL(TrimString(std::string(" foo ", 5)), std::string("foo", 3));
BOOST_CHECK_EQUAL(TrimStringView(std::string(" foo ", 5)), std::string("foo", 3));
BOOST_CHECK_EQUAL(TrimString(std::string("\t\t\0\0\n\n", 6)), std::string("\0\0", 2));
BOOST_CHECK_EQUAL(TrimString(std::string("\x05\x04\x03\x02\x01\x00", 6)), std::string("\x05\x04\x03\x02\x01\x00", 6));
BOOST_CHECK_EQUAL(TrimStringView(std::string("\x05\x04\x03\x02\x01\x00", 6)), std::string("\x05\x04\x03\x02\x01\x00", 6));
BOOST_CHECK_EQUAL(TrimString(std::string("\x05\x04\x03\x02\x01\x00", 6), std::string("\x05\x04\x03\x02\x01", 5)), std::string("\0", 1));
BOOST_CHECK_EQUAL(TrimString(std::string("\x05\x04\x03\x02\x01\x00", 6), std::string("\x05\x04\x03\x02\x01\x00", 6)), "");
BOOST_CHECK_EQUAL(TrimStringView(std::string("\x05\x04\x03\x02\x01\x00", 6), std::string("\x05\x04\x03\x02\x01\x00", 6)), "");
}
BOOST_AUTO_TEST_CASE(util_FormatParseISO8601DateTime)
@ -2618,13 +2618,13 @@ BOOST_AUTO_TEST_CASE(message_hash)
BOOST_AUTO_TEST_CASE(remove_prefix)
{
BOOST_CHECK_EQUAL(RemovePrefix("./util/system.h", "./"), "util/system.h");
BOOST_CHECK_EQUAL(RemovePrefix("foo", "foo"), "");
BOOST_CHECK_EQUAL(RemovePrefixView("foo", "foo"), "");
BOOST_CHECK_EQUAL(RemovePrefix("foo", "fo"), "o");
BOOST_CHECK_EQUAL(RemovePrefix("foo", "f"), "oo");
BOOST_CHECK_EQUAL(RemovePrefixView("foo", "f"), "oo");
BOOST_CHECK_EQUAL(RemovePrefix("foo", ""), "foo");
BOOST_CHECK_EQUAL(RemovePrefix("fo", "foo"), "fo");
BOOST_CHECK_EQUAL(RemovePrefixView("fo", "foo"), "fo");
BOOST_CHECK_EQUAL(RemovePrefix("f", "foo"), "f");
BOOST_CHECK_EQUAL(RemovePrefix("", "foo"), "");
BOOST_CHECK_EQUAL(RemovePrefixView("", "foo"), "");
BOOST_CHECK_EQUAL(RemovePrefix("", ""), "");
}

View file

@ -94,7 +94,7 @@ std::vector<unsigned char> ParseHex(std::string_view str)
return vch;
}
void SplitHostPort(std::string in, uint16_t& portOut, std::string& hostOut)
void SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut)
{
size_t colon = in.find_last_of(':');
// if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator
@ -176,7 +176,7 @@ std::string EncodeBase32(Span<const unsigned char> input, bool pad)
return str;
}
std::string EncodeBase32(const std::string& str, bool pad)
std::string EncodeBase32(std::string_view str, bool pad)
{
return EncodeBase32(MakeUCharSpan(str), pad);
}
@ -221,7 +221,7 @@ std::optional<std::vector<unsigned char>> DecodeBase32(std::string_view str)
namespace {
template <typename T>
bool ParseIntegral(const std::string& str, T* out)
bool ParseIntegral(std::string_view str, T* out)
{
static_assert(std::is_integral<T>::value);
// Replicate the exact behavior of strtol/strtoll/strtoul/strtoull when
@ -240,37 +240,37 @@ bool ParseIntegral(const std::string& str, T* out)
}
}; // namespace
bool ParseInt32(const std::string& str, int32_t* out)
bool ParseInt32(std::string_view str, int32_t* out)
{
return ParseIntegral<int32_t>(str, out);
}
bool ParseInt64(const std::string& str, int64_t* out)
bool ParseInt64(std::string_view str, int64_t* out)
{
return ParseIntegral<int64_t>(str, out);
}
bool ParseUInt8(const std::string& str, uint8_t* out)
bool ParseUInt8(std::string_view str, uint8_t* out)
{
return ParseIntegral<uint8_t>(str, out);
}
bool ParseUInt16(const std::string& str, uint16_t* out)
bool ParseUInt16(std::string_view str, uint16_t* out)
{
return ParseIntegral<uint16_t>(str, out);
}
bool ParseUInt32(const std::string& str, uint32_t* out)
bool ParseUInt32(std::string_view str, uint32_t* out)
{
return ParseIntegral<uint32_t>(str, out);
}
bool ParseUInt64(const std::string& str, uint64_t* out)
bool ParseUInt64(std::string_view str, uint64_t* out)
{
return ParseIntegral<uint64_t>(str, out);
}
std::string FormatParagraph(const std::string& in, size_t width, size_t indent)
std::string FormatParagraph(std::string_view in, size_t width, size_t indent)
{
assert(width >= indent);
std::stringstream out;
@ -339,7 +339,7 @@ static inline bool ProcessMantissaDigit(char ch, int64_t &mantissa, int &mantiss
return true;
}
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
bool ParseFixedPoint(std::string_view val, int decimals, int64_t *amount_out)
{
int64_t mantissa = 0;
int64_t exponent = 0;
@ -431,14 +431,14 @@ bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
return true;
}
std::string ToLower(const std::string& str)
std::string ToLower(std::string_view str)
{
std::string r;
for (auto ch : str) r += ToLower(ch);
return r;
}
std::string ToUpper(const std::string& str)
std::string ToUpper(std::string_view str)
{
std::string r;
for (auto ch : str) r += ToUpper(ch);
@ -466,7 +466,7 @@ std::string HexStr(const Span<const uint8_t> s)
return rv;
}
std::optional<uint64_t> ParseByteUnits(const std::string& str, ByteUnit default_multiplier)
std::optional<uint64_t> ParseByteUnits(std::string_view str, ByteUnit default_multiplier)
{
if (str.empty()) {
return std::nullopt;

View file

@ -67,7 +67,7 @@ bool IsHexNumber(std::string_view str);
std::optional<std::vector<unsigned char>> DecodeBase64(std::string_view str);
std::string EncodeBase64(Span<const unsigned char> input);
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)); }
inline std::string EncodeBase64(std::string_view str) { return EncodeBase64(MakeUCharSpan(str)); }
std::optional<std::vector<unsigned char>> DecodeBase32(std::string_view str);
/**
@ -82,9 +82,9 @@ std::string EncodeBase32(Span<const unsigned char> input, bool pad = true);
* 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);
std::string EncodeBase32(std::string_view str, bool pad = true);
void SplitHostPort(std::string in, uint16_t& portOut, std::string& hostOut);
void SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut);
// LocaleIndependentAtoi is provided for backwards compatibility reasons.
//
@ -98,12 +98,12 @@ void SplitHostPort(std::string in, uint16_t& portOut, std::string& hostOut);
// undefined behavior, while this function returns the maximum or minimum
// values, respectively.
template <typename T>
T LocaleIndependentAtoi(const std::string& str)
T LocaleIndependentAtoi(std::string_view str)
{
static_assert(std::is_integral<T>::value);
T result;
// Emulate atoi(...) handling of white space and leading +/-.
std::string s = TrimString(str);
std::string_view s = TrimStringView(str);
if (!s.empty() && s[0] == '+') {
if (s.length() >= 2 && s[1] == '-') {
return 0;
@ -159,7 +159,7 @@ constexpr inline bool IsSpace(char c) noexcept {
* parsed value is not in the range representable by the type T.
*/
template <typename T>
std::optional<T> ToIntegral(const std::string& str)
std::optional<T> ToIntegral(std::string_view str)
{
static_assert(std::is_integral<T>::value);
T result;
@ -175,42 +175,42 @@ std::optional<T> ToIntegral(const std::string& str)
* @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.
*/
[[nodiscard]] bool ParseInt32(const std::string& str, int32_t *out);
[[nodiscard]] bool ParseInt32(std::string_view str, int32_t *out);
/**
* 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.
*/
[[nodiscard]] bool ParseInt64(const std::string& str, int64_t *out);
[[nodiscard]] bool ParseInt64(std::string_view str, int64_t *out);
/**
* 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.
*/
[[nodiscard]] bool ParseUInt8(const std::string& str, uint8_t *out);
[[nodiscard]] bool ParseUInt8(std::string_view str, uint8_t *out);
/**
* 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);
[[nodiscard]] bool ParseUInt16(std::string_view str, uint16_t* out);
/**
* 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.
*/
[[nodiscard]] bool ParseUInt32(const std::string& str, uint32_t *out);
[[nodiscard]] bool ParseUInt32(std::string_view str, uint32_t *out);
/**
* 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.
*/
[[nodiscard]] bool ParseUInt64(const std::string& str, uint64_t *out);
[[nodiscard]] bool ParseUInt64(std::string_view str, uint64_t *out);
/**
* Convert a span of bytes to a lower-case hexadecimal string.
@ -223,7 +223,7 @@ inline std::string HexStr(const Span<const std::byte> s) { return HexStr(MakeUCh
* Format a paragraph of text to a fixed width, adding spaces for
* indentation to any added line.
*/
std::string FormatParagraph(const std::string& in, size_t width = 79, size_t indent = 0);
std::string FormatParagraph(std::string_view in, size_t width = 79, size_t indent = 0);
/**
* Timing-attack-resistant comparison.
@ -245,7 +245,7 @@ bool TimingResistantEqual(const T& a, const T& b)
* @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.
*/
[[nodiscard]] bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out);
[[nodiscard]] bool ParseFixedPoint(std::string_view, int decimals, int64_t *amount_out);
namespace {
/** Helper class for the default infn argument to ConvertBits (just returns the input). */
@ -306,7 +306,7 @@ constexpr char ToLower(char c)
* @param[in] str the string to convert to lowercase.
* @returns lowercased equivalent of str
*/
std::string ToLower(const std::string& str);
std::string ToLower(std::string_view str);
/**
* Converts the given character to its uppercase equivalent.
@ -332,7 +332,7 @@ constexpr char ToUpper(char c)
* @param[in] str the string to convert to uppercase.
* @returns UPPERCASED EQUIVALENT OF str
*/
std::string ToUpper(const std::string& str);
std::string ToUpper(std::string_view str);
/**
* Capitalizes the first character of the given string.
@ -356,6 +356,6 @@ std::string Capitalize(std::string str);
* @returns optional uint64_t bytes from str or nullopt
* if ToIntegral is false, str is empty, trailing whitespace or overflow
*/
std::optional<uint64_t> ParseByteUnits(const std::string& str, ByteUnit default_multiplier);
std::optional<uint64_t> ParseByteUnits(std::string_view str, ByteUnit default_multiplier);
#endif // BITCOIN_UTIL_STRENCODINGS_H

View file

@ -21,17 +21,22 @@
return spanparsing::Split<std::string>(str, sep);
}
[[nodiscard]] inline std::string TrimString(const std::string& str, const std::string& pattern = " \f\n\r\t\v")
[[nodiscard]] inline std::string_view TrimStringView(std::string_view str, std::string_view pattern = " \f\n\r\t\v")
{
std::string::size_type front = str.find_first_not_of(pattern);
if (front == std::string::npos) {
return std::string();
return {};
}
std::string::size_type end = str.find_last_not_of(pattern);
return str.substr(front, end - front + 1);
}
[[nodiscard]] inline std::string RemovePrefix(const std::string& str, const std::string& prefix)
[[nodiscard]] inline std::string TrimString(std::string_view str, std::string_view pattern = " \f\n\r\t\v")
{
return std::string(TrimStringView(str, pattern));
}
[[nodiscard]] inline std::string_view RemovePrefixView(std::string_view str, std::string_view prefix)
{
if (str.substr(0, prefix.size()) == prefix) {
return str.substr(prefix.size());
@ -39,6 +44,11 @@
return str;
}
[[nodiscard]] inline std::string RemovePrefix(std::string_view str, std::string_view prefix)
{
return std::string(RemovePrefixView(str, prefix));
}
/**
* Join a list of items
*
@ -58,14 +68,14 @@ auto Join(const std::vector<T>& list, const BaseType& separator, UnaryOp unary_o
return ret;
}
template <typename T>
T Join(const std::vector<T>& list, const T& separator)
template <typename T, typename T2>
T Join(const std::vector<T>& list, const T2& separator)
{
return Join(list, separator, [](const T& i) { return i; });
}
// Explicit overload needed for c_str arguments, which would otherwise cause a substitution failure in the template above.
inline std::string Join(const std::vector<std::string>& list, const std::string& separator)
inline std::string Join(const std::vector<std::string>& list, std::string_view separator)
{
return Join<std::string>(list, separator);
}
@ -81,9 +91,12 @@ inline std::string MakeUnorderedList(const std::vector<std::string>& items)
/**
* Check if a string does not contain any embedded NUL (\0) characters
*/
[[nodiscard]] inline bool ValidAsCString(const std::string& str) noexcept
[[nodiscard]] inline bool ValidAsCString(std::string_view str) noexcept
{
return str.size() == strlen(str.c_str());
for (auto c : str) {
if (c == 0) return false;
}
return true;
}
/**

View file

@ -853,8 +853,8 @@ static bool GetConfigOptions(std::istream& stream, const std::string& filepath,
error = strprintf("parse error on line %i: %s, options in configuration file must be specified without leading -", linenr, str);
return false;
} else if ((pos = str.find('=')) != std::string::npos) {
std::string name = prefix + TrimString(str.substr(0, pos), pattern);
std::string value = TrimString(str.substr(pos + 1), pattern);
std::string name = prefix + TrimString(std::string_view{str}.substr(0, pos), pattern);
std::string_view value = TrimStringView(std::string_view{str}.substr(pos + 1), pattern);
if (used_hash && name.find("rpcpassword") != std::string::npos) {
error = strprintf("parse error on line %i, using # in rpcpassword can be ambiguous and should be avoided", linenr);
return false;