PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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
19#include "common/unicode_case.h"
21#include "utils/pg_locale.h"
22
23/*
24 * For the libc provider, to provide as much functionality as possible on a
25 * variety of platforms without going so far as to implement everything from
26 * scratch, we use several implementation strategies depending on the
27 * situation:
28 *
29 * 1. In C/POSIX collations, we use hard-wired code. We can't depend on
30 * the <ctype.h> functions since those will obey LC_CTYPE. Note that these
31 * collations don't give a fig about multibyte characters.
32 *
33 * 2. When working in UTF8 encoding, we use the <wctype.h> functions.
34 * This assumes that every platform uses Unicode codepoints directly
35 * as the wchar_t representation of Unicode. (XXX: ICU makes this assumption
36 * even for non-UTF8 encodings, which may be a problem.) On some platforms
37 * wchar_t is only 16 bits wide, so we have to punt for codepoints > 0xFFFF.
38 *
39 * 3. In all other encodings, we use the <ctype.h> functions for pg_wchar
40 * values up to 255, and punt for values above that. This is 100% correct
41 * only in single-byte encodings such as LATINn. However, non-Unicode
42 * multibyte encodings are mostly Far Eastern character sets for which the
43 * properties being tested here aren't very relevant for higher code values
44 * anyway. The difficulty with using the <wctype.h> functions with
45 * non-Unicode multibyte encodings is that we can have no certainty that
46 * the platform's wchar_t representation matches what we do in pg_wchar
47 * conversions.
48 *
49 * As a special case, in the "default" collation, (2) and (3) force ASCII
50 * letters to follow ASCII upcase/downcase rules, while in a non-default
51 * collation we just let the library functions do what they will. The case
52 * where this matters is treatment of I/i in Turkish, and the behavior is
53 * meant to match the upper()/lower() SQL functions.
54 *
55 * We store the active collation setting in static variables. In principle
56 * it could be passed down to here via the regex library's "struct vars" data
57 * structure; but that would require somewhat invasive changes in the regex
58 * library, and right now there's no real benefit to be gained from that.
59 *
60 * NB: the coding here assumes pg_wchar is an unsigned type.
61 */
62
63typedef enum
64{
65 PG_REGEX_STRATEGY_C, /* C locale (encoding independent) */
66 PG_REGEX_STRATEGY_BUILTIN, /* built-in Unicode semantics */
67 PG_REGEX_STRATEGY_LIBC_WIDE, /* Use locale_t <wctype.h> functions */
68 PG_REGEX_STRATEGY_LIBC_1BYTE, /* Use locale_t <ctype.h> functions */
69 PG_REGEX_STRATEGY_ICU, /* Use ICU uchar.h functions */
71
74
75/*
76 * Hard-wired character properties for C locale
77 */
78#define PG_ISDIGIT 0x01
79#define PG_ISALPHA 0x02
80#define PG_ISALNUM (PG_ISDIGIT | PG_ISALPHA)
81#define PG_ISUPPER 0x04
82#define PG_ISLOWER 0x08
83#define PG_ISGRAPH 0x10
84#define PG_ISPRINT 0x20
85#define PG_ISPUNCT 0x40
86#define PG_ISSPACE 0x80
87
88static const unsigned char pg_char_properties[128] = {
89 /* NUL */ 0,
90 /* ^A */ 0,
91 /* ^B */ 0,
92 /* ^C */ 0,
93 /* ^D */ 0,
94 /* ^E */ 0,
95 /* ^F */ 0,
96 /* ^G */ 0,
97 /* ^H */ 0,
98 /* ^I */ PG_ISSPACE,
99 /* ^J */ PG_ISSPACE,
100 /* ^K */ PG_ISSPACE,
101 /* ^L */ PG_ISSPACE,
102 /* ^M */ PG_ISSPACE,
103 /* ^N */ 0,
104 /* ^O */ 0,
105 /* ^P */ 0,
106 /* ^Q */ 0,
107 /* ^R */ 0,
108 /* ^S */ 0,
109 /* ^T */ 0,
110 /* ^U */ 0,
111 /* ^V */ 0,
112 /* ^W */ 0,
113 /* ^X */ 0,
114 /* ^Y */ 0,
115 /* ^Z */ 0,
116 /* ^[ */ 0,
117 /* ^\ */ 0,
118 /* ^] */ 0,
119 /* ^^ */ 0,
120 /* ^_ */ 0,
121 /* */ PG_ISPRINT | PG_ISSPACE,
122 /* ! */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
123 /* " */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
124 /* # */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
125 /* $ */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
126 /* % */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
127 /* & */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
128 /* ' */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
129 /* ( */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
130 /* ) */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
131 /* * */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
132 /* + */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
133 /* , */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
134 /* - */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
135 /* . */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
136 /* / */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
137 /* 0 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
138 /* 1 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
139 /* 2 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
140 /* 3 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
141 /* 4 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
142 /* 5 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
143 /* 6 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
144 /* 7 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
145 /* 8 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
146 /* 9 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
147 /* : */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
148 /* ; */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
149 /* < */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
150 /* = */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
151 /* > */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
152 /* ? */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
153 /* @ */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
180 /* [ */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
181 /* \ */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
182 /* ] */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
183 /* ^ */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
184 /* _ */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
185 /* ` */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
212 /* { */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
213 /* | */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
214 /* } */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
215 /* ~ */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
216 /* DEL */ 0
217};
218
219
220/*
221 * pg_set_regex_collation: set collation for these functions to obey
222 *
223 * This is called when beginning compilation or execution of a regexp.
224 * Since there's no need for reentrancy of regexp operations, it's okay
225 * to store the results in static variables.
226 */
227void
229{
231 PG_Locale_Strategy strategy;
232
233 if (!OidIsValid(collation))
234 {
235 /*
236 * This typically means that the parser could not resolve a conflict
237 * of implicit collations, so report it that way.
238 */
240 (errcode(ERRCODE_INDETERMINATE_COLLATION),
241 errmsg("could not determine which collation to use for regular expression"),
242 errhint("Use the COLLATE clause to set the collation explicitly.")));
243 }
244
245 if (collation == C_COLLATION_OID)
246 {
247 /*
248 * Some callers expect regexes to work for C_COLLATION_OID before
249 * catalog access is available, so we can't call
250 * pg_newlocale_from_collation().
251 */
252 strategy = PG_REGEX_STRATEGY_C;
253 locale = 0;
254 }
255 else
256 {
258
259 if (!locale->deterministic)
261 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
262 errmsg("nondeterministic collations are not supported for regular expressions")));
263
264 if (locale->ctype_is_c)
265 {
266 /*
267 * C/POSIX collations use this path regardless of database
268 * encoding
269 */
270 strategy = PG_REGEX_STRATEGY_C;
271 locale = 0;
272 }
273 else if (locale->provider == COLLPROVIDER_BUILTIN)
274 {
276 strategy = PG_REGEX_STRATEGY_BUILTIN;
277 }
278#ifdef USE_ICU
279 else if (locale->provider == COLLPROVIDER_ICU)
280 {
281 strategy = PG_REGEX_STRATEGY_ICU;
282 }
283#endif
284 else
285 {
286 Assert(locale->provider == COLLPROVIDER_LIBC);
289 else
291 }
292 }
293
294 pg_regex_strategy = strategy;
296}
297
298static int
300{
301 switch (pg_regex_strategy)
302 {
304 return (c <= (pg_wchar) 127 &&
309 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
310 return iswdigit_l((wint_t) c, pg_regex_locale->info.lt);
311 /* FALL THRU */
313 return (c <= (pg_wchar) UCHAR_MAX &&
314 isdigit_l((unsigned char) c, pg_regex_locale->info.lt));
315 break;
317#ifdef USE_ICU
318 return u_isdigit(c);
319#endif
320 break;
321 }
322 return 0; /* can't get here, but keep compiler quiet */
323}
324
325static int
327{
328 switch (pg_regex_strategy)
329 {
331 return (c <= (pg_wchar) 127 &&
334 return pg_u_isalpha(c);
336 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
337 return iswalpha_l((wint_t) c, pg_regex_locale->info.lt);
338 /* FALL THRU */
340 return (c <= (pg_wchar) UCHAR_MAX &&
341 isalpha_l((unsigned char) c, pg_regex_locale->info.lt));
342 break;
344#ifdef USE_ICU
345 return u_isalpha(c);
346#endif
347 break;
348 }
349 return 0; /* can't get here, but keep compiler quiet */
350}
351
352static int
354{
355 switch (pg_regex_strategy)
356 {
358 return (c <= (pg_wchar) 127 &&
363 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
364 return iswalnum_l((wint_t) c, pg_regex_locale->info.lt);
365 /* FALL THRU */
367 return (c <= (pg_wchar) UCHAR_MAX &&
368 isalnum_l((unsigned char) c, pg_regex_locale->info.lt));
369 break;
371#ifdef USE_ICU
372 return u_isalnum(c);
373#endif
374 break;
375 }
376 return 0; /* can't get here, but keep compiler quiet */
377}
378
379static int
381{
382 /* We define word characters as alnum class plus underscore */
383 if (c == CHR('_'))
384 return 1;
385 return pg_wc_isalnum(c);
386}
387
388static int
390{
391 switch (pg_regex_strategy)
392 {
394 return (c <= (pg_wchar) 127 &&
397 return pg_u_isupper(c);
399 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
400 return iswupper_l((wint_t) c, pg_regex_locale->info.lt);
401 /* FALL THRU */
403 return (c <= (pg_wchar) UCHAR_MAX &&
404 isupper_l((unsigned char) c, pg_regex_locale->info.lt));
405 break;
407#ifdef USE_ICU
408 return u_isupper(c);
409#endif
410 break;
411 }
412 return 0; /* can't get here, but keep compiler quiet */
413}
414
415static int
417{
418 switch (pg_regex_strategy)
419 {
421 return (c <= (pg_wchar) 127 &&
424 return pg_u_islower(c);
426 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
427 return iswlower_l((wint_t) c, pg_regex_locale->info.lt);
428 /* FALL THRU */
430 return (c <= (pg_wchar) UCHAR_MAX &&
431 islower_l((unsigned char) c, pg_regex_locale->info.lt));
432 break;
434#ifdef USE_ICU
435 return u_islower(c);
436#endif
437 break;
438 }
439 return 0; /* can't get here, but keep compiler quiet */
440}
441
442static int
444{
445 switch (pg_regex_strategy)
446 {
448 return (c <= (pg_wchar) 127 &&
451 return pg_u_isgraph(c);
453 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
454 return iswgraph_l((wint_t) c, pg_regex_locale->info.lt);
455 /* FALL THRU */
457 return (c <= (pg_wchar) UCHAR_MAX &&
458 isgraph_l((unsigned char) c, pg_regex_locale->info.lt));
459 break;
461#ifdef USE_ICU
462 return u_isgraph(c);
463#endif
464 break;
465 }
466 return 0; /* can't get here, but keep compiler quiet */
467}
468
469static int
471{
472 switch (pg_regex_strategy)
473 {
475 return (c <= (pg_wchar) 127 &&
478 return pg_u_isprint(c);
480 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
481 return iswprint_l((wint_t) c, pg_regex_locale->info.lt);
482 /* FALL THRU */
484 return (c <= (pg_wchar) UCHAR_MAX &&
485 isprint_l((unsigned char) c, pg_regex_locale->info.lt));
486 break;
488#ifdef USE_ICU
489 return u_isprint(c);
490#endif
491 break;
492 }
493 return 0; /* can't get here, but keep compiler quiet */
494}
495
496static int
498{
499 switch (pg_regex_strategy)
500 {
502 return (c <= (pg_wchar) 127 &&
507 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
508 return iswpunct_l((wint_t) c, pg_regex_locale->info.lt);
509 /* FALL THRU */
511 return (c <= (pg_wchar) UCHAR_MAX &&
512 ispunct_l((unsigned char) c, pg_regex_locale->info.lt));
513 break;
515#ifdef USE_ICU
516 return u_ispunct(c);
517#endif
518 break;
519 }
520 return 0; /* can't get here, but keep compiler quiet */
521}
522
523static int
525{
526 switch (pg_regex_strategy)
527 {
529 return (c <= (pg_wchar) 127 &&
532 return pg_u_isspace(c);
534 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
535 return iswspace_l((wint_t) c, pg_regex_locale->info.lt);
536 /* FALL THRU */
538 return (c <= (pg_wchar) UCHAR_MAX &&
539 isspace_l((unsigned char) c, pg_regex_locale->info.lt));
540 break;
542#ifdef USE_ICU
543 return u_isspace(c);
544#endif
545 break;
546 }
547 return 0; /* can't get here, but keep compiler quiet */
548}
549
550static pg_wchar
552{
553 switch (pg_regex_strategy)
554 {
556 if (c <= (pg_wchar) 127)
557 return pg_ascii_toupper((unsigned char) c);
558 return c;
562 /* force C behavior for ASCII characters, per comments above */
563 if (pg_regex_locale->is_default && c <= (pg_wchar) 127)
564 return pg_ascii_toupper((unsigned char) c);
565 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
566 return towupper_l((wint_t) c, pg_regex_locale->info.lt);
567 /* FALL THRU */
569 /* force C behavior for ASCII characters, per comments above */
570 if (pg_regex_locale->is_default && c <= (pg_wchar) 127)
571 return pg_ascii_toupper((unsigned char) c);
572 if (c <= (pg_wchar) UCHAR_MAX)
573 return toupper_l((unsigned char) c, pg_regex_locale->info.lt);
574 return c;
576#ifdef USE_ICU
577 return u_toupper(c);
578#endif
579 break;
580 }
581 return 0; /* can't get here, but keep compiler quiet */
582}
583
584static pg_wchar
586{
587 switch (pg_regex_strategy)
588 {
590 if (c <= (pg_wchar) 127)
591 return pg_ascii_tolower((unsigned char) c);
592 return c;
596 /* force C behavior for ASCII characters, per comments above */
597 if (pg_regex_locale->is_default && c <= (pg_wchar) 127)
598 return pg_ascii_tolower((unsigned char) c);
599 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
600 return towlower_l((wint_t) c, pg_regex_locale->info.lt);
601 /* FALL THRU */
603 /* force C behavior for ASCII characters, per comments above */
604 if (pg_regex_locale->is_default && c <= (pg_wchar) 127)
605 return pg_ascii_tolower((unsigned char) c);
606 if (c <= (pg_wchar) UCHAR_MAX)
607 return tolower_l((unsigned char) c, pg_regex_locale->info.lt);
608 return c;
610#ifdef USE_ICU
611 return u_tolower(c);
612#endif
613 break;
614 }
615 return 0; /* can't get here, but keep compiler quiet */
616}
617
618
619/*
620 * These functions cache the results of probing libc's ctype behavior for
621 * all character codes of interest in a given encoding/collation. The
622 * result is provided as a "struct cvec", but notice that the representation
623 * is a touch different from a cvec created by regc_cvec.c: we allocate the
624 * chrs[] and ranges[] arrays separately from the struct so that we can
625 * realloc them larger at need. This is okay since the cvecs made here
626 * should never be freed by freecvec().
627 *
628 * We use malloc not palloc since we mustn't lose control on out-of-memory;
629 * the main regex code expects us to return a failure indication instead.
630 */
631
632typedef int (*pg_wc_probefunc) (pg_wchar c);
633
634typedef struct pg_ctype_cache
635{
636 pg_wc_probefunc probefunc; /* pg_wc_isalpha or a sibling */
637 pg_locale_t locale; /* locale this entry is for */
638 struct cvec cv; /* cache entry contents */
639 struct pg_ctype_cache *next; /* chain link */
641
643
644/*
645 * Add a chr or range to pcc->cv; return false if run out of memory
646 */
647static bool
648store_match(pg_ctype_cache *pcc, pg_wchar chr1, int nchrs)
649{
650 chr *newchrs;
651
652 if (nchrs > 1)
653 {
654 if (pcc->cv.nranges >= pcc->cv.rangespace)
655 {
656 pcc->cv.rangespace *= 2;
657 newchrs = (chr *) realloc(pcc->cv.ranges,
658 pcc->cv.rangespace * sizeof(chr) * 2);
659 if (newchrs == NULL)
660 return false;
661 pcc->cv.ranges = newchrs;
662 }
663 pcc->cv.ranges[pcc->cv.nranges * 2] = chr1;
664 pcc->cv.ranges[pcc->cv.nranges * 2 + 1] = chr1 + nchrs - 1;
665 pcc->cv.nranges++;
666 }
667 else
668 {
669 assert(nchrs == 1);
670 if (pcc->cv.nchrs >= pcc->cv.chrspace)
671 {
672 pcc->cv.chrspace *= 2;
673 newchrs = (chr *) realloc(pcc->cv.chrs,
674 pcc->cv.chrspace * sizeof(chr));
675 if (newchrs == NULL)
676 return false;
677 pcc->cv.chrs = newchrs;
678 }
679 pcc->cv.chrs[pcc->cv.nchrs++] = chr1;
680 }
681 return true;
682}
683
684/*
685 * Given a probe function (e.g., pg_wc_isalpha) get a struct cvec for all
686 * chrs satisfying the probe function. The active collation is the one
687 * previously set by pg_set_regex_collation. Return NULL if out of memory.
688 *
689 * Note that the result must not be freed or modified by caller.
690 */
691static struct cvec *
693{
694 pg_ctype_cache *pcc;
695 pg_wchar max_chr;
696 pg_wchar cur_chr;
697 int nmatches;
698 chr *newchrs;
699
700 /*
701 * Do we already have the answer cached?
702 */
703 for (pcc = pg_ctype_cache_list; pcc != NULL; pcc = pcc->next)
704 {
705 if (pcc->probefunc == probefunc &&
706 pcc->locale == pg_regex_locale)
707 return &pcc->cv;
708 }
709
710 /*
711 * Nope, so initialize some workspace ...
712 */
713 pcc = (pg_ctype_cache *) malloc(sizeof(pg_ctype_cache));
714 if (pcc == NULL)
715 return NULL;
716 pcc->probefunc = probefunc;
717 pcc->locale = pg_regex_locale;
718 pcc->cv.nchrs = 0;
719 pcc->cv.chrspace = 128;
720 pcc->cv.chrs = (chr *) malloc(pcc->cv.chrspace * sizeof(chr));
721 pcc->cv.nranges = 0;
722 pcc->cv.rangespace = 64;
723 pcc->cv.ranges = (chr *) malloc(pcc->cv.rangespace * sizeof(chr) * 2);
724 if (pcc->cv.chrs == NULL || pcc->cv.ranges == NULL)
725 goto out_of_memory;
726 pcc->cv.cclasscode = cclasscode;
727
728 /*
729 * Decide how many character codes we ought to look through. In general
730 * we don't go past MAX_SIMPLE_CHR; chr codes above that are handled at
731 * runtime using the "high colormap" mechanism. However, in C locale
732 * there's no need to go further than 127, and if we only have a 1-byte
733 * <ctype.h> API there's no need to go further than that can handle.
734 *
735 * If it's not MAX_SIMPLE_CHR that's constraining the search, mark the
736 * output cvec as not having any locale-dependent behavior, since there
737 * will be no need to do any run-time locale checks. (The #if's here
738 * would always be true for production values of MAX_SIMPLE_CHR, but it's
739 * useful to allow it to be small for testing purposes.)
740 */
741 switch (pg_regex_strategy)
742 {
744#if MAX_SIMPLE_CHR >= 127
745 max_chr = (pg_wchar) 127;
746 pcc->cv.cclasscode = -1;
747#else
748 max_chr = (pg_wchar) MAX_SIMPLE_CHR;
749#endif
750 break;
752 max_chr = (pg_wchar) MAX_SIMPLE_CHR;
753 break;
755 max_chr = (pg_wchar) MAX_SIMPLE_CHR;
756 break;
758#if MAX_SIMPLE_CHR >= UCHAR_MAX
759 max_chr = (pg_wchar) UCHAR_MAX;
760 pcc->cv.cclasscode = -1;
761#else
762 max_chr = (pg_wchar) MAX_SIMPLE_CHR;
763#endif
764 break;
766 max_chr = (pg_wchar) MAX_SIMPLE_CHR;
767 break;
768 default:
769 Assert(false);
770 max_chr = 0; /* can't get here, but keep compiler quiet */
771 break;
772 }
773
774 /*
775 * And scan 'em ...
776 */
777 nmatches = 0; /* number of consecutive matches */
778
779 for (cur_chr = 0; cur_chr <= max_chr; cur_chr++)
780 {
781 if ((*probefunc) (cur_chr))
782 nmatches++;
783 else if (nmatches > 0)
784 {
785 if (!store_match(pcc, cur_chr - nmatches, nmatches))
786 goto out_of_memory;
787 nmatches = 0;
788 }
789 }
790
791 if (nmatches > 0)
792 if (!store_match(pcc, cur_chr - nmatches, nmatches))
793 goto out_of_memory;
794
795 /*
796 * We might have allocated more memory than needed, if so free it
797 */
798 if (pcc->cv.nchrs == 0)
799 {
800 free(pcc->cv.chrs);
801 pcc->cv.chrs = NULL;
802 pcc->cv.chrspace = 0;
803 }
804 else if (pcc->cv.nchrs < pcc->cv.chrspace)
805 {
806 newchrs = (chr *) realloc(pcc->cv.chrs,
807 pcc->cv.nchrs * sizeof(chr));
808 if (newchrs == NULL)
809 goto out_of_memory;
810 pcc->cv.chrs = newchrs;
811 pcc->cv.chrspace = pcc->cv.nchrs;
812 }
813 if (pcc->cv.nranges == 0)
814 {
815 free(pcc->cv.ranges);
816 pcc->cv.ranges = NULL;
817 pcc->cv.rangespace = 0;
818 }
819 else if (pcc->cv.nranges < pcc->cv.rangespace)
820 {
821 newchrs = (chr *) realloc(pcc->cv.ranges,
822 pcc->cv.nranges * sizeof(chr) * 2);
823 if (newchrs == NULL)
824 goto out_of_memory;
825 pcc->cv.ranges = newchrs;
826 pcc->cv.rangespace = pcc->cv.nranges;
827 }
828
829 /*
830 * Success, link it into cache chain
831 */
834
835 return &pcc->cv;
836
837 /*
838 * Failure, clean up
839 */
840out_of_memory:
841 free(pcc->cv.chrs);
842 free(pcc->cv.ranges);
843 free(pcc);
844
845 return NULL;
846}
#define OidIsValid(objectId)
Definition: c.h:746
int errhint(const char *fmt,...)
Definition: elog.c:1318
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:149
Assert(PointerIsAligned(start, uint64))
#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
int GetDatabaseEncoding(void)
Definition: mbutils.c:1261
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1188
@ PG_UTF8
Definition: pg_wchar.h:232
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:30
char * c
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
#define PG_ISPRINT
static int pg_wc_ispunct(pg_wchar c)
#define PG_ISALPHA
static pg_ctype_cache * pg_ctype_cache_list
static int pg_wc_isgraph(pg_wchar c)
#define PG_ISGRAPH
static pg_wchar pg_wc_toupper(pg_wchar c)
#define PG_ISPUNCT
static bool store_match(pg_ctype_cache *pcc, pg_wchar chr1, int nchrs)
static int pg_wc_isprint(pg_wchar c)
#define PG_ISDIGIT
#define PG_ISUPPER
static int pg_wc_isalnum(pg_wchar c)
int(* pg_wc_probefunc)(pg_wchar c)
static int pg_wc_isdigit(pg_wchar c)
static PG_Locale_Strategy pg_regex_strategy
#define PG_ISALNUM
PG_Locale_Strategy
@ PG_REGEX_STRATEGY_C
@ PG_REGEX_STRATEGY_LIBC_WIDE
@ PG_REGEX_STRATEGY_ICU
@ PG_REGEX_STRATEGY_BUILTIN
@ PG_REGEX_STRATEGY_LIBC_1BYTE
#define PG_ISSPACE
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
static int pg_wc_isupper(pg_wchar c)
static int pg_wc_isalpha(pg_wchar c)
static const unsigned char pg_char_properties[128]
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
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
struct pg_locale_struct::@161::@162 builtin
union pg_locale_struct::@161 info
pg_wchar unicode_uppercase_simple(pg_wchar code)
Definition: unicode_case.c:66
pg_wchar unicode_lowercase_simple(pg_wchar code)
Definition: unicode_case.c:50
bool pg_u_isspace(pg_wchar code)
bool pg_u_ispunct(pg_wchar code, bool posix)
bool pg_u_isprint(pg_wchar code)
bool pg_u_islower(pg_wchar code)
bool pg_u_isalpha(pg_wchar code)
bool pg_u_isalnum(pg_wchar code, bool posix)
bool pg_u_isupper(pg_wchar code)
bool pg_u_isdigit(pg_wchar code, bool posix)
bool pg_u_isgraph(pg_wchar code)
#define toupper_l
Definition: win32_port.h:434
#define iswalnum_l
Definition: win32_port.h:442
#define isgraph_l
Definition: win32_port.h:447
#define towupper_l
Definition: win32_port.h:436
#define ispunct_l
Definition: win32_port.h:451
#define isalpha_l
Definition: win32_port.h:439
#define iswgraph_l
Definition: win32_port.h:448
#define towlower_l
Definition: win32_port.h:435
#define iswspace_l
Definition: win32_port.h:454
#define isdigit_l
Definition: win32_port.h:437
#define tolower_l
Definition: win32_port.h:433
#define iswupper_l
Definition: win32_port.h:444
#define iswalpha_l
Definition: win32_port.h:440
#define isprint_l
Definition: win32_port.h:449
#define iswprint_l
Definition: win32_port.h:450
#define isupper_l
Definition: win32_port.h:443
#define isalnum_l
Definition: win32_port.h:441
#define islower_l
Definition: win32_port.h:445
#define iswlower_l
Definition: win32_port.h:446
#define iswpunct_l
Definition: win32_port.h:452
#define isspace_l
Definition: win32_port.h:453
#define iswdigit_l
Definition: win32_port.h:438