Refactored TypeScript code to add assert() function to make it easier to read the intent and compare to other language ports.

pull/134/head
Project Nayuki 3 years ago
parent ad537b93d9
commit cb4cecf5e3

@ -121,17 +121,14 @@ namespace qrcodegen {
for (const b of seg.getData()) for (const b of seg.getData())
bb.push(b); bb.push(b);
} }
if (bb.length != dataUsedBits) assert(bb.length == dataUsedBits);
throw "Assertion error";
// Add terminator and pad up to a byte if applicable // Add terminator and pad up to a byte if applicable
const dataCapacityBits: int = QrCode.getNumDataCodewords(version, ecl) * 8; const dataCapacityBits: int = QrCode.getNumDataCodewords(version, ecl) * 8;
if (bb.length > dataCapacityBits) assert(bb.length <= dataCapacityBits);
throw "Assertion error";
appendBits(0, Math.min(4, dataCapacityBits - bb.length), bb); appendBits(0, Math.min(4, dataCapacityBits - bb.length), bb);
appendBits(0, (8 - bb.length % 8) % 8, bb); appendBits(0, (8 - bb.length % 8) % 8, bb);
if (bb.length % 8 != 0) assert(bb.length % 8 == 0);
throw "Assertion error";
// Pad with alternating bytes until data capacity is reached // Pad with alternating bytes until data capacity is reached
for (let padByte = 0xEC; bb.length < dataCapacityBits; padByte ^= 0xEC ^ 0x11) for (let padByte = 0xEC; bb.length < dataCapacityBits; padByte ^= 0xEC ^ 0x11)
@ -221,8 +218,7 @@ namespace qrcodegen {
this.applyMask(i); // Undoes the mask due to XOR this.applyMask(i); // Undoes the mask due to XOR
} }
} }
if (msk < 0 || msk > 7) assert(0 <= msk && msk <= 7);
throw "Assertion error";
this.mask = msk; this.mask = msk;
this.applyMask(msk); // Apply the final choice of mask this.applyMask(msk); // Apply the final choice of mask
this.drawFormatBits(msk); // Overwrite old format bits this.drawFormatBits(msk); // Overwrite old format bits
@ -282,8 +278,7 @@ namespace qrcodegen {
for (let i = 0; i < 10; i++) for (let i = 0; i < 10; i++)
rem = (rem << 1) ^ ((rem >>> 9) * 0x537); rem = (rem << 1) ^ ((rem >>> 9) * 0x537);
const bits = (data << 10 | rem) ^ 0x5412; // uint15 const bits = (data << 10 | rem) ^ 0x5412; // uint15
if (bits >>> 15 != 0) assert(bits >>> 15 == 0);
throw "Assertion error";
// Draw first copy // Draw first copy
for (let i = 0; i <= 5; i++) for (let i = 0; i <= 5; i++)
@ -314,8 +309,7 @@ namespace qrcodegen {
for (let i = 0; i < 12; i++) for (let i = 0; i < 12; i++)
rem = (rem << 1) ^ ((rem >>> 11) * 0x1F25); rem = (rem << 1) ^ ((rem >>> 11) * 0x1F25);
const bits: int = this.version << 12 | rem; // uint18 const bits: int = this.version << 12 | rem; // uint18
if (bits >>> 18 != 0) assert(bits >>> 18 == 0);
throw "Assertion error";
// Draw two copies // Draw two copies
for (let i = 0; i < 18; i++) { for (let i = 0; i < 18; i++) {
@ -399,8 +393,7 @@ namespace qrcodegen {
result.push(block[i]); result.push(block[i]);
}); });
} }
if (result.length != rawCodewords) assert(result.length == rawCodewords);
throw "Assertion error";
return result; return result;
} }
@ -429,8 +422,7 @@ namespace qrcodegen {
} }
} }
} }
if (i != data.length * 8) assert(i == data.length * 8);
throw "Assertion error";
} }
@ -531,11 +523,9 @@ namespace qrcodegen {
const total: int = this.size * this.size; // Note that size is odd, so dark/total != 1/2 const total: int = this.size * this.size; // Note that size is odd, so dark/total != 1/2
// Compute the smallest integer k >= 0 such that (45-5k)% <= dark/total <= (55+5k)% // Compute the smallest integer k >= 0 such that (45-5k)% <= dark/total <= (55+5k)%
const k: int = Math.ceil(Math.abs(dark * 20 - total * 10) / total) - 1; const k: int = Math.ceil(Math.abs(dark * 20 - total * 10) / total) - 1;
if (!(0 <= k && k <= 9)) assert(0 <= k && k <= 9);
throw "Assertion error";
result += k * QrCode.PENALTY_N4; result += k * QrCode.PENALTY_N4;
if (!(0 <= result && result <= 2568888)) // Non-tight upper bound based on default values of PENALTY_N1, ..., N4 assert(0 <= result && result <= 2568888); // Non-tight upper bound based on default values of PENALTY_N1, ..., N4
throw "Assertion error";
return result; return result;
} }
@ -573,8 +563,7 @@ namespace qrcodegen {
if (ver >= 7) if (ver >= 7)
result -= 36; result -= 36;
} }
if (!(208 <= result && result <= 29648)) assert(208 <= result && result <= 29648);
throw "Assertion error";
return result; return result;
} }
@ -642,8 +631,7 @@ namespace qrcodegen {
z = (z << 1) ^ ((z >>> 7) * 0x11D); z = (z << 1) ^ ((z >>> 7) * 0x11D);
z ^= ((y >>> i) & 1) * x; z ^= ((y >>> i) & 1) * x;
} }
if (z >>> 8 != 0) assert(z >>> 8 == 0);
throw "Assertion error";
return z as byte; return z as byte;
} }
@ -652,8 +640,7 @@ namespace qrcodegen {
// returns either 0, 1, or 2. A helper function for getPenaltyScore(). // returns either 0, 1, or 2. A helper function for getPenaltyScore().
private finderPenaltyCountPatterns(runHistory: Readonly<Array<int>>): int { private finderPenaltyCountPatterns(runHistory: Readonly<Array<int>>): int {
const n: int = runHistory[1]; const n: int = runHistory[1];
if (n > this.size * 3) assert(n <= this.size * 3);
throw "Assertion error";
const core: boolean = n > 0 && runHistory[2] == n && runHistory[3] == n * 3 && runHistory[4] == n && runHistory[5] == n; const core: boolean = n > 0 && runHistory[2] == n && runHistory[3] == n * 3 && runHistory[4] == n && runHistory[5] == n;
return (core && runHistory[0] >= n * 4 && runHistory[6] >= n ? 1 : 0) return (core && runHistory[0] >= n * 4 && runHistory[6] >= n ? 1 : 0)
+ (core && runHistory[6] >= n * 4 && runHistory[0] >= n ? 1 : 0); + (core && runHistory[6] >= n * 4 && runHistory[0] >= n ? 1 : 0);
@ -731,6 +718,13 @@ namespace qrcodegen {
} }
// Throws an exception if the given condition is false.
function assert(cond: boolean): void {
if (!cond)
throw "Assertion error";
}
/*---- Data segment class ----*/ /*---- Data segment class ----*/

Loading…
Cancel
Save