From 012d469ca5d0b173a1d003bae842ac691e0f952f Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Wed, 3 Oct 2018 20:59:02 +0000 Subject: [PATCH] Inlined some Java functions - refactored QrSegmentAdvanced, increased visibility of a constant in QrSegment. --- java/io/nayuki/qrcodegen/QrSegment.java | 2 +- .../nayuki/qrcodegen/QrSegmentAdvanced.java | 20 +++---------------- 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/java/io/nayuki/qrcodegen/QrSegment.java b/java/io/nayuki/qrcodegen/QrSegment.java index eb45d56..ceee566 100644 --- a/java/io/nayuki/qrcodegen/QrSegment.java +++ b/java/io/nayuki/qrcodegen/QrSegment.java @@ -223,7 +223,7 @@ public final class QrSegment { public static final Pattern ALPHANUMERIC_REGEX = Pattern.compile("[A-Z0-9 $%*+./:-]*"); /** 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 $%*+-./:"; + static final String ALPHANUMERIC_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"; diff --git a/java/io/nayuki/qrcodegen/QrSegmentAdvanced.java b/java/io/nayuki/qrcodegen/QrSegmentAdvanced.java index 549e446..6b52f67 100644 --- a/java/io/nayuki/qrcodegen/QrSegmentAdvanced.java +++ b/java/io/nayuki/qrcodegen/QrSegmentAdvanced.java @@ -125,11 +125,11 @@ public final class QrSegmentAdvanced { charModes[i][0] = modeTypes[0]; } // Extend a segment if possible - if (isAlphanumeric(c)) { + if (QrSegment.ALPHANUMERIC_CHARSET.indexOf(c) != -1) { curCosts[1] = prevCosts[1] + 33; // 5.5 bits per alphanumeric char charModes[i][1] = modeTypes[1]; } - if (isNumeric(c)) { + if ('0' <= c && c <= '9') { curCosts[2] = prevCosts[2] + 20; // 3.33 bits per digit charModes[i][2] = modeTypes[2]; } @@ -141,7 +141,7 @@ public final class QrSegmentAdvanced { // Start new segment at the end to switch modes for (int j = 0; j < numModes; j++) { // To mode for (int k = 0; k < numModes; k++) { // From mode - int newCost = roundUp6(curCosts[k]) + headCosts[j]; + int newCost = (curCosts[k] + 5) / 6 * 6 + headCosts[j]; if (charModes[i][k] != null && (charModes[i][j] == null || newCost < curCosts[j])) { curCosts[j] = newCost; charModes[i][j] = modeTypes[k]; @@ -216,20 +216,6 @@ public final class QrSegmentAdvanced { } - private static boolean isAlphanumeric(int c) { - return isNumeric(c) || 'A' <= c && c <= 'Z' || " $%*+./:-".indexOf(c) != -1; - } - - private static boolean isNumeric(int c) { - return '0' <= c && c <= '9'; - } - - - private static int roundUp6(int x) { - return (x + 5) / 6 * 6; - } - - /*---- Kanji mode segment encoder ----*/