Skip to content

Commit

Permalink
Use std::string_view throughout util strencodings/string
Browse files Browse the repository at this point in the history
  • Loading branch information
sipa authored and MacroFake committed Apr 27, 2022
1 parent 8ffbd14 commit e7d2fbd
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/bitcoin-tx.cpp
Expand Up @@ -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 + "'");
}
Expand Down
2 changes: 1 addition & 1 deletion src/httprpc.cpp
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/test/fuzz/base_encode_decode.cpp
Expand Up @@ -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)));
}

Expand All @@ -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)));
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/fuzz/http_request.cpp
Expand Up @@ -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);
Expand Down
20 changes: 10 additions & 10 deletions src/test/util_tests.cpp
Expand Up @@ -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)
Expand Down Expand Up @@ -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("", ""), "");
}

Expand Down
28 changes: 14 additions & 14 deletions src/util/strencodings.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
34 changes: 17 additions & 17 deletions src/util/strencodings.h
Expand Up @@ -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);

/**
Expand All @@ -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.
//
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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). */
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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

0 comments on commit e7d2fbd

Please sign in to comment.