Skip to content

Commit e7d2fbd

Browse files
sipaMacroFake
authored andcommitted
Use std::string_view throughout util strencodings/string
1 parent 8ffbd14 commit e7d2fbd

File tree

9 files changed

+69
-56
lines changed

9 files changed

+69
-56
lines changed

src/bitcoin-tx.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ static void MutateTxRBFOptIn(CMutableTransaction& tx, const std::string& strInId
240240
template <typename T>
241241
static T TrimAndParse(const std::string& int_str, const std::string& err)
242242
{
243-
const auto parsed{ToIntegral<T>(TrimString(int_str))};
243+
const auto parsed{ToIntegral<T>(TrimStringView(int_str))};
244244
if (!parsed.has_value()) {
245245
throw std::runtime_error(err + " '" + int_str + "'");
246246
}

src/httprpc.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ static bool RPCAuthorized(const std::string& strAuth, std::string& strAuthUserna
131131
return false;
132132
if (strAuth.substr(0, 6) != "Basic ")
133133
return false;
134-
std::string strUserPass64 = TrimString(strAuth.substr(6));
134+
std::string_view strUserPass64 = TrimStringView(std::string_view{strAuth}.substr(6));
135135
auto userpass_data = DecodeBase64(strUserPass64);
136136
std::string strUserPass;
137137
if (!userpass_data) return false;

src/test/fuzz/base_encode_decode.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ FUZZ_TARGET_INIT(base_encode_decode, initialize_base_encode_decode)
2626
std::vector<unsigned char> decoded;
2727
if (DecodeBase58(random_encoded_string, decoded, 100)) {
2828
const std::string encoded_string = EncodeBase58(decoded);
29-
assert(encoded_string == TrimString(encoded_string));
29+
assert(encoded_string == TrimStringView(encoded_string));
3030
assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
3131
}
3232

@@ -39,7 +39,7 @@ FUZZ_TARGET_INIT(base_encode_decode, initialize_base_encode_decode)
3939
auto result = DecodeBase32(random_encoded_string);
4040
if (result) {
4141
const std::string encoded_string = EncodeBase32(*result);
42-
assert(encoded_string == TrimString(encoded_string));
42+
assert(encoded_string == TrimStringView(encoded_string));
4343
assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
4444
}
4545

src/test/fuzz/http_request.cpp

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

src/test/util_tests.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -226,17 +226,17 @@ BOOST_AUTO_TEST_CASE(util_Join)
226226
BOOST_AUTO_TEST_CASE(util_TrimString)
227227
{
228228
BOOST_CHECK_EQUAL(TrimString(" foo bar "), "foo bar");
229-
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");
229+
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");
230230
BOOST_CHECK_EQUAL(TrimString("\t \n foo \n\tbar\t \n "), "foo \n\tbar");
231-
BOOST_CHECK_EQUAL(TrimString("\t \n foo \n\tbar\t \n ", "fobar"), "\t \n foo \n\tbar\t \n ");
231+
BOOST_CHECK_EQUAL(TrimStringView("\t \n foo \n\tbar\t \n ", "fobar"), "\t \n foo \n\tbar\t \n ");
232232
BOOST_CHECK_EQUAL(TrimString("foo bar"), "foo bar");
233-
BOOST_CHECK_EQUAL(TrimString("foo bar", "fobar"), " ");
233+
BOOST_CHECK_EQUAL(TrimStringView("foo bar", "fobar"), " ");
234234
BOOST_CHECK_EQUAL(TrimString(std::string("\0 foo \0 ", 8)), std::string("\0 foo \0", 7));
235-
BOOST_CHECK_EQUAL(TrimString(std::string(" foo ", 5)), std::string("foo", 3));
235+
BOOST_CHECK_EQUAL(TrimStringView(std::string(" foo ", 5)), std::string("foo", 3));
236236
BOOST_CHECK_EQUAL(TrimString(std::string("\t\t\0\0\n\n", 6)), std::string("\0\0", 2));
237-
BOOST_CHECK_EQUAL(TrimString(std::string("\x05\x04\x03\x02\x01\x00", 6)), std::string("\x05\x04\x03\x02\x01\x00", 6));
237+
BOOST_CHECK_EQUAL(TrimStringView(std::string("\x05\x04\x03\x02\x01\x00", 6)), std::string("\x05\x04\x03\x02\x01\x00", 6));
238238
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));
239-
BOOST_CHECK_EQUAL(TrimString(std::string("\x05\x04\x03\x02\x01\x00", 6), std::string("\x05\x04\x03\x02\x01\x00", 6)), "");
239+
BOOST_CHECK_EQUAL(TrimStringView(std::string("\x05\x04\x03\x02\x01\x00", 6), std::string("\x05\x04\x03\x02\x01\x00", 6)), "");
240240
}
241241

242242
BOOST_AUTO_TEST_CASE(util_FormatParseISO8601DateTime)
@@ -2618,13 +2618,13 @@ BOOST_AUTO_TEST_CASE(message_hash)
26182618
BOOST_AUTO_TEST_CASE(remove_prefix)
26192619
{
26202620
BOOST_CHECK_EQUAL(RemovePrefix("./util/system.h", "./"), "util/system.h");
2621-
BOOST_CHECK_EQUAL(RemovePrefix("foo", "foo"), "");
2621+
BOOST_CHECK_EQUAL(RemovePrefixView("foo", "foo"), "");
26222622
BOOST_CHECK_EQUAL(RemovePrefix("foo", "fo"), "o");
2623-
BOOST_CHECK_EQUAL(RemovePrefix("foo", "f"), "oo");
2623+
BOOST_CHECK_EQUAL(RemovePrefixView("foo", "f"), "oo");
26242624
BOOST_CHECK_EQUAL(RemovePrefix("foo", ""), "foo");
2625-
BOOST_CHECK_EQUAL(RemovePrefix("fo", "foo"), "fo");
2625+
BOOST_CHECK_EQUAL(RemovePrefixView("fo", "foo"), "fo");
26262626
BOOST_CHECK_EQUAL(RemovePrefix("f", "foo"), "f");
2627-
BOOST_CHECK_EQUAL(RemovePrefix("", "foo"), "");
2627+
BOOST_CHECK_EQUAL(RemovePrefixView("", "foo"), "");
26282628
BOOST_CHECK_EQUAL(RemovePrefix("", ""), "");
26292629
}
26302630

src/util/strencodings.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ std::vector<unsigned char> ParseHex(std::string_view str)
9494
return vch;
9595
}
9696

97-
void SplitHostPort(std::string in, uint16_t& portOut, std::string& hostOut)
97+
void SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut)
9898
{
9999
size_t colon = in.find_last_of(':');
100100
// 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)
176176
return str;
177177
}
178178

179-
std::string EncodeBase32(const std::string& str, bool pad)
179+
std::string EncodeBase32(std::string_view str, bool pad)
180180
{
181181
return EncodeBase32(MakeUCharSpan(str), pad);
182182
}
@@ -221,7 +221,7 @@ std::optional<std::vector<unsigned char>> DecodeBase32(std::string_view str)
221221

222222
namespace {
223223
template <typename T>
224-
bool ParseIntegral(const std::string& str, T* out)
224+
bool ParseIntegral(std::string_view str, T* out)
225225
{
226226
static_assert(std::is_integral<T>::value);
227227
// Replicate the exact behavior of strtol/strtoll/strtoul/strtoull when
@@ -240,37 +240,37 @@ bool ParseIntegral(const std::string& str, T* out)
240240
}
241241
}; // namespace
242242

243-
bool ParseInt32(const std::string& str, int32_t* out)
243+
bool ParseInt32(std::string_view str, int32_t* out)
244244
{
245245
return ParseIntegral<int32_t>(str, out);
246246
}
247247

248-
bool ParseInt64(const std::string& str, int64_t* out)
248+
bool ParseInt64(std::string_view str, int64_t* out)
249249
{
250250
return ParseIntegral<int64_t>(str, out);
251251
}
252252

253-
bool ParseUInt8(const std::string& str, uint8_t* out)
253+
bool ParseUInt8(std::string_view str, uint8_t* out)
254254
{
255255
return ParseIntegral<uint8_t>(str, out);
256256
}
257257

258-
bool ParseUInt16(const std::string& str, uint16_t* out)
258+
bool ParseUInt16(std::string_view str, uint16_t* out)
259259
{
260260
return ParseIntegral<uint16_t>(str, out);
261261
}
262262

263-
bool ParseUInt32(const std::string& str, uint32_t* out)
263+
bool ParseUInt32(std::string_view str, uint32_t* out)
264264
{
265265
return ParseIntegral<uint32_t>(str, out);
266266
}
267267

268-
bool ParseUInt64(const std::string& str, uint64_t* out)
268+
bool ParseUInt64(std::string_view str, uint64_t* out)
269269
{
270270
return ParseIntegral<uint64_t>(str, out);
271271
}
272272

273-
std::string FormatParagraph(const std::string& in, size_t width, size_t indent)
273+
std::string FormatParagraph(std::string_view in, size_t width, size_t indent)
274274
{
275275
assert(width >= indent);
276276
std::stringstream out;
@@ -339,7 +339,7 @@ static inline bool ProcessMantissaDigit(char ch, int64_t &mantissa, int &mantiss
339339
return true;
340340
}
341341

342-
bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
342+
bool ParseFixedPoint(std::string_view val, int decimals, int64_t *amount_out)
343343
{
344344
int64_t mantissa = 0;
345345
int64_t exponent = 0;
@@ -431,14 +431,14 @@ bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
431431
return true;
432432
}
433433

434-
std::string ToLower(const std::string& str)
434+
std::string ToLower(std::string_view str)
435435
{
436436
std::string r;
437437
for (auto ch : str) r += ToLower(ch);
438438
return r;
439439
}
440440

441-
std::string ToUpper(const std::string& str)
441+
std::string ToUpper(std::string_view str)
442442
{
443443
std::string r;
444444
for (auto ch : str) r += ToUpper(ch);
@@ -466,7 +466,7 @@ std::string HexStr(const Span<const uint8_t> s)
466466
return rv;
467467
}
468468

469-
std::optional<uint64_t> ParseByteUnits(const std::string& str, ByteUnit default_multiplier)
469+
std::optional<uint64_t> ParseByteUnits(std::string_view str, ByteUnit default_multiplier)
470470
{
471471
if (str.empty()) {
472472
return std::nullopt;

src/util/strencodings.h

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ bool IsHexNumber(std::string_view str);
6767
std::optional<std::vector<unsigned char>> DecodeBase64(std::string_view str);
6868
std::string EncodeBase64(Span<const unsigned char> input);
6969
inline std::string EncodeBase64(Span<const std::byte> input) { return EncodeBase64(MakeUCharSpan(input)); }
70-
inline std::string EncodeBase64(const std::string& str) { return EncodeBase64(MakeUCharSpan(str)); }
70+
inline std::string EncodeBase64(std::string_view str) { return EncodeBase64(MakeUCharSpan(str)); }
7171
std::optional<std::vector<unsigned char>> DecodeBase32(std::string_view str);
7272

7373
/**
@@ -82,9 +82,9 @@ std::string EncodeBase32(Span<const unsigned char> input, bool pad = true);
8282
* If `pad` is true, then the output will be padded with '=' so that its length
8383
* is a multiple of 8.
8484
*/
85-
std::string EncodeBase32(const std::string& str, bool pad = true);
85+
std::string EncodeBase32(std::string_view str, bool pad = true);
8686

87-
void SplitHostPort(std::string in, uint16_t& portOut, std::string& hostOut);
87+
void SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut);
8888

8989
// LocaleIndependentAtoi is provided for backwards compatibility reasons.
9090
//
@@ -98,12 +98,12 @@ void SplitHostPort(std::string in, uint16_t& portOut, std::string& hostOut);
9898
// undefined behavior, while this function returns the maximum or minimum
9999
// values, respectively.
100100
template <typename T>
101-
T LocaleIndependentAtoi(const std::string& str)
101+
T LocaleIndependentAtoi(std::string_view str)
102102
{
103103
static_assert(std::is_integral<T>::value);
104104
T result;
105105
// Emulate atoi(...) handling of white space and leading +/-.
106-
std::string s = TrimString(str);
106+
std::string_view s = TrimStringView(str);
107107
if (!s.empty() && s[0] == '+') {
108108
if (s.length() >= 2 && s[1] == '-') {
109109
return 0;
@@ -159,7 +159,7 @@ constexpr inline bool IsSpace(char c) noexcept {
159159
* parsed value is not in the range representable by the type T.
160160
*/
161161
template <typename T>
162-
std::optional<T> ToIntegral(const std::string& str)
162+
std::optional<T> ToIntegral(std::string_view str)
163163
{
164164
static_assert(std::is_integral<T>::value);
165165
T result;
@@ -175,42 +175,42 @@ std::optional<T> ToIntegral(const std::string& str)
175175
* @returns true if the entire string could be parsed as valid integer,
176176
* false if not the entire string could be parsed or when overflow or underflow occurred.
177177
*/
178-
[[nodiscard]] bool ParseInt32(const std::string& str, int32_t *out);
178+
[[nodiscard]] bool ParseInt32(std::string_view str, int32_t *out);
179179

180180
/**
181181
* Convert string to signed 64-bit integer with strict parse error feedback.
182182
* @returns true if the entire string could be parsed as valid integer,
183183
* false if not the entire string could be parsed or when overflow or underflow occurred.
184184
*/
185-
[[nodiscard]] bool ParseInt64(const std::string& str, int64_t *out);
185+
[[nodiscard]] bool ParseInt64(std::string_view str, int64_t *out);
186186

187187
/**
188188
* Convert decimal string to unsigned 8-bit integer with strict parse error feedback.
189189
* @returns true if the entire string could be parsed as valid integer,
190190
* false if not the entire string could be parsed or when overflow or underflow occurred.
191191
*/
192-
[[nodiscard]] bool ParseUInt8(const std::string& str, uint8_t *out);
192+
[[nodiscard]] bool ParseUInt8(std::string_view str, uint8_t *out);
193193

194194
/**
195195
* Convert decimal string to unsigned 16-bit integer with strict parse error feedback.
196196
* @returns true if the entire string could be parsed as valid integer,
197197
* false if the entire string could not be parsed or if overflow or underflow occurred.
198198
*/
199-
[[nodiscard]] bool ParseUInt16(const std::string& str, uint16_t* out);
199+
[[nodiscard]] bool ParseUInt16(std::string_view str, uint16_t* out);
200200

201201
/**
202202
* Convert decimal string to unsigned 32-bit integer with strict parse error feedback.
203203
* @returns true if the entire string could be parsed as valid integer,
204204
* false if not the entire string could be parsed or when overflow or underflow occurred.
205205
*/
206-
[[nodiscard]] bool ParseUInt32(const std::string& str, uint32_t *out);
206+
[[nodiscard]] bool ParseUInt32(std::string_view str, uint32_t *out);
207207

208208
/**
209209
* Convert decimal string to unsigned 64-bit integer with strict parse error feedback.
210210
* @returns true if the entire string could be parsed as valid integer,
211211
* false if not the entire string could be parsed or when overflow or underflow occurred.
212212
*/
213-
[[nodiscard]] bool ParseUInt64(const std::string& str, uint64_t *out);
213+
[[nodiscard]] bool ParseUInt64(std::string_view str, uint64_t *out);
214214

215215
/**
216216
* 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
223223
* Format a paragraph of text to a fixed width, adding spaces for
224224
* indentation to any added line.
225225
*/
226-
std::string FormatParagraph(const std::string& in, size_t width = 79, size_t indent = 0);
226+
std::string FormatParagraph(std::string_view in, size_t width = 79, size_t indent = 0);
227227

228228
/**
229229
* Timing-attack-resistant comparison.
@@ -245,7 +245,7 @@ bool TimingResistantEqual(const T& a, const T& b)
245245
* @returns true on success, false on error.
246246
* @note The result must be in the range (-10^18,10^18), otherwise an overflow error will trigger.
247247
*/
248-
[[nodiscard]] bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out);
248+
[[nodiscard]] bool ParseFixedPoint(std::string_view, int decimals, int64_t *amount_out);
249249

250250
namespace {
251251
/** Helper class for the default infn argument to ConvertBits (just returns the input). */
@@ -306,7 +306,7 @@ constexpr char ToLower(char c)
306306
* @param[in] str the string to convert to lowercase.
307307
* @returns lowercased equivalent of str
308308
*/
309-
std::string ToLower(const std::string& str);
309+
std::string ToLower(std::string_view str);
310310

311311
/**
312312
* Converts the given character to its uppercase equivalent.
@@ -332,7 +332,7 @@ constexpr char ToUpper(char c)
332332
* @param[in] str the string to convert to uppercase.
333333
* @returns UPPERCASED EQUIVALENT OF str
334334
*/
335-
std::string ToUpper(const std::string& str);
335+
std::string ToUpper(std::string_view str);
336336

337337
/**
338338
* Capitalizes the first character of the given string.
@@ -356,6 +356,6 @@ std::string Capitalize(std::string str);
356356
* @returns optional uint64_t bytes from str or nullopt
357357
* if ToIntegral is false, str is empty, trailing whitespace or overflow
358358
*/
359-
std::optional<uint64_t> ParseByteUnits(const std::string& str, ByteUnit default_multiplier);
359+
std::optional<uint64_t> ParseByteUnits(std::string_view str, ByteUnit default_multiplier);
360360

361361
#endif // BITCOIN_UTIL_STRENCODINGS_H

0 commit comments

Comments
 (0)