Inlined some Java functions - refactored QrSegmentAdvanced, increased visibility of a constant in QrSegment.

This commit is contained in:
Project Nayuki 2018-10-03 20:59:02 +00:00
parent 4f61adf9ba
commit 012d469ca5
2 changed files with 4 additions and 18 deletions

View File

@ -223,7 +223,7 @@ public final class QrSegment {
public static final Pattern ALPHANUMERIC_REGEX = Pattern.compile("[A-Z0-9 $%*+./:-]*"); 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. */ /** 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 $%*+-./:";

View File

@ -125,11 +125,11 @@ public final class QrSegmentAdvanced {
charModes[i][0] = modeTypes[0]; charModes[i][0] = modeTypes[0];
} }
// Extend a segment if possible // 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 curCosts[1] = prevCosts[1] + 33; // 5.5 bits per alphanumeric char
charModes[i][1] = modeTypes[1]; charModes[i][1] = modeTypes[1];
} }
if (isNumeric(c)) { if ('0' <= c && c <= '9') {
curCosts[2] = prevCosts[2] + 20; // 3.33 bits per digit curCosts[2] = prevCosts[2] + 20; // 3.33 bits per digit
charModes[i][2] = modeTypes[2]; charModes[i][2] = modeTypes[2];
} }
@ -141,7 +141,7 @@ public final class QrSegmentAdvanced {
// Start new segment at the end to switch modes // Start new segment at the end to switch modes
for (int j = 0; j < numModes; j++) { // To mode for (int j = 0; j < numModes; j++) { // To mode
for (int k = 0; k < numModes; k++) { // From 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])) { if (charModes[i][k] != null && (charModes[i][j] == null || newCost < curCosts[j])) {
curCosts[j] = newCost; curCosts[j] = newCost;
charModes[i][j] = modeTypes[k]; 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 ----*/ /*---- Kanji mode segment encoder ----*/