Tweaked usages of C++ QrCode::Ecc class to be passed by value instead of const reference.

pull/16/head
Project Nayuki 8 years ago
parent 236a999637
commit 8bbfa3938b

@ -59,19 +59,19 @@ const QrCode::Ecc QrCode::Ecc::QUARTILE(2, 3);
const QrCode::Ecc QrCode::Ecc::HIGH (3, 2); const QrCode::Ecc QrCode::Ecc::HIGH (3, 2);
QrCode QrCode::encodeText(const char *text, const Ecc &ecl) { QrCode QrCode::encodeText(const char *text, Ecc ecl) {
vector<QrSegment> segs(QrSegment::makeSegments(text)); vector<QrSegment> segs(QrSegment::makeSegments(text));
return encodeSegments(segs, ecl); return encodeSegments(segs, ecl);
} }
QrCode QrCode::encodeBinary(const vector<uint8_t> &data, const Ecc &ecl) { QrCode QrCode::encodeBinary(const vector<uint8_t> &data, Ecc ecl) {
vector<QrSegment> segs{QrSegment::makeBytes(data)}; vector<QrSegment> segs{QrSegment::makeBytes(data)};
return encodeSegments(segs, ecl); return encodeSegments(segs, ecl);
} }
QrCode QrCode::encodeSegments(const vector<QrSegment> &segs, const Ecc &ecl, QrCode QrCode::encodeSegments(const vector<QrSegment> &segs, Ecc ecl,
int minVersion, int maxVersion, int mask, bool boostEcl) { int minVersion, int maxVersion, int mask, bool boostEcl) {
if (!(1 <= minVersion && minVersion <= maxVersion && maxVersion <= 40) || mask < -1 || mask > 7) if (!(1 <= minVersion && minVersion <= maxVersion && maxVersion <= 40) || mask < -1 || mask > 7)
throw "Invalid value"; throw "Invalid value";
@ -90,14 +90,13 @@ QrCode QrCode::encodeSegments(const vector<QrSegment> &segs, const Ecc &ecl,
throw "Assertion error"; throw "Assertion error";
// Increase the error correction level while the data still fits in the current version number // Increase the error correction level while the data still fits in the current version number
const Ecc *newEcl = &ecl; for (Ecc newEcl : vector<Ecc>{Ecc::MEDIUM, Ecc::QUARTILE, Ecc::HIGH}) {
for (const Ecc *anEcl : vector<const Ecc*>{&Ecc::MEDIUM, &Ecc::QUARTILE, &Ecc::HIGH}) { if (boostEcl && dataUsedBits <= getNumDataCodewords(version, newEcl) * 8)
if (boostEcl && dataUsedBits <= getNumDataCodewords(version, *anEcl) * 8) ecl = newEcl;
newEcl = anEcl;
} }
// Create the data bit string by concatenating all segments // Create the data bit string by concatenating all segments
size_t dataCapacityBits = getNumDataCodewords(version, *newEcl) * 8; size_t dataCapacityBits = getNumDataCodewords(version, ecl) * 8;
BitBuffer bb; BitBuffer bb;
for (const QrSegment &seg : segs) { for (const QrSegment &seg : segs) {
bb.appendBits(seg.getMode().getModeBits(), 4); bb.appendBits(seg.getMode().getModeBits(), 4);
@ -116,11 +115,11 @@ QrCode QrCode::encodeSegments(const vector<QrSegment> &segs, const Ecc &ecl,
throw "Assertion error"; throw "Assertion error";
// Create the QR Code symbol // Create the QR Code symbol
return QrCode(version, *newEcl, bb.getBytes(), mask); return QrCode(version, ecl, bb.getBytes(), mask);
} }
QrCode::QrCode(int ver, const Ecc &ecl, const vector<uint8_t> &dataCodewords, int mask) : QrCode::QrCode(int ver, Ecc ecl, const vector<uint8_t> &dataCodewords, int mask) :
// Initialize scalar fields // Initialize scalar fields
version(ver), version(ver),
size(1 <= ver && ver <= 40 ? ver * 4 + 17 : -1), // Avoid signed overflow undefined behavior size(1 <= ver && ver <= 40 ? ver * 4 + 17 : -1), // Avoid signed overflow undefined behavior
@ -558,7 +557,7 @@ int QrCode::getNumRawDataModules(int ver) {
} }
int QrCode::getNumDataCodewords(int ver, const Ecc &ecl) { int QrCode::getNumDataCodewords(int ver, Ecc ecl) {
if (ver < 1 || ver > 40) if (ver < 1 || ver > 40)
throw "Version number out of range"; throw "Version number out of range";
return getNumRawDataModules(ver) / 8 return getNumRawDataModules(ver) / 8

@ -73,7 +73,7 @@ class QrCode final {
* QR Code version is automatically chosen for the output. The ECC level of the result may be higher than * QR Code version is automatically chosen for the output. The ECC level of the result may be higher than
* the ecl argument if it can be done without increasing the version. * the ecl argument if it can be done without increasing the version.
*/ */
public: static QrCode encodeText(const char *text, const Ecc &ecl); public: static QrCode encodeText(const char *text, Ecc ecl);
/* /*
@ -82,7 +82,7 @@ class QrCode final {
* bytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output. * bytes allowed is 2953. The smallest possible QR Code version is automatically chosen for the output.
* The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version. * The ECC level of the result may be higher than the ecl argument if it can be done without increasing the version.
*/ */
public: static QrCode encodeBinary(const std::vector<std::uint8_t> &data, const Ecc &ecl); public: static QrCode encodeBinary(const std::vector<std::uint8_t> &data, Ecc ecl);
/* /*
@ -92,7 +92,7 @@ class QrCode final {
* between modes (such as alphanumeric and binary) to encode text more efficiently. * between modes (such as alphanumeric and binary) to encode text more efficiently.
* This function is considered to be lower level than simply encoding text or binary data. * This function is considered to be lower level than simply encoding text or binary data.
*/ */
public: static QrCode encodeSegments(const std::vector<QrSegment> &segs, const Ecc &ecl, public: static QrCode encodeSegments(const std::vector<QrSegment> &segs, Ecc ecl,
int minVersion=1, int maxVersion=40, int mask=-1, bool boostEcl=true); // All optional parameters int minVersion=1, int maxVersion=40, int mask=-1, bool boostEcl=true); // All optional parameters
@ -129,7 +129,7 @@ class QrCode final {
* and mask number. This is a cumbersome low-level constructor that should not be invoked directly by the user. * and mask number. This is a cumbersome low-level constructor that should not be invoked directly by the user.
* To go one level up, see the encodeSegments() function. * To go one level up, see the encodeSegments() function.
*/ */
public: QrCode(int ver, const Ecc &ecl, const std::vector<std::uint8_t> &dataCodewords, int mask); public: QrCode(int ver, Ecc ecl, const std::vector<std::uint8_t> &dataCodewords, int mask);
/* /*
@ -252,7 +252,7 @@ class QrCode final {
// Returns the number of 8-bit data (i.e. not error correction) codewords contained in any // Returns the number of 8-bit data (i.e. not error correction) codewords contained in any
// QR Code of the given version number and error correction level, with remainder bits discarded. // QR Code of the given version number and error correction level, with remainder bits discarded.
// This stateless pure function could be implemented as a (40*4)-cell lookup table. // This stateless pure function could be implemented as a (40*4)-cell lookup table.
private: static int getNumDataCodewords(int ver, const Ecc &ecl); private: static int getNumDataCodewords(int ver, Ecc ecl);
/*---- Private tables of constants ----*/ /*---- Private tables of constants ----*/

@ -58,7 +58,7 @@ int main() {
// Creates a single QR Code, then prints it to the console. // Creates a single QR Code, then prints it to the console.
static void doBasicDemo() { static void doBasicDemo() {
const char *text = "Hello, world!"; // User-supplied text const char *text = "Hello, world!"; // User-supplied text
const QrCode::Ecc &errCorLvl = QrCode::Ecc::LOW; // Error correction level const QrCode::Ecc errCorLvl = QrCode::Ecc::LOW; // Error correction level
// Make and print the QR Code symbol // Make and print the QR Code symbol
const QrCode qr = QrCode::encodeText(text, errCorLvl); const QrCode qr = QrCode::encodeText(text, errCorLvl);

Loading…
Cancel
Save