Skip to content

Commit 59e00c7

Browse files
author
MacroFake
committed
Merge bitcoin/bitcoin#25714: univalue: Avoid std::string copies
fa09525 univalue: string_view test (MacroFake) 1111c7e univalue: Avoid std::string copies (MacroFake) Pull request description: This shouldn't matter too much, unless a really large string is pushed into a json struct, but I think it also clarifies the code. ACKs for top commit: martinus: Code review ACK bitcoin/bitcoin@fa09525 aureleoules: reACK fa09525 ryanofsky: Code review ACK fa09525 Tree-SHA512: 74c441912bd0b00cdb9ea7890121f71ae5d62a7594e7d29aa402c9e3f033710c5d3afb27a37c552e6513804b249aa37e375ce013a3db853a25d1fd7b6e6cd3a8
2 parents 7ef730c + fa09525 commit 59e00c7

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
@@ -25,10 +25,7 @@ class UniValue {
2525
};
2626

2727
UniValue() { typ = VNULL; }
28-
UniValue(UniValue::VType initialType, const std::string& initialStr = "") {
29-
typ = initialType;
30-
val = initialStr;
31-
}
28+
UniValue(UniValue::VType type, std::string str = {}) : typ{type}, val{std::move(str)} {}
3229
template <typename Ref, typename T = std::remove_cv_t<std::remove_reference_t<Ref>>,
3330
std::enable_if_t<std::is_floating_point_v<T> || // setFloat
3431
std::is_same_v<bool, T> || // setBool
@@ -54,12 +51,12 @@ class UniValue {
5451

5552
void setNull();
5653
void setBool(bool val);
57-
void setNumStr(const std::string& val);
54+
void setNumStr(std::string str);
5855
void setInt(uint64_t val);
5956
void setInt(int64_t val);
6057
void setInt(int val_) { return setInt(int64_t{val_}); }
6158
void setFloat(double val);
62-
void setStr(const std::string& val);
59+
void setStr(std::string str);
6360
void setArray();
6461
void setObject();
6562

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)