add a new method to insert a new value in an array at specific index. (#949)

* add a new method to insert a new value in an array at specific index.

* update: index > length, return false;

* fix clang-format
This commit is contained in:
dota17
2019-10-16 14:33:53 +08:00
committed by GitHub
parent c5f66ab816
commit bdacfd7bc0
3 changed files with 75 additions and 0 deletions

View File

@@ -1148,6 +1148,7 @@ Value const& Value::operator[](CppTL::ConstString const& key) const {
#endif
Value& Value::append(const Value& value) { return append(Value(value)); }
Value& Value::append(Value&& value) {
JSON_ASSERT_MESSAGE(type() == nullValue || type() == arrayValue,
"in Json::Value::append: requires arrayValue");
@@ -1157,6 +1158,21 @@ Value& Value::append(Value&& value) {
return this->value_.map_->emplace(size(), std::move(value)).first->second;
}
bool Value::insert(ArrayIndex index, Value newValue) {
JSON_ASSERT_MESSAGE(type() == nullValue || type() == arrayValue,
"in Json::Value::insert: requires arrayValue");
ArrayIndex length = size();
if (index > length) {
return false;
} else {
for (ArrayIndex i = length; i > index; i--) {
(*this)[i] = std::move((*this)[i - 1]);
}
(*this)[index] = std::move(newValue);
return true;
}
}
Value Value::get(char const* begin, char const* end,
Value const& defaultValue) const {
Value const* found = find(begin, end);