From 17e01555008cfeedb947d51443c488452f84e59a Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Sat, 6 May 2017 17:19:00 +0000 Subject: [PATCH] Added local variable to appendErrorCorrection() in {Java, C++, JavaScript, Python} language versions to reduce code repetition and synchronize with C version. --- cpp/QrCode.cpp | 7 ++++--- java/io/nayuki/qrcodegen/QrCode.java | 7 ++++--- javascript/qrcodegen.js | 7 ++++--- python/qrcodegen.py | 7 ++++--- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/cpp/QrCode.cpp b/cpp/QrCode.cpp index 587b655..8448c14 100644 --- a/cpp/QrCode.cpp +++ b/cpp/QrCode.cpp @@ -311,8 +311,9 @@ vector QrCode::appendErrorCorrection(const vector &data) const // Calculate parameter numbers int numBlocks = NUM_ERROR_CORRECTION_BLOCKS[errorCorrectionLevel.ordinal][version]; int blockEccLen = ECC_CODEWORDS_PER_BLOCK[errorCorrectionLevel.ordinal][version]; - int numShortBlocks = numBlocks - getNumRawDataModules(version) / 8 % numBlocks; - int shortBlockLen = getNumRawDataModules(version) / 8 / numBlocks; + int rawCodewords = getNumRawDataModules(version) / 8; + int numShortBlocks = numBlocks - rawCodewords % numBlocks; + int shortBlockLen = rawCodewords / numBlocks; // Split data into blocks and append ECC to each block vector > blocks; @@ -337,7 +338,7 @@ vector QrCode::appendErrorCorrection(const vector &data) const result.push_back(blocks.at(j).at(i)); } } - if (result.size() != static_cast(getNumRawDataModules(version) / 8)) + if (result.size() != static_cast(rawCodewords)) throw "Assertion error"; return result; } diff --git a/java/io/nayuki/qrcodegen/QrCode.java b/java/io/nayuki/qrcodegen/QrCode.java index 684ab12..27befd5 100644 --- a/java/io/nayuki/qrcodegen/QrCode.java +++ b/java/io/nayuki/qrcodegen/QrCode.java @@ -458,8 +458,9 @@ public final class QrCode { // Calculate parameter numbers int numBlocks = NUM_ERROR_CORRECTION_BLOCKS[errorCorrectionLevel.ordinal()][version]; int blockEccLen = ECC_CODEWORDS_PER_BLOCK[errorCorrectionLevel.ordinal()][version]; - int numShortBlocks = numBlocks - getNumRawDataModules(version) / 8 % numBlocks; - int shortBlockLen = getNumRawDataModules(version) / 8 / numBlocks; + int rawCodewords = getNumRawDataModules(version) / 8; + int numShortBlocks = numBlocks - rawCodewords % numBlocks; + int shortBlockLen = rawCodewords / numBlocks; // Split data into blocks and append ECC to each block 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 - 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 j = 0; j < blocks.length; j++) { // Skip the padding byte in short blocks diff --git a/javascript/qrcodegen.js b/javascript/qrcodegen.js index 722c54c..405432e 100644 --- a/javascript/qrcodegen.js +++ b/javascript/qrcodegen.js @@ -358,8 +358,9 @@ var qrcodegen = new function() { // Calculate parameter numbers var numBlocks = QrCode.NUM_ERROR_CORRECTION_BLOCKS[errCorLvl.ordinal][version]; var blockEccLen = QrCode.ECC_CODEWORDS_PER_BLOCK[errCorLvl.ordinal][version]; - var numShortBlocks = numBlocks - Math.floor(QrCode.getNumRawDataModules(version) / 8) % numBlocks; - var shortBlockLen = Math.floor(QrCode.getNumRawDataModules(version) / (numBlocks * 8)); + var rawCodewords = Math.floor(QrCode.getNumRawDataModules(version) / 8); + var numShortBlocks = numBlocks - rawCodewords % numBlocks; + var shortBlockLen = Math.floor(rawCodewords / numBlocks); // Split data into blocks and append ECC to each block var blocks = []; @@ -385,7 +386,7 @@ var qrcodegen = new function() { result.push(blocks[j][i]); } } - if (result.length != Math.floor(QrCode.getNumRawDataModules(version) / 8)) + if (result.length != rawCodewords) throw "Assertion error"; return result; } diff --git a/python/qrcodegen.py b/python/qrcodegen.py index d61297b..7edb2b3 100644 --- a/python/qrcodegen.py +++ b/python/qrcodegen.py @@ -362,8 +362,9 @@ class QrCode(object): # Calculate parameter numbers numblocks = QrCode._NUM_ERROR_CORRECTION_BLOCKS[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 - shortblocklen = self._get_num_raw_data_modules(version) // 8 // numblocks + rawcodewords = QrCode._get_num_raw_data_modules(version) // 8 + numshortblocks = numblocks - rawcodewords % numblocks + shortblocklen = rawcodewords // numblocks # Split data into blocks and append ECC to each block blocks = [] @@ -386,7 +387,7 @@ class QrCode(object): # Skip the padding byte in short blocks if i != shortblocklen - blockecclen or j >= numshortblocks: result.append(blk[i]) - assert len(result) == QrCode._get_num_raw_data_modules(version) // 8 + assert len(result) == rawcodewords return result