Page MenuHomePhabricator

D17151.diff
No OneTemporary

D17151.diff

diff --git a/arcanist/linter/LocaleDependenceLinter.php b/arcanist/linter/LocaleDependenceLinter.php
--- a/arcanist/linter/LocaleDependenceLinter.php
+++ b/arcanist/linter/LocaleDependenceLinter.php
@@ -40,15 +40,11 @@
final class LocaleDependenceLinter extends ArcanistLinter {
const KNOWN_VIOLATIONS = array(
- "src/bitcoin-tx.cpp" => [
- "stoul",
- "trim_right",
- ],
+ "src/bitcoin-tx.cpp" => ["stoul"],
"src/dbwrapper.cpp" => [
"stoul",
"vsnprintf"
],
- "src/httprpc.cpp" => ["trim"],
"src/node/blockstorage.cpp" => ["atoi"],
"src/netbase.cpp" => ["to_lower"],
"src/qt/rpcconsole.cpp" => [
diff --git a/src/bitcoin-tx.cpp b/src/bitcoin-tx.cpp
--- a/src/bitcoin-tx.cpp
+++ b/src/bitcoin-tx.cpp
@@ -30,8 +30,6 @@
#include <univalue.h>
-#include <boost/algorithm/string.hpp> // trim_right
-
#include <cstdio>
#include <functional>
#include <memory>
@@ -801,9 +799,7 @@
throw std::runtime_error("error reading stdin");
}
- boost::algorithm::trim_right(ret);
-
- return ret;
+ return TrimString(ret);
}
static int CommandLineRawTx(int argc, char *argv[],
diff --git a/src/common/configfile.cpp b/src/common/configfile.cpp
--- a/src/common/configfile.cpp
+++ b/src/common/configfile.cpp
@@ -61,8 +61,10 @@
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);
+ 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(
diff --git a/src/httprpc.cpp b/src/httprpc.cpp
--- a/src/httprpc.cpp
+++ b/src/httprpc.cpp
@@ -11,10 +11,11 @@
#include <logging.h>
#include <rpc/protocol.h>
#include <util/strencodings.h>
+#include <util/string.h>
#include <util/translation.h>
#include <walletinitinterface.h>
-#include <boost/algorithm/string.hpp> // boost::trim
+#include <boost/algorithm/string.hpp>
#include <algorithm>
#include <cstdio>
@@ -150,8 +151,8 @@
return false;
}
- std::string strUserPass64 = strAuth.substr(6);
- boost::trim(strUserPass64);
+ std::string_view strUserPass64 =
+ TrimStringView(std::string_view{strAuth}.substr(6));
auto userpass_data = DecodeBase64(strUserPass64);
std::string strUserPass;
if (!userpass_data) {
diff --git a/src/test/fuzz/base_encode_decode.cpp b/src/test/fuzz/base_encode_decode.cpp
--- a/src/test/fuzz/base_encode_decode.cpp
+++ b/src/test/fuzz/base_encode_decode.cpp
@@ -20,7 +20,7 @@
std::vector<uint8_t> 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)));
}
@@ -35,7 +35,7 @@
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)));
}
diff --git a/src/test/fuzz/http_request.cpp b/src/test/fuzz/http_request.cpp
--- a/src/test/fuzz/http_request.cpp
+++ b/src/test/fuzz/http_request.cpp
@@ -61,7 +61,7 @@
// 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()});
+ 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 ||
diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp
--- a/src/test/util_tests.cpp
+++ b/src/test/util_tests.cpp
@@ -228,6 +228,34 @@
"FOO, BAR");
}
+BOOST_AUTO_TEST_CASE(util_TrimString) {
+ BOOST_CHECK_EQUAL(TrimString(" foo bar "), "foo bar");
+ 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(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(TrimStringView("foo bar", "fobar"), " ");
+ BOOST_CHECK_EQUAL(TrimString(std::string("\0 foo \0 ", 8)),
+ std::string("\0 foo \0", 7));
+ 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(
+ 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(
+ TrimStringView(std::string("\x05\x04\x03\x02\x01\x00", 6),
+ std::string("\x05\x04\x03\x02\x01\x00", 6)),
+ "");
+}
+
BOOST_AUTO_TEST_CASE(util_ReplaceAll) {
const std::string original("A test \"%s\" string '%s'.");
auto test_replaceall = [&original](const std::string &search,
@@ -2826,13 +2854,13 @@
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("", ""), "");
}
diff --git a/src/util/strencodings.h b/src/util/strencodings.h
--- a/src/util/strencodings.h
+++ b/src/util/strencodings.h
@@ -55,7 +55,7 @@
inline std::string EncodeBase64(Span<const std::byte> input) {
return EncodeBase64(MakeUCharSpan(input));
}
-inline std::string EncodeBase64(const std::string &str) {
+inline std::string EncodeBase64(std::string_view str) {
return EncodeBase64(MakeUCharSpan(str));
}
std::optional<std::vector<uint8_t>> DecodeBase32(std::string_view str);
@@ -72,9 +72,10 @@
* 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);
int64_t atoi64(const std::string &str);
int atoi(const std::string &str);
@@ -112,7 +113,7 @@
* @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) {
+template <typename T> std::optional<T> ToIntegral(std::string_view str) {
static_assert(std::is_integral<T>::value);
T result;
const auto [first_nonmatching, error_condition] =
@@ -129,14 +130,14 @@
* @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
@@ -145,7 +146,7 @@
* 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
@@ -154,7 +155,7 @@
* 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
@@ -162,7 +163,7 @@
* @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
@@ -170,7 +171,7 @@
* @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.
@@ -187,7 +188,7 @@
* 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,
+std::string FormatParagraph(std::string_view in, size_t width = 79,
size_t indent = 0);
/**
@@ -212,7 +213,7 @@
* @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,
+[[nodiscard]] bool ParseFixedPoint(std::string_view, int decimals,
int64_t *amount_out);
namespace {
@@ -287,7 +288,7 @@
* @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.
@@ -312,7 +313,7 @@
* @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.
diff --git a/src/util/strencodings.cpp b/src/util/strencodings.cpp
--- a/src/util/strencodings.cpp
+++ b/src/util/strencodings.cpp
@@ -100,7 +100,8 @@
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
@@ -194,7 +195,7 @@
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);
}
@@ -247,7 +248,7 @@
}
namespace {
-template <typename T> bool ParseIntegral(const std::string &str, T *out) {
+template <typename T> 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
// handling leading +/- for backwards compatibility.
@@ -266,32 +267,31 @@
}
}; // 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) {
std::stringstream out;
size_t ptr = 0;
size_t indented = 0;
@@ -372,8 +372,7 @@
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;
int mantissa_tzeros = 0;
@@ -493,7 +492,7 @@
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);
@@ -501,7 +500,7 @@
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);
diff --git a/src/util/string.h b/src/util/string.h
--- a/src/util/string.h
+++ b/src/util/string.h
@@ -24,24 +24,34 @@
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());
}
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
*
@@ -62,22 +72,26 @@
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) {
+ std::string_view separator) {
return Join<std::string>(list, separator);
}
/**
* Check if a string does not contain any embedded NUL (\0) characters
*/
-[[nodiscard]] inline bool ValidAsCString(const std::string &str) noexcept {
- return str.size() == strlen(str.c_str());
+[[nodiscard]] inline bool ValidAsCString(std::string_view str) noexcept {
+ for (auto c : str) {
+ if (c == 0) return false;
+ }
+ return true;
}
/**

File Metadata

Mime Type
text/plain
Expires
Sat, Apr 26, 11:18 (6 h, 4 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5573386
Default Alt Text
D17151.diff (17 KB)

Event Timeline