Skip to content

Commit 7888c61

Browse files
committed
Fix bool abuse
path_encode's "closed" argument used to take three values: TRUE, FALSE, or -1, while being of type bool. Replace that with a three-valued enum for more clarity.
1 parent 12fbe2b commit 7888c61

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/backend/utils/adt/geo_ops.c

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
* Internal routines
3333
*/
3434

35+
enum path_delim { PATH_NONE, PATH_OPEN, PATH_CLOSED };
36+
3537
static int point_inside(Point *p, int npts, Point *plist);
3638
static int lseg_crossing(double x, double y, double px, double py);
3739
static BOX *box_construct(double x1, double x2, double y1, double y2);
@@ -57,7 +59,7 @@ static int pair_decode(char *str, float8 *x, float8 *y, char **s);
5759
static int pair_encode(float8 x, float8 y, char *str);
5860
static int pair_count(char *s, char delim);
5961
static int path_decode(int opentype, int npts, char *str, int *isopen, char **ss, Point *p);
60-
static char *path_encode(bool closed, int npts, Point *pt);
62+
static char *path_encode(enum path_delim path_delim, int npts, Point *pt);
6163
static void statlseg_construct(LSEG *lseg, Point *pt1, Point *pt2);
6264
static double box_ar(BOX *box);
6365
static void box_cn(Point *center, BOX *box);
@@ -280,7 +282,7 @@ path_decode(int opentype, int npts, char *str, int *isopen, char **ss, Point *p)
280282
} /* path_decode() */
281283

282284
static char *
283-
path_encode(bool closed, int npts, Point *pt)
285+
path_encode(enum path_delim path_delim, int npts, Point *pt)
284286
{
285287
int size = npts * (P_MAXLEN + 3) + 2;
286288
char *result;
@@ -296,15 +298,15 @@ path_encode(bool closed, int npts, Point *pt)
296298
result = palloc(size);
297299

298300
cp = result;
299-
switch (closed)
301+
switch (path_delim)
300302
{
301-
case TRUE:
303+
case PATH_CLOSED:
302304
*cp++ = LDELIM;
303305
break;
304-
case FALSE:
306+
case PATH_OPEN:
305307
*cp++ = LDELIM_EP;
306308
break;
307-
default:
309+
case PATH_NONE:
308310
break;
309311
}
310312

@@ -322,15 +324,15 @@ path_encode(bool closed, int npts, Point *pt)
322324
pt++;
323325
}
324326
cp--;
325-
switch (closed)
327+
switch (path_delim)
326328
{
327-
case TRUE:
329+
case PATH_CLOSED:
328330
*cp++ = RDELIM;
329331
break;
330-
case FALSE:
332+
case PATH_OPEN:
331333
*cp++ = RDELIM_EP;
332334
break;
333-
default:
335+
case PATH_NONE:
334336
break;
335337
}
336338
*cp = '\0';
@@ -415,7 +417,7 @@ box_out(PG_FUNCTION_ARGS)
415417
{
416418
BOX *box = PG_GETARG_BOX_P(0);
417419

418-
PG_RETURN_CSTRING(path_encode(-1, 2, &(box->high)));
420+
PG_RETURN_CSTRING(path_encode(PATH_NONE, 2, &(box->high)));
419421
}
420422

421423
/*
@@ -1018,7 +1020,7 @@ line_out(PG_FUNCTION_ARGS)
10181020
{
10191021
}
10201022

1021-
return path_encode(TRUE, 2, (Point *) &(ls->p[0]));
1023+
return path_encode(PATH_CLOSED, 2, (Point *) &(ls->p[0]));
10221024
#else
10231025
ereport(ERROR,
10241026
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -1441,7 +1443,7 @@ path_out(PG_FUNCTION_ARGS)
14411443
{
14421444
PATH *path = PG_GETARG_PATH_P(0);
14431445

1444-
PG_RETURN_CSTRING(path_encode(path->closed, path->npts, path->p));
1446+
PG_RETURN_CSTRING(path_encode(path->closed ? PATH_CLOSED : PATH_OPEN, path->npts, path->p));
14451447
}
14461448

14471449
/*
@@ -1823,7 +1825,7 @@ point_out(PG_FUNCTION_ARGS)
18231825
{
18241826
Point *pt = PG_GETARG_POINT_P(0);
18251827

1826-
PG_RETURN_CSTRING(path_encode(-1, 1, pt));
1828+
PG_RETURN_CSTRING(path_encode(PATH_NONE, 1, pt));
18271829
}
18281830

18291831
/*
@@ -2051,7 +2053,7 @@ lseg_out(PG_FUNCTION_ARGS)
20512053
{
20522054
LSEG *ls = PG_GETARG_LSEG_P(0);
20532055

2054-
PG_RETURN_CSTRING(path_encode(FALSE, 2, (Point *) &(ls->p[0])));
2056+
PG_RETURN_CSTRING(path_encode(PATH_OPEN, 2, (Point *) &(ls->p[0])));
20552057
}
20562058

20572059
/*
@@ -3494,7 +3496,7 @@ poly_out(PG_FUNCTION_ARGS)
34943496
{
34953497
POLYGON *poly = PG_GETARG_POLYGON_P(0);
34963498

3497-
PG_RETURN_CSTRING(path_encode(TRUE, poly->npts, poly->p));
3499+
PG_RETURN_CSTRING(path_encode(PATH_CLOSED, poly->npts, poly->p));
34983500
}
34993501

35003502
/*

0 commit comments

Comments
 (0)