Simplified and tweaked various small pieces of C++ code.

pull/16/head
Project Nayuki 7 years ago
parent 5279a4c88b
commit 9f2c8d9e96

@ -21,7 +21,6 @@
* Software. * Software.
*/ */
#include <climits>
#include "BitBuffer.hpp" #include "BitBuffer.hpp"

@ -55,8 +55,7 @@ QrCode QrCode::encodeText(const char *text, const Ecc &ecl) {
QrCode QrCode::encodeBinary(const vector<uint8_t> &data, const Ecc &ecl) { QrCode QrCode::encodeBinary(const vector<uint8_t> &data, const Ecc &ecl) {
vector<QrSegment> segs; vector<QrSegment> segs{QrSegment::makeBytes(data)};
segs.push_back(QrSegment::makeBytes(data));
return encodeSegments(segs, ecl); return encodeSegments(segs, ecl);
} }
@ -97,7 +96,7 @@ QrCode QrCode::encodeSegments(const vector<QrSegment> &segs, const Ecc &ecl,
} }
// Add terminator and pad up to a byte if applicable // Add terminator and pad up to a byte if applicable
bb.appendBits(0, std::min(static_cast<size_t>(4), dataCapacityBits - bb.size())); bb.appendBits(0, std::min<size_t>(4, dataCapacityBits - bb.size()));
bb.appendBits(0, (8 - bb.size() % 8) % 8); bb.appendBits(0, (8 - bb.size() % 8) % 8);
// Pad with alternate bytes until data capacity is reached // Pad with alternate bytes until data capacity is reached
@ -323,14 +322,13 @@ vector<uint8_t> QrCode::appendErrorCorrection(const vector<uint8_t> &data) const
vector<vector<uint8_t> > blocks; vector<vector<uint8_t> > blocks;
const ReedSolomonGenerator rs(blockEccLen); const ReedSolomonGenerator rs(blockEccLen);
for (int i = 0, k = 0; i < numBlocks; i++) { for (int i = 0, k = 0; i < numBlocks; i++) {
vector<uint8_t> dat; vector<uint8_t> dat(data.cbegin() + k, data.cbegin() + (k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1)));
dat.insert(dat.begin(), data.begin() + k, data.begin() + (k + shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1)));
k += dat.size(); k += dat.size();
const vector<uint8_t> ecc(rs.getRemainder(dat)); const vector<uint8_t> ecc(rs.getRemainder(dat));
if (i < numShortBlocks) if (i < numShortBlocks)
dat.push_back(0); dat.push_back(0);
dat.insert(dat.end(), ecc.begin(), ecc.end()); dat.insert(dat.end(), ecc.cbegin(), ecc.cend());
blocks.push_back(dat); blocks.push_back(std::move(dat));
} }
// Interleave (not concatenate) the bytes from every block into a single sequence // Interleave (not concatenate) the bytes from every block into a single sequence
@ -487,9 +485,9 @@ long QrCode::getPenaltyScore() const {
// Balance of black and white modules // Balance of black and white modules
int black = 0; int black = 0;
for (int y = 0; y < size; y++) { for (const vector<bool> &row : modules) {
for (int x = 0; x < size; x++) { for (bool color : row) {
if (module(x, y)) if (color)
black++; black++;
} }
} }

@ -110,8 +110,8 @@ class QrCode final {
private: int mask; private: int mask;
// Private grids of modules/pixels (conceptually immutable) // Private grids of modules/pixels (conceptually immutable)
private: std::vector<std::vector<bool>> modules; // The modules of this QR Code symbol (false = white, true = black) private: std::vector<std::vector<bool> > modules; // The modules of this QR Code symbol (false = white, true = black)
private: std::vector<std::vector<bool>> isFunction; // Indicates function modules that are not subjected to masking private: std::vector<std::vector<bool> > isFunction; // Indicates function modules that are not subjected to masking

@ -22,7 +22,6 @@
*/ */
#include <climits> #include <climits>
#include <cstddef>
#include <cstring> #include <cstring>
#include "QrSegment.hpp" #include "QrSegment.hpp"

Loading…
Cancel
Save