Deleted unnecessary version range check in two private functions, in all language versions except Rust.

pull/39/merge
Project Nayuki 6 years ago
parent 7fe69730c4
commit 16b59e207d

@ -231,8 +231,7 @@ testable void addEccAndInterleave(uint8_t data[], int version, enum qrcodegen_Ec
// for the given version number and error correction level. The result is in the range [9, 2956]. // for the given version number and error correction level. The result is in the range [9, 2956].
testable int getNumDataCodewords(int version, enum qrcodegen_Ecc ecl) { testable int getNumDataCodewords(int version, enum qrcodegen_Ecc ecl) {
int v = version, e = (int)ecl; int v = version, e = (int)ecl;
assert(qrcodegen_VERSION_MIN <= v && v <= qrcodegen_VERSION_MAX assert(0 <= e && e < 4);
&& 0 <= e && e < 4);
return getNumRawDataModules(v) / 8 return getNumRawDataModules(v) / 8
- ECC_CODEWORDS_PER_BLOCK[e][v] - ECC_CODEWORDS_PER_BLOCK[e][v]
* NUM_ERROR_CORRECTION_BLOCKS[e][v]; * NUM_ERROR_CORRECTION_BLOCKS[e][v];
@ -977,7 +976,6 @@ bool qrcodegen_encodeSegmentsAdvanced(const struct qrcodegen_Segment segs[], siz
// many characters to fit its length field, or the total bits exceeds INT16_MAX. // many characters to fit its length field, or the total bits exceeds INT16_MAX.
testable int getTotalBits(const struct qrcodegen_Segment segs[], size_t len, int version) { testable int getTotalBits(const struct qrcodegen_Segment segs[], size_t len, int version) {
assert(segs != NULL || len == 0); assert(segs != NULL || len == 0);
assert(qrcodegen_VERSION_MIN <= version && version <= qrcodegen_VERSION_MAX);
long result = 0; long result = 0;
for (size_t i = 0; i < len; i++) { for (size_t i = 0; i < len; i++) {
int numChars = segs[i].numChars; int numChars = segs[i].numChars;

@ -526,8 +526,6 @@ int QrCode::getNumRawDataModules(int ver) {
int QrCode::getNumDataCodewords(int ver, Ecc ecl) { int QrCode::getNumDataCodewords(int ver, Ecc ecl) {
if (ver < MIN_VERSION || ver > MAX_VERSION)
throw std::domain_error("Version number out of range");
return getNumRawDataModules(ver) / 8 return getNumRawDataModules(ver) / 8
- ECC_CODEWORDS_PER_BLOCK[static_cast<int>(ecl)][ver] - ECC_CODEWORDS_PER_BLOCK[static_cast<int>(ecl)][ver]
* NUM_ERROR_CORRECTION_BLOCKS[static_cast<int>(ecl)][ver]; * NUM_ERROR_CORRECTION_BLOCKS[static_cast<int>(ecl)][ver];

@ -171,8 +171,6 @@ QrSegment::QrSegment(Mode md, int numCh, std::vector<bool> &&dt) :
int QrSegment::getTotalBits(const vector<QrSegment> &segs, int version) { int QrSegment::getTotalBits(const vector<QrSegment> &segs, int version) {
if (version < 1 || version > 40)
throw std::domain_error("Version number out of range");
int result = 0; int result = 0;
for (const QrSegment &seg : segs) { for (const QrSegment &seg : segs) {
int ccbits = seg.mode.numCharCountBits(version); int ccbits = seg.mode.numCharCountBits(version);

@ -692,8 +692,6 @@ public final class QrCode {
// QR Code of the given version number and error correction level, with remainder bits discarded. // QR Code of the given version number and error correction level, with remainder bits discarded.
// This stateless pure function could be implemented as a (40*4)-cell lookup table. // This stateless pure function could be implemented as a (40*4)-cell lookup table.
static int getNumDataCodewords(int ver, Ecc ecl) { static int getNumDataCodewords(int ver, Ecc ecl) {
if (ver < MIN_VERSION || ver > MAX_VERSION)
throw new IllegalArgumentException("Version number out of range");
return getNumRawDataModules(ver) / 8 return getNumRawDataModules(ver) / 8
- ECC_CODEWORDS_PER_BLOCK[ecl.ordinal()][ver] - ECC_CODEWORDS_PER_BLOCK[ecl.ordinal()][ver]
* NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal()][ver]; * NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal()][ver];

@ -202,9 +202,6 @@ public final class QrSegment {
// many characters to fit its length field, or the total bits exceeds Integer.MAX_VALUE. // many characters to fit its length field, or the total bits exceeds Integer.MAX_VALUE.
static int getTotalBits(List<QrSegment> segs, int version) { static int getTotalBits(List<QrSegment> segs, int version) {
Objects.requireNonNull(segs); Objects.requireNonNull(segs);
if (version < 1 || version > 40)
throw new IllegalArgumentException("Version number out of range");
long result = 0; long result = 0;
for (QrSegment seg : segs) { for (QrSegment seg : segs) {
Objects.requireNonNull(seg); Objects.requireNonNull(seg);

@ -636,8 +636,6 @@ var qrcodegen = new function() {
// QR Code of the given version number and error correction level, with remainder bits discarded. // QR Code of the given version number and error correction level, with remainder bits discarded.
// This stateless pure function could be implemented as a (40*4)-cell lookup table. // This stateless pure function could be implemented as a (40*4)-cell lookup table.
QrCode.getNumDataCodewords = function(ver, ecl) { QrCode.getNumDataCodewords = function(ver, ecl) {
if (ver < MIN_VERSION || ver > MAX_VERSION)
throw "Version number out of range";
return Math.floor(QrCode.getNumRawDataModules(ver) / 8) - return Math.floor(QrCode.getNumRawDataModules(ver) / 8) -
QrCode.ECC_CODEWORDS_PER_BLOCK[ecl.ordinal][ver] * QrCode.ECC_CODEWORDS_PER_BLOCK[ecl.ordinal][ver] *
QrCode.NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal][ver]; QrCode.NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal][ver];
@ -816,8 +814,6 @@ var qrcodegen = new function() {
// (Package-private) Calculates and returns the number of bits needed to encode the given segments at the // (Package-private) Calculates and returns the number of bits needed to encode the given segments at the
// given version. The result is infinity if a segment has too many characters to fit its length field. // given version. The result is infinity if a segment has too many characters to fit its length field.
this.QrSegment.getTotalBits = function(segs, version) { this.QrSegment.getTotalBits = function(segs, version) {
if (version < MIN_VERSION || version > MAX_VERSION)
throw "Version number out of range";
var result = 0; var result = 0;
for (var i = 0; i < segs.length; i++) { for (var i = 0; i < segs.length; i++) {
var seg = segs[i]; var seg = segs[i];

@ -522,8 +522,6 @@ class QrCode(object):
"""Returns the number of 8-bit data (i.e. not error correction) codewords contained in any """Returns the number of 8-bit data (i.e. not error correction) codewords contained in any
QR Code of the given version number and error correction level, with remainder bits discarded. QR Code of the given version number and error correction level, with remainder bits discarded.
This stateless pure function could be implemented as a (40*4)-cell lookup table.""" This stateless pure function could be implemented as a (40*4)-cell lookup table."""
if not (QrCode.MIN_VERSION <= ver <= QrCode.MAX_VERSION):
raise ValueError("Version number out of range")
return QrCode._get_num_raw_data_modules(ver) // 8 \ return QrCode._get_num_raw_data_modules(ver) // 8 \
- QrCode._ECC_CODEWORDS_PER_BLOCK[ecl.ordinal][ver] \ - QrCode._ECC_CODEWORDS_PER_BLOCK[ecl.ordinal][ver] \
* QrCode._NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal][ver] * QrCode._NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal][ver]
@ -703,8 +701,6 @@ class QrSegment(object):
"""Calculates the number of bits needed to encode the given segments at """Calculates the number of bits needed to encode the given segments at
the given version. Returns a non-negative number if successful. Otherwise the given version. Returns a non-negative number if successful. Otherwise
returns None if a segment has too many characters to fit its length field.""" returns None if a segment has too many characters to fit its length field."""
if not (QrCode.MIN_VERSION <= version <= QrCode.MAX_VERSION):
raise ValueError("Version number out of range")
result = 0 result = 0
for seg in segs: for seg in segs:
ccbits = seg.get_mode().num_char_count_bits(version) ccbits = seg.get_mode().num_char_count_bits(version)

@ -599,8 +599,6 @@ namespace qrcodegen {
// QR Code of the given version number and error correction level, with remainder bits discarded. // QR Code of the given version number and error correction level, with remainder bits discarded.
// This stateless pure function could be implemented as a (40*4)-cell lookup table. // This stateless pure function could be implemented as a (40*4)-cell lookup table.
private static getNumDataCodewords(ver: int, ecl: QrCode.Ecc): int { private static getNumDataCodewords(ver: int, ecl: QrCode.Ecc): int {
if (ver < QrCode.MIN_VERSION || ver > QrCode.MAX_VERSION)
throw "Version number out of range";
return Math.floor(QrCode.getNumRawDataModules(ver) / 8) - return Math.floor(QrCode.getNumRawDataModules(ver) / 8) -
QrCode.ECC_CODEWORDS_PER_BLOCK[ecl.ordinal][ver] * QrCode.ECC_CODEWORDS_PER_BLOCK[ecl.ordinal][ver] *
QrCode.NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal][ver]; QrCode.NUM_ERROR_CORRECTION_BLOCKS[ecl.ordinal][ver];
@ -772,8 +770,6 @@ namespace qrcodegen {
// (Package-private) Calculates and returns the number of bits needed to encode the given segments at // (Package-private) Calculates and returns the number of bits needed to encode the given segments at
// the given version. The result is infinity if a segment has too many characters to fit its length field. // the given version. The result is infinity if a segment has too many characters to fit its length field.
public static getTotalBits(segs: Array<QrSegment>, version: int): number { public static getTotalBits(segs: Array<QrSegment>, version: int): number {
if (version < QrCode.MIN_VERSION || version > QrCode.MAX_VERSION)
throw "Version number out of range";
let result: number = 0; let result: number = 0;
for (let i = 0; i < segs.length; i++) { for (let i = 0; i < segs.length; i++) {
let seg: QrSegment = segs[i]; let seg: QrSegment = segs[i];

Loading…
Cancel
Save