Tweaked Python, Rust, TypeScript code to rename the constructor `mask` parameter to `msk` for consistency with Java and C++ language ports (related to commit 67c62461d3), and to avoid potential confusion with the object field named `mask`.

pull/134/head
Project Nayuki 3 years ago
parent d6be9c5e44
commit f0945bc682

@ -161,7 +161,7 @@ class QrCode:
# ---- Constructor (low level) ---- # ---- 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, """Creates a new QR Code with the given version number,
error correction level, data codeword bytes, and mask number. error correction level, data codeword bytes, and mask number.
This is a low-level API that most users should not use directly. 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 # Check scalar arguments and set fields
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): if not (-1 <= msk <= 7):
raise ValueError("Mask value out of range") 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")
@ -189,20 +189,20 @@ class QrCode:
self._draw_codewords(allcodewords) self._draw_codewords(allcodewords)
# Do masking # Do masking
if mask == -1: # Automatically choose best mask if msk == -1: # Automatically choose best mask
minpenalty: int = 1 << 32 minpenalty: int = 1 << 32
for i in range(8): for i in range(8):
self._apply_mask(i) self._apply_mask(i)
self._draw_format_bits(i) self._draw_format_bits(i)
penalty = self._get_penalty_score() penalty = self._get_penalty_score()
if penalty < minpenalty: if penalty < minpenalty:
mask = i msk = i
minpenalty = penalty minpenalty = penalty
self._apply_mask(i) # Undoes the mask due to XOR self._apply_mask(i) # Undoes the mask due to XOR
assert 0 <= mask <= 7 assert 0 <= msk <= 7
self._mask = mask self._mask = msk
self._apply_mask(mask) # Apply the final choice of mask self._apply_mask(msk) # Apply the final choice of mask
self._draw_format_bits(mask) # Overwrite old format bits self._draw_format_bits(msk) # Overwrite old format bits
del self._isfunction del self._isfunction

@ -277,7 +277,7 @@ impl QrCode {
/// ///
/// This is a low-level API that most users should not use directly. /// This is a low-level API that most users should not use directly.
/// A mid-level API is the `encode_segments()` function. /// A mid-level API is the `encode_segments()` function.
pub fn encode_codewords(ver: Version, ecl: QrCodeEcc, datacodewords: &[u8], mut mask: Option<Mask>) -> Self { pub fn encode_codewords(ver: Version, ecl: QrCodeEcc, datacodewords: &[u8], mut msk: Option<Mask>) -> Self {
// Initialize fields // Initialize fields
let size = usize::from(ver.value()) * 4 + 17; let size = usize::from(ver.value()) * 4 + 17;
let mut result = Self { let mut result = Self {
@ -295,7 +295,7 @@ impl QrCode {
result.draw_codewords(&allcodewords); result.draw_codewords(&allcodewords);
// Do masking // Do masking
if mask.is_none() { // Automatically choose best mask if msk.is_none() { // Automatically choose best mask
let mut minpenalty = std::i32::MAX; let mut minpenalty = std::i32::MAX;
for i in 0u8 .. 8 { for i in 0u8 .. 8 {
let newmask = Mask::new(i); let newmask = Mask::new(i);
@ -303,16 +303,16 @@ impl QrCode {
result.draw_format_bits(newmask); result.draw_format_bits(newmask);
let penalty: i32 = result.get_penalty_score(); let penalty: i32 = result.get_penalty_score();
if penalty < minpenalty { if penalty < minpenalty {
mask = Some(newmask); msk = Some(newmask);
minpenalty = penalty; minpenalty = penalty;
} }
result.apply_mask(newmask); // Undoes the mask due to XOR result.apply_mask(newmask); // Undoes the mask due to XOR
} }
} }
let mask: Mask = mask.unwrap(); let msk: Mask = msk.unwrap();
result.mask = mask; result.mask = msk;
result.apply_mask(mask); // Apply the final choice of mask result.apply_mask(msk); // Apply the final choice of mask
result.draw_format_bits(mask); // Overwrite old format bits result.draw_format_bits(msk); // Overwrite old format bits
result.isfunction.clear(); result.isfunction.clear();
result.isfunction.shrink_to_fit(); result.isfunction.shrink_to_fit();

@ -155,6 +155,11 @@ namespace qrcodegen {
// 21 and 177 (inclusive). This is equal to version * 4 + 17. // 21 and 177 (inclusive). This is equal to version * 4 + 17.
public readonly size: int; 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). // The modules of this QR Code (false = light, true = dark).
// Immutable after constructor finishes. Accessed through getModule(). // Immutable after constructor finishes. Accessed through getModule().
private readonly modules : Array<Array<boolean>> = []; private readonly modules : Array<Array<boolean>> = [];
@ -179,15 +184,12 @@ namespace qrcodegen {
dataCodewords: Readonly<Array<byte>>, dataCodewords: Readonly<Array<byte>>,
// The index of the mask pattern used in this QR Code, which is between 0 and 7 (inclusive). msk: int) {
// 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) {
// Check scalar arguments // Check scalar arguments
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) if (msk < -1 || msk > 7)
throw "Mask value out of range"; throw "Mask value out of range";
this.size = version * 4 + 17; this.size = version * 4 + 17;
@ -206,24 +208,24 @@ namespace qrcodegen {
this.drawCodewords(allCodewords); this.drawCodewords(allCodewords);
// Do masking // Do masking
if (mask == -1) { // Automatically choose best mask if (msk == -1) { // Automatically choose best mask
let minPenalty: int = 1000000000; let minPenalty: int = 1000000000;
for (let i = 0; i < 8; i++) { for (let i = 0; i < 8; i++) {
this.applyMask(i); this.applyMask(i);
this.drawFormatBits(i); this.drawFormatBits(i);
const penalty: int = this.getPenaltyScore(); const penalty: int = this.getPenaltyScore();
if (penalty < minPenalty) { if (penalty < minPenalty) {
mask = i; msk = i;
minPenalty = penalty; minPenalty = penalty;
} }
this.applyMask(i); // Undoes the mask due to XOR this.applyMask(i); // Undoes the mask due to XOR
} }
} }
if (mask < 0 || mask > 7) if (msk < 0 || msk > 7)
throw "Assertion error"; throw "Assertion error";
this.mask = mask; this.mask = msk;
this.applyMask(mask); // Apply the final choice of mask this.applyMask(msk); // Apply the final choice of mask
this.drawFormatBits(mask); // Overwrite old format bits this.drawFormatBits(msk); // Overwrite old format bits
this.isFunction = []; this.isFunction = [];
} }

Loading…
Cancel
Save