[postgis-tickets] r17532 - Refactor decode_geohash_bbox

Raul raul at rmr.ninja
Fri Jun 14 01:55:37 PDT 2019


Author: algunenano
Date: 2019-06-14 01:55:37 -0700 (Fri, 14 Jun 2019)
New Revision: 17532

Modified:
   trunk/liblwgeom/lwalgorithm.c
Log:
Refactor decode_geohash_bbox

Applied suggestion by Michael Entin and some extra cleanup



Modified: trunk/liblwgeom/lwalgorithm.c
===================================================================
--- trunk/liblwgeom/lwalgorithm.c	2019-06-13 23:54:32 UTC (rev 17531)
+++ trunk/liblwgeom/lwalgorithm.c	2019-06-14 08:55:37 UTC (rev 17532)
@@ -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,9 +714,7 @@
 */
 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;
@@ -722,28 +721,29 @@
 	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 @@
 	*/
 	return geohash_point(lon, lat, precision);
 }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-



More information about the postgis-tickets mailing list