From 221bbd932a5ff2ead9d8fbbfeb2bb10c1ffdc9a8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ra=C3=BAl=20Mar=C3=ADn=20Rodr=C3=ADguez?= Date: Fri, 14 Jun 2019 08:55:37 +0000 Subject: [PATCH] Refactor decode_geohash_bbox 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 | 53 ++++++++++++----------------------------- 1 file changed, 15 insertions(+), 38 deletions(-) diff --git a/liblwgeom/lwalgorithm.c b/liblwgeom/lwalgorithm.c index f81f49276..1a2b8237a 100644 --- a/liblwgeom/lwalgorithm.c +++ b/liblwgeom/lwalgorithm.c @@ -26,6 +26,7 @@ #include "liblwgeom_internal.h" #include "lwgeom_log.h" #include /* for tolower */ +#include 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); } - - - - - - - - - - - - - - - - - - - - - - - -- 2.40.0