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

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

Loading…
Cancel
Save