Simplified QrSegment.Mode.numCharCountBits() in all language versions.

pull/39/merge
Project Nayuki 6 years ago
parent 16b59e207d
commit 362d552c92

@ -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]. // given mode at the given version number. The result is in the range [0, 16].
static int numCharCountBits(enum qrcodegen_Mode mode, int version) { static int numCharCountBits(enum qrcodegen_Mode mode, int version) {
assert(qrcodegen_VERSION_MIN <= version && version <= qrcodegen_VERSION_MAX); assert(qrcodegen_VERSION_MIN <= version && version <= qrcodegen_VERSION_MAX);
int i; int i = (version + 7) / 17;
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
switch (mode) { switch (mode) {
case qrcodegen_Mode_NUMERIC : { static const int temp[] = {10, 12, 14}; return temp[i]; } 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]; } case qrcodegen_Mode_ALPHANUMERIC: { static const int temp[] = { 9, 11, 13}; return temp[i]; }

@ -47,10 +47,7 @@ int QrSegment::Mode::getModeBits() const {
int QrSegment::Mode::numCharCountBits(int ver) const { int QrSegment::Mode::numCharCountBits(int ver) const {
if ( 1 <= ver && ver <= 9) return numBitsCharCount[0]; return numBitsCharCount[(ver + 7) / 17];
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");
} }

@ -270,10 +270,8 @@ public final class QrSegment {
* @throws IllegalArgumentException if the version number is out of range * @throws IllegalArgumentException if the version number is out of range
*/ */
int numCharCountBits(int ver) { int numCharCountBits(int ver) {
if ( 1 <= ver && ver <= 9) return numBitsCharCount[0]; assert QrCode.MIN_VERSION <= ver && ver <= QrCode.MAX_VERSION;
else if (10 <= ver && ver <= 26) return numBitsCharCount[1]; return numBitsCharCount[(ver + 7) / 17];
else if (27 <= ver && ver <= 40) return numBitsCharCount[2];
else throw new IllegalArgumentException("Version number out of range");
} }
} }

@ -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. // (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) { this.numCharCountBits = function(ver) {
if ( 1 <= ver && ver <= 9) return ccbits[0]; return ccbits[Math.floor((ver + 7) / 17)];
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";
}; };
} }

@ -740,10 +740,7 @@ class QrSegment(object):
# Package-private method # Package-private method
def num_char_count_bits(self, ver): 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.""" """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] return self._charcounts[(ver + 7) // 17]
elif 10 <= ver <= 26: return self._charcounts[1]
elif 27 <= ver <= 40: return self._charcounts[2]
else: raise ValueError("Version number out of range")
# Public constants. Create them outside the class. # Public constants. Create them outside the class.
Mode.NUMERIC = Mode(0x1, (10, 12, 14)) Mode.NUMERIC = Mode(0x1, (10, 12, 14))

@ -1048,24 +1048,13 @@ impl QrSegmentMode {
// for this mode object at the given version number. // for this mode object at the given version number.
pub fn num_char_count_bits(&self, ver: Version) -> u8 { pub fn num_char_count_bits(&self, ver: Version) -> u8 {
use QrSegmentMode::*; use QrSegmentMode::*;
let array: [u8; 3] = match *self { (match *self {
Numeric => [10, 12, 14], Numeric => [10, 12, 14],
Alphanumeric => [ 9, 11, 13], Alphanumeric => [ 9, 11, 13],
Byte => [ 8, 16, 16], Byte => [ 8, 16, 16],
Kanji => [ 8, 10, 12], Kanji => [ 8, 10, 12],
Eci => [ 0, 0, 0], Eci => [ 0, 0, 0],
}; })[((ver.value() + 7) / 17) as usize]
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");
}
} }
} }

@ -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. // (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 { public numCharCountBits(ver: int): int {
if ( 1 <= ver && ver <= 9) return this.numBitsCharCount[0]; return this.numBitsCharCount[Math.floor((ver + 7) / 17)];
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";
} }
} }

Loading…
Cancel
Save