Added local variable to appendErrorCorrection() in {Java, C++, JavaScript, Python} language versions to reduce code repetition and synchronize with C version.

This commit is contained in:
Project Nayuki 2017-05-06 17:19:00 +00:00
parent a712ccc230
commit 17e0155500
4 changed files with 16 additions and 12 deletions

View File

@ -311,8 +311,9 @@ vector<uint8_t> QrCode::appendErrorCorrection(const vector<uint8_t> &data) const
// Calculate parameter numbers // Calculate parameter numbers
int numBlocks = NUM_ERROR_CORRECTION_BLOCKS[errorCorrectionLevel.ordinal][version]; int numBlocks = NUM_ERROR_CORRECTION_BLOCKS[errorCorrectionLevel.ordinal][version];
int blockEccLen = ECC_CODEWORDS_PER_BLOCK[errorCorrectionLevel.ordinal][version]; int blockEccLen = ECC_CODEWORDS_PER_BLOCK[errorCorrectionLevel.ordinal][version];
int numShortBlocks = numBlocks - getNumRawDataModules(version) / 8 % numBlocks; int rawCodewords = getNumRawDataModules(version) / 8;
int shortBlockLen = getNumRawDataModules(version) / 8 / numBlocks; int numShortBlocks = numBlocks - rawCodewords % numBlocks;
int shortBlockLen = rawCodewords / numBlocks;
// Split data into blocks and append ECC to each block // Split data into blocks and append ECC to each block
vector<vector<uint8_t> > blocks; vector<vector<uint8_t> > blocks;
@ -337,7 +338,7 @@ vector<uint8_t> QrCode::appendErrorCorrection(const vector<uint8_t> &data) const
result.push_back(blocks.at(j).at(i)); result.push_back(blocks.at(j).at(i));
} }
} }
if (result.size() != static_cast<unsigned int>(getNumRawDataModules(version) / 8)) if (result.size() != static_cast<unsigned int>(rawCodewords))
throw "Assertion error"; throw "Assertion error";
return result; return result;
} }

View File

@ -458,8 +458,9 @@ public final class QrCode {
// Calculate parameter numbers // Calculate parameter numbers
int numBlocks = NUM_ERROR_CORRECTION_BLOCKS[errorCorrectionLevel.ordinal()][version]; int numBlocks = NUM_ERROR_CORRECTION_BLOCKS[errorCorrectionLevel.ordinal()][version];
int blockEccLen = ECC_CODEWORDS_PER_BLOCK[errorCorrectionLevel.ordinal()][version]; int blockEccLen = ECC_CODEWORDS_PER_BLOCK[errorCorrectionLevel.ordinal()][version];
int numShortBlocks = numBlocks - getNumRawDataModules(version) / 8 % numBlocks; int rawCodewords = getNumRawDataModules(version) / 8;
int shortBlockLen = getNumRawDataModules(version) / 8 / numBlocks; int numShortBlocks = numBlocks - rawCodewords % numBlocks;
int shortBlockLen = rawCodewords / numBlocks;
// Split data into blocks and append ECC to each block // Split data into blocks and append ECC to each block
byte[][] blocks = new byte[numBlocks][]; byte[][] blocks = new byte[numBlocks][];
@ -474,7 +475,7 @@ public final class QrCode {
} }
// Interleave (not concatenate) the bytes from every block into a single sequence // Interleave (not concatenate) the bytes from every block into a single sequence
byte[] result = new byte[getNumRawDataModules(version) / 8]; byte[] result = new byte[rawCodewords];
for (int i = 0, k = 0; i < blocks[0].length; i++) { for (int i = 0, k = 0; i < blocks[0].length; i++) {
for (int j = 0; j < blocks.length; j++) { for (int j = 0; j < blocks.length; j++) {
// Skip the padding byte in short blocks // Skip the padding byte in short blocks

View File

@ -358,8 +358,9 @@ var qrcodegen = new function() {
// Calculate parameter numbers // Calculate parameter numbers
var numBlocks = QrCode.NUM_ERROR_CORRECTION_BLOCKS[errCorLvl.ordinal][version]; var numBlocks = QrCode.NUM_ERROR_CORRECTION_BLOCKS[errCorLvl.ordinal][version];
var blockEccLen = QrCode.ECC_CODEWORDS_PER_BLOCK[errCorLvl.ordinal][version]; var blockEccLen = QrCode.ECC_CODEWORDS_PER_BLOCK[errCorLvl.ordinal][version];
var numShortBlocks = numBlocks - Math.floor(QrCode.getNumRawDataModules(version) / 8) % numBlocks; var rawCodewords = Math.floor(QrCode.getNumRawDataModules(version) / 8);
var shortBlockLen = Math.floor(QrCode.getNumRawDataModules(version) / (numBlocks * 8)); var numShortBlocks = numBlocks - rawCodewords % numBlocks;
var shortBlockLen = Math.floor(rawCodewords / numBlocks);
// Split data into blocks and append ECC to each block // Split data into blocks and append ECC to each block
var blocks = []; var blocks = [];
@ -385,7 +386,7 @@ var qrcodegen = new function() {
result.push(blocks[j][i]); result.push(blocks[j][i]);
} }
} }
if (result.length != Math.floor(QrCode.getNumRawDataModules(version) / 8)) if (result.length != rawCodewords)
throw "Assertion error"; throw "Assertion error";
return result; return result;
} }

View File

@ -362,8 +362,9 @@ class QrCode(object):
# Calculate parameter numbers # Calculate parameter numbers
numblocks = QrCode._NUM_ERROR_CORRECTION_BLOCKS[self._errcorlvl.ordinal][version] numblocks = QrCode._NUM_ERROR_CORRECTION_BLOCKS[self._errcorlvl.ordinal][version]
blockecclen = QrCode._ECC_CODEWORDS_PER_BLOCK[self._errcorlvl.ordinal][version] blockecclen = QrCode._ECC_CODEWORDS_PER_BLOCK[self._errcorlvl.ordinal][version]
numshortblocks = numblocks - QrCode._get_num_raw_data_modules(version) // 8 % numblocks rawcodewords = QrCode._get_num_raw_data_modules(version) // 8
shortblocklen = self._get_num_raw_data_modules(version) // 8 // numblocks numshortblocks = numblocks - rawcodewords % numblocks
shortblocklen = rawcodewords // numblocks
# Split data into blocks and append ECC to each block # Split data into blocks and append ECC to each block
blocks = [] blocks = []
@ -386,7 +387,7 @@ class QrCode(object):
# Skip the padding byte in short blocks # Skip the padding byte in short blocks
if i != shortblocklen - blockecclen or j >= numshortblocks: if i != shortblocklen - blockecclen or j >= numshortblocks:
result.append(blk[i]) result.append(blk[i])
assert len(result) == QrCode._get_num_raw_data_modules(version) // 8 assert len(result) == rawcodewords
return result return result