Skip to content

Commit 41eb3b0

Browse files
committed
merge bitcoin#25714: Avoid std::string copies
1 parent 6f03a13 commit 41eb3b0

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-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()

src/univalue/test/object.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <memory>
1212
#include <stdexcept>
1313
#include <string>
14+
#include <string_view>
1415
#include <vector>
1516

1617
#define BOOST_CHECK(expr) assert(expr)
@@ -160,6 +161,14 @@ void univalue_set()
160161
BOOST_CHECK(v.isStr());
161162
BOOST_CHECK_EQUAL(v.getValStr(), "zum");
162163

164+
{
165+
std::string_view sv{"ab\0c", 4};
166+
UniValue j{sv};
167+
BOOST_CHECK(j.isStr());
168+
BOOST_CHECK_EQUAL(j.getValStr(), sv);
169+
BOOST_CHECK_EQUAL(j.write(), "\"ab\\u0000c\"");
170+
}
171+
163172
v.setFloat(-1.01);
164173
BOOST_CHECK(v.isNum());
165174
BOOST_CHECK_EQUAL(v.getValStr(), "-1.01");

0 commit comments

Comments
 (0)