-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_container.c
312 lines (293 loc) · 9.24 KB
/
parse_container.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
/home/jian/postgres_misc/parse_container.c
gcc -I/home/jian/postgres/2023_05_25_beta5421/include/server -fPIC -c /home/jian/postgres_misc/parse_container.c
gcc -shared -o /home/jian/postgres_misc/parse_container.so /home/jian/postgres_misc/parse_container.0
*/
#include "postgres.h"
#include "utils/builtins.h"
#include "funcapi.h"
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(container_1d_parse);
static bool
ContainerCount(const char *str, int *dim, char typdelim, Node *escontext);
Datum
container_1d_parse(PG_FUNCTION_ARGS)
{
int dim;
char typdelim = ',';
text *src_txt = PG_GETARG_TEXT_PP(0);
char *src = text_to_cstring(src_txt);
Node *escontext = fcinfo->context;
if (ContainerCount(src,&dim,typdelim, escontext))
PG_RETURN_INT32(dim);
}
/*
* container_isspace() --- a non-locale-dependent isspace()
*
* We used to use isspace() for parsing container values, but that has
* undesirable results: an container value might be silently interpreted
* differently depending on the locale setting. Now we just hard-wire
* the traditional ASCII definition of isspace().
*/
static bool
container_isspace(char ch)
{
if (ch == ' ' ||
ch == '\t' ||
ch == '\n' ||
ch == '\r' ||
ch == '\v' ||
ch == '\f')
return true;
return false;
}
typedef enum
{
CONTAINER_NO_LEVEL,
CONTAINER_LEVEL_STARTED,
CONTAINER_ELEM_STARTED,
CONTAINER_ELEM_COMPLETED,
CONTAINER_QUOTED_ELEM_STARTED,
CONTAINER_QUOTED_ELEM_COMPLETED,
CONTAINER_LEVEL_COMPLETED,
CONTAINER_DELIMITED,
CONTAINER_END
} ContainerParseState;
/*
* ContainerCount
* Determines the dimensions for an container string. This includes
* syntax-checking the container structure decoration (braces and delimiters).
*
* If we detect an error, fill *escontext with error details and return -1
* (unless escontext isn't provided, in which case errors will be thrown).
*/
static bool
ContainerCount(const char *str, int *dim, char typdelim, Node *escontext)
{
int nest_level = 0,
i;
int ndim = 1, // one dimension only.
nelems; // dimension elements only.
const char *ptr;
ContainerParseState parse_state = CONTAINER_NO_LEVEL;
dim[0] = -1;
/* Scan string until we reach closing brace */
ptr = str;
while (parse_state != CONTAINER_END)
{
bool new_element = false;
switch (*ptr)
{
case '\0':
/* Signal a premature end of the string */
ereturn(escontext, -1,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed conatiner literal: \"%s\"", str),
errdetail("Unexpected end of input.")));
case '\\':
/*
* An escape must be after a level start, after an element
* start, or after an element delimiter. In any case we now
* must be past an element start.
*/
switch (parse_state)
{
case CONTAINER_LEVEL_STARTED:
case CONTAINER_DELIMITED:
/* start new unquoted element */
parse_state = CONTAINER_ELEM_STARTED;
new_element = true;
break;
case CONTAINER_ELEM_STARTED:
case CONTAINER_QUOTED_ELEM_STARTED:
/* already in element */
break;
default:
ereturn(escontext, -1,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed container literal: \"%s\"", str),
errdetail("Unexpected \"%c\" character.",
'\\')));
}
/* skip the escaped character */
if (*(ptr + 1))
ptr++;
else
ereturn(escontext, -1,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed container literal: \"%s\"", str),
errdetail("Unexpected end of input.")));
break;
case '"':
/*
* A quote must be after a level start, after a quoted element
* start, or after an element delimiter. In any case we now
* must be past an element start.
*/
switch (parse_state)
{
case CONTAINER_LEVEL_STARTED:
case CONTAINER_DELIMITED:
parse_state = CONTAINER_QUOTED_ELEM_STARTED;
new_element = true;
break;
case CONTAINER_QUOTED_ELEM_STARTED:
parse_state = CONTAINER_QUOTED_ELEM_COMPLETED;
break;
default:
ereturn(escontext, -1,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed container literal: \"%s\"", str),
errdetail("Unexpected container element.")));
}
break;
case '{':
if (parse_state != CONTAINER_QUOTED_ELEM_STARTED)
{
/*
* A left brace can occur if no nesting has occurred yet,
* after a level start
*/
if (parse_state != CONTAINER_NO_LEVEL &&
parse_state != CONTAINER_LEVEL_STARTED &&
parse_state != CONTAINER_DELIMITED)
ereturn(escontext, -1,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed container literal: \"%s\"", str),
errdetail("Unexpected \"%c\" character.",
'{')));
parse_state = CONTAINER_LEVEL_STARTED;
/* Initialize element counting in the new level */
if (nest_level >= 1)
ereturn(escontext, -1,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("number of container dimensions (%d) exceeds the maximum allowed (%d)",
nest_level + 1, 1)));
nelems = 0;
nest_level++;
}
break;
case '}':
if (parse_state != CONTAINER_QUOTED_ELEM_STARTED)
{
/*
* A right brace can occur after an element start, an
* element completion, a quoted element completion, or a
* level completion. We also allow it after a level
* start, that is an empty sub-container "{}" --- but that
* freezes the number of dimensions and all such
* sub-container must be at the same level, just like
* sub-containers containing elements.
*/
switch (parse_state)
{
case CONTAINER_ELEM_STARTED:
case CONTAINER_QUOTED_ELEM_COMPLETED:
case CONTAINER_LEVEL_COMPLETED:
/* okay */
break;
case CONTAINER_LEVEL_STARTED:
if (nest_level != ndim)
ereturn(escontext, -1,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed container literal: \"%s\"", str),
errdetail("Multidimensional container must have sub-containers with matching dimensions.")));
break;
default:
ereturn(escontext, -1,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed container literal: \"%s\"", str),
errdetail("Unexpected \"%c\" character.",
'}')));
}
parse_state = CONTAINER_LEVEL_COMPLETED;
/* The parse state check assured we're in a level. */
Assert(nest_level == 1);
nest_level--;
dim[0] = nelems;
/* Done if this is the outermost level's '}' */
if (nest_level == 0)
parse_state = CONTAINER_END;
}
break;
default:
if (parse_state != CONTAINER_QUOTED_ELEM_STARTED)
{
if (*ptr == typdelim)
{
/*
* Delimiters can occur after an element start, a
* quoted element completion, or a level completion.
*/
if (parse_state != CONTAINER_ELEM_STARTED &&
parse_state != CONTAINER_QUOTED_ELEM_COMPLETED &&
parse_state != CONTAINER_LEVEL_COMPLETED)
ereturn(escontext, -1,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed container literal: \"%s\"", str),
errdetail("Unexpected \"%c\" character.",
typdelim)));
parse_state = CONTAINER_DELIMITED;
}
else if (!container_isspace(*ptr))
{
/*
* Other non-space characters must be after a level
* start, after an element start, or after an element
* delimiter. In any case we now must be past an
* element start.
*
* If it's a space character, we can ignore it; it
* might be data or not, but it doesn't change the
* parsing state.
*/
switch (parse_state)
{
case CONTAINER_LEVEL_STARTED:
case CONTAINER_DELIMITED:
/* start new unquoted element */
parse_state = CONTAINER_ELEM_STARTED;
new_element = true;
break;
case CONTAINER_ELEM_STARTED:
/* already in element */
break;
default:
ereturn(escontext, -1,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed container literal: \"%s\"", str),
errdetail("Unexpected container element.")));
}
}
}
break;
}
/* To reduce duplication, all new-element cases go through here. */
if (new_element)
{
/*
* Once we have found an element, the number of dimensions can no
* longer increase, and subsequent elements must all be at the
* same nesting depth.
*/
if (nest_level != ndim)
ereturn(escontext, -1,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed container literal: \"%s\"", str),
errdetail("Multidimensional container must have sub-containers with matching dimensions.")));
/* Count the new element */
nelems++;
}
ptr++;
}
/* only whitespace is allowed after the closing brace */
while (*ptr)
{
if (!container_isspace(*ptr++))
ereturn(escontext, -1,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("malformed container literal: \"%s\"", str),
errdetail("Junk after closing right brace.")));
}
return true;
}