From 0e5e3c1b6125ffadfa3e05f2070773ce00ae7683 Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Sat, 25 Aug 2018 23:52:36 +0000 Subject: [PATCH] Tweaked code to convert most explicit assertion checks to native assert statements. --- src/io/nayuki/fastqrcodegen/QrCode.java | 12 ++++-------- src/io/nayuki/fastqrcodegen/QrTemplate.java | 6 ++---- .../nayuki/fastqrcodegen/ReedSolomonGenerator.java | 6 ++---- 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/io/nayuki/fastqrcodegen/QrCode.java b/src/io/nayuki/fastqrcodegen/QrCode.java index 4ceb507..cdb6bab 100644 --- a/src/io/nayuki/fastqrcodegen/QrCode.java +++ b/src/io/nayuki/fastqrcodegen/QrCode.java @@ -70,8 +70,7 @@ public final class QrCode { if (version >= maxVersion) // All versions in the range could not fit the given data throw new IllegalArgumentException("Data too long"); } - if (dataUsedBits == -1) - throw new AssertionError(); + assert dataUsedBits != -1; // Increase the error correction level while the data still fits in the current version number for (Ecc newEcl : Ecc.values()) { @@ -95,8 +94,7 @@ public final class QrCode { // Pad with alternate bytes until data capacity is reached for (int padByte = 0xEC; bb.bitLength < dataCapacityBits; padByte ^= 0xEC ^ 0x11) bb.appendBits(padByte, 8); - if (bb.bitLength % 8 != 0) - throw new AssertionError(); + assert bb.bitLength % 8 == 0; // Create the QR Code symbol return new QrCode(version, ecl, bb.getBytes(), mask); @@ -245,8 +243,7 @@ public final class QrCode { rem = (rem << 1) ^ ((rem >>> 9) * 0x537); data = data << 10 | rem; data ^= 0x5412; // uint15 - if (data >>> 15 != 0) - throw new AssertionError(); + assert data >>> 15 == 0; // Draw first copy for (int i = 0; i <= 5; i++) @@ -363,8 +360,7 @@ public final class QrCode { applyMask(masks[i]); // Undoes the mask due to XOR } } - if (mask < 0 || mask > 7) - throw new AssertionError(); + assert 0 <= mask && mask <= 7; drawFormatBits(mask); // Overwrite old format bits applyMask(masks[mask]); // Apply the final choice of mask return mask; // The caller shall assign this value to the final-declared field diff --git a/src/io/nayuki/fastqrcodegen/QrTemplate.java b/src/io/nayuki/fastqrcodegen/QrTemplate.java index 61acfa6..4353e40 100644 --- a/src/io/nayuki/fastqrcodegen/QrTemplate.java +++ b/src/io/nayuki/fastqrcodegen/QrTemplate.java @@ -169,8 +169,7 @@ final class QrTemplate { for (int i = 0; i < 12; i++) rem = (rem << 1) ^ ((rem >>> 11) * 0x1F25); int data = version << 12 | rem; // uint18 - if (data >>> 18 != 0) - throw new AssertionError(); + assert data >>> 18 == 0; // Draw two copies for (int i = 0; i < 18; i++) { @@ -249,8 +248,7 @@ final class QrTemplate { } } } - if (i != result.length) - throw new AssertionError(); + assert i == result.length; return result; } diff --git a/src/io/nayuki/fastqrcodegen/ReedSolomonGenerator.java b/src/io/nayuki/fastqrcodegen/ReedSolomonGenerator.java index 7662b65..ae17275 100644 --- a/src/io/nayuki/fastqrcodegen/ReedSolomonGenerator.java +++ b/src/io/nayuki/fastqrcodegen/ReedSolomonGenerator.java @@ -140,16 +140,14 @@ final class ReedSolomonGenerator { // 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. private static int multiply(int x, int y) { - if (x >>> 8 != 0 || y >>> 8 != 0) - throw new IllegalArgumentException("Byte out of range"); + assert x >>> 8 == 0 && y >>> 8 == 0; // Russian peasant multiplication int z = 0; for (int i = 7; i >= 0; i--) { z = (z << 1) ^ ((z >>> 7) * 0x11D); z ^= ((y >>> i) & 1) * x; } - if (z >>> 8 != 0) - throw new AssertionError(); + assert z >>> 8 == 0; return z; }