Skip to content

Commit

Permalink
Minor code tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
srowen committed Oct 22, 2021
1 parent 2e22d09 commit 6c034f9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dependency-reduced-pom.xml
target
.idea
*.iml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ public String toString() {
*
* @param stringToEncode The string to encode
* @param priorityCharset The preferred {@link Charset}. When the value of the argument is null, the algorithm
* chooses charsets that leads to a minimal representation. Otherwise the algorithm will use the priority
* charset to encode any character in the input that can be encoded by it if the charset is among the
* chooses charsets that leads to a minimal representation. Otherwise the algorithm will use the priority
* charset to encode any character in the input that can be encoded by it if the charset is among the
* supported charsets.
* @param isGS1 {@code true} if a FNC1 is to be prepended; {@code false} otherwise
* @param ecLevel The error correction level.
Expand Down Expand Up @@ -169,8 +169,7 @@ public String toString() {
}

if (neededEncoders.size() == 1 && !needUnicodeEncoder) {
encoders = new CharsetEncoder[1];
encoders[0] = neededEncoders.get(0);
encoders = new CharsetEncoder[] { neededEncoders.get(0) };
} else {
encoders = new CharsetEncoder[neededEncoders.size() + 2];
int index = 0;
Expand All @@ -185,7 +184,7 @@ public String toString() {
int priorityEncoderIndexValue = -1;
if (priorityCharset != null) {
for (int i = 0; i < encoders.length; i++) {
if (priorityCharset.name().equals(encoders[i].charset().name())) {
if (encoders[i] != null && priorityCharset.name().equals(encoders[i].charset().name())) {
priorityEncoderIndexValue = i;
break;
}
Expand All @@ -198,11 +197,11 @@ public String toString() {
* Encodes the string minimally
*
* @param stringToEncode The string to encode
* @param version The preferred {@link Version}. A minimal version is computed (see
* @param version The preferred {@link Version}. A minimal version is computed (see
* {@link ResultList#getVersion method} when the value of the argument is null
* @param priorityCharset The preferred {@link Charset}. When the value of the argument is null, the algorithm
* chooses charsets that leads to a minimal representation. Otherwise the algorithm will use the priority
* charset to encode any character in the input that can be encoded by it if the charset is among the
* chooses charsets that leads to a minimal representation. Otherwise the algorithm will use the priority
* charset to encode any character in the input that can be encoded by it if the charset is among the
* supported charsets.
* @param isGS1 {@code true} if a FNC1 is to be prepended; {@code false} otherwise
* @param ecLevel The error correction level.
Expand Down Expand Up @@ -275,15 +274,6 @@ static boolean isAlphanumeric(char c) {
return Encoder.getAlphanumericCode(c) != -1;
}

/**
* Returns the maximum number of encodeable characters in the given mode for the given version. Example: in
* Version 1, 2^10 digits or 2^8 bytes can be encoded. In Version 3 it is 2^14 digits and 2^16 bytes
*/
static int getMaximumNumberOfEncodeableCharacters(Version version, Mode mode) {
int count = mode.getCharacterCountBits(version);
return count == 0 ? 0 : 1 << count;
}

boolean canEncode(Mode mode, char c) {
switch (mode) {
case KANJI: return isDoubleByteKanji(c);
Expand Down Expand Up @@ -383,7 +373,7 @@ ResultList encodeSpecificVersion(Version version) throws WriterException {
*
* Example 1 encoding the string "ABCDE":
* Note: This example assumes that alphanumeric encoding is only possible in multiples of two characters so that
* the example is both short and showing the principle. In reality this restriction does not exist.
* the example is both short and showing the principle. In reality this restriction does not exist.
*
* Initial situation
* (initial) -- BYTE(A) (20) --> (1_BYTE)
Expand Down Expand Up @@ -533,9 +523,10 @@ private final class Edge {
private final int characterLength;
private final Edge previous;
private final int cachedTotalSize;

private Edge(Mode mode, int fromPosition, int charsetEncoderIndex, int characterLength, Edge previous,
Version version) {
this.mode = mode;
Version version) {
this.mode = mode;
this.fromPosition = fromPosition;
this.charsetEncoderIndex = mode == Mode.BYTE || previous == null ? charsetEncoderIndex :
previous.charsetEncoderIndex; // inherit the encoding if not of type BYTE
Expand Down Expand Up @@ -573,9 +564,10 @@ private Edge(Mode mode, int fromPosition, int charsetEncoderIndex, int character
}
}

final class ResultList extends ArrayList<ResultList.ResultNode> {
final class ResultList {

final Version version;
private final List<ResultList.ResultNode> list = new ArrayList<>();
private final Version version;

ResultList(Version version, Edge solution) {
int length = 0;
Expand All @@ -595,29 +587,29 @@ final class ResultList extends ArrayList<ResultList.ResultNode> {
}

if (previous == null || previous.mode != current.mode || needECI) {
add(0, new ResultNode(current.mode, current.fromPosition, current.charsetEncoderIndex, length));
list.add(0, new ResultNode(current.mode, current.fromPosition, current.charsetEncoderIndex, length));
length = 0;
}

if (needECI) {
add(0, new ResultNode(Mode.ECI, current.fromPosition, current.charsetEncoderIndex, 0));
list.add(0, new ResultNode(Mode.ECI, current.fromPosition, current.charsetEncoderIndex, 0));
}
current = previous;
}

// prepend FNC1 if needed. If the bits contain an ECI then the FNC1 must be preceeded by an ECI.
// If there is no ECI at the beginning then we put an ECI to the default charset (ISO-8859-1)
if (isGS1) {
ResultNode first = get(0);
ResultNode first = list.get(0);
if (first != null && first.mode != Mode.ECI && containsECI) {
// prepend a default character set ECI
add(0, new ResultNode(Mode.ECI, 0, 0, 0));
list.add(0, new ResultNode(Mode.ECI, 0, 0, 0));
}
first = get(0);
first = list.get(0);
// prepend or insert a FNC1_FIRST_POSITION after the ECI (if any)
add(first.mode != Mode.ECI ? 0 : 1, new ResultNode(Mode.FNC1_FIRST_POSITION, 0, 0, 0));
list.add(first.mode != Mode.ECI ? 0 : 1, new ResultNode(Mode.FNC1_FIRST_POSITION, 0, 0, 0));
}

// set version to smallest version into which the bits fit.
int versionNumber = version.getVersionNumber();
int lowerLimit;
Expand Down Expand Up @@ -660,7 +652,7 @@ int getSize() {

private int getSize(Version version) {
int result = 0;
for (ResultNode resultNode : this) {
for (ResultNode resultNode : list) {
result += resultNode.getSize(version);
}
return result;
Expand All @@ -670,7 +662,7 @@ private int getSize(Version version) {
* appends the bits
*/
void getBits(BitArray bits) throws WriterException {
for (ResultNode resultNode : this) {
for (ResultNode resultNode : list) {
resultNode.getBits(bits);
}
}
Expand All @@ -682,7 +674,7 @@ Version getVersion() {
public String toString() {
StringBuilder result = new StringBuilder();
ResultNode previous = null;
for (ResultNode current : this) {
for (ResultNode current : list) {
if (previous != null) {
result.append(",");
}
Expand Down

0 comments on commit 6c034f9

Please sign in to comment.