Tweaked comments and statement order in QrCode constructor, in most languages.

This commit is contained in:
Project Nayuki 2018-10-05 02:14:43 +00:00
parent d060e97e03
commit 06d80aade3
5 changed files with 16 additions and 20 deletions

View File

@ -115,16 +115,14 @@ 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 mask) :
// Initialize fields // Initialize fields and check arguments
version(ver), version(ver),
size(MIN_VERSION <= ver && ver <= MAX_VERSION ? ver * 4 + 17 : -1), // Avoid signed overflow undefined behavior
errorCorrectionLevel(ecl), errorCorrectionLevel(ecl),
modules (size, vector<bool>(size)), // Initially all white modules (size, vector<bool>(size)), // Initially all white
isFunction(size, vector<bool>(size)) { isFunction(size, vector<bool>(size)) {
// Check arguments
if (ver < MIN_VERSION || ver > MAX_VERSION || mask < -1 || mask > 7) if (ver < MIN_VERSION || ver > MAX_VERSION || mask < -1 || mask > 7)
throw std::domain_error("Value out of range"); throw std::domain_error("Value out of range");
size = ver * 4 + 17;
// Compute ECC, draw modules, do masking // Compute ECC, draw modules, do masking
drawFunctionPatterns(); drawFunctionPatterns();

View File

@ -213,15 +213,13 @@ public final class QrCode {
* or if the data is the wrong length for the specified version and error correction level * 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 mask) {
// Check arguments // Check arguments and initialize fields
errorCorrectionLevel = Objects.requireNonNull(ecl);
Objects.requireNonNull(dataCodewords);
if (ver < MIN_VERSION || ver > MAX_VERSION || mask < -1 || mask > 7) if (ver < MIN_VERSION || ver > MAX_VERSION || mask < -1 || mask > 7)
throw new IllegalArgumentException("Value out of range"); throw new IllegalArgumentException("Value out of range");
// Initialize fields
version = ver; version = ver;
size = ver * 4 + 17; size = ver * 4 + 17;
errorCorrectionLevel = Objects.requireNonNull(ecl);
Objects.requireNonNull(dataCodewords);
modules = new boolean[size][size]; // Initially all white modules = new boolean[size][size]; // Initially all white
isFunction = new boolean[size][size]; isFunction = new boolean[size][size];

View File

@ -72,11 +72,11 @@ var qrcodegen = new function() {
/*---- Constructor (low level) ----*/ /*---- Constructor (low level) ----*/
// Check arguments and handle simple scalar fields // Check scalar arguments
if (mask < -1 || mask > 7)
throw "Mask value out of range";
if (version < MIN_VERSION || version > MAX_VERSION) if (version < MIN_VERSION || version > MAX_VERSION)
throw "Version value out of range"; throw "Version value out of range";
if (mask < -1 || mask > 7)
throw "Mask value out of range";
var size = version * 4 + 17; var size = version * 4 + 17;
// Initialize both grids to be size*size arrays of Boolean false // Initialize both grids to be size*size arrays of Boolean false

View File

@ -151,21 +151,21 @@ class QrCode(object):
and mask number. mask = -1 is for automatic choice, or 0 to 7 for fixed choice. This is a cumbersome low-level constructor and mask number. mask = -1 is for automatic choice, or 0 to 7 for fixed choice. This is a cumbersome low-level constructor
that should not be invoked directly by the user. To go one level up, see the QrCode.encode_segments() function.""" that should not be invoked directly by the user. To go one level up, see the QrCode.encode_segments() function."""
# Check arguments and handle simple scalar fields # Check scalar arguments and set fields
if not (-1 <= mask <= 7):
raise ValueError("Mask value out of range")
if not (QrCode.MIN_VERSION <= version <= QrCode.MAX_VERSION): if not (QrCode.MIN_VERSION <= version <= QrCode.MAX_VERSION):
raise ValueError("Version value out of range") raise ValueError("Version value out of range")
if not (-1 <= mask <= 7):
raise ValueError("Mask value out of range")
if not isinstance(errcorlvl, QrCode.Ecc): if not isinstance(errcorlvl, QrCode.Ecc):
raise TypeError("QrCode.Ecc expected") raise TypeError("QrCode.Ecc expected")
self._version = version self._version = version
self._errcorlvl = errcorlvl
self._size = version * 4 + 17 self._size = version * 4 + 17
self._errcorlvl = errcorlvl
if len(datacodewords) != QrCode._get_num_data_codewords(version, errcorlvl): if len(datacodewords) != QrCode._get_num_data_codewords(version, errcorlvl):
raise ValueError("Invalid array length") raise ValueError("Invalid array length")
# Initialize grids of modules # Initialize both grids to be size*size arrays of Boolean false
# The modules of this QR Code symbol (False = white, True = black). Immutable after constructor finishes # The modules of this QR Code symbol (False = white, True = black). Immutable after constructor finishes
self._modules = [[False] * self._size for _ in range(self._size)] # Initially all white self._modules = [[False] * self._size for _ in range(self._size)] # Initially all white
# Indicates function modules that are not subjected to masking. Discarded when constructor finishes # Indicates function modules that are not subjected to masking. Discarded when constructor finishes

View File

@ -160,11 +160,11 @@ namespace qrcodegen {
// (mask = -1), the resulting object will still have a mask value between 0 and 7. // (mask = -1), the resulting object will still have a mask value between 0 and 7.
public readonly mask: int) { public readonly mask: int) {
// Check arguments and handle simple scalar fields // Check scalar arguments
if (mask < -1 || mask > 7)
throw "Mask value out of range";
if (version < QrCode.MIN_VERSION || version > QrCode.MAX_VERSION) if (version < QrCode.MIN_VERSION || version > QrCode.MAX_VERSION)
throw "Version value out of range"; throw "Version value out of range";
if (mask < -1 || mask > 7)
throw "Mask value out of range";
this.size = version * 4 + 17; this.size = version * 4 + 17;
// Initialize both grids to be size*size arrays of Boolean false // Initialize both grids to be size*size arrays of Boolean false