Simplified QrSegment.makeNumeric() logic in {Java, JavaScript, TypeScript, Python} language versions.

pull/39/merge
Project Nayuki 6 years ago
parent 8db3cfef81
commit 3e642cf468

@ -69,12 +69,11 @@ public final class QrSegment {
throw new IllegalArgumentException("String contains non-numeric characters"); throw new IllegalArgumentException("String contains non-numeric characters");
BitBuffer bb = new BitBuffer(); BitBuffer bb = new BitBuffer();
int i; for (int i = 0; i < digits.length(); ) { // Consume up to 3 digits per iteration
for (i = 0; i <= digits.length() - 3; i += 3) // Process groups of 3 int n = Math.min(digits.length() - i, 3);
bb.appendBits(Integer.parseInt(digits.substring(i, i + 3)), 10); bb.appendBits(Integer.parseInt(digits.substring(i, i + n)), n * 3 + 1);
int rem = digits.length() - i; i += n;
if (rem > 0) // 1 or 2 digits remaining }
bb.appendBits(Integer.parseInt(digits.substring(i)), rem * 3 + 1);
return new QrSegment(Mode.NUMERIC, digits.length(), bb); return new QrSegment(Mode.NUMERIC, digits.length(), bb);
} }

@ -738,12 +738,11 @@ var qrcodegen = new function() {
if (!this.NUMERIC_REGEX.test(digits)) if (!this.NUMERIC_REGEX.test(digits))
throw "String contains non-numeric characters"; throw "String contains non-numeric characters";
var bb = new BitBuffer(); var bb = new BitBuffer();
var i; for (var i = 0; i < digits.length; ) { // Consume up to 3 digits per iteration
for (i = 0; i + 3 <= digits.length; i += 3) // Process groups of 3 var n = Math.min(digits.length - i, 3);
bb.appendBits(parseInt(digits.substr(i, 3), 10), 10); bb.appendBits(parseInt(digits.substr(i, n), 10), n * 3 + 1);
var rem = digits.length - i; i += n;
if (rem > 0) // 1 or 2 digits remaining }
bb.appendBits(parseInt(digits.substring(i), 10), rem * 3 + 1);
return new this(this.Mode.NUMERIC, digits.length, bb); return new this(this.Mode.NUMERIC, digits.length, bb);
}; };

@ -611,11 +611,11 @@ class QrSegment(object):
if QrSegment.NUMERIC_REGEX.match(digits) is None: if QrSegment.NUMERIC_REGEX.match(digits) is None:
raise ValueError("String contains non-numeric characters") raise ValueError("String contains non-numeric characters")
bb = _BitBuffer() bb = _BitBuffer()
for i in range(0, len(digits) - 2, 3): # Process groups of 3 i = 0
bb.append_bits(int(digits[i : i + 3]), 10) while i < len(digits): # Consume up to 3 digits per iteration
rem = len(digits) % 3 n = min(len(digits) - i, 3)
if rem > 0: # 1 or 2 digits remaining bb.append_bits(int(digits[i : i + n]), n * 3 + 1)
bb.append_bits(int(digits[-rem : ]), rem * 3 + 1) i += n
return QrSegment(QrSegment.Mode.NUMERIC, len(digits), bb) return QrSegment(QrSegment.Mode.NUMERIC, len(digits), bb)

@ -673,12 +673,11 @@ namespace qrcodegen {
if (!this.NUMERIC_REGEX.test(digits)) if (!this.NUMERIC_REGEX.test(digits))
throw "String contains non-numeric characters"; throw "String contains non-numeric characters";
let bb = new BitBuffer(); let bb = new BitBuffer();
let i: int; for (let i = 0; i < digits.length; ) { // Consume up to 3 digits per iteration
for (i = 0; i + 3 <= digits.length; i += 3) // Process groups of 3 let n: int = Math.min(digits.length - i, 3);
bb.appendBits(parseInt(digits.substr(i, 3), 10), 10); bb.appendBits(parseInt(digits.substr(i, n), 10), n * 3 + 1);
let rem: int = digits.length - i; i += n;
if (rem > 0) // 1 or 2 digits remaining }
bb.appendBits(parseInt(digits.substring(i), 10), rem * 3 + 1);
return new QrSegment(QrSegment.Mode.NUMERIC, digits.length, bb); return new QrSegment(QrSegment.Mode.NUMERIC, digits.length, bb);
} }

Loading…
Cancel
Save