Reduced redundancy in C++ code by wrapping .cpp source files in namespace{} blocks.

This commit is contained in:
Project Nayuki 2017-04-21 05:30:59 +00:00
parent 771bdaf069
commit cc2a5d4ce9
3 changed files with 68 additions and 56 deletions

View File

@ -27,22 +27,24 @@
#include "BitBuffer.hpp"
qrcodegen::BitBuffer::BitBuffer() :
namespace qrcodegen {
BitBuffer::BitBuffer() :
data(),
bitLength(0) {}
int qrcodegen::BitBuffer::getBitLength() const {
int BitBuffer::getBitLength() const {
return bitLength;
}
std::vector<uint8_t> qrcodegen::BitBuffer::getBytes() const {
std::vector<uint8_t> BitBuffer::getBytes() const {
return data;
}
void qrcodegen::BitBuffer::appendBits(uint32_t val, int len) {
void BitBuffer::appendBits(uint32_t val, int len) {
if (len < 0 || len > 32 || (len < 32 && (val >> len) != 0))
throw "Value out of range";
if (INT_MAX - bitLength < len)
@ -55,7 +57,7 @@ void qrcodegen::BitBuffer::appendBits(uint32_t val, int len) {
}
void qrcodegen::BitBuffer::appendData(const QrSegment &seg) {
void BitBuffer::appendData(const QrSegment &seg) {
if (INT_MAX - bitLength < seg.bitLength)
throw "Buffer too long";
unsigned int newByteLen = ((unsigned int)bitLength + seg.bitLength + 7) / 8;
@ -66,3 +68,5 @@ void qrcodegen::BitBuffer::appendData(const QrSegment &seg) {
data.at(bitLength >> 3) |= bit << (7 - (bitLength & 7));
}
}
}

View File

@ -30,31 +30,33 @@
#include "QrCode.hpp"
qrcodegen::QrCode::Ecc::Ecc(int ord, int fb) :
namespace qrcodegen {
QrCode::Ecc::Ecc(int ord, int fb) :
ordinal(ord),
formatBits(fb) {}
const qrcodegen::QrCode::Ecc qrcodegen::QrCode::Ecc::LOW (0, 1);
const qrcodegen::QrCode::Ecc qrcodegen::QrCode::Ecc::MEDIUM (1, 0);
const qrcodegen::QrCode::Ecc qrcodegen::QrCode::Ecc::QUARTILE(2, 3);
const qrcodegen::QrCode::Ecc qrcodegen::QrCode::Ecc::HIGH (3, 2);
const QrCode::Ecc QrCode::Ecc::LOW (0, 1);
const QrCode::Ecc QrCode::Ecc::MEDIUM (1, 0);
const QrCode::Ecc QrCode::Ecc::QUARTILE(2, 3);
const QrCode::Ecc QrCode::Ecc::HIGH (3, 2);
qrcodegen::QrCode qrcodegen::QrCode::encodeText(const char *text, const Ecc &ecl) {
QrCode QrCode::encodeText(const char *text, const Ecc &ecl) {
std::vector<QrSegment> segs(QrSegment::makeSegments(text));
return encodeSegments(segs, ecl);
}
qrcodegen::QrCode qrcodegen::QrCode::encodeBinary(const std::vector<uint8_t> &data, const Ecc &ecl) {
QrCode QrCode::encodeBinary(const std::vector<uint8_t> &data, const Ecc &ecl) {
std::vector<QrSegment> segs;
segs.push_back(QrSegment::makeBytes(data));
return encodeSegments(segs, ecl);
}
qrcodegen::QrCode qrcodegen::QrCode::encodeSegments(const std::vector<QrSegment> &segs, const Ecc &ecl,
QrCode QrCode::encodeSegments(const std::vector<QrSegment> &segs, const Ecc &ecl,
int minVersion, int maxVersion, int mask, bool boostEcl) {
if (!(1 <= minVersion && minVersion <= maxVersion && maxVersion <= 40) || mask < -1 || mask > 7)
throw "Invalid value";
@ -105,7 +107,7 @@ qrcodegen::QrCode qrcodegen::QrCode::encodeSegments(const std::vector<QrSegment>
}
qrcodegen::QrCode::QrCode(int ver, const Ecc &ecl, const std::vector<uint8_t> &dataCodewords, int mask) :
QrCode::QrCode(int ver, const Ecc &ecl, const std::vector<uint8_t> &dataCodewords, int mask) :
// Initialize scalar fields
version(ver),
size(1 <= ver && ver <= 40 ? ver * 4 + 17 : -1), // Avoid signed overflow undefined behavior
@ -129,7 +131,7 @@ qrcodegen::QrCode::QrCode(int ver, const Ecc &ecl, const std::vector<uint8_t> &d
}
qrcodegen::QrCode::QrCode(const QrCode &qr, int mask) :
QrCode::QrCode(const QrCode &qr, int mask) :
// Copy scalar fields
version(qr.version),
size(qr.size),
@ -149,12 +151,12 @@ qrcodegen::QrCode::QrCode(const QrCode &qr, int mask) :
}
int qrcodegen::QrCode::getMask() const {
int QrCode::getMask() const {
return mask;
}
int qrcodegen::QrCode::getModule(int x, int y) const {
int QrCode::getModule(int x, int y) const {
if (0 <= x && x < size && 0 <= y && y < size)
return modules.at(y).at(x) ? 1 : 0;
else
@ -162,7 +164,7 @@ int qrcodegen::QrCode::getModule(int x, int y) const {
}
std::string qrcodegen::QrCode::toSvgString(int border) const {
std::string QrCode::toSvgString(int border) const {
if (border < 0)
throw "Border must be non-negative";
std::ostringstream sb;
@ -190,7 +192,7 @@ std::string qrcodegen::QrCode::toSvgString(int border) const {
}
void qrcodegen::QrCode::drawFunctionPatterns() {
void QrCode::drawFunctionPatterns() {
// Draw horizontal and vertical timing patterns
for (int i = 0; i < size; i++) {
setFunctionModule(6, i, i % 2 == 0);
@ -220,7 +222,7 @@ void qrcodegen::QrCode::drawFunctionPatterns() {
}
void qrcodegen::QrCode::drawFormatBits(int mask) {
void QrCode::drawFormatBits(int mask) {
// Calculate error correction code and pack bits
int data = errorCorrectionLevel.formatBits << 3 | mask; // errCorrLvl is uint2, mask is uint3
int rem = data;
@ -249,7 +251,7 @@ void qrcodegen::QrCode::drawFormatBits(int mask) {
}
void qrcodegen::QrCode::drawVersion() {
void QrCode::drawVersion() {
if (version < 7)
return;
@ -271,7 +273,7 @@ void qrcodegen::QrCode::drawVersion() {
}
void qrcodegen::QrCode::drawFinderPattern(int x, int y) {
void QrCode::drawFinderPattern(int x, int y) {
for (int i = -4; i <= 4; i++) {
for (int j = -4; j <= 4; j++) {
int dist = std::max(std::abs(i), std::abs(j)); // Chebyshev/infinity norm
@ -283,7 +285,7 @@ void qrcodegen::QrCode::drawFinderPattern(int x, int y) {
}
void qrcodegen::QrCode::drawAlignmentPattern(int x, int y) {
void QrCode::drawAlignmentPattern(int x, int y) {
for (int i = -2; i <= 2; i++) {
for (int j = -2; j <= 2; j++)
setFunctionModule(x + j, y + i, std::max(std::abs(i), std::abs(j)) != 1);
@ -291,13 +293,13 @@ void qrcodegen::QrCode::drawAlignmentPattern(int x, int y) {
}
void qrcodegen::QrCode::setFunctionModule(int x, int y, bool isBlack) {
void QrCode::setFunctionModule(int x, int y, bool isBlack) {
modules.at(y).at(x) = isBlack;
isFunction.at(y).at(x) = true;
}
std::vector<uint8_t> qrcodegen::QrCode::appendErrorCorrection(const std::vector<uint8_t> &data) const {
std::vector<uint8_t> QrCode::appendErrorCorrection(const std::vector<uint8_t> &data) const {
if (data.size() != static_cast<unsigned int>(getNumDataCodewords(version, errorCorrectionLevel)))
throw "Invalid argument";
@ -336,7 +338,7 @@ std::vector<uint8_t> qrcodegen::QrCode::appendErrorCorrection(const std::vector<
}
void qrcodegen::QrCode::drawCodewords(const std::vector<uint8_t> &data) {
void QrCode::drawCodewords(const std::vector<uint8_t> &data) {
if (data.size() != static_cast<unsigned int>(getNumRawDataModules(version) / 8))
throw "Invalid argument";
@ -364,7 +366,7 @@ void qrcodegen::QrCode::drawCodewords(const std::vector<uint8_t> &data) {
}
void qrcodegen::QrCode::applyMask(int mask) {
void QrCode::applyMask(int mask) {
if (mask < 0 || mask > 7)
throw "Mask value out of range";
for (int y = 0; y < size; y++) {
@ -387,7 +389,7 @@ void qrcodegen::QrCode::applyMask(int mask) {
}
int qrcodegen::QrCode::handleConstructorMasking(int mask) {
int QrCode::handleConstructorMasking(int mask) {
if (mask == -1) { // Automatically choose best mask
long minPenalty = LONG_MAX;
for (int i = 0; i < 8; i++) {
@ -409,7 +411,7 @@ int qrcodegen::QrCode::handleConstructorMasking(int mask) {
}
long qrcodegen::QrCode::getPenaltyScore() const {
long QrCode::getPenaltyScore() const {
long result = 0;
// Adjacent modules in row having same color
@ -489,7 +491,7 @@ long qrcodegen::QrCode::getPenaltyScore() const {
}
std::vector<int> qrcodegen::QrCode::getAlignmentPatternPositions(int ver) {
std::vector<int> QrCode::getAlignmentPatternPositions(int ver) {
if (ver < 1 || ver > 40)
throw "Version number out of range";
else if (ver == 1)
@ -512,7 +514,7 @@ std::vector<int> qrcodegen::QrCode::getAlignmentPatternPositions(int ver) {
}
int qrcodegen::QrCode::getNumRawDataModules(int ver) {
int QrCode::getNumRawDataModules(int ver) {
if (ver < 1 || ver > 40)
throw "Version number out of range";
int result = (16 * ver + 128) * ver + 64;
@ -526,7 +528,7 @@ int qrcodegen::QrCode::getNumRawDataModules(int ver) {
}
int qrcodegen::QrCode::getNumDataCodewords(int ver, const Ecc &ecl) {
int QrCode::getNumDataCodewords(int ver, const Ecc &ecl) {
if (ver < 1 || ver > 40)
throw "Version number out of range";
return getNumRawDataModules(ver) / 8 - ECC_CODEWORDS_PER_BLOCK[ecl.ordinal][ver] * NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal][ver];
@ -535,13 +537,13 @@ int qrcodegen::QrCode::getNumDataCodewords(int ver, const Ecc &ecl) {
/*---- Tables of constants ----*/
const int qrcodegen::QrCode::PENALTY_N1 = 3;
const int qrcodegen::QrCode::PENALTY_N2 = 3;
const int qrcodegen::QrCode::PENALTY_N3 = 40;
const int qrcodegen::QrCode::PENALTY_N4 = 10;
const int QrCode::PENALTY_N1 = 3;
const int QrCode::PENALTY_N2 = 3;
const int QrCode::PENALTY_N3 = 40;
const int QrCode::PENALTY_N4 = 10;
const int8_t qrcodegen::QrCode::ECC_CODEWORDS_PER_BLOCK[4][41] = {
const int8_t QrCode::ECC_CODEWORDS_PER_BLOCK[4][41] = {
// Version: (note that index 0 is for padding, and is set to an illegal value)
//0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level
{-1, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // Low
@ -550,7 +552,7 @@ const int8_t qrcodegen::QrCode::ECC_CODEWORDS_PER_BLOCK[4][41] = {
{-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // High
};
const int8_t qrcodegen::QrCode::NUM_ERROR_CORRECTION_BLOCKS[4][41] = {
const int8_t QrCode::NUM_ERROR_CORRECTION_BLOCKS[4][41] = {
// Version: (note that index 0 is for padding, and is set to an illegal value)
//0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40 Error correction level
{-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25}, // Low
@ -560,7 +562,7 @@ const int8_t qrcodegen::QrCode::NUM_ERROR_CORRECTION_BLOCKS[4][41] = {
};
qrcodegen::QrCode::ReedSolomonGenerator::ReedSolomonGenerator(int degree) :
QrCode::ReedSolomonGenerator::ReedSolomonGenerator(int degree) :
coefficients() {
if (degree < 1 || degree > 255)
throw "Degree out of range";
@ -585,7 +587,7 @@ qrcodegen::QrCode::ReedSolomonGenerator::ReedSolomonGenerator(int degree) :
}
std::vector<uint8_t> qrcodegen::QrCode::ReedSolomonGenerator::getRemainder(const std::vector<uint8_t> &data) const {
std::vector<uint8_t> QrCode::ReedSolomonGenerator::getRemainder(const std::vector<uint8_t> &data) const {
// Compute the remainder by performing polynomial division
std::vector<uint8_t> result(coefficients.size());
for (size_t i = 0; i < data.size(); i++) {
@ -599,7 +601,7 @@ std::vector<uint8_t> qrcodegen::QrCode::ReedSolomonGenerator::getRemainder(const
}
uint8_t qrcodegen::QrCode::ReedSolomonGenerator::multiply(uint8_t x, uint8_t y) {
uint8_t QrCode::ReedSolomonGenerator::multiply(uint8_t x, uint8_t y) {
// Russian peasant multiplication
int z = 0;
for (int i = 7; i >= 0; i--) {
@ -610,3 +612,5 @@ uint8_t qrcodegen::QrCode::ReedSolomonGenerator::multiply(uint8_t x, uint8_t y)
throw "Assertion error";
return static_cast<uint8_t>(z);
}
}

View File

@ -28,7 +28,9 @@
#include "QrSegment.hpp"
qrcodegen::QrSegment::Mode::Mode(int mode, int cc0, int cc1, int cc2) :
namespace qrcodegen {
QrSegment::Mode::Mode(int mode, int cc0, int cc1, int cc2) :
modeBits(mode) {
numBitsCharCount[0] = cc0;
numBitsCharCount[1] = cc1;
@ -36,7 +38,7 @@ qrcodegen::QrSegment::Mode::Mode(int mode, int cc0, int cc1, int cc2) :
}
int qrcodegen::QrSegment::Mode::numCharCountBits(int ver) const {
int QrSegment::Mode::numCharCountBits(int ver) const {
if ( 1 <= ver && ver <= 9) return numBitsCharCount[0];
else if (10 <= ver && ver <= 26) return numBitsCharCount[1];
else if (27 <= ver && ver <= 40) return numBitsCharCount[2];
@ -44,19 +46,19 @@ int qrcodegen::QrSegment::Mode::numCharCountBits(int ver) const {
}
const qrcodegen::QrSegment::Mode qrcodegen::QrSegment::Mode::NUMERIC (0x1, 10, 12, 14);
const qrcodegen::QrSegment::Mode qrcodegen::QrSegment::Mode::ALPHANUMERIC(0x2, 9, 11, 13);
const qrcodegen::QrSegment::Mode qrcodegen::QrSegment::Mode::BYTE (0x4, 8, 16, 16);
const qrcodegen::QrSegment::Mode qrcodegen::QrSegment::Mode::KANJI (0x8, 8, 10, 12);
const QrSegment::Mode QrSegment::Mode::NUMERIC (0x1, 10, 12, 14);
const QrSegment::Mode QrSegment::Mode::ALPHANUMERIC(0x2, 9, 11, 13);
const QrSegment::Mode QrSegment::Mode::BYTE (0x4, 8, 16, 16);
const QrSegment::Mode QrSegment::Mode::KANJI (0x8, 8, 10, 12);
qrcodegen::QrSegment qrcodegen::QrSegment::makeBytes(const std::vector<uint8_t> &data) {
QrSegment QrSegment::makeBytes(const std::vector<uint8_t> &data) {
return QrSegment(Mode::BYTE, data.size(), data, data.size() * 8);
}
qrcodegen::QrSegment qrcodegen::QrSegment::makeNumeric(const char *digits) {
QrSegment QrSegment::makeNumeric(const char *digits) {
BitBuffer bb;
int accumData = 0;
int accumCount = 0;
@ -79,7 +81,7 @@ qrcodegen::QrSegment qrcodegen::QrSegment::makeNumeric(const char *digits) {
}
qrcodegen::QrSegment qrcodegen::QrSegment::makeAlphanumeric(const char *text) {
QrSegment QrSegment::makeAlphanumeric(const char *text) {
BitBuffer bb;
int accumData = 0;
int accumCount = 0;
@ -102,7 +104,7 @@ qrcodegen::QrSegment qrcodegen::QrSegment::makeAlphanumeric(const char *text) {
}
std::vector<qrcodegen::QrSegment> qrcodegen::QrSegment::makeSegments(const char *text) {
std::vector<QrSegment> QrSegment::makeSegments(const char *text) {
// Select the most efficient segment encoding automatically
std::vector<QrSegment> result;
if (*text == '\0'); // Leave the vector empty
@ -120,7 +122,7 @@ std::vector<qrcodegen::QrSegment> qrcodegen::QrSegment::makeSegments(const char
}
qrcodegen::QrSegment::QrSegment(const Mode &md, int numCh, const std::vector<uint8_t> &b, int bitLen) :
QrSegment::QrSegment(const Mode &md, int numCh, const std::vector<uint8_t> &b, int bitLen) :
mode(md),
numChars(numCh),
data(b),
@ -130,7 +132,7 @@ qrcodegen::QrSegment::QrSegment(const Mode &md, int numCh, const std::vector<uin
}
int qrcodegen::QrSegment::getTotalBits(const std::vector<QrSegment> &segs, int version) {
int QrSegment::getTotalBits(const std::vector<QrSegment> &segs, int version) {
if (version < 1 || version > 40)
throw "Version number out of range";
int result = 0;
@ -149,7 +151,7 @@ int qrcodegen::QrSegment::getTotalBits(const std::vector<QrSegment> &segs, int v
}
bool qrcodegen::QrSegment::isAlphanumeric(const char *text) {
bool QrSegment::isAlphanumeric(const char *text) {
for (; *text != '\0'; text++) {
char c = *text;
if (c < ' ' || c > 'Z' || ALPHANUMERIC_ENCODING_TABLE[c - ' '] == -1)
@ -159,7 +161,7 @@ bool qrcodegen::QrSegment::isAlphanumeric(const char *text) {
}
bool qrcodegen::QrSegment::isNumeric(const char *text) {
bool QrSegment::isNumeric(const char *text) {
for (; *text != '\0'; text++) {
char c = *text;
if (c < '0' || c > '9')
@ -169,9 +171,11 @@ bool qrcodegen::QrSegment::isNumeric(const char *text) {
}
const int8_t qrcodegen::QrSegment::ALPHANUMERIC_ENCODING_TABLE[59] = {
const int8_t QrSegment::ALPHANUMERIC_ENCODING_TABLE[59] = {
// SP, !, ", #, $, %, &, ', (, ), *, +, ,, -, ., /, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, :, ;, <, =, >, ?, @, // ASCII codes 32 to 64
36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1, -1, // Array indices 0 to 32
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, // Array indices 33 to 58
// A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, // ASCII codes 65 to 90
};
}