aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Chow <github@achow101.com>2023-11-02 15:37:51 -0400
committerAndrew Chow <github@achow101.com>2023-11-02 15:45:13 -0400
commit9b68c9b85efebfa23daec6471b87e9cbb514a006 (patch)
treeca430cb0545842ca53b7facd3897a3850bfefe52
parent5f886221914fc4cb8a606f1b74aab2cddd8c0c84 (diff)
parentbb91131d545d986aab81c4bb13676c4520169259 (diff)
downloadbitcoin-9b68c9b85efebfa23daec6471b87e9cbb514a006.tar.xz
Merge bitcoin/bitcoin#28172: refactor: use string_view for passing string literals to Parse{Hash,Hex}
bb91131d545d986aab81c4bb13676c4520169259 doc: remove out-of-date external link in src/util/strencodings.h (Jon Atack) 7d494a48ddf4248ef3b1753b6e7f2eeab3a8ecb7 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 https://github.com/bitcoin/bitcoin/pull/28230. Should be trivial to re-ACK. maflcko: lgtm ACK bb91131d545d986aab81c4bb13676c4520169259 ns-xvrn: ACK https://github.com/bitcoin/bitcoin/commit/bb91131d545d986aab81c4bb13676c4520169259 achow101: ACK bb91131d545d986aab81c4bb13676c4520169259 brunoerg: crACK bb91131d545d986aab81c4bb13676c4520169259 Tree-SHA512: 9734fe022c9e43fd93c23a917770d332dbbd3132c80a234059714c32faa6469391e59349954749fc86c4ef0b18d5fd99bf8f4b7b82d9f799943799c1253272ae
-rw-r--r--src/rpc/util.cpp15
-rw-r--r--src/rpc/util.h9
-rw-r--r--src/util/strencodings.h1
3 files changed, 13 insertions, 12 deletions
diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp
index a11366bd47..639237132b 100644
--- a/src/rpc/util.cpp
+++ b/src/rpc/util.cpp
@@ -20,6 +20,7 @@
#include <util/string.h>
#include <util/translation.h>
+#include <string_view>
#include <tuple>
const std::string UNIX_EPOCH_TIME = "UNIX epoch time";
@@ -74,29 +75,29 @@ CAmount AmountFromValue(const UniValue& value, int decimals)
return amount;
}
-uint256 ParseHashV(const UniValue& v, std::string strName)
+uint256 ParseHashV(const UniValue& v, std::string_view name)
{
const std::string& strHex(v.get_str());
if (64 != strHex.length())
- throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be of length %d (not %d, for '%s')", strName, 64, strHex.length(), strHex));
+ throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be of length %d (not %d, for '%s')", name, 64, strHex.length(), strHex));
if (!IsHex(strHex)) // Note: IsHex("") is false
- throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be hexadecimal string (not '"+strHex+"')");
+ throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be hexadecimal string (not '%s')", name, strHex));
return uint256S(strHex);
}
-uint256 ParseHashO(const UniValue& o, std::string strKey)
+uint256 ParseHashO(const UniValue& o, std::string_view strKey)
{
return ParseHashV(o.find_value(strKey), strKey);
}
-std::vector<unsigned char> ParseHexV(const UniValue& v, std::string strName)
+std::vector<unsigned char> ParseHexV(const UniValue& v, std::string_view name)
{
std::string strHex;
if (v.isStr())
strHex = v.get_str();
if (!IsHex(strHex))
- throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be hexadecimal string (not '"+strHex+"')");
+ throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s must be hexadecimal string (not '%s')", name, strHex));
return ParseHex(strHex);
}
-std::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKey)
+std::vector<unsigned char> ParseHexO(const UniValue& o, std::string_view strKey)
{
return ParseHexV(o.find_value(strKey), strKey);
}
diff --git a/src/rpc/util.h b/src/rpc/util.h
index 392540ffad..addf9000d0 100644
--- a/src/rpc/util.h
+++ b/src/rpc/util.h
@@ -26,6 +26,7 @@
#include <map>
#include <optional>
#include <string>
+#include <string_view>
#include <type_traits>
#include <utility>
#include <variant>
@@ -91,10 +92,10 @@ void RPCTypeCheckObj(const UniValue& o,
* Utilities: convert hex-encoded Values
* (throws error if not hex).
*/
-uint256 ParseHashV(const UniValue& v, std::string strName);
-uint256 ParseHashO(const UniValue& o, std::string strKey);
-std::vector<unsigned char> ParseHexV(const UniValue& v, std::string strName);
-std::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKey);
+uint256 ParseHashV(const UniValue& v, std::string_view name);
+uint256 ParseHashO(const UniValue& o, std::string_view strKey);
+std::vector<unsigned char> ParseHexV(const UniValue& v, std::string_view name);
+std::vector<unsigned char> ParseHexO(const UniValue& o, std::string_view strKey);
/**
* Validate and return a CAmount from a UniValue number or string.
diff --git a/src/util/strencodings.h b/src/util/strencodings.h
index d792562735..439678c24a 100644
--- a/src/util/strencodings.h
+++ b/src/util/strencodings.h
@@ -260,7 +260,6 @@ bool TimingResistantEqual(const T& a, const T& b)
}
/** Parse number as fixed point according to JSON number syntax.
- * See https://json.org/number.gif
* @returns true on success, false on error.
* @note The result must be in the range (-10^18,10^18), otherwise an overflow error will trigger.
*/