Skip to content

Commit 6490a3e

Browse files
committed
Merge bitcoin/bitcoin#22859: Replace uses of boost::trim* with locale-independent alternatives (#18130 rebased)
696c76d tests: Add TrimString(...) tests (practicalswift) 4bf18b0 Replace use of boost::trim_right with locale-independent TrimString (Ben Woosley) 9355186 Replace use of boost::trim use with locale-independent TrimString (Ben Woosley) Pull request description: This is [#18130 rebased](bitcoin/bitcoin#18130 (comment)). > `TrimString` is an existing alternative. > Note `TrimString` uses `" \f\n\r\t\v"` as the pattern, which is consistent with the default behavior of `std::isspace`. See: https://en.cppreference.com/w/cpp/string/byte/isspace ACKs for top commit: jb55: utACK 696c76d practicalswift: ACK 696c76d jonatack: ACK 696c76d theStack: Code-review ACK 696c76d Tree-SHA512: 6a70e3777602dfa65a60353e5c6874eb951e4a806844cd4bdaa4237cad980a4f61ec205defc05a29f9707776835975838f6cc635259c42adfe37ceb02ba9358d
2 parents f4e12fd + 696c76d commit 6490a3e

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

src/bitcoin-tx.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -772,9 +772,7 @@ static std::string readStdin()
772772
if (ferror(stdin))
773773
throw std::runtime_error("error reading stdin");
774774

775-
boost::algorithm::trim_right(ret);
776-
777-
return ret;
775+
return TrimString(ret);
778776
}
779777

780778
static int CommandLineRawTx(int argc, char* argv[])

src/httprpc.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <rpc/protocol.h>
1111
#include <rpc/server.h>
1212
#include <util/strencodings.h>
13+
#include <util/string.h>
1314
#include <util/system.h>
1415
#include <util/translation.h>
1516
#include <walletinitinterface.h>
@@ -22,7 +23,7 @@
2223
#include <set>
2324
#include <string>
2425

25-
#include <boost/algorithm/string.hpp> // boost::trim
26+
#include <boost/algorithm/string.hpp>
2627

2728
/** WWW-Authenticate to present with 401 Unauthorized response */
2829
static const char* WWW_AUTH_HEADER_DATA = "Basic realm=\"jsonrpc\"";
@@ -130,8 +131,7 @@ static bool RPCAuthorized(const std::string& strAuth, std::string& strAuthUserna
130131
return false;
131132
if (strAuth.substr(0, 6) != "Basic ")
132133
return false;
133-
std::string strUserPass64 = strAuth.substr(6);
134-
boost::trim(strUserPass64);
134+
std::string strUserPass64 = TrimString(strAuth.substr(6));
135135
std::string strUserPass = DecodeBase64(strUserPass64);
136136

137137
if (strUserPass.find(':') != std::string::npos)

src/test/util_tests.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,22 @@ BOOST_AUTO_TEST_CASE(util_Join)
173173
BOOST_CHECK_EQUAL(Join<std::string>({"foo", "bar"}, ", ", op_upper), "FOO, BAR");
174174
}
175175

176+
BOOST_AUTO_TEST_CASE(util_TrimString)
177+
{
178+
BOOST_CHECK_EQUAL(TrimString(" foo bar "), "foo bar");
179+
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");
180+
BOOST_CHECK_EQUAL(TrimString("\t \n foo \n\tbar\t \n "), "foo \n\tbar");
181+
BOOST_CHECK_EQUAL(TrimString("\t \n foo \n\tbar\t \n ", "fobar"), "\t \n foo \n\tbar\t \n ");
182+
BOOST_CHECK_EQUAL(TrimString("foo bar"), "foo bar");
183+
BOOST_CHECK_EQUAL(TrimString("foo bar", "fobar"), " ");
184+
BOOST_CHECK_EQUAL(TrimString(std::string("\0 foo \0 ", 8)), std::string("\0 foo \0", 7));
185+
BOOST_CHECK_EQUAL(TrimString(std::string(" foo ", 5)), std::string("foo", 3));
186+
BOOST_CHECK_EQUAL(TrimString(std::string("\t\t\0\0\n\n", 6)), std::string("\0\0", 2));
187+
BOOST_CHECK_EQUAL(TrimString(std::string("\x05\x04\x03\x02\x01\x00", 6)), std::string("\x05\x04\x03\x02\x01\x00", 6));
188+
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));
189+
BOOST_CHECK_EQUAL(TrimString(std::string("\x05\x04\x03\x02\x01\x00", 6), std::string("\x05\x04\x03\x02\x01\x00", 6)), "");
190+
}
191+
176192
BOOST_AUTO_TEST_CASE(util_FormatParseISO8601DateTime)
177193
{
178194
BOOST_CHECK_EQUAL(FormatISO8601DateTime(1317425777), "2011-09-30T23:36:17Z");

test/lint/lint-locale-dependence.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ export LC_ALL=C
3939

4040
KNOWN_VIOLATIONS=(
4141
"src/bitcoin-tx.cpp.*stoul"
42-
"src/bitcoin-tx.cpp.*trim_right"
4342
"src/dbwrapper.cpp.*stoul"
4443
"src/dbwrapper.cpp:.*vsnprintf"
45-
"src/httprpc.cpp.*trim"
4644
"src/node/blockstorage.cpp:.*atoi"
4745
"src/qt/rpcconsole.cpp:.*atoi"
4846
"src/rest.cpp:.*strtol"

0 commit comments

Comments
 (0)