diff --git a/c/qrcodegen.c b/c/qrcodegen.c index 6a93925..ccc690f 100644 --- a/c/qrcodegen.c +++ b/c/qrcodegen.c @@ -999,12 +999,7 @@ testable int getTotalBits(const struct qrcodegen_Segment segs[], size_t len, int // given mode at the given version number. The result is in the range [0, 16]. static int numCharCountBits(enum qrcodegen_Mode mode, int version) { assert(qrcodegen_VERSION_MIN <= version && version <= qrcodegen_VERSION_MAX); - int i; - if ( 1 <= version && version <= 9) i = 0; - else if (10 <= version && version <= 26) i = 1; - else if (27 <= version && version <= 40) i = 2; - else { assert(false); return -1; } // Dummy value - + int i = (version + 7) / 17; switch (mode) { case qrcodegen_Mode_NUMERIC : { static const int temp[] = {10, 12, 14}; return temp[i]; } case qrcodegen_Mode_ALPHANUMERIC: { static const int temp[] = { 9, 11, 13}; return temp[i]; } diff --git a/cpp/QrSegment.cpp b/cpp/QrSegment.cpp index 1b674ef..4dbae22 100644 --- a/cpp/QrSegment.cpp +++ b/cpp/QrSegment.cpp @@ -47,10 +47,7 @@ int QrSegment::Mode::getModeBits() const { int QrSegment::Mode::numCharCountBits(int ver) const { - if ( 1 <= ver && ver <= 9) return numBitsCharCount[0]; - else if (10 <= ver && ver <= 26) return numBitsCharCount[1]; - else if (27 <= ver && ver <= 40) return numBitsCharCount[2]; - else throw std::domain_error("Version number out of range"); + return numBitsCharCount[(ver + 7) / 17]; } diff --git a/java/io/nayuki/qrcodegen/QrSegment.java b/java/io/nayuki/qrcodegen/QrSegment.java index 81e754c..109b016 100644 --- a/java/io/nayuki/qrcodegen/QrSegment.java +++ b/java/io/nayuki/qrcodegen/QrSegment.java @@ -270,10 +270,8 @@ public final class QrSegment { * @throws IllegalArgumentException if the version number is out of range */ int numCharCountBits(int ver) { - if ( 1 <= ver && ver <= 9) return numBitsCharCount[0]; - else if (10 <= ver && ver <= 26) return numBitsCharCount[1]; - else if (27 <= ver && ver <= 40) return numBitsCharCount[2]; - else throw new IllegalArgumentException("Version number out of range"); + assert QrCode.MIN_VERSION <= ver && ver <= QrCode.MAX_VERSION; + return numBitsCharCount[(ver + 7) / 17]; } } diff --git a/javascript/qrcodegen.js b/javascript/qrcodegen.js index 331536f..babdfeb 100644 --- a/javascript/qrcodegen.js +++ b/javascript/qrcodegen.js @@ -861,10 +861,7 @@ var qrcodegen = new function() { // (Package-private) Returns the bit width of the segment character count field for this mode object at the given version number. this.numCharCountBits = function(ver) { - if ( 1 <= ver && ver <= 9) return ccbits[0]; - else if (10 <= ver && ver <= 26) return ccbits[1]; - else if (27 <= ver && ver <= 40) return ccbits[2]; - else throw "Version number out of range"; + return ccbits[Math.floor((ver + 7) / 17)]; }; } diff --git a/python/qrcodegen.py b/python/qrcodegen.py index d37ce14..6083a69 100644 --- a/python/qrcodegen.py +++ b/python/qrcodegen.py @@ -740,10 +740,7 @@ class QrSegment(object): # Package-private method def num_char_count_bits(self, ver): """Returns the bit width of the segment character count field for this mode object at the given version number.""" - if 1 <= ver <= 9: return self._charcounts[0] - elif 10 <= ver <= 26: return self._charcounts[1] - elif 27 <= ver <= 40: return self._charcounts[2] - else: raise ValueError("Version number out of range") + return self._charcounts[(ver + 7) // 17] # Public constants. Create them outside the class. Mode.NUMERIC = Mode(0x1, (10, 12, 14)) diff --git a/rust/src/lib.rs b/rust/src/lib.rs index eec747b..1f6bf23 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -1048,24 +1048,13 @@ impl QrSegmentMode { // for this mode object at the given version number. pub fn num_char_count_bits(&self, ver: Version) -> u8 { use QrSegmentMode::*; - let array: [u8; 3] = match *self { + (match *self { Numeric => [10, 12, 14], Alphanumeric => [ 9, 11, 13], Byte => [ 8, 16, 16], Kanji => [ 8, 10, 12], Eci => [ 0, 0, 0], - }; - - let ver = ver.value(); - if 1 <= ver && ver <= 9 { - array[0] - } else if 10 <= ver && ver <= 26 { - array[1] - } else if 27 <= ver && ver <= 40 { - array[2] - } else { - panic!("Version number out of range"); - } + })[((ver.value() + 7) / 17) as usize] } } diff --git a/typescript/qrcodegen.ts b/typescript/qrcodegen.ts index f9d44a0..f419145 100644 --- a/typescript/qrcodegen.ts +++ b/typescript/qrcodegen.ts @@ -1005,10 +1005,7 @@ namespace qrcodegen.QrSegment { // (Package-private) Returns the bit width of the segment character count field for this mode object at the given version number. public numCharCountBits(ver: int): int { - if ( 1 <= ver && ver <= 9) return this.numBitsCharCount[0]; - else if (10 <= ver && ver <= 26) return this.numBitsCharCount[1]; - else if (27 <= ver && ver <= 40) return this.numBitsCharCount[2]; - else throw "Version number out of range"; + return this.numBitsCharCount[Math.floor((ver + 7) / 17)]; } }