Tweaked and simplified QrSegment.getTotalBits() to use infinity value, in JavaScript and TypeScript code.

pull/39/merge
Project Nayuki 6 years ago
parent 719caf4d99
commit b095504d5c

@ -542,7 +542,7 @@ var qrcodegen = new function() {
for (version = minVersion; ; version++) {
var dataCapacityBits = QrCode.getNumDataCodewords(version, ecl) * 8; // Number of data bits available
dataUsedBits = qrcodegen.QrSegment.getTotalBits(segs, version);
if (dataUsedBits != null && dataUsedBits <= dataCapacityBits)
if (dataUsedBits <= dataCapacityBits)
break; // This version number is found to be suitable
if (version >= maxVersion) // All versions in the range could not fit the given data
throw "Data too long";
@ -827,7 +827,7 @@ var qrcodegen = new function() {
var ccbits = seg.mode.numCharCountBits(version);
// Fail if segment length value doesn't fit in the length field's bit-width
if (seg.numChars >= (1 << ccbits))
return null;
return Infinity;
result += 4 + ccbits + seg.getBits().length;
}
return result;

@ -85,8 +85,8 @@ namespace qrcodegen {
let dataUsedBits: int;
for (version = minVersion; ; version++) {
let dataCapacityBits: int = QrCode.getNumDataCodewords(version, ecl) * 8; // Number of data bits available
let usedBits: number|null = QrSegment.getTotalBits(segs, version);
if (usedBits != null && usedBits <= dataCapacityBits) {
let usedBits: number = QrSegment.getTotalBits(segs, version);
if (usedBits <= dataCapacityBits) {
dataUsedBits = usedBits;
break; // This version number is found to be suitable
}
@ -774,16 +774,16 @@ namespace qrcodegen {
// Package-private helper function.
public static getTotalBits(segs: Array<QrSegment>, version: int): int|null {
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: int = 0;
let result: number = 0;
for (let i = 0; i < segs.length; i++) {
let seg: QrSegment = segs[i];
let ccbits: int = seg.mode.numCharCountBits(version);
// Fail if segment length value doesn't fit in the length field's bit-width
if (seg.numChars >= (1 << ccbits))
return null;
return Infinity;
result += 4 + ccbits + seg.getBits().length;
}
return result;

Loading…
Cancel
Save