PostgreSQL Source Code: src/bin/psql/stringutils.c Source File (original) (raw)
1
2
3
4
5
6
7
9
10#include <ctype.h>
11
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
51char *
53 const char *whitespace,
54 const char *delim,
55 const char *quote,
56 char escape,
57 bool e_strings,
58 bool del_quotes,
60{
61 static char *storage = NULL;
62
63 static char *string = NULL;
64
65
66
67 unsigned int offset;
69 char *p;
70
71 if (s)
72 {
74
75
76
77
78
79
83 }
84
86 return NULL;
87
88
89 offset = strspn(string, whitespace);
90 start = &string[offset];
91
92
93 if (*start == '\0')
94 {
95
98 string = NULL;
99 return NULL;
100 }
101
102
103 if (delim && strchr(delim, *start))
104 {
105
106
107
108
109
110
111
113 if (*p != '\0')
114 {
115 if (!strchr(whitespace, *p))
116 memmove(p + 1, p, strlen(p) + 1);
117 *p = '\0';
118 string = p + 1;
119 }
120 else
121 {
122
123 string = p;
124 }
125
127 }
128
129
131 if (e_strings &&
132 (*p == 'E' || *p == 'e') &&
133 p[1] == '\'')
134 {
135 quote = "'";
136 escape = '\\';
137 p++;
138 }
139
140
141 if (quote && strchr(quote, *p))
142 {
143
144 char thisquote = *p++;
145
147 {
148 if (*p == escape && p[1] != '\0')
149 p++;
150 else if (*p == thisquote && p[1] == thisquote)
151 p++;
152 else if (*p == thisquote)
153 {
154 p++;
155 break;
156 }
157 }
158
159
160
161
162
163 if (*p != '\0')
164 {
165 if (!strchr(whitespace, *p))
166 memmove(p + 1, p, strlen(p) + 1);
167 *p = '\0';
168 string = p + 1;
169 }
170 else
171 {
172
173 string = p;
174 }
175
176
177 if (del_quotes)
179
181 }
182
183
184
185
186
187
188 offset = strcspn(start, whitespace);
189
190 if (delim)
191 {
192 unsigned int offset2 = strcspn(start, delim);
193
194 if (offset > offset2)
195 offset = offset2;
196 }
197
198 if (quote)
199 {
200 unsigned int offset2 = strcspn(start, quote);
201
202 if (offset > offset2)
203 offset = offset2;
204 }
205
206 p = start + offset;
207
208
209
210
211
212 if (*p != '\0')
213 {
214 if (!strchr(whitespace, *p))
215 memmove(p + 1, p, strlen(p) + 1);
216 *p = '\0';
217 string = p + 1;
218 }
219 else
220 {
221
222 string = p;
223 }
224
226}
227
228
229
230
231
232
233
234
235
236
237
238
239void
241{
242 char *src;
243 char *dst;
244
246 Assert(quote != '\0');
247
249
250 if (*src && *src == quote)
251 src++;
252
253 while (*src)
254 {
255 char c = *src;
256 int i;
257
258 if (c == quote && src[1] == '\0')
259 break;
260 else if (c == quote && src[1] == quote)
261 src++;
262 else if (c == escape && src[1] != '\0')
263 src++;
264
266 while (i--)
267 *dst++ = *src++;
268 }
269
270 *dst = '\0';
271}
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291char *
293 char quote, char escape, bool force_quote,
295{
296 const char *src;
297 char *ret;
298 char *dst;
299 bool need_quotes = force_quote;
300
302 Assert(quote != '\0');
303
305 dst = ret = pg_malloc(2 * strlen(src) + 3);
306
307 *dst++ = quote;
308
309 while (*src)
310 {
311 char c = *src;
312 int i;
313
314 if (c == quote)
315 {
316 need_quotes = true;
317 *dst++ = quote;
318 }
319 else if (c == escape)
320 {
321 need_quotes = true;
322 *dst++ = escape;
323 }
324 else if (strchr(entails_quote, c))
325 need_quotes = true;
326
328 while (i--)
329 *dst++ = *src++;
330 }
331
332 *dst++ = quote;
333 *dst = '\0';
334
335 if (!need_quotes)
336 {
338 ret = NULL;
339 }
340
341 return ret;
342}
int PQmblenBounded(const char *s, int encoding)
void * pg_malloc(size_t size)
Assert(PointerIsAligned(start, uint64))
static rewind_source * source
char * strtokx(const char *s, const char *whitespace, const char *delim, const char *quote, char escape, bool e_strings, bool del_quotes, int encoding)
char * quote_if_needed(const char *source, const char *entails_quote, char quote, char escape, bool force_quote, int encoding)
void strip_quotes(char *source, char quote, char escape, int encoding)