Skip to content

Commit b8c7552

Browse files
Stricter conversion to other alphabets
1 parent 92ae2f9 commit b8c7552

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

mapcodelib/mapcoder.c

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,35 +2134,49 @@ int decodeMapcodeToLatLon(double *lat, double *lon, const char *input,
21342134
UWORD *convertToAlphabet(UWORD *unibuf, int maxlength, const char *mapcode, int alphabet) // 0=roman, 2=cyrillic
21352135
{
21362136
UWORD *startbuf = unibuf;
2137+
UWORD *lastspot = &unibuf[maxlength - 1];
2138+
if (maxlength>0) {
2139+
char u[MAX_MAPCODE_RESULT_LEN];
2140+
2141+
// skip leading spaces
2142+
while (*mapcode > 0 && *mapcode <= 32) { mapcode++; }
2143+
2144+
// straight-copy everything up to and including first space
2145+
{
2146+
char *e = strchr(mapcode, ' ');
2147+
if (e) {
2148+
while (mapcode <= e) {
2149+
if (unibuf == lastspot) { // buffer fully filled?
2150+
// zero-terminate and return
2151+
*unibuf = 0;
2152+
return startbuf;
2153+
}
2154+
*unibuf++ = *mapcode++;
2155+
}
2156+
}
2157+
}
21372158

2138-
while (*mapcode > 0 && *mapcode < 32) { mapcode++; }
2139-
{ // skip lead
2140-
char *e = strrchr(mapcode, ' ');
2141-
if (e) {
2142-
while (mapcode <= e) {
2143-
*unibuf++ = *mapcode++;
2144-
}
2145-
}
2146-
}
2147-
2148-
if (asc2lan[alphabet][4] == 0x003f) { // alphabet has no letter E
2149-
if (strchr(mapcode, 'E') || strchr(mapcode, 'U') || strchr(mapcode, 'e') ||
2150-
strchr(mapcode, 'u')) // v1.50 get rid of E and U
2151-
{
2152-
// safely copy mapcode into temporary buffer u
2153-
char u[MAX_MAPCODE_RESULT_LEN];
2154-
int len = (int) strlen(mapcode);
2155-
if (len >= MAX_MAPCODE_RESULT_LEN) {
2156-
len = MAX_MAPCODE_RESULT_LEN - 1;
2159+
// re-pack E/U-voweled mapcodes when necessary:
2160+
if (asc2lan[alphabet][4] == 0x003f) { // alphabet has no letter E
2161+
if (strchr(mapcode, 'E') || strchr(mapcode, 'U') ||
2162+
strchr(mapcode, 'e') || strchr(mapcode, 'u'))
2163+
{
2164+
// copy trimmed mapcode into temporary buffer u
2165+
int len = (int) strlen(mapcode);
2166+
if (len > MAX_MAPCODE_RESULT_LEN - 1) {
2167+
len = MAX_MAPCODE_RESULT_LEN - 1;
2168+
}
2169+
while (len>0 && mapcode[len-1]>0 && mapcode[len-1]<=32) { len--; }
2170+
memcpy(u, mapcode, len);
2171+
u[len] = 0;
2172+
// re-pack into A-voweled mapcode
2173+
unpack_if_alldigits(u);
2174+
repack_if_alldigits(u, 1);
2175+
mapcode = u;
21572176
}
2158-
memcpy(u, mapcode, len);
2159-
u[len] = 0;
2160-
unpack_if_alldigits(u);
2161-
repack_if_alldigits(u, 1);
2162-
mapcode = u;
21632177
}
2178+
encode_utf16(unibuf, 1 + (int)(lastspot - unibuf), mapcode, alphabet);
21642179
}
2165-
encode_utf16(unibuf, maxlength, mapcode, alphabet);
21662180
return startbuf;
21672181
}
21682182

0 commit comments

Comments
 (0)