Skip to content

Commit 7b7993b

Browse files
committed
Cleaned
1 parent ee6fc45 commit 7b7993b

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

src/main/java/com/mapcode/Decoder.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -645,25 +645,8 @@ static String decodeUTF16(final String mapcode) {
645645
return result;
646646
}
647647

648-
static String encodeUTF16(final String string, final int alphabet) {
649-
final StringBuilder sb = new StringBuilder();
650-
for (char ch : string.toCharArray()) {
651-
ch = Character.toUpperCase(ch);
652-
if (ch > 'Z') {
653-
// Not in any valid range?
654-
sb.append('?');
655-
} else if (ch < 'A') {
656-
// Valid but not a letter (e.g. a dot, a space...).
657-
// Leave untranslated.
658-
sb.append(ch);
659-
} else {
660-
sb.append((char) ASCII2LANGUAGE[alphabet][(int) ch - (int) 'A']);
661-
}
662-
}
663-
return sb.toString();
664-
}
665-
666-
static String encodeToAlphabetCode(final String mapcode, int alphabetCode) {
648+
static String encodeUTF16(final String mapcode, int alphabetCode) {
649+
final String mapcodeToEncode;
667650
if (ASCII2LANGUAGE[alphabetCode][4] == 0x003f) {
668651

669652
// Alphabet does not contain 'E'.
@@ -673,11 +656,29 @@ static String encodeToAlphabetCode(final String mapcode, int alphabetCode) {
673656
throw new AssertionError("encodeToAlphabetCode: cannot encode '" + mapcode +
674657
"' to alphabet " + alphabetCode + ' ' + Alphabet.fromCode(alphabetCode));
675658
}
676-
final String packed = Encoder.aeuPack(unpacked, true);
677-
return encodeUTF16(packed, alphabetCode);
659+
mapcodeToEncode = Encoder.aeuPack(unpacked, true);
678660
}
661+
else {
662+
mapcodeToEncode = mapcode;
663+
}
664+
}
665+
else {
666+
mapcodeToEncode = mapcode;
679667
}
680-
return encodeUTF16(mapcode, alphabetCode);
668+
final StringBuilder sb = new StringBuilder();
669+
for (char ch : mapcodeToEncode.toCharArray()) {
670+
ch = Character.toUpperCase(ch);
671+
if (ch > 'Z') {
672+
// Not in any valid range?
673+
sb.append('?');
674+
} else if (ch < 'A') {
675+
// Valid but not a letter (e.g. a dot, a space...). Leave untranslated.
676+
sb.append(ch);
677+
} else {
678+
sb.append((char) ASCII2LANGUAGE[alphabetCode][(int) ch - (int) 'A']);
679+
}
680+
}
681+
return sb.toString();
681682
}
682683

683684
@Nonnull

src/main/java/com/mapcode/Mapcode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ static String convertMapcodeToPlainAscii(@Nonnull final String mapcode) {
394394
@Nonnull
395395
static String convertMapcodeToAlphabet(@Nonnull final String mapcode, @Nullable final Alphabet alphabet) throws IllegalArgumentException {
396396
checkMapcodeCode("mapcode", mapcode);
397-
return (alphabet != null) ? Decoder.encodeToAlphabetCode(mapcode.toUpperCase(), alphabet.code) : mapcode.toUpperCase();
397+
return (alphabet != null) ? Decoder.encodeUTF16(mapcode.toUpperCase(), alphabet.code) : mapcode.toUpperCase();
398398
}
399399

400400
/**

0 commit comments

Comments
 (0)