From 3e642cf468b14319c4387c1645fb76b69dd1fe97 Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Fri, 31 Aug 2018 17:54:39 +0000 Subject: [PATCH] Simplified QrSegment.makeNumeric() logic in {Java, JavaScript, TypeScript, Python} language versions. --- java/io/nayuki/qrcodegen/QrSegment.java | 11 +++++------ javascript/qrcodegen.js | 11 +++++------ python/qrcodegen.py | 10 +++++----- typescript/qrcodegen.ts | 11 +++++------ 4 files changed, 20 insertions(+), 23 deletions(-) diff --git a/java/io/nayuki/qrcodegen/QrSegment.java b/java/io/nayuki/qrcodegen/QrSegment.java index 8328570..eb45d56 100644 --- a/java/io/nayuki/qrcodegen/QrSegment.java +++ b/java/io/nayuki/qrcodegen/QrSegment.java @@ -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); } diff --git a/javascript/qrcodegen.js b/javascript/qrcodegen.js index ffc6fe0..0af8399 100644 --- a/javascript/qrcodegen.js +++ b/javascript/qrcodegen.js @@ -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); }; diff --git a/python/qrcodegen.py b/python/qrcodegen.py index 67e45ae..512be56 100644 --- a/python/qrcodegen.py +++ b/python/qrcodegen.py @@ -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) diff --git a/typescript/qrcodegen.ts b/typescript/qrcodegen.ts index 52d2906..d197129 100644 --- a/typescript/qrcodegen.ts +++ b/typescript/qrcodegen.ts @@ -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); }