Tweaked Java code to convert most explicit assertion checks to native assert statements, for compactness and because the code quality ensures that the checks shouldn't fail in normal usage.

This commit is contained in:
Project Nayuki 2018-08-22 18:15:15 +00:00
parent d8d2da49e4
commit 0e2ecff58e
2 changed files with 9 additions and 18 deletions

View File

@ -128,8 +128,7 @@ public final class QrCode {
if (version >= maxVersion) // All versions in the range could not fit the given data if (version >= maxVersion) // All versions in the range could not fit the given data
throw new IllegalArgumentException("Data too long"); throw new IllegalArgumentException("Data too long");
} }
if (dataUsedBits == -1) assert dataUsedBits != -1;
throw new AssertionError();
// Increase the error correction level while the data still fits in the current version number // Increase the error correction level while the data still fits in the current version number
for (Ecc newEcl : Ecc.values()) { for (Ecc newEcl : Ecc.values()) {
@ -153,8 +152,7 @@ public final class QrCode {
// Pad with alternate bytes until data capacity is reached // Pad with alternate bytes until data capacity is reached
for (int padByte = 0xEC; bb.bitLength() < dataCapacityBits; padByte ^= 0xEC ^ 0x11) for (int padByte = 0xEC; bb.bitLength() < dataCapacityBits; padByte ^= 0xEC ^ 0x11)
bb.appendBits(padByte, 8); bb.appendBits(padByte, 8);
if (bb.bitLength() % 8 != 0) assert bb.bitLength() % 8 == 0;
throw new AssertionError();
// Create the QR Code symbol // Create the QR Code symbol
return new QrCode(version, ecl, bb.getBytes(), mask); return new QrCode(version, ecl, bb.getBytes(), mask);
@ -354,8 +352,7 @@ public final class QrCode {
rem = (rem << 1) ^ ((rem >>> 9) * 0x537); rem = (rem << 1) ^ ((rem >>> 9) * 0x537);
data = data << 10 | rem; data = data << 10 | rem;
data ^= 0x5412; // uint15 data ^= 0x5412; // uint15
if (data >>> 15 != 0) assert data >>> 15 == 0;
throw new AssertionError();
// Draw first copy // Draw first copy
for (int i = 0; i <= 5; i++) for (int i = 0; i <= 5; i++)
@ -386,8 +383,7 @@ public final class QrCode {
for (int i = 0; i < 12; i++) for (int i = 0; i < 12; i++)
rem = (rem << 1) ^ ((rem >>> 11) * 0x1F25); rem = (rem << 1) ^ ((rem >>> 11) * 0x1F25);
int data = version << 12 | rem; // uint18 int data = version << 12 | rem; // uint18
if (data >>> 18 != 0) assert data >>> 18 == 0;
throw new AssertionError();
// Draw two copies // Draw two copies
for (int i = 0; i < 18; i++) { for (int i = 0; i < 18; i++) {
@ -497,8 +493,7 @@ public final class QrCode {
} }
} }
} }
if (i != data.length * 8) assert i == data.length * 8;
throw new AssertionError();
} }
@ -546,8 +541,7 @@ public final class QrCode {
applyMask(i); // Undoes the mask due to XOR applyMask(i); // Undoes the mask due to XOR
} }
} }
if (mask < 0 || mask > 7) assert 0 <= mask && mask <= 7;
throw new AssertionError();
drawFormatBits(mask); // Overwrite old format bits drawFormatBits(mask); // Overwrite old format bits
applyMask(mask); // Apply the final choice of mask applyMask(mask); // Apply the final choice of mask
return mask; // The caller shall assign this value to the final-declared field return mask; // The caller shall assign this value to the final-declared field
@ -837,16 +831,14 @@ public final class QrCode {
// Returns the product of the two given field elements modulo GF(2^8/0x11D). The arguments and result // Returns the product of the two given field elements modulo GF(2^8/0x11D). The arguments and result
// are unsigned 8-bit integers. This could be implemented as a lookup table of 256*256 entries of uint8. // are unsigned 8-bit integers. This could be implemented as a lookup table of 256*256 entries of uint8.
private static int multiply(int x, int y) { private static int multiply(int x, int y) {
if (x >>> 8 != 0 || y >>> 8 != 0) assert x >>> 8 == 0 && y >>> 8 == 0;
throw new IllegalArgumentException("Byte out of range");
// Russian peasant multiplication // Russian peasant multiplication
int z = 0; int z = 0;
for (int i = 7; i >= 0; i--) { for (int i = 7; i >= 0; i--) {
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 new AssertionError();
return z; return z;
} }

View File

@ -393,8 +393,7 @@ public final class QrSegmentAdvanced {
int j = ((bytes[i] & 0xFF) << 8) | (bytes[i + 1] & 0xFF); int j = ((bytes[i] & 0xFF) << 8) | (bytes[i + 1] & 0xFF);
if (j == 0xFFFF) if (j == 0xFFFF)
continue; continue;
if (UNICODE_TO_QR_KANJI[j] != -1) assert UNICODE_TO_QR_KANJI[j] == -1;
throw new AssertionError();
UNICODE_TO_QR_KANJI[j] = (short)(i / 2); UNICODE_TO_QR_KANJI[j] = (short)(i / 2);
} }
} }