diff --git a/src/corelib/tools/qbitarray.cpp b/src/corelib/tools/qbitarray.cpp index f0b81cce66..4db0f61599 100644 --- a/src/corelib/tools/qbitarray.cpp +++ b/src/corelib/tools/qbitarray.cpp @@ -342,6 +342,40 @@ QBitArray QBitArray::fromBits(const char *data, qsizetype size) return result; } +/*! + \since 5.13 + + Returns the array of bit converted to an int. The conversion is based of endianness value. + Converts up to the first 32 bits of the array to \c uint32_t and returns it, + obeying \a endianness. If the array has more than 32 bits, \a ok is set to false + and this function returns zero; otherwise, it's set to true. +*/ +quint32 QBitArray::toUInt32(QSysInfo::Endian endianness, bool *ok) const noexcept +{ + if (size() > 32) { + if (ok != nullptr) { + *ok = false; + } + + return 0; + } + + if (ok != nullptr) { + *ok = true; + } + + auto factor = 1; + quint32 total = 0; + for (auto i = 0; i < size(); ++i, factor *= 2) { + const auto index = endianness == QSysInfo::Endian::LittleEndian ? i : (size() - i - 1); + if (testBit(index)) { + total += factor; + } + } + + return total; +} + /*! \fn bool QBitArray::isDetached() const \internal diff --git a/src/corelib/tools/qbitarray.h b/src/corelib/tools/qbitarray.h index 4ea613a442..ac3a8771d9 100644 --- a/src/corelib/tools/qbitarray.h +++ b/src/corelib/tools/qbitarray.h @@ -105,6 +105,8 @@ public: const char *bits() const { return isEmpty() ? nullptr : d.constData() + 1; } static QBitArray fromBits(const char *data, qsizetype len); + quint32 toUInt32(QSysInfo::Endian endianness, bool *ok = nullptr) const noexcept; + public: typedef QByteArray::DataPointer DataPtr; inline DataPtr &data_ptr() { return d.data_ptr(); } diff --git a/tests/auto/corelib/tools/qbitarray/tst_qbitarray.cpp b/tests/auto/corelib/tools/qbitarray/tst_qbitarray.cpp index 9a7c099228..b2c7915aa0 100644 --- a/tests/auto/corelib/tools/qbitarray/tst_qbitarray.cpp +++ b/tests/auto/corelib/tools/qbitarray/tst_qbitarray.cpp @@ -86,6 +86,9 @@ private slots: void resize(); void fromBits_data(); void fromBits(); + + void toUInt32_data(); + void toUInt32(); }; void tst_QBitArray::size_data() @@ -667,5 +670,95 @@ void tst_QBitArray::fromBits() QCOMPARE(QBitArray::fromBits(fromBits.bits(), fromBits.size()), expected); } +void tst_QBitArray::toUInt32_data() +{ + QTest::addColumn("data"); + QTest::addColumn("endianness"); + QTest::addColumn("check"); + QTest::addColumn("result"); + + QTest::newRow("ctor") << QBitArray() + << static_cast(QSysInfo::Endian::LittleEndian) + << true + << quint32(0); + + QTest::newRow("empty") << QBitArray(0) + << static_cast(QSysInfo::Endian::LittleEndian) + << true + << quint32(0); + + QTest::newRow("LittleEndian4") << QStringToQBitArray(QString("0111")) + << static_cast(QSysInfo::Endian::LittleEndian) + << true + << quint32(14); + + QTest::newRow("BigEndian4") << QStringToQBitArray(QString("0111")) + << static_cast(QSysInfo::Endian::BigEndian) + << true + << quint32(7); + + QTest::newRow("LittleEndian8") << QStringToQBitArray(QString("01111111")) + << static_cast(QSysInfo::Endian::LittleEndian) + << true + << quint32(254); + + QTest::newRow("BigEndian8") << QStringToQBitArray(QString("01111111")) + << static_cast(QSysInfo::Endian::BigEndian) + << true + << quint32(127); + + QTest::newRow("LittleEndian16") << QStringToQBitArray(QString("0111111111111111")) + << static_cast(QSysInfo::Endian::LittleEndian) + << true + << quint32(65534); + + QTest::newRow("BigEndian16") << QStringToQBitArray(QString("0111111111111111")) + << static_cast(QSysInfo::Endian::BigEndian) + << true + << quint32(32767); + + QTest::newRow("LittleEndian31") << QBitArray(31, true) + << static_cast(QSysInfo::Endian::LittleEndian) + << true + << quint32(2147483647); + + QTest::newRow("BigEndian31") << QBitArray(31, true) + << static_cast(QSysInfo::Endian::BigEndian) + << true + << quint32(2147483647); + + QTest::newRow("LittleEndian32") << QBitArray(32, true) + << static_cast(QSysInfo::Endian::LittleEndian) + << true + << quint32(4294967295); + + QTest::newRow("BigEndian32") << QBitArray(32, true) + << static_cast(QSysInfo::Endian::BigEndian) + << true + << quint32(4294967295); + + QTest::newRow("LittleEndian33") << QBitArray(33, true) + << static_cast(QSysInfo::Endian::LittleEndian) + << false + << quint32(0); + + QTest::newRow("BigEndian33") << QBitArray(33, true) + << static_cast(QSysInfo::Endian::BigEndian) + << false + << quint32(0); +} + +void tst_QBitArray::toUInt32() +{ + QFETCH(QBitArray, data); + QFETCH(int, endianness); + QFETCH(bool, check); + QFETCH(quint32, result); + bool ok = false; + + QCOMPARE(data.toUInt32(static_cast(endianness), &ok), result); + QCOMPARE(ok, check); +} + QTEST_APPLESS_MAIN(tst_QBitArray) #include "tst_qbitarray.moc"