PostgreSQL Source Code: src/backend/regex/regc_pg_locale.c Source File

PostgreSQL Source Code git master
regc_pg_locale.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 *
3 * regc_pg_locale.c
4 * ctype functions adapted to work on pg_wchar (a/k/a chr),
5 * and functions to cache the results of wholesale ctype probing.
6 *
7 * This file is #included by regcomp.c; it's not meant to compile standalone.
8 *
9 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
10 * Portions Copyright (c) 1994, Regents of the University of California
11 *
12 * IDENTIFICATION
13 * src/backend/regex/regc_pg_locale.c
14 *
15 *-------------------------------------------------------------------------
16 */
17
18#include "catalog/pg_collation.h"
19#include "common/unicode_case.h"
20#include "common/unicode_category.h"
21#include "utils/pg_locale.h"
22
23 static pg_locale_t pg_regex_locale;
24
25 static struct pg_locale_struct dummy_c_locale = {
26 .collate_is_c = true,
27 .ctype_is_c = true,
28};
29
30/*
31 * Hard-wired character properties for C locale
32 */
33 #define PG_ISDIGIT 0x01
34 #define PG_ISALPHA 0x02
35 #define PG_ISALNUM (PG_ISDIGIT | PG_ISALPHA)
36 #define PG_ISUPPER 0x04
37 #define PG_ISLOWER 0x08
38 #define PG_ISGRAPH 0x10
39 #define PG_ISPRINT 0x20
40 #define PG_ISPUNCT 0x40
41 #define PG_ISSPACE 0x80
42
43 static const unsigned char pg_char_properties[128] = {
44 /* NUL */ 0,
45 /* ^A */ 0,
46 /* ^B */ 0,
47 /* ^C */ 0,
48 /* ^D */ 0,
49 /* ^E */ 0,
50 /* ^F */ 0,
51 /* ^G */ 0,
52 /* ^H */ 0,
53 /* ^I */ PG_ISSPACE,
54 /* ^J */ PG_ISSPACE,
55 /* ^K */ PG_ISSPACE,
56 /* ^L */ PG_ISSPACE,
57 /* ^M */ PG_ISSPACE,
58 /* ^N */ 0,
59 /* ^O */ 0,
60 /* ^P */ 0,
61 /* ^Q */ 0,
62 /* ^R */ 0,
63 /* ^S */ 0,
64 /* ^T */ 0,
65 /* ^U */ 0,
66 /* ^V */ 0,
67 /* ^W */ 0,
68 /* ^X */ 0,
69 /* ^Y */ 0,
70 /* ^Z */ 0,
71 /* ^[ */ 0,
72 /* ^\ */ 0,
73 /* ^] */ 0,
74 /* ^^ */ 0,
75 /* ^_ */ 0,
76 /* */ PG_ISPRINT | PG_ISSPACE,
77 /* ! */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
78 /* " */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
79 /* # */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
80 /* $ */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
81 /* % */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
82 /* & */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
83 /* ' */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
84 /* ( */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
85 /* ) */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
86 /* * */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
87 /* + */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
88 /* , */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
89 /* - */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
90 /* . */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
91 /* / */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
92 /* 0 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
93 /* 1 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
94 /* 2 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
95 /* 3 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
96 /* 4 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
97 /* 5 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
98 /* 6 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
99 /* 7 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
100 /* 8 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
101 /* 9 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
102 /* : */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
103 /* ; */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
104 /* < */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
105 /* = */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
106 /* > */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
107 /* ? */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
108 /* @ */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
109 /* A */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
110 /* B */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
111 /* C */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
112 /* D */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
113 /* E */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
114 /* F */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
115 /* G */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
116 /* H */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
117 /* I */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
118 /* J */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
119 /* K */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
120 /* L */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
121 /* M */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
122 /* N */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
123 /* O */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
124 /* P */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
125 /* Q */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
126 /* R */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
127 /* S */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
128 /* T */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
129 /* U */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
130 /* V */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
131 /* W */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
132 /* X */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
133 /* Y */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
134 /* Z */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
135 /* [ */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
136 /* \ */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
137 /* ] */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
138 /* ^ */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
139 /* _ */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
140 /* ` */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
141 /* a */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
142 /* b */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
143 /* c */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
144 /* d */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
145 /* e */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
146 /* f */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
147 /* g */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
148 /* h */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
149 /* i */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
150 /* j */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
151 /* k */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
152 /* l */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
153 /* m */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
154 /* n */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
155 /* o */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
156 /* p */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
157 /* q */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
158 /* r */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
159 /* s */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
160 /* t */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
161 /* u */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
162 /* v */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
163 /* w */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
164 /* x */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
165 /* y */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
166 /* z */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
167 /* { */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
168 /* | */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
169 /* } */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
170 /* ~ */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
171 /* DEL */ 0
172};
173
174
175/*
176 * pg_set_regex_collation: set collation for these functions to obey
177 *
178 * This is called when beginning compilation or execution of a regexp.
179 * Since there's no need for reentrancy of regexp operations, it's okay
180 * to store the results in static variables.
181 */
182void
183 pg_set_regex_collation(Oid collation)
184{
185 pg_locale_t locale = 0;
186
187 if (!OidIsValid(collation))
188 {
189 /*
190 * This typically means that the parser could not resolve a conflict
191 * of implicit collations, so report it that way.
192 */
193 ereport(ERROR,
194 (errcode(ERRCODE_INDETERMINATE_COLLATION),
195 errmsg("could not determine which collation to use for regular expression"),
196 errhint("Use the COLLATE clause to set the collation explicitly.")));
197 }
198
199 if (collation == C_COLLATION_OID)
200 {
201 /*
202 * Some callers expect regexes to work for C_COLLATION_OID before
203 * catalog access is available, so we can't call
204 * pg_newlocale_from_collation().
205 */
206 locale = &dummy_c_locale;
207 }
208 else
209 {
210 locale = pg_newlocale_from_collation(collation);
211
212 if (!locale->deterministic)
213 ereport(ERROR,
214 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
215 errmsg("nondeterministic collations are not supported for regular expressions")));
216
217 if (locale->ctype_is_c)
218 {
219 /*
220 * C/POSIX collations use this path regardless of database
221 * encoding
222 */
223 locale = &dummy_c_locale;
224 }
225 }
226
227 pg_regex_locale = locale;
228}
229
230static int
231 pg_wc_isdigit(pg_wchar c)
232{
233 if (pg_regex_locale->ctype_is_c)
234 return (c <= (pg_wchar) 127 &&
235 (pg_char_properties[c] & PG_ISDIGIT));
236 else
237 return pg_regex_locale->ctype->wc_isdigit(c, pg_regex_locale);
238}
239
240static int
241 pg_wc_isalpha(pg_wchar c)
242{
243 if (pg_regex_locale->ctype_is_c)
244 return (c <= (pg_wchar) 127 &&
245 (pg_char_properties[c] & PG_ISALPHA));
246 else
247 return pg_regex_locale->ctype->wc_isalpha(c, pg_regex_locale);
248}
249
250static int
251 pg_wc_isalnum(pg_wchar c)
252{
253 if (pg_regex_locale->ctype_is_c)
254 return (c <= (pg_wchar) 127 &&
255 (pg_char_properties[c] & PG_ISALNUM));
256 else
257 return pg_regex_locale->ctype->wc_isalnum(c, pg_regex_locale);
258}
259
260static int
261 pg_wc_isword(pg_wchar c)
262{
263 /* We define word characters as alnum class plus underscore */
264 if (c == CHR('_'))
265 return 1;
266 return pg_wc_isalnum(c);
267}
268
269static int
270 pg_wc_isupper(pg_wchar c)
271{
272 if (pg_regex_locale->ctype_is_c)
273 return (c <= (pg_wchar) 127 &&
274 (pg_char_properties[c] & PG_ISUPPER));
275 else
276 return pg_regex_locale->ctype->wc_isupper(c, pg_regex_locale);
277}
278
279static int
280 pg_wc_islower(pg_wchar c)
281{
282 if (pg_regex_locale->ctype_is_c)
283 return (c <= (pg_wchar) 127 &&
284 (pg_char_properties[c] & PG_ISLOWER));
285 else
286 return pg_regex_locale->ctype->wc_islower(c, pg_regex_locale);
287}
288
289static int
290 pg_wc_isgraph(pg_wchar c)
291{
292 if (pg_regex_locale->ctype_is_c)
293 return (c <= (pg_wchar) 127 &&
294 (pg_char_properties[c] & PG_ISGRAPH));
295 else
296 return pg_regex_locale->ctype->wc_isgraph(c, pg_regex_locale);
297}
298
299static int
300 pg_wc_isprint(pg_wchar c)
301{
302 if (pg_regex_locale->ctype_is_c)
303 return (c <= (pg_wchar) 127 &&
304 (pg_char_properties[c] & PG_ISPRINT));
305 else
306 return pg_regex_locale->ctype->wc_isprint(c, pg_regex_locale);
307}
308
309static int
310 pg_wc_ispunct(pg_wchar c)
311{
312 if (pg_regex_locale->ctype_is_c)
313 return (c <= (pg_wchar) 127 &&
314 (pg_char_properties[c] & PG_ISPUNCT));
315 else
316 return pg_regex_locale->ctype->wc_ispunct(c, pg_regex_locale);
317}
318
319static int
320 pg_wc_isspace(pg_wchar c)
321{
322 if (pg_regex_locale->ctype_is_c)
323 return (c <= (pg_wchar) 127 &&
324 (pg_char_properties[c] & PG_ISSPACE));
325 else
326 return pg_regex_locale->ctype->wc_isspace(c, pg_regex_locale);
327}
328
329static pg_wchar
330 pg_wc_toupper(pg_wchar c)
331{
332 if (pg_regex_locale->ctype_is_c)
333 {
334 if (c <= (pg_wchar) 127)
335 return pg_ascii_toupper((unsigned char) c);
336 return c;
337 }
338 else
339 return pg_regex_locale->ctype->wc_toupper(c, pg_regex_locale);
340}
341
342static pg_wchar
343 pg_wc_tolower(pg_wchar c)
344{
345 if (pg_regex_locale->ctype_is_c)
346 {
347 if (c <= (pg_wchar) 127)
348 return pg_ascii_tolower((unsigned char) c);
349 return c;
350 }
351 else
352 return pg_regex_locale->ctype->wc_tolower(c, pg_regex_locale);
353}
354
355
356/*
357 * These functions cache the results of probing libc's ctype behavior for
358 * all character codes of interest in a given encoding/collation. The
359 * result is provided as a "struct cvec", but notice that the representation
360 * is a touch different from a cvec created by regc_cvec.c: we allocate the
361 * chrs[] and ranges[] arrays separately from the struct so that we can
362 * realloc them larger at need. This is okay since the cvecs made here
363 * should never be freed by freecvec().
364 *
365 * We use malloc not palloc since we mustn't lose control on out-of-memory;
366 * the main regex code expects us to return a failure indication instead.
367 */
368
369 typedef int (*pg_wc_probefunc) (pg_wchar c);
370
371 typedef struct pg_ctype_cache
372{
373 pg_wc_probefunc probefunc; /* pg_wc_isalpha or a sibling */
374 pg_locale_t locale; /* locale this entry is for */
375 struct cvec cv; /* cache entry contents */
376 struct pg_ctype_cache *next; /* chain link */
377 } pg_ctype_cache;
378
379 static pg_ctype_cache *pg_ctype_cache_list = NULL;
380
381/*
382 * Add a chr or range to pcc->cv; return false if run out of memory
383 */
384static bool
385 store_match(pg_ctype_cache *pcc, pg_wchar chr1, int nchrs)
386{
387 chr *newchrs;
388
389 if (nchrs > 1)
390 {
391 if (pcc->cv.nranges >= pcc->cv.rangespace)
392 {
393 pcc->cv.rangespace *= 2;
394 newchrs = (chr *) realloc(pcc->cv.ranges,
395 pcc->cv.rangespace * sizeof(chr) * 2);
396 if (newchrs == NULL)
397 return false;
398 pcc->cv.ranges = newchrs;
399 }
400 pcc->cv.ranges[pcc->cv.nranges * 2] = chr1;
401 pcc->cv.ranges[pcc->cv.nranges * 2 + 1] = chr1 + nchrs - 1;
402 pcc->cv.nranges++;
403 }
404 else
405 {
406 assert(nchrs == 1);
407 if (pcc->cv.nchrs >= pcc->cv.chrspace)
408 {
409 pcc->cv.chrspace *= 2;
410 newchrs = (chr *) realloc(pcc->cv.chrs,
411 pcc->cv.chrspace * sizeof(chr));
412 if (newchrs == NULL)
413 return false;
414 pcc->cv.chrs = newchrs;
415 }
416 pcc->cv.chrs[pcc->cv.nchrs++] = chr1;
417 }
418 return true;
419}
420
421/*
422 * Given a probe function (e.g., pg_wc_isalpha) get a struct cvec for all
423 * chrs satisfying the probe function. The active collation is the one
424 * previously set by pg_set_regex_collation. Return NULL if out of memory.
425 *
426 * Note that the result must not be freed or modified by caller.
427 */
428static struct cvec *
429 pg_ctype_get_cache(pg_wc_probefunc probefunc, int cclasscode)
430{
431 pg_ctype_cache *pcc;
432 pg_wchar max_chr;
433 pg_wchar cur_chr;
434 int nmatches;
435 chr *newchrs;
436
437 /*
438 * Do we already have the answer cached?
439 */
440 for (pcc = pg_ctype_cache_list; pcc != NULL; pcc = pcc->next)
441 {
442 if (pcc->probefunc == probefunc &&
443 pcc->locale == pg_regex_locale)
444 return &pcc->cv;
445 }
446
447 /*
448 * Nope, so initialize some workspace ...
449 */
450 pcc = (pg_ctype_cache *) malloc(sizeof(pg_ctype_cache));
451 if (pcc == NULL)
452 return NULL;
453 pcc->probefunc = probefunc;
454 pcc->locale = pg_regex_locale;
455 pcc->cv.nchrs = 0;
456 pcc->cv.chrspace = 128;
457 pcc->cv.chrs = (chr *) malloc(pcc->cv.chrspace * sizeof(chr));
458 pcc->cv.nranges = 0;
459 pcc->cv.rangespace = 64;
460 pcc->cv.ranges = (chr *) malloc(pcc->cv.rangespace * sizeof(chr) * 2);
461 if (pcc->cv.chrs == NULL || pcc->cv.ranges == NULL)
462 goto out_of_memory;
463 pcc->cv.cclasscode = cclasscode;
464
465 /*
466 * Decide how many character codes we ought to look through. In general
467 * we don't go past MAX_SIMPLE_CHR; chr codes above that are handled at
468 * runtime using the "high colormap" mechanism. However, in C locale
469 * there's no need to go further than 127, and if we only have a 1-byte
470 * <ctype.h> API there's no need to go further than that can handle.
471 *
472 * If it's not MAX_SIMPLE_CHR that's constraining the search, mark the
473 * output cvec as not having any locale-dependent behavior, since there
474 * will be no need to do any run-time locale checks. (The #if's here
475 * would always be true for production values of MAX_SIMPLE_CHR, but it's
476 * useful to allow it to be small for testing purposes.)
477 */
478 if (pg_regex_locale->ctype_is_c)
479 {
480#if MAX_SIMPLE_CHR >= 127
481 max_chr = (pg_wchar) 127;
482 pcc->cv.cclasscode = -1;
483#else
484 max_chr = (pg_wchar) MAX_SIMPLE_CHR;
485#endif
486 }
487 else
488 {
489 if (pg_regex_locale->ctype->max_chr != 0 &&
490 pg_regex_locale->ctype->max_chr <= MAX_SIMPLE_CHR)
491 {
492 max_chr = pg_regex_locale->ctype->max_chr;
493 pcc->cv.cclasscode = -1;
494 }
495 else
496 max_chr = (pg_wchar) MAX_SIMPLE_CHR;
497 }
498
499 /*
500 * And scan 'em ...
501 */
502 nmatches = 0; /* number of consecutive matches */
503
504 for (cur_chr = 0; cur_chr <= max_chr; cur_chr++)
505 {
506 if ((*probefunc) (cur_chr))
507 nmatches++;
508 else if (nmatches > 0)
509 {
510 if (!store_match(pcc, cur_chr - nmatches, nmatches))
511 goto out_of_memory;
512 nmatches = 0;
513 }
514 }
515
516 if (nmatches > 0)
517 if (!store_match(pcc, cur_chr - nmatches, nmatches))
518 goto out_of_memory;
519
520 /*
521 * We might have allocated more memory than needed, if so free it
522 */
523 if (pcc->cv.nchrs == 0)
524 {
525 free(pcc->cv.chrs);
526 pcc->cv.chrs = NULL;
527 pcc->cv.chrspace = 0;
528 }
529 else if (pcc->cv.nchrs < pcc->cv.chrspace)
530 {
531 newchrs = (chr *) realloc(pcc->cv.chrs,
532 pcc->cv.nchrs * sizeof(chr));
533 if (newchrs == NULL)
534 goto out_of_memory;
535 pcc->cv.chrs = newchrs;
536 pcc->cv.chrspace = pcc->cv.nchrs;
537 }
538 if (pcc->cv.nranges == 0)
539 {
540 free(pcc->cv.ranges);
541 pcc->cv.ranges = NULL;
542 pcc->cv.rangespace = 0;
543 }
544 else if (pcc->cv.nranges < pcc->cv.rangespace)
545 {
546 newchrs = (chr *) realloc(pcc->cv.ranges,
547 pcc->cv.nranges * sizeof(chr) * 2);
548 if (newchrs == NULL)
549 goto out_of_memory;
550 pcc->cv.ranges = newchrs;
551 pcc->cv.rangespace = pcc->cv.nranges;
552 }
553
554 /*
555 * Success, link it into cache chain
556 */
557 pcc->next = pg_ctype_cache_list;
558 pg_ctype_cache_list = pcc;
559
560 return &pcc->cv;
561
562 /*
563 * Failure, clean up
564 */
565out_of_memory:
566 free(pcc->cv.chrs);
567 free(pcc->cv.ranges);
568 free(pcc);
569
570 return NULL;
571}
#define OidIsValid(objectId)
Definition: c.h:774
int errhint(const char *fmt,...)
Definition: elog.c:1321
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
#define realloc(a, b)
Definition: header.h:60
#define free(a)
Definition: header.h:65
#define malloc(a)
Definition: header.h:50
static char * locale
Definition: initdb.c:140
unsigned int pg_wchar
Definition: mbprint.c:31
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1166
unsigned char pg_ascii_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:146
unsigned char pg_ascii_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:135
unsigned int Oid
Definition: postgres_ext.h:32
c
char * c
Definition: preproc-cursor.c:31
static int pg_wc_islower(pg_wchar c)
static int pg_wc_isword(pg_wchar c)
static int pg_wc_isspace(pg_wchar c)
static pg_wchar pg_wc_tolower(pg_wchar c)
#define PG_ISLOWER
Definition: regc_pg_locale.c:37
#define PG_ISPRINT
Definition: regc_pg_locale.c:39
static int pg_wc_ispunct(pg_wchar c)
#define PG_ISALPHA
Definition: regc_pg_locale.c:34
static pg_ctype_cache * pg_ctype_cache_list
static int pg_wc_isgraph(pg_wchar c)
#define PG_ISGRAPH
Definition: regc_pg_locale.c:38
static pg_wchar pg_wc_toupper(pg_wchar c)
#define PG_ISPUNCT
Definition: regc_pg_locale.c:40
static bool store_match(pg_ctype_cache *pcc, pg_wchar chr1, int nchrs)
static int pg_wc_isprint(pg_wchar c)
#define PG_ISDIGIT
Definition: regc_pg_locale.c:33
#define PG_ISUPPER
Definition: regc_pg_locale.c:36
static int pg_wc_isalnum(pg_wchar c)
int(* pg_wc_probefunc)(pg_wchar c)
static int pg_wc_isdigit(pg_wchar c)
#define PG_ISALNUM
Definition: regc_pg_locale.c:35
static struct pg_locale_struct dummy_c_locale
Definition: regc_pg_locale.c:25
#define PG_ISSPACE
Definition: regc_pg_locale.c:41
void pg_set_regex_collation(Oid collation)
static struct cvec * pg_ctype_get_cache(pg_wc_probefunc probefunc, int cclasscode)
static pg_locale_t pg_regex_locale
Definition: regc_pg_locale.c:23
static int pg_wc_isupper(pg_wchar c)
static int pg_wc_isalpha(pg_wchar c)
static const unsigned char pg_char_properties[128]
Definition: regc_pg_locale.c:43
struct pg_ctype_cache pg_ctype_cache
#define MAX_SIMPLE_CHR
Definition: regcustom.h:87
pg_wchar chr
Definition: regcustom.h:59
#define CHR(c)
Definition: regcustom.h:62
#define assert(x)
Definition: regcustom.h:56
pg_wchar(* wc_toupper)(pg_wchar wc, pg_locale_t locale)
Definition: pg_locale.h:113
pg_wchar max_chr
Definition: pg_locale.h:130
bool(* wc_ispunct)(pg_wchar wc, pg_locale_t locale)
Definition: pg_locale.h:111
bool(* wc_isprint)(pg_wchar wc, pg_locale_t locale)
Definition: pg_locale.h:110
bool(* wc_isalpha)(pg_wchar wc, pg_locale_t locale)
Definition: pg_locale.h:105
pg_wchar(* wc_tolower)(pg_wchar wc, pg_locale_t locale)
Definition: pg_locale.h:114
bool(* wc_isupper)(pg_wchar wc, pg_locale_t locale)
Definition: pg_locale.h:107
bool(* wc_isspace)(pg_wchar wc, pg_locale_t locale)
Definition: pg_locale.h:112
bool(* wc_isgraph)(pg_wchar wc, pg_locale_t locale)
Definition: pg_locale.h:109
bool(* wc_islower)(pg_wchar wc, pg_locale_t locale)
Definition: pg_locale.h:108
bool(* wc_isalnum)(pg_wchar wc, pg_locale_t locale)
Definition: pg_locale.h:106
bool(* wc_isdigit)(pg_wchar wc, pg_locale_t locale)
Definition: pg_locale.h:104
Definition: regguts.h:279
int chrspace
Definition: regguts.h:281
int nchrs
Definition: regguts.h:280
int rangespace
Definition: regguts.h:284
chr * chrs
Definition: regguts.h:282
chr * ranges
Definition: regguts.h:285
int cclasscode
Definition: regguts.h:286
int nranges
Definition: regguts.h:283
pg_wc_probefunc probefunc
struct pg_ctype_cache * next
pg_locale_t locale
struct cvec cv
const struct ctype_methods * ctype
Definition: pg_locale.h:157
bool collate_is_c
Definition: pg_locale.h:152
bool ctype_is_c
Definition: pg_locale.h:153

AltStyle によって変換されたページ (->オリジナル) /