mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-22 06:52:36 +01:00
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
This commit is contained in:
parent
c9273f68f6
commit
7d494a48dd
2 changed files with 13 additions and 11 deletions
|
@ -20,6 +20,7 @@
|
||||||
#include <util/string.h>
|
#include <util/string.h>
|
||||||
#include <util/translation.h>
|
#include <util/translation.h>
|
||||||
|
|
||||||
|
#include <string_view>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
|
||||||
const std::string UNIX_EPOCH_TIME = "UNIX epoch time";
|
const std::string UNIX_EPOCH_TIME = "UNIX epoch time";
|
||||||
|
@ -74,29 +75,29 @@ CAmount AmountFromValue(const UniValue& value, int decimals)
|
||||||
return amount;
|
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());
|
const std::string& strHex(v.get_str());
|
||||||
if (64 != strHex.length())
|
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
|
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);
|
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);
|
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;
|
std::string strHex;
|
||||||
if (v.isStr())
|
if (v.isStr())
|
||||||
strHex = v.get_str();
|
strHex = v.get_str();
|
||||||
if (!IsHex(strHex))
|
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);
|
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);
|
return ParseHexV(o.find_value(strKey), strKey);
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <string_view>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <variant>
|
#include <variant>
|
||||||
|
@ -91,10 +92,10 @@ void RPCTypeCheckObj(const UniValue& o,
|
||||||
* Utilities: convert hex-encoded Values
|
* Utilities: convert hex-encoded Values
|
||||||
* (throws error if not hex).
|
* (throws error if not hex).
|
||||||
*/
|
*/
|
||||||
uint256 ParseHashV(const UniValue& v, std::string strName);
|
uint256 ParseHashV(const UniValue& v, std::string_view name);
|
||||||
uint256 ParseHashO(const UniValue& o, std::string strKey);
|
uint256 ParseHashO(const UniValue& o, std::string_view 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::vector<unsigned char> ParseHexO(const UniValue& o, std::string strKey);
|
std::vector<unsigned char> ParseHexO(const UniValue& o, std::string_view strKey);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate and return a CAmount from a UniValue number or string.
|
* Validate and return a CAmount from a UniValue number or string.
|
||||||
|
|
Loading…
Add table
Reference in a new issue