From 52694b5327006ff46933a2ee4b83007c7d54a2a7 Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Mon, 8 May 2017 06:20:50 +0000 Subject: [PATCH] Simplified/deoptimized Java and JavaScript code to use string search instead of lookup table for alphanumeric mode encoding, without changing functionality. This is similar to the previous commit 106e31d9c7f2. By comparison, the Python code had always used a proper dictionary, and the C code implemented string search from the start. --- java/io/nayuki/qrcodegen/QrSegment.java | 15 +++++---------- javascript/qrcodegen.js | 15 +++++---------- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/java/io/nayuki/qrcodegen/QrSegment.java b/java/io/nayuki/qrcodegen/QrSegment.java index 058ac92..19662bf 100644 --- a/java/io/nayuki/qrcodegen/QrSegment.java +++ b/java/io/nayuki/qrcodegen/QrSegment.java @@ -94,12 +94,12 @@ public final class QrSegment { BitBuffer bb = new BitBuffer(); int i; for (i = 0; i + 2 <= text.length(); i += 2) { // Process groups of 2 - int temp = ALPHANUMERIC_ENCODING_TABLE[text.charAt(i) - ' '] * 45; - temp += ALPHANUMERIC_ENCODING_TABLE[text.charAt(i + 1) - ' ']; + int temp = ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)) * 45; + temp += ALPHANUMERIC_CHARSET.indexOf(text.charAt(i + 1)); bb.appendBits(temp, 11); } if (i < text.length()) // 1 character remaining - bb.appendBits(ALPHANUMERIC_ENCODING_TABLE[text.charAt(i) - ' '], 6); + bb.appendBits(ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)), 6); return new QrSegment(Mode.ALPHANUMERIC, text.length(), bb.getBytes(), bb.bitLength()); } @@ -211,13 +211,8 @@ public final class QrSegment { /** Can test whether a string is encodable in alphanumeric mode (such as by using {@link #makeAlphanumeric(String)}). */ public static final Pattern ALPHANUMERIC_REGEX = Pattern.compile("[A-Z0-9 $%*+./:-]*"); - /** Maps shifted ASCII codes to alphanumeric mode character codes. */ - private static final byte[] ALPHANUMERIC_ENCODING_TABLE = { - // SP, !, ", #, $, %, &, ', (, ), *, +, ,, -, ., /, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, :, ;, <, =, >, ?, @, // ASCII codes 32 to 64 - 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1, -1, // Array indices 0 to 32 - 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, // Array indices 33 to 58 - // A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, // ASCII codes 65 to 90 - }; + /** The set of all legal characters in alphanumeric mode, where each character value maps to the index in the string. */ + private static final String ALPHANUMERIC_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"; diff --git a/javascript/qrcodegen.js b/javascript/qrcodegen.js index 09b7ad0..13fa456 100644 --- a/javascript/qrcodegen.js +++ b/javascript/qrcodegen.js @@ -790,12 +790,12 @@ var qrcodegen = new function() { var bb = new BitBuffer(); var i; for (i = 0; i + 2 <= text.length; i += 2) { // Process groups of 2 - var temp = QrSegment.ALPHANUMERIC_ENCODING_TABLE[text.charCodeAt(i) - 32] * 45; - temp += QrSegment.ALPHANUMERIC_ENCODING_TABLE[text.charCodeAt(i + 1) - 32]; + var temp = QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)) * 45; + temp += QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i + 1)); bb.appendBits(temp, 11); } if (i < text.length) // 1 character remaining - bb.appendBits(QrSegment.ALPHANUMERIC_ENCODING_TABLE[text.charCodeAt(i) - 32], 6); + bb.appendBits(QrSegment.ALPHANUMERIC_CHARSET.indexOf(text.charAt(i)), 6); return new this(this.Mode.ALPHANUMERIC, text.length, bb.getBits()); }; @@ -844,13 +844,8 @@ var qrcodegen = new function() { // (Public) Can test whether a string is encodable in alphanumeric mode (such as by using QrSegment.makeAlphanumeric()). this.QrSegment.ALPHANUMERIC_REGEX = /^[A-Z0-9 $%*+.\/:-]*$/; - // (Private) Maps shifted ASCII codes to alphanumeric mode character codes. - QrSegment.ALPHANUMERIC_ENCODING_TABLE = [ - // SP, !, ", #, $, %, &, ', (, ), *, +, ,, -, ., /, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, :, ;, <, =, >, ?, @, // ASCII codes 32 to 64 - 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1, -1, // Array indices 0 to 32 - 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, // Array indices 33 to 58 - // A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z, // ASCII codes 65 to 90 - ]; + // (Private) The set of all legal characters in alphanumeric mode, where each character value maps to the index in the string. + QrSegment.ALPHANUMERIC_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"; /*---- Public helper enumeration ----*/