Skip to content

Commit 16d531a

Browse files
committed
Refactor multirange_in()
This commit preserves the logic of multirange_in() but makes it more clear what's going on. Also, this commit fixes the compiler warning spotted by the buildfarm. Reported-by: Tom Lane Discussion: https://postgr.es/m/2246043.1609290699%40sss.pgh.pa.us
1 parent 7ca37fb commit 16d531a

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

src/backend/utils/adt/multirangetypes.c

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ multirange_in(PG_FUNCTION_ARGS)
128128
MultirangeType *ret;
129129
MultirangeParseState parse_state;
130130
const char *ptr = input_str;
131-
const char *range_str = NULL;
131+
const char *range_str_begin = NULL;
132132
int32 range_str_len;
133-
char *range_str_copy;
133+
char *range_str;
134134

135135
cache = get_multirange_io_data(fcinfo, mltrngtypoid, IOFunc_input);
136136
rangetyp = cache->typcache->rngtype;
@@ -170,7 +170,7 @@ multirange_in(PG_FUNCTION_ARGS)
170170
case MULTIRANGE_BEFORE_RANGE:
171171
if (ch == '[' || ch == '(')
172172
{
173-
range_str = ptr;
173+
range_str_begin = ptr;
174174
parse_state = MULTIRANGE_IN_RANGE;
175175
}
176176
else if (ch == '}' && ranges_seen == 0)
@@ -191,14 +191,10 @@ multirange_in(PG_FUNCTION_ARGS)
191191
errdetail("Expected range start.")));
192192
break;
193193
case MULTIRANGE_IN_RANGE:
194-
if (ch == '"')
195-
parse_state = MULTIRANGE_IN_RANGE_QUOTED;
196-
else if (ch == '\\')
197-
parse_state = MULTIRANGE_IN_RANGE_ESCAPED;
198-
else if (ch == ']' || ch == ')')
194+
if (ch == ']' || ch == ')')
199195
{
200-
range_str_len = ptr - range_str + 1;
201-
range_str_copy = pnstrdup(range_str, range_str_len);
196+
range_str_len = ptr - range_str_begin + 1;
197+
range_str = pnstrdup(range_str_begin, range_str_len);
202198
if (range_capacity == range_count)
203199
{
204200
range_capacity *= 2;
@@ -207,18 +203,30 @@ multirange_in(PG_FUNCTION_ARGS)
207203
}
208204
ranges_seen++;
209205
range = DatumGetRangeTypeP(InputFunctionCall(&cache->typioproc,
210-
range_str_copy,
206+
range_str,
211207
cache->typioparam,
212208
typmod));
213209
if (!RangeIsEmpty(range))
214210
ranges[range_count++] = range;
215211
parse_state = MULTIRANGE_AFTER_RANGE;
216212
}
217213
else
218-
/* include it in range_str */ ;
214+
{
215+
if (ch == '"')
216+
parse_state = MULTIRANGE_IN_RANGE_QUOTED;
217+
else if (ch == '\\')
218+
parse_state = MULTIRANGE_IN_RANGE_ESCAPED;
219+
/*
220+
* We will include this character into range_str once we
221+
* find the end of the range value.
222+
*/
223+
}
219224
break;
220225
case MULTIRANGE_IN_RANGE_ESCAPED:
221-
/* include it in range_str */
226+
/*
227+
* We will include this character into range_str once we find
228+
* the end of the range value.
229+
*/
222230
parse_state = MULTIRANGE_IN_RANGE;
223231
break;
224232
case MULTIRANGE_IN_RANGE_QUOTED:
@@ -232,8 +240,11 @@ multirange_in(PG_FUNCTION_ARGS)
232240
parse_state = MULTIRANGE_IN_RANGE;
233241
else if (ch == '\\')
234242
parse_state = MULTIRANGE_IN_RANGE_QUOTED_ESCAPED;
235-
else
236-
/* include it in range_str */ ;
243+
244+
/*
245+
* We will include this character into range_str once we
246+
* find the end of the range value.
247+
*/
237248
break;
238249
case MULTIRANGE_AFTER_RANGE:
239250
if (ch == ',')
@@ -248,7 +259,10 @@ multirange_in(PG_FUNCTION_ARGS)
248259
errdetail("Expected comma or end of multirange.")));
249260
break;
250261
case MULTIRANGE_IN_RANGE_QUOTED_ESCAPED:
251-
/* include it in range_str */
262+
/*
263+
* We will include this character into range_str once we find
264+
* the end of the range value.
265+
*/
252266
parse_state = MULTIRANGE_IN_RANGE_QUOTED;
253267
break;
254268
default:

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy