]> granicus.if.org Git - postgis/commitdiff
Refactor decode_geohash_bbox
authorRaúl Marín Rodríguez <rmrodriguez@carto.com>
Fri, 14 Jun 2019 08:55:37 +0000 (08:55 +0000)
committerRaúl Marín Rodríguez <rmrodriguez@carto.com>
Fri, 14 Jun 2019 08:55:37 +0000 (08:55 +0000)
Applied suggestion by Michael Entin and some extra cleanup

git-svn-id: http://svn.osgeo.org/postgis/trunk@17532 b70326c6-7e19-0410-871a-916f4a2858ee

liblwgeom/lwalgorithm.c

index f81f492762bcd705eb577b7a70f4d472cff9383b..1a2b8237a86a60a89c19e1712db5a706fdf12e7e 100644 (file)
@@ -26,6 +26,7 @@
 #include "liblwgeom_internal.h"
 #include "lwgeom_log.h"
 #include <ctype.h> /* for tolower */
+#include <stdbool.h>
 
 int
 p4d_same(const POINT4D *p1, const POINT4D *p2)
@@ -713,37 +714,36 @@ unsigned int geohash_point_as_int(POINT2D *pt)
 */
 void decode_geohash_bbox(char *geohash, double *lat, double *lon, int precision)
 {
-       int i, j, hashlen;
-       char c, cd, mask, is_even = 1;
-       static char bits[] = {16, 8, 4, 2, 1};
+       bool is_even = 1;
 
        lat[0] = -90.0;
        lat[1] = 90.0;
        lon[0] = -180.0;
        lon[1] = 180.0;
 
-       hashlen = strlen(geohash);
-
-       if (precision < 0 || precision > hashlen)
+       size_t hashlen = strlen(geohash);
+       if (precision < 0 || (size_t)precision > hashlen)
        {
-               precision = hashlen;
+               precision = (int)hashlen;
        }
 
-       for (i = 0; i < precision; i++)
+       for (int i = 0; i < precision; i++)
        {
-               c = tolower(geohash[i]);
-               /* Valid characters are all digits and letters except a, i, l and o */
-               if (!(((c >= '0') && (c <= '9')) ||
-                     ((c >= 'b') && (c <= 'z') && (c != 'i') && (c != 'l') && (c != 'o'))))
+               char c = tolower(geohash[i]);
+
+               /* Valid characters are all digits in base32 */
+               char *base32_pos = strchr(base32, c);
+               if (!base32_pos)
                {
                        lwerror("%s: Invalid character '%c'", __func__, geohash[i]);
                        return;
                }
-               cd = strchr(base32, c) - base32;
+               char cd = base32_pos - base32;
 
-               for (j = 0; j < 5; j++)
+               for (size_t j = 0; j < 5; j++)
                {
-                       mask = bits[j];
+                       const char bits[] = {16, 8, 4, 2, 1};
+                       char mask = bits[j];
                        if (is_even)
                        {
                                lon[!(cd & mask)] = (lon[0] + lon[1]) / 2;
@@ -892,26 +892,3 @@ char *lwgeom_geohash(const LWGEOM *lwgeom, int precision)
        */
        return geohash_point(lon, lat, precision);
 }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-