Skip to content

Commit

Permalink
Add NODE_DELIMITER_LEN, simplify the code
Browse files Browse the repository at this point in the history
  • Loading branch information
za-arthur committed Mar 11, 2021
1 parent d0464de commit 91f3d23
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
1 change: 1 addition & 0 deletions ltree.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "tsearch/ts_locale.h"

#define NODE_DELIMITER_CHAR ':'
#define NODE_DELIMITER_LEN 2
#define ESCAPE_CHAR '\\'

typedef int32 int4;
Expand Down
45 changes: 19 additions & 26 deletions ltree_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ typedef struct
*
* Returns true if sequential characters are equal to wltree's delimiter.
*/
static bool
static inline bool
is_delimiter(char *start_ptr, int charlen)
{
if (charlen == 1 && t_iseq(start_ptr, NODE_DELIMITER_CHAR))
{
char *ptr = start_ptr + charlen;

if (pg_mblen(ptr) == 1 && t_iseq(ptr, NODE_DELIMITER_CHAR))
if (*ptr && pg_mblen(ptr) == 1 && t_iseq(ptr, NODE_DELIMITER_CHAR))
return true;
}

Expand Down Expand Up @@ -84,7 +84,7 @@ ltree_in(PG_FUNCTION_ARGS)

if (is_delimiter(ptr, charlen))
{
ptr += charlen * 2;
ptr += NODE_DELIMITER_LEN;
num++;
}
else
Expand All @@ -98,15 +98,15 @@ ltree_in(PG_FUNCTION_ARGS)
while (*ptr)
{
charlen = pg_mblen(ptr);
delimiter = false;
delimiter = is_delimiter(ptr, charlen);

if (state == LTPRS_WAITNAME)
{
lptr->start = ptr;
lptr->wlen = 0;

/* handle corner case when label starts with a delimiter character */
if (is_delimiter(ptr, charlen))
if (delimiter)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("syntax error"),
Expand All @@ -116,10 +116,8 @@ ltree_in(PG_FUNCTION_ARGS)
}
else if (state == LTPRS_WAITDELIM)
{
if (is_delimiter(ptr, charlen))
if (delimiter)
{
delimiter = true;

lptr->len = ptr - lptr->start;
if (lptr->wlen > 255)
ereport(ERROR,
Expand All @@ -146,8 +144,8 @@ ltree_in(PG_FUNCTION_ARGS)
*/
if (delimiter)
{
ptr += charlen * 2;
pos += 2;
ptr += NODE_DELIMITER_LEN;
pos += NODE_DELIMITER_LEN;
}
else
{
Expand Down Expand Up @@ -266,15 +264,12 @@ lquery_in(PG_FUNCTION_ARGS)
while (*ptr)
{
charlen = pg_mblen(ptr);
delimiter = false;
delimiter = is_delimiter(ptr, charlen);

if (charlen == 1)
{
if (!escaped_char && is_delimiter(ptr, charlen))
{
delimiter = true;
if (!escaped_char && delimiter)
num++;
}
else if (! escaped_char && t_iseq(ptr, '|'))
{
escaped_char = false;
Expand All @@ -287,7 +282,7 @@ lquery_in(PG_FUNCTION_ARGS)
escaped_char = false;

if (delimiter)
ptr += charlen * 2;
ptr += NODE_DELIMITER_LEN;
else
ptr += charlen;
}
Expand All @@ -301,7 +296,8 @@ lquery_in(PG_FUNCTION_ARGS)
while (*ptr)
{
charlen = pg_mblen(ptr);
delimiter = false;
delimiter = is_delimiter(ptr, charlen);

if (state == LQPRS_WAITLEVEL)
{
if (! escaped_char && charlen == 1 && t_iseq(ptr, '!'))
Expand All @@ -323,7 +319,7 @@ lquery_in(PG_FUNCTION_ARGS)
lptr->escnum = 0;

/* handle corner case when label starts with a delimiter character */
if (!escaped_char && is_delimiter(ptr, charlen))
if (!escaped_char && delimiter)
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("syntax error"),
Expand Down Expand Up @@ -381,7 +377,7 @@ lquery_in(PG_FUNCTION_ARGS)

state = LQPRS_WAITVAR;
}
else if (!escaped_char && is_delimiter(ptr, charlen))
else if (!escaped_char && delimiter)
{
if (lptr->start == ptr)
UNCHAR;
Expand All @@ -400,7 +396,6 @@ lquery_in(PG_FUNCTION_ARGS)
lptr->wlen, pos)));
}

delimiter = true;
escaped_char = false;
state = LQPRS_WAITLEVEL;
curqlevel = NEXTLEV(curqlevel);
Expand All @@ -416,9 +411,8 @@ lquery_in(PG_FUNCTION_ARGS)
{
if (charlen == 1 && t_iseq(ptr, '{'))
state = LQPRS_WAITFNUM;
else if (is_delimiter(ptr, charlen))
else if (delimiter)
{
delimiter = true;
curqlevel->low = 0;
curqlevel->high = 0xffff;
state = LQPRS_WAITLEVEL;
Expand Down Expand Up @@ -475,9 +469,8 @@ lquery_in(PG_FUNCTION_ARGS)
}
else if (state == LQPRS_WAITEND)
{
if (is_delimiter(ptr, charlen))
if (delimiter)
{
delimiter = true;
state = LQPRS_WAITLEVEL;
curqlevel = NEXTLEV(curqlevel);
}
Expand All @@ -490,8 +483,8 @@ lquery_in(PG_FUNCTION_ARGS)

if (delimiter)
{
ptr += charlen * 2;
pos += 2;
ptr += NODE_DELIMITER_LEN;
pos += NODE_DELIMITER_LEN;
}
else
{
Expand Down

0 comments on commit 91f3d23

Please sign in to comment.