Skip to content

Commit 7d494a4

Browse files
committed
refactor: use string_view to pass string literals to Parse{Hash,Hex}
as string_view is optimized to be trivially copiable, and in these use cases we only perform read operations on the passed object. 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
1 parent c9273f6 commit 7d494a4

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
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.

0 commit comments

Comments
 (0)