Skip to content

Commit

Permalink
Renamed some method parameters to completely avoid variable shadowing…
Browse files Browse the repository at this point in the history
…, in C++ and Java versions.
  • Loading branch information
nayuki committed Oct 14, 2019
1 parent fe99275 commit 67c6246
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
26 changes: 13 additions & 13 deletions cpp/QrCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ QrCode QrCode::encodeSegments(const vector<QrSegment> &segs, Ecc ecl,
}


QrCode::QrCode(int ver, Ecc ecl, const vector<uint8_t> &dataCodewords, int mask) :
QrCode::QrCode(int ver, Ecc ecl, const vector<uint8_t> &dataCodewords, int msk) :
// Initialize fields and check arguments
version(ver),
errorCorrectionLevel(ecl) {
if (ver < MIN_VERSION || ver > MAX_VERSION)
throw std::domain_error("Version value out of range");
if (mask < -1 || mask > 7)
if (msk < -1 || msk > 7)
throw std::domain_error("Mask value out of range");
size = ver * 4 + 17;
size_t sz = static_cast<size_t>(size);
Expand All @@ -145,24 +145,24 @@ QrCode::QrCode(int ver, Ecc ecl, const vector<uint8_t> &dataCodewords, int mask)
drawCodewords(allCodewords);

// Do masking
if (mask == -1) { // Automatically choose best mask
if (msk == -1) { // Automatically choose best mask
long minPenalty = LONG_MAX;
for (int i = 0; i < 8; i++) {
applyMask(i);
drawFormatBits(i);
long penalty = getPenaltyScore();
if (penalty < minPenalty) {
mask = i;
msk = i;
minPenalty = penalty;
}
applyMask(i); // Undoes the mask due to XOR
}
}
if (mask < 0 || mask > 7)
if (msk < 0 || msk > 7)
throw std::logic_error("Assertion error");
this->mask = mask;
applyMask(mask); // Apply the final choice of mask
drawFormatBits(mask); // Overwrite old format bits
this->mask = msk;
applyMask(msk); // Apply the final choice of mask
drawFormatBits(msk); // Overwrite old format bits

isFunction.clear();
isFunction.shrink_to_fit();
Expand Down Expand Up @@ -251,9 +251,9 @@ void QrCode::drawFunctionPatterns() {
}


void QrCode::drawFormatBits(int mask) {
void QrCode::drawFormatBits(int msk) {
// Calculate error correction code and pack bits
int data = getFormatBits(errorCorrectionLevel) << 3 | mask; // errCorrLvl is uint2, mask is uint3
int data = getFormatBits(errorCorrectionLevel) << 3 | msk; // errCorrLvl is uint2, msk is uint3
int rem = data;
for (int i = 0; i < 10; i++)
rem = (rem << 1) ^ ((rem >> 9) * 0x537);
Expand Down Expand Up @@ -402,14 +402,14 @@ void QrCode::drawCodewords(const vector<uint8_t> &data) {
}


void QrCode::applyMask(int mask) {
if (mask < 0 || mask > 7)
void QrCode::applyMask(int msk) {
if (msk < 0 || msk > 7)
throw std::domain_error("Mask value out of range");
size_t sz = static_cast<size_t>(size);
for (size_t y = 0; y < sz; y++) {
for (size_t x = 0; x < sz; x++) {
bool invert;
switch (mask) {
switch (msk) {
case 0: invert = (x + y) % 2 == 0; break;
case 1: invert = y % 2 == 0; break;
case 2: invert = x % 3 == 0; break;
Expand Down
6 changes: 3 additions & 3 deletions cpp/QrCode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class QrCode final {
* This is a low-level API that most users should not use directly.
* A mid-level API is the encodeSegments() function.
*/
public: QrCode(int ver, 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 msk);



Expand Down Expand Up @@ -201,7 +201,7 @@ class QrCode final {

// Draws two copies of the format bits (with its own error correction code)
// based on the given mask and this object's error correction level field.
private: void drawFormatBits(int mask);
private: void drawFormatBits(int msk);


// Draws two copies of the version bits (with its own error correction code),
Expand Down Expand Up @@ -245,7 +245,7 @@ class QrCode final {
// before masking. Due to the arithmetic of XOR, calling applyMask() with
// the same mask value a second time will undo the mask. A final well-formed
// QR Code needs exactly one (not zero, two, etc.) mask applied.
private: void applyMask(int mask);
private: void applyMask(int msk);


// Calculates and returns the penalty score based on state of this QR Code's current modules.
Expand Down
32 changes: 16 additions & 16 deletions java/src/main/java/io/nayuki/qrcodegen/QrCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,16 @@ public static QrCode encodeSegments(List<QrSegment> segs, Ecc ecl, int minVersio
* @param ver the version number to use, which must be in the range 1 to 40 (inclusive)
* @param ecl the error correction level to use
* @param dataCodewords the bytes representing segments to encode (without ECC)
* @param mask the mask pattern to use, which is either &#x2212;1 for automatic choice or from 0 to 7 for fixed choice
* @param msk the mask pattern to use, which is either &#x2212;1 for automatic choice or from 0 to 7 for fixed choice
* @throws NullPointerException if the byte array or error correction level is {@code null}
* @throws IllegalArgumentException if the version or mask value is out of range,
* or if the data is the wrong length for the specified version and error correction level
*/
public QrCode(int ver, Ecc ecl, byte[] dataCodewords, int mask) {
public QrCode(int ver, Ecc ecl, byte[] dataCodewords, int msk) {
// Check arguments and initialize fields
if (ver < MIN_VERSION || ver > MAX_VERSION)
throw new IllegalArgumentException("Version value out of range");
if (mask < -1 || mask > 7)
if (msk < -1 || msk > 7)
throw new IllegalArgumentException("Mask value out of range");
version = ver;
size = ver * 4 + 17;
Expand All @@ -263,7 +263,7 @@ public QrCode(int ver, Ecc ecl, byte[] dataCodewords, int mask) {
drawFunctionPatterns();
byte[] allCodewords = addEccAndInterleave(dataCodewords);
drawCodewords(allCodewords);
this.mask = handleConstructorMasking(mask);
this.mask = handleConstructorMasking(msk);
isFunction = null;
}

Expand Down Expand Up @@ -382,9 +382,9 @@ private void drawFunctionPatterns() {

// Draws two copies of the format bits (with its own error correction code)
// based on the given mask and this object's error correction level field.
private void drawFormatBits(int mask) {
private void drawFormatBits(int msk) {
// Calculate error correction code and pack bits
int data = errorCorrectionLevel.formatBits << 3 | mask; // errCorrLvl is uint2, mask is uint3
int data = errorCorrectionLevel.formatBits << 3 | msk; // errCorrLvl is uint2, mask is uint3
int rem = data;
for (int i = 0; i < 10; i++)
rem = (rem << 1) ^ ((rem >>> 9) * 0x537);
Expand Down Expand Up @@ -543,13 +543,13 @@ private void drawCodewords(byte[] data) {
// before masking. Due to the arithmetic of XOR, calling applyMask() with
// the same mask value a second time will undo the mask. A final well-formed
// QR Code needs exactly one (not zero, two, etc.) mask applied.
private void applyMask(int mask) {
if (mask < 0 || mask > 7)
private void applyMask(int msk) {
if (msk < 0 || msk > 7)
throw new IllegalArgumentException("Mask value out of range");
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
boolean invert;
switch (mask) {
switch (msk) {
case 0: invert = (x + y) % 2 == 0; break;
case 1: invert = y % 2 == 0; break;
case 2: invert = x % 3 == 0; break;
Expand All @@ -569,24 +569,24 @@ private void applyMask(int mask) {
// A messy helper function for the constructor. This QR Code must be in an unmasked state when this
// method is called. The given argument is the requested mask, which is -1 for auto or 0 to 7 for fixed.
// This method applies and returns the actual mask chosen, from 0 to 7.
private int handleConstructorMasking(int mask) {
if (mask == -1) { // Automatically choose best mask
private int handleConstructorMasking(int msk) {
if (msk == -1) { // Automatically choose best mask
int minPenalty = Integer.MAX_VALUE;
for (int i = 0; i < 8; i++) {
applyMask(i);
drawFormatBits(i);
int penalty = getPenaltyScore();
if (penalty < minPenalty) {
mask = i;
msk = i;
minPenalty = penalty;
}
applyMask(i); // Undoes the mask due to XOR
}
}
assert 0 <= mask && mask <= 7;
applyMask(mask); // Apply the final choice of mask
drawFormatBits(mask); // Overwrite old format bits
return mask; // The caller shall assign this value to the final-declared field
assert 0 <= msk && msk <= 7;
applyMask(msk); // Apply the final choice of mask
drawFormatBits(msk); // Overwrite old format bits
return msk; // The caller shall assign this value to the final-declared field
}


Expand Down

0 comments on commit 67c6246

Please sign in to comment.