From ff7f09d18708119bebb94202d3a3dcb26149ee5a Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 20 Sep 2018 13:36:05 +0200 Subject: Change QCborArray to pad with invalid on inserting past end Likewise have mutating operator[] insert an invalid entry at its target index, if beyond the end of the array. This makes it possible to fill an array from high index to low, for example. Change-Id: If71699c20e2623142214ce2c11c4d6e4a120c989 Reviewed-by: Thiago Macieira --- src/corelib/serialization/qcborvalue.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/corelib/serialization/qcborvalue.cpp') diff --git a/src/corelib/serialization/qcborvalue.cpp b/src/corelib/serialization/qcborvalue.cpp index 077a4754dc..81ea1ed3b2 100644 --- a/src/corelib/serialization/qcborvalue.cpp +++ b/src/corelib/serialization/qcborvalue.cpp @@ -859,6 +859,23 @@ QCborContainerPrivate *QCborContainerPrivate::detach(QCborContainerPrivate *d, q return d; } +/*! + Prepare for an insertion at position \a index + + Detaches and ensures there are at least index entries in the array, padding + with Undefined as needed. +*/ +QCborContainerPrivate *QCborContainerPrivate::grow(QCborContainerPrivate *d, qsizetype index) +{ + Q_ASSERT(index >= 0); + d = detach(d, index + 1); + Q_ASSERT(d); + int j = d->elements.size(); + while (j < index) + d->append(Undefined()); + return d; +} + // Copies or moves \a value into element at position \a e. If \a disp is // CopyContainer, then this function increases the reference count of the // container, but otherwise leaves it unmodified. If \a disp is MoveContainer, -- cgit v1.2.3