Skip to content

Commit 6614241

Browse files
committed
Added convertToAscii method
1 parent 9296ed0 commit 6614241

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public enum MapcodeFormatType {
187187
public static MapcodeFormatType getMapcodeFormatType(@Nonnull final String mapcode) {
188188

189189
// First, decode to ASCII.
190-
final String decodedMapcode = Decoder.decodeUTF16(mapcode);
190+
final String decodedMapcode = convertToAscii(mapcode);
191191

192192
// Syntax needs to be OK.
193193
if (!PATTERN_MAPCODE_FORMAT.matcher(decodedMapcode).matches()) {
@@ -218,6 +218,17 @@ public static boolean isValidMapcodeFormat(@Nonnull final String mapcode) {
218218
return getMapcodeFormatType(mapcode) != MapcodeFormatType.MAPCODE_TYPE_INVALID;
219219
}
220220

221+
/**
222+
* Convert a mapcode which potentially contains Unicode characters, to an ASCII veriant.
223+
*
224+
* @param mapcode Mapcode, with optional Unicode characters.
225+
* @return ASCII, non-Unicode string.
226+
*/
227+
@Nonnull
228+
public static String convertToAscii(@Nonnull final String mapcode) {
229+
return Decoder.decodeUTF16(mapcode);
230+
}
231+
221232
/**
222233
* Return the local mapcode string, potentially ambiguous.
223234
*

src/site/apt/ReleaseNotes.apt.vm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ Release Notes (Version ${project.version})
2020
if the character 'Z' or equivalent Unicode character is contained in the high precision part according to
2121
the Mapcode documenation.
2222

23+
* Added method <<<convertToAscii>>> which produces the ASCII, non-Unicode variant of a mapcode which contains
24+
Unicode characters§.
25+
2326
[]
2427

2528
* 1.40.1

src/test/java/com/mapcode/MapcodeTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,11 @@ public void checkMapcodeFormatType() {
9090
assertEquals(Mapcode.MapcodeFormatType.MAPCODE_TYPE_PRECISION_1, Mapcode.getMapcodeFormatType("AA.BB-1"));
9191
assertEquals(Mapcode.MapcodeFormatType.MAPCODE_TYPE_PRECISION_2, Mapcode.getMapcodeFormatType("AA.BB-12"));
9292
}
93-
}
93+
94+
@Test
95+
public void checkConvertToAscii() {
96+
LOG.info("checkConvertToAscii");
97+
assertEquals("KM.8K", Mapcode.convertToAscii("\u30c1\u30ca.8\u30c1"));
98+
assertEquals("HJ.Q2-Z", Mapcode.convertToAscii("\u0397\u03a0.\u03982-\u0411"));
99+
}
100+
}

0 commit comments

Comments
 (0)