summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2018-01-18 20:44:43 -0800
committerThiago Macieira <thiago.macieira@intel.com>2018-01-28 22:15:05 +0000
commit678aae2c43726635053fede2a6c3875508cc3599 (patch)
treea2f9bd8494ffa8d7fd4f3100775d95e4e8e993b6
parent48bce2e8f0d787342f3e0f86335460fa25e8ac8f (diff)
QBitArray: add manipulate a dense bit array directly
[ChangeLog][QtCore][QBitArray] Added fromBits(), which creates a QBitArray from a dense bit array, and bits(), which returns that. Change-Id: Ia9c88b83534240a5872dfffd150b1c8b1c36ced5 Reviewed-by: Lars Knoll <lars.knoll@qt.io>
-rw-r--r--src/corelib/tools/qbitarray.cpp40
-rw-r--r--src/corelib/tools/qbitarray.h3
2 files changed, 43 insertions, 0 deletions
diff --git a/src/corelib/tools/qbitarray.cpp b/src/corelib/tools/qbitarray.cpp
index 12e4687b3c..f68a807203 100644
--- a/src/corelib/tools/qbitarray.cpp
+++ b/src/corelib/tools/qbitarray.cpp
@@ -300,6 +300,46 @@ void QBitArray::fill(bool value, int begin, int end)
setBit(begin++, value);
}
+/*!
+ \fn const char *QBitArray::bits() const
+ \since 5.11
+
+ Returns a pointer to a dense bit array for this QBitArray. Bits are counted
+ upwards from the least significant bit in each byte. The the number of bits
+ relevant in the last byte is given by \c{size() % 8}.
+
+ \sa fromBits(), size()
+ */
+
+/*!
+ \since 5.11
+
+ Creates a QBitArray with the dense bit array located at \a data, with \a
+ len bits. The byte array at \a data must be at least \a size / 8 (rounded up)
+ bytes long.
+
+ If \a size is not a multiple of 8, this function will include the lowest
+ \a size % 8 bits from the last byte in \a data.
+
+ \sa bits()
+ */
+QBitArray QBitArray::fromBits(const char *data, qsizetype size)
+{
+ QBitArray result;
+ qsizetype nbytes = (size + 7) / 8;
+
+ result.d = QByteArray(nbytes + 1, Qt::Uninitialized);
+ char *bits = result.d.data();
+ memcpy(bits + 1, data, nbytes);
+
+ // clear any unused bits from the last byte
+ if (size & 7)
+ bits[nbytes] &= 0xffU >> (size & 7);
+
+ *bits = result.d.size() * 8 - size;
+ return result;
+}
+
/*! \fn bool QBitArray::isDetached() const
\internal
diff --git a/src/corelib/tools/qbitarray.h b/src/corelib/tools/qbitarray.h
index 8fa5323127..ff40bf5654 100644
--- a/src/corelib/tools/qbitarray.h
+++ b/src/corelib/tools/qbitarray.h
@@ -104,6 +104,9 @@ public:
inline void truncate(int pos) { if (pos < size()) resize(pos); }
+ const char *bits() const { return isEmpty() ? nullptr : d.constData() + 1; }
+ static QBitArray fromBits(const char *data, qsizetype len);
+
public:
typedef QByteArray::DataPtr DataPtr;
inline DataPtr &data_ptr() { return d.data_ptr(); }