Skip to content

Commit d5848ed

Browse files
committed
modify insert method
1 parent 589b601 commit d5848ed

File tree

3 files changed

+36
-32
lines changed

3 files changed

+36
-32
lines changed

include/json/value.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ Json::Value obj_value(Json::objectValue); // {}
467467
bool insert(ArrayIndex index, const Value& newValue);
468468
#if JSON_HAS_RVALUE_REFERENCES
469469
Value& append(Value&& value);
470-
bool insert(ArrayIndex index, Value&& newValue);
470+
bool insert(ArrayIndex index, const Value&& newValue);
471471
#endif
472472

473473
/// Access an object value by name, create a null member if it does not exist.

src/lib_json/json_value.cpp

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,48 +1168,47 @@ Value const& Value::operator[](CppTL::ConstString const& key) const {
11681168
return *found;
11691169
}
11701170
#endif
1171-
11721171
Value& Value::append(const Value& value) { return (*this)[size()] = value; }
11731172
/// \brief Insert value in array at specific index
11741173
bool Value::insert(ArrayIndex index, const Value& newValue) {
11751174
if (type() != arrayValue) {
11761175
return false;
11771176
}
1178-
if (!isValidIndex(index)) {
1179-
return false;
1180-
}
11811177
ArrayIndex oldsize = size();
1182-
resize(oldsize + 1);
1183-
ArrayIndex length = size();
1184-
1185-
for (ArrayIndex i = length; i > index; i--) {
1186-
CZString key(i);
1187-
(*value_.map_)[key] = (*this)[i - 1];
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;
11881189
}
1189-
(*this)[index] = newValue;
1190-
return true;
11911190
}
11921191
#if JSON_HAS_RVALUE_REFERENCES
11931192
Value& Value::append(Value&& value) {
11941193
return (*this)[size()] = std::move(value);
11951194
}
1196-
bool Value::insert(ArrayIndex index, Value&& newValue) {
1195+
bool Value::insert(ArrayIndex index, const Value&& newValue) {
11971196
if (type() != arrayValue) {
11981197
return false;
11991198
}
1200-
if (!isValidIndex(index)) {
1201-
return false;
1202-
}
12031199
ArrayIndex oldsize = size();
1204-
resize(oldsize + 1);
1205-
ArrayIndex length = size();
1206-
1207-
for (ArrayIndex i = length; i > index; i--) {
1208-
CZString key(i);
1209-
(*value_.map_)[key] = std::move((*this)[i - 1]);
1200+
if (index > oldsize) {
1201+
append(newValue);
1202+
return true;
1203+
} else {
1204+
(*this)[oldsize] = null;
1205+
ArrayIndex length = size();
1206+
for (ArrayIndex i = length - 1; i > index; i--) {
1207+
(*this)[i] = std::move((*this)[i - 1]);
1208+
}
1209+
(*this)[index] = std::move(newValue);
1210+
return true;
12101211
}
1211-
(*this)[index] = std::move(newValue);
1212-
return true;
12131212
}
12141213
#endif
12151214
Value Value::get(char const* begin,

src/test_lib_json/main.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -312,31 +312,36 @@ JSONTEST_FIXTURE(ValueTest, arrayIssue252) {
312312
}
313313
JSONTEST_FIXTURE(ValueTest, arrayInsertAtRandomIndex) {
314314
Json::Value array;
315-
JSONCPP_STRING str = "index5";
315+
JSONCPP_STRING str0 = "index2";
316+
JSONCPP_STRING str1 = "index4";
316317
array.append("index0");
317318
array.append("index1");
318-
array.append("index2");
319-
array.append("index3");
319+
array.append(str0);
320+
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]);
321+
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]);
322+
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]);
320323

324+
array.insert(3, "index3"); // rvalue
321325
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]);
322326
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]);
323327
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]);
324328
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[3]);
325329

326-
array.insert(3, "index4"); // rvalue
330+
array.insert(3, str1); // lvalue
327331
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]);
328332
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]);
329333
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]);
330334
JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[3]);
331335
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[4]);
332336

333-
array.insert(4, str); // lvalue
337+
array.insert(9, "index6");
338+
// beyond size(). it should be allowed to insert into its tail.
334339
JSONTEST_ASSERT_EQUAL(Json::Value("index0"), array[0]);
335340
JSONTEST_ASSERT_EQUAL(Json::Value("index1"), array[1]);
336341
JSONTEST_ASSERT_EQUAL(Json::Value("index2"), array[2]);
337342
JSONTEST_ASSERT_EQUAL(Json::Value("index4"), array[3]);
338-
JSONTEST_ASSERT_EQUAL(Json::Value("index5"), array[4]);
339-
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[5]);
343+
JSONTEST_ASSERT_EQUAL(Json::Value("index3"), array[4]);
344+
JSONTEST_ASSERT_EQUAL(Json::Value("index6"), array[5]);
340345
}
341346
JSONTEST_FIXTURE(ValueTest, null) {
342347
JSONTEST_ASSERT_EQUAL(Json::nullValue, null_.type());

0 commit comments

Comments
 (0)