Updated C++ QrSegment's constructor and fields to use BitBuffer instead of vector<uint8_t>.

pull/16/head
Project Nayuki 7 years ago
parent ff0eed8700
commit 4a62fb138a

@ -93,8 +93,7 @@ QrCode QrCode::encodeSegments(const vector<QrSegment> &segs, const Ecc &ecl,
for (const QrSegment &seg : segs) {
bb.appendBits(seg.mode.modeBits, 4);
bb.appendBits(seg.numChars, seg.mode.numCharCountBits(version));
for (int i = 0; i < seg.bitLength; i++)
bb.push_back(((seg.data.at(i >> 3) >> (7 - (i & 7))) & 1) != 0);
bb.insert(bb.end(), seg.data.begin(), seg.data.end());
}
// Add terminator and pad up to a byte if applicable

@ -146,16 +146,11 @@ QrSegment QrSegment::makeEci(long assignVal) {
}
QrSegment::QrSegment(const Mode &md, int numCh, const BitBuffer &data)
: QrSegment(md, numCh, data.getBytes(), data.size()) {}
QrSegment::QrSegment(const Mode &md, int numCh, const vector<uint8_t> &b, int bitLen) :
QrSegment::QrSegment(const Mode &md, int numCh, const BitBuffer &dt) :
mode(md),
numChars(numCh),
data(b),
bitLength(bitLen) {
if (numCh < 0 || bitLen < 0 || b.size() != static_cast<unsigned int>((bitLen + 7) / 8))
data(dt) {
if (numCh < 0)
throw "Invalid value";
}
@ -167,9 +162,9 @@ int QrSegment::getTotalBits(const vector<QrSegment> &segs, int version) {
for (const QrSegment &seg : segs) {
int ccbits = seg.mode.numCharCountBits(version);
// Fail if segment length value doesn't fit in the length field's bit-width
if (seg.numChars >= (1L << ccbits) || seg.bitLength > INT16_MAX)
if (seg.numChars >= (1L << ccbits) || seg.data.size() > INT16_MAX)
return -1;
long temp = (long)result + 4 + ccbits + seg.bitLength;
long temp = (long)result + 4 + ccbits + seg.data.size();
if (temp > INT_MAX)
return -1;
result = temp;

@ -137,11 +137,8 @@ class QrSegment final {
/* The length of this segment's unencoded data, measured in characters. Always zero or positive. */
public: const int numChars;
/* The bits of this segment packed into a byte array in big endian. */
public: const std::vector<std::uint8_t> data;
/* The length of this segment's encoded data, measured in bits. Satisfies ceil(bitLength / 8) = data.size(). */
public: const int bitLength;
/* The data bits of this segment. */
public: const BitBuffer data;
/*---- Constructor ----*/
@ -149,10 +146,7 @@ class QrSegment final {
/*
* Creates a new QR Code data segment with the given parameters and data.
*/
public: QrSegment(const Mode &md, int numCh, const BitBuffer &data);
public: QrSegment(const Mode &md, int numCh, const std::vector<std::uint8_t> &b, int bitLen);
public: QrSegment(const Mode &md, int numCh, const BitBuffer &dt);
// Package-private helper function.

Loading…
Cancel
Save