From f0945bc682766bd46647f7be54ff34a5f2f8dc70 Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Sat, 6 Nov 2021 05:42:47 +0000 Subject: [PATCH] Tweaked Python, Rust, TypeScript code to rename the constructor `mask` parameter to `msk` for consistency with Java and C++ language ports (related to commit 67c62461d380), and to avoid potential confusion with the object field named `mask`. --- python/qrcodegen.py | 16 ++++++++-------- rust/src/lib.rs | 14 +++++++------- typescript-javascript/qrcodegen.ts | 24 +++++++++++++----------- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/python/qrcodegen.py b/python/qrcodegen.py index a563aed..254238c 100644 --- a/python/qrcodegen.py +++ b/python/qrcodegen.py @@ -161,7 +161,7 @@ class QrCode: # ---- Constructor (low level) ---- - def __init__(self, version: int, errcorlvl: QrCode.Ecc, datacodewords: Union[bytes,Sequence[int]], mask: int) -> None: + def __init__(self, version: int, errcorlvl: QrCode.Ecc, datacodewords: Union[bytes,Sequence[int]], msk: int) -> None: """Creates a new QR Code with the given version number, error correction level, data codeword bytes, and mask number. This is a low-level API that most users should not use directly. @@ -170,7 +170,7 @@ class QrCode: # Check scalar arguments and set fields if not (QrCode.MIN_VERSION <= version <= QrCode.MAX_VERSION): raise ValueError("Version value out of range") - if not (-1 <= mask <= 7): + if not (-1 <= msk <= 7): raise ValueError("Mask value out of range") if not isinstance(errcorlvl, QrCode.Ecc): raise TypeError("QrCode.Ecc expected") @@ -189,20 +189,20 @@ class QrCode: self._draw_codewords(allcodewords) # Do masking - if mask == -1: # Automatically choose best mask + if msk == -1: # Automatically choose best mask minpenalty: int = 1 << 32 for i in range(8): self._apply_mask(i) self._draw_format_bits(i) penalty = self._get_penalty_score() if penalty < minpenalty: - mask = i + msk = i minpenalty = penalty self._apply_mask(i) # Undoes the mask due to XOR - assert 0 <= mask <= 7 - self._mask = mask - self._apply_mask(mask) # Apply the final choice of mask - self._draw_format_bits(mask) # Overwrite old format bits + assert 0 <= msk <= 7 + self._mask = msk + self._apply_mask(msk) # Apply the final choice of mask + self._draw_format_bits(msk) # Overwrite old format bits del self._isfunction diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 32d0809..63bc129 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -277,7 +277,7 @@ impl QrCode { /// /// This is a low-level API that most users should not use directly. /// A mid-level API is the `encode_segments()` function. - pub fn encode_codewords(ver: Version, ecl: QrCodeEcc, datacodewords: &[u8], mut mask: Option) -> Self { + pub fn encode_codewords(ver: Version, ecl: QrCodeEcc, datacodewords: &[u8], mut msk: Option) -> Self { // Initialize fields let size = usize::from(ver.value()) * 4 + 17; let mut result = Self { @@ -295,7 +295,7 @@ impl QrCode { result.draw_codewords(&allcodewords); // Do masking - if mask.is_none() { // Automatically choose best mask + if msk.is_none() { // Automatically choose best mask let mut minpenalty = std::i32::MAX; for i in 0u8 .. 8 { let newmask = Mask::new(i); @@ -303,16 +303,16 @@ impl QrCode { result.draw_format_bits(newmask); let penalty: i32 = result.get_penalty_score(); if penalty < minpenalty { - mask = Some(newmask); + msk = Some(newmask); minpenalty = penalty; } result.apply_mask(newmask); // Undoes the mask due to XOR } } - let mask: Mask = mask.unwrap(); - result.mask = mask; - result.apply_mask(mask); // Apply the final choice of mask - result.draw_format_bits(mask); // Overwrite old format bits + let msk: Mask = msk.unwrap(); + result.mask = msk; + result.apply_mask(msk); // Apply the final choice of mask + result.draw_format_bits(msk); // Overwrite old format bits result.isfunction.clear(); result.isfunction.shrink_to_fit(); diff --git a/typescript-javascript/qrcodegen.ts b/typescript-javascript/qrcodegen.ts index d27c0dc..e0426fc 100644 --- a/typescript-javascript/qrcodegen.ts +++ b/typescript-javascript/qrcodegen.ts @@ -155,6 +155,11 @@ namespace qrcodegen { // 21 and 177 (inclusive). This is equal to version * 4 + 17. public readonly size: int; + // The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive). + // Even if a QR Code is created with automatic masking requested (mask = -1), + // the resulting object still has a mask value between 0 and 7. + public readonly mask: int; + // The modules of this QR Code (false = light, true = dark). // Immutable after constructor finishes. Accessed through getModule(). private readonly modules : Array> = []; @@ -179,15 +184,12 @@ namespace qrcodegen { dataCodewords: Readonly>, - // The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive). - // Even if a QR Code is created with automatic masking requested (mask = -1), - // the resulting object still has a mask value between 0 and 7. - public readonly mask: int) { + msk: int) { // Check scalar arguments if (version < QrCode.MIN_VERSION || version > QrCode.MAX_VERSION) throw "Version value out of range"; - if (mask < -1 || mask > 7) + if (msk < -1 || msk > 7) throw "Mask value out of range"; this.size = version * 4 + 17; @@ -206,24 +208,24 @@ namespace qrcodegen { this.drawCodewords(allCodewords); // Do masking - if (mask == -1) { // Automatically choose best mask + if (msk == -1) { // Automatically choose best mask let minPenalty: int = 1000000000; for (let i = 0; i < 8; i++) { this.applyMask(i); this.drawFormatBits(i); const penalty: int = this.getPenaltyScore(); if (penalty < minPenalty) { - mask = i; + msk = i; minPenalty = penalty; } this.applyMask(i); // Undoes the mask due to XOR } } - if (mask < 0 || mask > 7) + if (msk < 0 || msk > 7) throw "Assertion error"; - this.mask = mask; - this.applyMask(mask); // Apply the final choice of mask - this.drawFormatBits(mask); // Overwrite old format bits + this.mask = msk; + this.applyMask(msk); // Apply the final choice of mask + this.drawFormatBits(msk); // Overwrite old format bits this.isFunction = []; }