QBitArray: Add method to get int value
It is useful to use an array of bit as an integer value. I add also a prarameter to set endianness when converting value to UInt32. [ChangeLog][QtCore][QBitArray] Added toUInt32() to return the bit array's value as a uint32_t. Change-Id: I9d8c7a33f11e7ce94cb67aa9a50b11fa42d56168 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
62c3dd5632
commit
a0eb51c387
@ -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
|
||||
|
@ -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(); }
|
||||
|
@ -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<QBitArray>("data");
|
||||
QTest::addColumn<int>("endianness");
|
||||
QTest::addColumn<bool>("check");
|
||||
QTest::addColumn<quint32>("result");
|
||||
|
||||
QTest::newRow("ctor") << QBitArray()
|
||||
<< static_cast<int>(QSysInfo::Endian::LittleEndian)
|
||||
<< true
|
||||
<< quint32(0);
|
||||
|
||||
QTest::newRow("empty") << QBitArray(0)
|
||||
<< static_cast<int>(QSysInfo::Endian::LittleEndian)
|
||||
<< true
|
||||
<< quint32(0);
|
||||
|
||||
QTest::newRow("LittleEndian4") << QStringToQBitArray(QString("0111"))
|
||||
<< static_cast<int>(QSysInfo::Endian::LittleEndian)
|
||||
<< true
|
||||
<< quint32(14);
|
||||
|
||||
QTest::newRow("BigEndian4") << QStringToQBitArray(QString("0111"))
|
||||
<< static_cast<int>(QSysInfo::Endian::BigEndian)
|
||||
<< true
|
||||
<< quint32(7);
|
||||
|
||||
QTest::newRow("LittleEndian8") << QStringToQBitArray(QString("01111111"))
|
||||
<< static_cast<int>(QSysInfo::Endian::LittleEndian)
|
||||
<< true
|
||||
<< quint32(254);
|
||||
|
||||
QTest::newRow("BigEndian8") << QStringToQBitArray(QString("01111111"))
|
||||
<< static_cast<int>(QSysInfo::Endian::BigEndian)
|
||||
<< true
|
||||
<< quint32(127);
|
||||
|
||||
QTest::newRow("LittleEndian16") << QStringToQBitArray(QString("0111111111111111"))
|
||||
<< static_cast<int>(QSysInfo::Endian::LittleEndian)
|
||||
<< true
|
||||
<< quint32(65534);
|
||||
|
||||
QTest::newRow("BigEndian16") << QStringToQBitArray(QString("0111111111111111"))
|
||||
<< static_cast<int>(QSysInfo::Endian::BigEndian)
|
||||
<< true
|
||||
<< quint32(32767);
|
||||
|
||||
QTest::newRow("LittleEndian31") << QBitArray(31, true)
|
||||
<< static_cast<int>(QSysInfo::Endian::LittleEndian)
|
||||
<< true
|
||||
<< quint32(2147483647);
|
||||
|
||||
QTest::newRow("BigEndian31") << QBitArray(31, true)
|
||||
<< static_cast<int>(QSysInfo::Endian::BigEndian)
|
||||
<< true
|
||||
<< quint32(2147483647);
|
||||
|
||||
QTest::newRow("LittleEndian32") << QBitArray(32, true)
|
||||
<< static_cast<int>(QSysInfo::Endian::LittleEndian)
|
||||
<< true
|
||||
<< quint32(4294967295);
|
||||
|
||||
QTest::newRow("BigEndian32") << QBitArray(32, true)
|
||||
<< static_cast<int>(QSysInfo::Endian::BigEndian)
|
||||
<< true
|
||||
<< quint32(4294967295);
|
||||
|
||||
QTest::newRow("LittleEndian33") << QBitArray(33, true)
|
||||
<< static_cast<int>(QSysInfo::Endian::LittleEndian)
|
||||
<< false
|
||||
<< quint32(0);
|
||||
|
||||
QTest::newRow("BigEndian33") << QBitArray(33, true)
|
||||
<< static_cast<int>(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<QSysInfo::Endian>(endianness), &ok), result);
|
||||
QCOMPARE(ok, check);
|
||||
}
|
||||
|
||||
QTEST_APPLESS_MAIN(tst_QBitArray)
|
||||
#include "tst_qbitarray.moc"
|
||||
|
Loading…
Reference in New Issue
Block a user