Skip to content

Commit 1111c7e

Browse files
author
MacroFake
committed
univalue: Avoid std::string copies
1 parent e864f2e commit 1111c7e

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

src/univalue/include/univalue.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ class UniValue {
2020
enum VType { VNULL, VOBJ, VARR, VSTR, VNUM, VBOOL, };
2121

2222
UniValue() { typ = VNULL; }
23-
UniValue(UniValue::VType initialType, const std::string& initialStr = "") {
24-
typ = initialType;
25-
val = initialStr;
26-
}
23+
UniValue(UniValue::VType type, std::string str = {}) : typ{type}, val{std::move(str)} {}
2724
template <typename Ref, typename T = std::remove_cv_t<std::remove_reference_t<Ref>>,
2825
std::enable_if_t<std::is_floating_point_v<T> || // setFloat
2926
std::is_same_v<bool, T> || // setBool
@@ -49,12 +46,12 @@ class UniValue {
4946

5047
void setNull();
5148
void setBool(bool val);
52-
void setNumStr(const std::string& val);
49+
void setNumStr(std::string str);
5350
void setInt(uint64_t val);
5451
void setInt(int64_t val);
5552
void setInt(int val_) { return setInt(int64_t{val_}); }
5653
void setFloat(double val);
57-
void setStr(const std::string& val);
54+
void setStr(std::string str);
5855
void setArray();
5956
void setObject();
6057

src/univalue/lib/univalue.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,15 @@ static bool validNumStr(const std::string& s)
4444
return (tt == JTOK_NUMBER);
4545
}
4646

47-
void UniValue::setNumStr(const std::string& val_)
47+
void UniValue::setNumStr(std::string str)
4848
{
49-
if (!validNumStr(val_)) {
50-
throw std::runtime_error{"The string '" + val_ + "' is not a valid JSON number"};
49+
if (!validNumStr(str)) {
50+
throw std::runtime_error{"The string '" + str + "' is not a valid JSON number"};
5151
}
5252

5353
clear();
5454
typ = VNUM;
55-
val = val_;
55+
val = std::move(str);
5656
}
5757

5858
void UniValue::setInt(uint64_t val_)
@@ -82,11 +82,11 @@ void UniValue::setFloat(double val_)
8282
return setNumStr(oss.str());
8383
}
8484

85-
void UniValue::setStr(const std::string& val_)
85+
void UniValue::setStr(std::string str)
8686
{
8787
clear();
8888
typ = VSTR;
89-
val = val_;
89+
val = std::move(str);
9090
}
9191

9292
void UniValue::setArray()

0 commit comments

Comments
 (0)