Skip to content

Commit 9b68c9b

Browse files
committed
Merge bitcoin/bitcoin#28172: refactor: use string_view for passing string literals to Parse{Hash,Hex}
bb91131 doc: remove out-of-date external link in src/util/strencodings.h (Jon Atack) 7d494a4 refactor: use string_view to pass string literals to Parse{Hash,Hex} (Jon Atack) Pull request description: as `string_view` is optimized to be trivially copiable, whereas the current code creates a `std::string` copy at each call. These utility methods are called by quite a few RPCs and tests, as well as by each other. ``` $ git grep "ParseHashV\|ParseHashO\|ParseHexV\|ParseHexO" | wc -l 61 ``` Also remove an out-of-date external link. ACKs for top commit: jonatack: Rebased per `git range-diff c9273f6 b94581a bb91131` for an include header from the merge of bitcoin/bitcoin#28230. Should be trivial to re-ACK. maflcko: lgtm ACK bb91131 ns-xvrn: ACK bitcoin/bitcoin@bb91131 achow101: ACK bb91131 brunoerg: crACK bb91131 Tree-SHA512: 9734fe022c9e43fd93c23a917770d332dbbd3132c80a234059714c32faa6469391e59349954749fc86c4ef0b18d5fd99bf8f4b7b82d9f799943799c1253272ae
2 parents 5f88622 + bb91131 commit 9b68c9b

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

src/rpc/util.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <util/string.h>
2121
#include <util/translation.h>
2222

23+
#include <string_view>
2324
#include <tuple>
2425

2526
const std::string UNIX_EPOCH_TIME = "UNIX epoch time";
@@ -74,29 +75,29 @@ CAmount AmountFromValue(const UniValue& value, int decimals)
7475
return amount;
7576
}
7677

77-
uint256 ParseHashV(const UniValue& v, std::string strName)
78+
uint256 ParseHashV(const UniValue& v, std::string_view name)
7879
{
7980
const std::string& strHex(v.get_str());
8081
if (64 != strHex.length())
81-
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be of length %d (not %d, for '%s')", strName, 64, strHex.length(), strHex));
82+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be of length %d (not %d, for '%s')", name, 64, strHex.length(), strHex));
8283
if (!IsHex(strHex)) // Note: IsHex("") is false
83-
throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be hexadecimal string (not '"+strHex+"')");
84+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be hexadecimal string (not '%s')", name, strHex));
8485
return uint256S(strHex);
8586
}
86-
uint256 ParseHashO(const UniValue& o, std::string strKey)
87+
uint256 ParseHashO(const UniValue& o, std::string_view strKey)
8788
{
8889
return ParseHashV(o.find_value(strKey), strKey);
8990
}
90-
std::vector<unsigned char> ParseHexV(const UniValue& v, std::string strName)
91+
std::vector<unsigned char> ParseHexV(const UniValue& v, std::string_view name)
9192
{
9293
std::string strHex;
9394
if (v.isStr())
9495
strHex = v.get_str();
9596
if (!IsHex(strHex))
96-
throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be hexadecimal string (not '"+strHex+"')");
97+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be hexadecimal string (not '%s')", name, strHex));
9798
return ParseHex(strHex);
9899
}
99-
std::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKey)
100+
std::vector<unsigned char> ParseHexO(const UniValue& o, std::string_view strKey)
100101
{
101102
return ParseHexV(o.find_value(strKey), strKey);
102103
}

src/rpc/util.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <map>
2727
#include <optional>
2828
#include <string>
29+
#include <string_view>
2930
#include <type_traits>
3031
#include <utility>
3132
#include <variant>
@@ -91,10 +92,10 @@ void RPCTypeCheckObj(const UniValue& o,
9192
* Utilities: convert hex-encoded Values
9293
* (throws error if not hex).
9394
*/
94-
uint256 ParseHashV(const UniValue& v, std::string strName);
95-
uint256 ParseHashO(const UniValue& o, std::string strKey);
96-
std::vector<unsigned char> ParseHexV(const UniValue& v, std::string strName);
97-
std::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKey);
95+
uint256 ParseHashV(const UniValue& v, std::string_view name);
96+
uint256 ParseHashO(const UniValue& o, std::string_view strKey);
97+
std::vector<unsigned char> ParseHexV(const UniValue& v, std::string_view name);
98+
std::vector<unsigned char> ParseHexO(const UniValue& o, std::string_view strKey);
9899

99100
/**
100101
* Validate and return a CAmount from a UniValue number or string.

src/util/strencodings.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,6 @@ bool TimingResistantEqual(const T& a, const T& b)
260260
}
261261

262262
/** Parse number as fixed point according to JSON number syntax.
263-
* See https://json.org/number.gif
264263
* @returns true on success, false on error.
265264
* @note The result must be in the range (-10^18,10^18), otherwise an overflow error will trigger.
266265
*/

0 commit comments

Comments
 (0)