diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2012-01-09 12:57:01 +0100 |
---|---|---|
committer | Lars Knoll <lars.knoll@nokia.com> | 2012-01-09 13:41:30 +0100 |
commit | 81dc96b485fe093c0fc9c6abf1be6da85d5b38a8 (patch) | |
tree | e78d307584c3b72b33ec73a326af544b3f2619b4 | |
parent | ea327b79730279db89aeefd5eb1dc0ce0ffa1423 (diff) |
Implement QJsonArray::replace(int, QJsonValue)
Change-Id: If5c49e063223ac82800d041e72438ff884d7942e
Sanity-Review: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Simon Hausmann <simon.hausmann@nokia.com>
-rw-r--r-- | src/qjsonarray.cpp | 26 | ||||
-rw-r--r-- | src/qjsonarray.h | 3 | ||||
-rw-r--r-- | tests/auto/tst_qtjson.cpp | 6 |
3 files changed, 33 insertions, 2 deletions
diff --git a/src/qjsonarray.cpp b/src/qjsonarray.cpp index 9e25107..f413c41 100644 --- a/src/qjsonarray.cpp +++ b/src/qjsonarray.cpp @@ -210,6 +210,32 @@ void QJsonArray::insert(int i, const QJsonValue &value) value.copyData((char *)a + valueOffset, compressed); } +void QJsonArray::replace(int i, const QJsonValue &value) +{ + Q_ASSERT (a && i >= 0 && i < (int)(a->length)); + + bool compressed; + int valueSize = value.requiredStorage(&compressed); + + detach(valueSize); + + if (!a->length) + a->tableOffset = sizeof(Private::Array); + + int valueOffset = a->reserveSpace(valueSize, i, 0); + Private::Value &v = (*a)[i]; + v.type = (value.t == QJsonValue::Undefined ? QJsonValue::Null : value.t); + v.latinOrIntValue = compressed; + v.latinKey = false; + v.val = value.valueToStore(valueOffset); + if (valueSize) + value.copyData((char *)a + valueOffset, compressed); + + ++d->compactionCounter; + if (d->compactionCounter > 32 && d->compactionCounter >= (int)a->length/2) + compact(); +} + bool QJsonArray::contains(const QJsonValue &element) const { for (int i = 0; i < size(); i++) { diff --git a/src/qjsonarray.h b/src/qjsonarray.h index e094207..6e52a6b 100644 --- a/src/qjsonarray.h +++ b/src/qjsonarray.h @@ -76,8 +76,7 @@ public: QJsonValue takeAt(int i); void insert(int i, const QJsonValue &value); - - // ### void replace(int i, const QJsonValue &value); + void replace(int i, const QJsonValue &value); bool contains(const QJsonValue &element) const; // ### JsonValueRef &operator[](int i); diff --git a/tests/auto/tst_qtjson.cpp b/tests/auto/tst_qtjson.cpp index f2e40c5..bccbcf9 100644 --- a/tests/auto/tst_qtjson.cpp +++ b/tests/auto/tst_qtjson.cpp @@ -313,6 +313,12 @@ void TestQtJson::testArraySimple() QCOMPARE(array.at(-1), QJsonValue(QJsonValue::Undefined)); QCOMPARE(array.at(array.size()), QJsonValue(QJsonValue::Undefined)); + + array.replace(0, -555); + QCOMPARE(array.first().type(), QJsonValue::Number); + QCOMPARE(array.first(), QJsonValue(-555)); + QCOMPARE(array.at(1).type(), QJsonValue::String); + QCOMPARE(array.at(1), QJsonValue(QLatin1String("test"))); } void TestQtJson::testValueObject() |