diff options
Diffstat (limited to 'fxbarcode/qrcode/BC_QRCoderBitVector.cpp')
-rw-r--r-- | fxbarcode/qrcode/BC_QRCoderBitVector.cpp | 110 |
1 files changed, 44 insertions, 66 deletions
diff --git a/fxbarcode/qrcode/BC_QRCoderBitVector.cpp b/fxbarcode/qrcode/BC_QRCoderBitVector.cpp index 2e3c341792..7a41ad0910 100644 --- a/fxbarcode/qrcode/BC_QRCoderBitVector.cpp +++ b/fxbarcode/qrcode/BC_QRCoderBitVector.cpp @@ -20,45 +20,34 @@ * limitations under the License. */ -#include "core/fxcrt/fx_memory.h" #include "fxbarcode/qrcode/BC_QRCoderBitVector.h" + +#include "core/fxcrt/fx_memory.h" #include "fxbarcode/utils.h" -CBC_QRCoderBitVector::CBC_QRCoderBitVector() { - m_sizeInBits = 0; - m_size = 32; -} -void CBC_QRCoderBitVector::Init() { - m_array = FX_Alloc(uint8_t, m_size); -} -CBC_QRCoderBitVector::~CBC_QRCoderBitVector() { - FX_Free(m_array); -} -void CBC_QRCoderBitVector::Clear() { - FX_Free(m_array); - m_sizeInBits = 0; - m_size = 32; - m_array = FX_Alloc(uint8_t, m_size); -} -int32_t CBC_QRCoderBitVector::At(int32_t index, int32_t& e) { - if (index < 0 || index >= m_sizeInBits) { +CBC_QRCoderBitVector::CBC_QRCoderBitVector() {} + +CBC_QRCoderBitVector::~CBC_QRCoderBitVector() {} + +int32_t CBC_QRCoderBitVector::At(size_t index, int32_t& e) const { + if (index >= m_sizeInBits) { e = BCExceptionBadIndexException; return 0; } int32_t value = m_array[index >> 3] & 0xff; return (value >> (7 - (index & 0x7))) & 1; } -int32_t CBC_QRCoderBitVector::sizeInBytes() { + +size_t CBC_QRCoderBitVector::sizeInBytes() const { return (m_sizeInBits + 7) >> 3; } -int32_t CBC_QRCoderBitVector::Size() { + +size_t CBC_QRCoderBitVector::Size() const { return m_sizeInBits; } -void CBC_QRCoderBitVector::AppendBit(int32_t bit, int32_t& e) { - if (!(bit == 0 || bit == 1)) { - e = BCExceptionBadValueException; - return; - } + +void CBC_QRCoderBitVector::AppendBit(int32_t bit) { + ASSERT(bit == 0 || bit == 1); int32_t numBitsInLastByte = m_sizeInBits & 0x7; if (numBitsInLastByte == 0) { AppendByte(0); @@ -67,61 +56,50 @@ void CBC_QRCoderBitVector::AppendBit(int32_t bit, int32_t& e) { m_array[m_sizeInBits >> 3] |= (bit << (7 - numBitsInLastByte)); ++m_sizeInBits; } -void CBC_QRCoderBitVector::AppendBits(int32_t value, - int32_t numBits, - int32_t& e) { - if (numBits < 0 || numBits > 32) { - e = BCExceptionBadNumBitsException; - return; - } + +void CBC_QRCoderBitVector::AppendBits(int32_t value, int32_t numBits) { + ASSERT(numBits > 0); + ASSERT(numBits <= 32); + int32_t numBitsLeft = numBits; while (numBitsLeft > 0) { if ((m_sizeInBits & 0x7) == 0 && numBitsLeft >= 8) { - int32_t newByte = (value >> (numBitsLeft - 8)) & 0xff; - AppendByte(newByte); + AppendByte(static_cast<int8_t>((value >> (numBitsLeft - 8)) & 0xff)); numBitsLeft -= 8; } else { - int32_t bit = (value >> (numBitsLeft - 1)) & 1; - AppendBit(bit, e); - if (e != BCExceptionNO) - return; + AppendBit((value >> (numBitsLeft - 1)) & 1); --numBitsLeft; } } } -void CBC_QRCoderBitVector::AppendBitVector(CBC_QRCoderBitVector* bits, - int32_t& e) { + +void CBC_QRCoderBitVector::AppendBitVector(CBC_QRCoderBitVector* bits) { int32_t size = bits->Size(); for (int32_t i = 0; i < size; i++) { + int e = BCExceptionNO; int32_t num = bits->At(i, e); - if (e != BCExceptionNO) - return; - AppendBit(num, e); - if (e != BCExceptionNO) - return; + ASSERT(e == BCExceptionNO); + AppendBit(num); } } -void CBC_QRCoderBitVector::XOR(CBC_QRCoderBitVector* other, int32_t& e) { - if (m_sizeInBits != other->Size()) { - e = BCExceptioncanNotOperatexorOperator; - return; - } - int32_t sizeInBytes = (m_sizeInBits + 7) >> 3; - for (int32_t i = 0; i < sizeInBytes; ++i) { - m_array[i] ^= (other->GetArray())[i]; - } + +bool CBC_QRCoderBitVector::XOR(const CBC_QRCoderBitVector* other) { + if (m_sizeInBits != other->Size()) + return false; + + const auto* pOther = other->GetArray(); + for (size_t i = 0; i < sizeInBytes(); ++i) + m_array[i] ^= pOther[i]; + return true; } -uint8_t* CBC_QRCoderBitVector::GetArray() { - return m_array; + +const uint8_t* CBC_QRCoderBitVector::GetArray() const { + return m_array.data(); } -void CBC_QRCoderBitVector::AppendByte(int32_t value) { - if ((m_sizeInBits >> 3) == m_size) { - uint8_t* newArray = FX_Alloc(uint8_t, m_size << 1); - memcpy(newArray, m_array, m_size); - FX_Free(m_array); - m_array = newArray; - m_size = m_size << 1; - } - m_array[m_sizeInBits >> 3] = (uint8_t)value; + +void CBC_QRCoderBitVector::AppendByte(int8_t value) { + if ((m_sizeInBits >> 3) == m_array.size()) + m_array.push_back(0); + m_array[m_sizeInBits >> 3] = value; m_sizeInBits += 8; } |