Skip to content

Commit e0cd860

Browse files
committed
update
1 parent d5848ed commit e0cd860

File tree

3 files changed

+22
-42
lines changed

3 files changed

+22
-42
lines changed

include/json/value.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -463,12 +463,10 @@ Json::Value obj_value(Json::objectValue); // {}
463463
///
464464
/// Equivalent to jsonvalue[jsonvalue.size()] = value;
465465
Value& append(const Value& value);
466+
Value& append(Value&& value);
466467
/// \brief Insert value in array at specific index
467468
bool insert(ArrayIndex index, const Value& newValue);
468-
#if JSON_HAS_RVALUE_REFERENCES
469-
Value& append(Value&& value);
470-
bool insert(ArrayIndex index, const Value&& newValue);
471-
#endif
469+
bool insert(ArrayIndex index, Value&& newValue);
472470

473471
/// Access an object value by name, create a null member if it does not exist.
474472
/// \note Because of our implementation, keys are limited to 2^30 -1 chars.

src/lib_json/json_value.cpp

+7-25
Original file line numberDiff line numberDiff line change
@@ -1169,48 +1169,30 @@ Value const& Value::operator[](CppTL::ConstString const& key) const {
11691169
}
11701170
#endif
11711171
Value& Value::append(const Value& value) { return (*this)[size()] = value; }
1172-
/// \brief Insert value in array at specific index
1173-
bool Value::insert(ArrayIndex index, const Value& newValue) {
1174-
if (type() != arrayValue) {
1175-
return false;
1176-
}
1177-
ArrayIndex oldsize = size();
1178-
if (index > oldsize) {
1179-
(*this)[oldsize] = newValue;
1180-
return true;
1181-
} else {
1182-
(*this)[oldsize] = null;
1183-
ArrayIndex length = size();
1184-
for (ArrayIndex i = length - 1; i > index; i--) {
1185-
(*this)[i] = (*this)[i - 1];
1186-
}
1187-
(*this)[index] = newValue;
1188-
return true;
1189-
}
1190-
}
1191-
#if JSON_HAS_RVALUE_REFERENCES
11921172
Value& Value::append(Value&& value) {
11931173
return (*this)[size()] = std::move(value);
11941174
}
1195-
bool Value::insert(ArrayIndex index, const Value&& newValue) {
1175+
/// \brief Insert value in array at specific index
1176+
bool Value::insert(ArrayIndex index, const Value& newValue) {
1177+
return insert(index,Value(newValue));
1178+
}
1179+
bool Value::insert(ArrayIndex index, Value&& newValue) {
11961180
if (type() != arrayValue) {
11971181
return false;
11981182
}
11991183
ArrayIndex oldsize = size();
12001184
if (index > oldsize) {
1201-
append(newValue);
1185+
append(std::move(newValue));
12021186
return true;
12031187
} else {
1204-
(*this)[oldsize] = null;
12051188
ArrayIndex length = size();
1206-
for (ArrayIndex i = length - 1; i > index; i--) {
1189+
for (ArrayIndex i = length; i > index; i--) {
12071190
(*this)[i] = std::move((*this)[i - 1]);
12081191
}
12091192
(*this)[index] = std::move(newValue);
12101193
return true;
12111194
}
12121195
}
1213-
#endif
12141196
Value Value::get(char const* begin,
12151197
char const* end,
12161198
Value const& defaultValue) const {

src/test_lib_json/main.cpp

+13-13
Original file line numberDiff line numberDiff line change
@@ -321,26 +321,26 @@ JSONTEST_FIXTURE(ValueTest, arrayInsertAtRandomIndex) {
321321
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]);
322322
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]);
323323

324-
array.insert(3, "index3"); // rvalue
325-
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]);
326-
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]);
327-
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]);
328-
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[3]);
324+
array.insert(0, "index3"); // rvalue
325+
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]);
326+
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]);
327+
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[2]);
328+
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[3]);
329329

330330
array.insert(3, str1); // lvalue
331-
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]);
332-
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]);
333-
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]);
331+
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]);
332+
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]);
333+
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[2]);
334334
JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[3]);
335-
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[4]);
335+
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[4]);
336336

337337
array.insert(9, "index6");
338338
// beyond size(). it should be allowed to insert into its tail.
339-
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]);
340-
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]);
341-
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]);
339+
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[0]);
340+
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[1]);
341+
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[2]);
342342
JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[3]);
343-
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[4]);
343+
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[4]);
344344
JSONTEST_ASSERT_EQUAL(Json::Value("index6"), array[5]);
345345
}
346346
JSONTEST_FIXTURE(ValueTest, null) {

0 commit comments

Comments
 (0)