Simplified Java QrSegmentAdvanced.splitIntoSegments() to reduce redundancy.

This commit is contained in:
Project Nayuki 2018-08-25 23:15:27 +00:00
parent 432f3e0fed
commit 78e234fb0d
1 changed files with 4 additions and 16 deletions

View File

@ -181,8 +181,8 @@ public final class QrSegmentAdvanced {
// Accumulate run of modes // Accumulate run of modes
QrSegment.Mode curMode = charModes[0]; QrSegment.Mode curMode = charModes[0];
int start = 0; int start = 0;
for (int i = 1; i < data.length; i++) { for (int i = 1; ; i++) {
if (charModes[i] != curMode) { if (i >= data.length || charModes[i] != curMode) {
if (curMode == BYTE) if (curMode == BYTE)
result.add(QrSegment.makeBytes(Arrays.copyOfRange(data, start, i))); result.add(QrSegment.makeBytes(Arrays.copyOfRange(data, start, i)));
else { else {
@ -194,24 +194,12 @@ public final class QrSegmentAdvanced {
else else
throw new AssertionError(); throw new AssertionError();
} }
if (i >= data.length)
return result;
curMode = charModes[i]; curMode = charModes[i];
start = i; start = i;
} }
} }
// Final segment
if (curMode == BYTE)
result.add(QrSegment.makeBytes(Arrays.copyOfRange(data, start, data.length)));
else {
String temp = new String(data, start, data.length - start, StandardCharsets.US_ASCII);
if (curMode == NUMERIC)
result.add(QrSegment.makeNumeric(temp));
else if (curMode == ALPHANUMERIC)
result.add(QrSegment.makeAlphanumeric(temp));
else
throw new AssertionError();
}
return result;
} }