|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
17 | | -#include <string.h> // strlen strcpy strcat memcpy memmove strstr strchr memcmp |
| 17 | +#include <string.h> // strlen strcpy strcat memcpy memmove strstr strchr memcmp strupr |
18 | 18 | #include <stdlib.h> // atof |
19 | 19 | #include <ctype.h> // toupper |
| 20 | +#include <math.h> // floor |
20 | 21 | #include "mapcoder.h" |
21 | 22 | #include "basics.h" |
22 | 23 |
|
@@ -170,12 +171,7 @@ static int disambiguate_str(const char *s, int len) // returns disambiguation 1- |
170 | 171 | if (len != 2 && len != 3) { return -923; } // solve bad args |
171 | 172 | memcpy(country, s, len); |
172 | 173 | country[len] = 0; |
173 | | - { |
174 | | - char *t; |
175 | | - for (t = country; *t != 0; t++) { |
176 | | - *t = (char) toupper(*t); |
177 | | - } |
178 | | - } |
| 174 | + strupr(country); |
179 | 175 | f = strstr(p, country); |
180 | 176 | if (f == NULL) { |
181 | 177 | return -23; // unknown country |
@@ -1357,6 +1353,10 @@ static int decoderEngine(decodeRec *dec) { |
1357 | 1353 | if (ccode == ccode_mex && len < 8) { |
1358 | 1354 | ccode = ccode_of_iso3("5MX", -1); |
1359 | 1355 | } // special case for mexico country vs state |
| 1356 | + if (*s=='u' || *s=='U') { |
| 1357 | + strcpy(s,s+1); |
| 1358 | + repack_if_alldigits(s, 1); |
| 1359 | + } |
1360 | 1360 | dec->context = ccode; |
1361 | 1361 | dec->mapcode = s; |
1362 | 1362 | dec->extension = ""; |
@@ -1905,23 +1905,25 @@ int cmp_alphacode(const void *e1, const void *e2) { |
1905 | 1905 | } // cmp |
1906 | 1906 |
|
1907 | 1907 | int binfindmatch(int parentcode, const char *str) { |
| 1908 | + // build a 4-letter uppercase search term |
1908 | 1909 | char tmp[5]; |
1909 | 1910 | if (parentcode < 0) { return -1; } |
1910 | 1911 | if (parentcode > 0) { |
1911 | 1912 | tmp[0] = '0' + parentcode; |
1912 | 1913 | memcpy(tmp + 1, str, 3); |
1913 | | - tmp[4] = 0; |
1914 | | - str = tmp; |
1915 | | - } // |
| 1914 | + } else { |
| 1915 | + memcpy(tmp, str, 4); |
| 1916 | + } |
| 1917 | + tmp[4] = 0; |
1916 | 1918 | { // binary-search the result |
1917 | 1919 | const alphaRec *p; |
1918 | 1920 | alphaRec t; |
1919 | | - t.alphaCode = str; |
| 1921 | + t.alphaCode = strupr(tmp); |
1920 | 1922 | t.ccode = parentcode; |
1921 | 1923 |
|
1922 | 1924 | p = (const alphaRec *) bsearch(&t, alphaSearch, NRTERREC, sizeof(alphaRec), cmp_alphacode); |
1923 | 1925 | if (p) { |
1924 | | - if (strcmp(str, p->alphaCode) == 0) { |
| 1926 | + if (strcmp(t.alphaCode, p->alphaCode) == 0) { // only interested in PERFECT match |
1925 | 1927 | return p->ccode + 1; |
1926 | 1928 | } // match |
1927 | 1929 | } // found |
|
0 commit comments