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");
BitBuffer bb = new BitBuffer();
int i;
for (i = 0; i <= digits.length() - 3; i += 3) // Process groups of 3
bb.appendBits(Integer.parseInt(digits.substring(i, i + 3)), 10);
int rem = digits.length() - i;
if (rem > 0) // 1 or 2 digits remaining
bb.appendBits(Integer.parseInt(digits.substring(i)), rem * 3 + 1);
for (int i = 0; i < digits.length(); ) { // Consume up to 3 digits per iteration
int n = Math.min(digits.length() - i, 3);
bb.appendBits(Integer.parseInt(digits.substring(i, i + n)), n * 3 + 1);
i += n;
}
return new QrSegment(Mode.NUMERIC, digits.length(), bb);
}

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

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

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

Loading…
Cancel
Save