diff options
author | MacroFake <falke.marco@gmail.com> | 2022-07-26 15:24:08 +0200 |
---|---|---|
committer | MacroFake <falke.marco@gmail.com> | 2022-09-05 14:24:14 +0200 |
commit | 1111c7e3f1f5c550f62016d71ccc518aafd97acf (patch) | |
tree | 9e415d99f2ff9070a8c58411456586c6337503e0 /src/univalue | |
parent | e864f2e4afdefd292e2659e4049c001d1140d6af (diff) | |
download | bitcoin-1111c7e3f1f5c550f62016d71ccc518aafd97acf.tar.xz |
univalue: Avoid std::string copies
Diffstat (limited to 'src/univalue')
-rw-r--r-- | src/univalue/include/univalue.h | 9 | ||||
-rw-r--r-- | src/univalue/lib/univalue.cpp | 12 |
2 files changed, 9 insertions, 12 deletions
diff --git a/src/univalue/include/univalue.h b/src/univalue/include/univalue.h index ccaf56a271..6ef60b16e2 100644 --- a/src/univalue/include/univalue.h +++ b/src/univalue/include/univalue.h @@ -20,10 +20,7 @@ public: enum VType { VNULL, VOBJ, VARR, VSTR, VNUM, VBOOL, }; UniValue() { typ = VNULL; } - UniValue(UniValue::VType initialType, const std::string& initialStr = "") { - typ = initialType; - val = initialStr; - } + UniValue(UniValue::VType type, std::string str = {}) : typ{type}, val{std::move(str)} {} template <typename Ref, typename T = std::remove_cv_t<std::remove_reference_t<Ref>>, std::enable_if_t<std::is_floating_point_v<T> || // setFloat std::is_same_v<bool, T> || // setBool @@ -49,12 +46,12 @@ public: void setNull(); void setBool(bool val); - void setNumStr(const std::string& val); + void setNumStr(std::string str); void setInt(uint64_t val); void setInt(int64_t val); void setInt(int val_) { return setInt(int64_t{val_}); } void setFloat(double val); - void setStr(const std::string& val); + void setStr(std::string str); void setArray(); void setObject(); diff --git a/src/univalue/lib/univalue.cpp b/src/univalue/lib/univalue.cpp index 12a434dd0e..d1d4f18d26 100644 --- a/src/univalue/lib/univalue.cpp +++ b/src/univalue/lib/univalue.cpp @@ -44,15 +44,15 @@ static bool validNumStr(const std::string& s) return (tt == JTOK_NUMBER); } -void UniValue::setNumStr(const std::string& val_) +void UniValue::setNumStr(std::string str) { - if (!validNumStr(val_)) { - throw std::runtime_error{"The string '" + val_ + "' is not a valid JSON number"}; + if (!validNumStr(str)) { + throw std::runtime_error{"The string '" + str + "' is not a valid JSON number"}; } clear(); typ = VNUM; - val = val_; + val = std::move(str); } void UniValue::setInt(uint64_t val_) @@ -82,11 +82,11 @@ void UniValue::setFloat(double val_) return setNumStr(oss.str()); } -void UniValue::setStr(const std::string& val_) +void UniValue::setStr(std::string str) { clear(); typ = VSTR; - val = val_; + val = std::move(str); } void UniValue::setArray() |