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-2024, 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"
21 #include "utils/pg_locale.h"
22 
23 /*
24  * To provide as much functionality as possible on a variety of platforms,
25  * without going so far as to implement everything from scratch, we use
26  * several implementation strategies depending on the situation:
27  *
28  * 1. In C/POSIX collations, we use hard-wired code. We can't depend on
29  * the <ctype.h> functions since those will obey LC_CTYPE. Note that these
30  * collations don't give a fig about multibyte characters.
31  *
32  * 2. In the "default" collation (which is supposed to obey LC_CTYPE):
33  *
34  * 2a. When working in UTF8 encoding, we use the <wctype.h> functions.
35  * This assumes that every platform uses Unicode codepoints directly
36  * as the wchar_t representation of Unicode. On some platforms
37  * wchar_t is only 16 bits wide, so we have to punt for codepoints > 0xFFFF.
38  *
39  * 2b. 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  * 3. Here, we use the locale_t-extended forms of the <wctype.h> and <ctype.h>
50  * functions, under exactly the same cases as #2.
51  *
52  * There is one notable difference between cases 2 and 3: in the "default"
53  * collation we force ASCII letters to follow ASCII upcase/downcase rules,
54  * while in a non-default collation we just let the library functions do what
55  * they will. The case where this matters is treatment of I/i in Turkish,
56  * and the behavior is meant to match the upper()/lower() SQL functions.
57  *
58  * We store the active collation setting in static variables. In principle
59  * it could be passed down to here via the regex library's "struct vars" data
60  * structure; but that would require somewhat invasive changes in the regex
61  * library, and right now there's no real benefit to be gained from that.
62  *
63  * NB: the coding here assumes pg_wchar is an unsigned type.
64  */
65 
66 typedef enum
67 {
68  PG_REGEX_LOCALE_C, /* C locale (encoding independent) */
69  PG_REGEX_BUILTIN, /* built-in Unicode semantics */
70  PG_REGEX_LOCALE_WIDE, /* Use <wctype.h> functions */
71  PG_REGEX_LOCALE_1BYTE, /* Use <ctype.h> functions */
72  PG_REGEX_LOCALE_WIDE_L, /* Use locale_t <wctype.h> functions */
73  PG_REGEX_LOCALE_1BYTE_L, /* Use locale_t <ctype.h> functions */
74  PG_REGEX_LOCALE_ICU, /* Use ICU uchar.h functions */
76 
80 
81 /*
82  * Hard-wired character properties for C locale
83  */
84 #define PG_ISDIGIT 0x01
85 #define PG_ISALPHA 0x02
86 #define PG_ISALNUM (PG_ISDIGIT | PG_ISALPHA)
87 #define PG_ISUPPER 0x04
88 #define PG_ISLOWER 0x08
89 #define PG_ISGRAPH 0x10
90 #define PG_ISPRINT 0x20
91 #define PG_ISPUNCT 0x40
92 #define PG_ISSPACE 0x80
93 
94 static const unsigned char pg_char_properties[128] = {
95  /* NUL */ 0,
96  /* ^A */ 0,
97  /* ^B */ 0,
98  /* ^C */ 0,
99  /* ^D */ 0,
100  /* ^E */ 0,
101  /* ^F */ 0,
102  /* ^G */ 0,
103  /* ^H */ 0,
104  /* ^I */ PG_ISSPACE,
105  /* ^J */ PG_ISSPACE,
106  /* ^K */ PG_ISSPACE,
107  /* ^L */ PG_ISSPACE,
108  /* ^M */ PG_ISSPACE,
109  /* ^N */ 0,
110  /* ^O */ 0,
111  /* ^P */ 0,
112  /* ^Q */ 0,
113  /* ^R */ 0,
114  /* ^S */ 0,
115  /* ^T */ 0,
116  /* ^U */ 0,
117  /* ^V */ 0,
118  /* ^W */ 0,
119  /* ^X */ 0,
120  /* ^Y */ 0,
121  /* ^Z */ 0,
122  /* ^[ */ 0,
123  /* ^\ */ 0,
124  /* ^] */ 0,
125  /* ^^ */ 0,
126  /* ^_ */ 0,
127  /* */ PG_ISPRINT | PG_ISSPACE,
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  /* * */ 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  /* . */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
142  /* / */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
143  /* 0 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
144  /* 1 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
145  /* 2 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
146  /* 3 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
147  /* 4 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
148  /* 5 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
149  /* 6 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
150  /* 7 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
151  /* 8 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
152  /* 9 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
153  /* : */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
154  /* ; */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
155  /* < */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
156  /* = */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
157  /* > */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
158  /* ? */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
159  /* @ */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
186  /* [ */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
187  /* \ */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
188  /* ] */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
189  /* ^ */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
190  /* _ */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
191  /* ` */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
218  /* { */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
219  /* | */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
220  /* } */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
221  /* ~ */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
222  /* DEL */ 0
223 };
224 
225 
226 /*
227  * pg_set_regex_collation: set collation for these functions to obey
228  *
229  * This is called when beginning compilation or execution of a regexp.
230  * Since there's no need for reentrancy of regexp operations, it's okay
231  * to store the results in static variables.
232  */
233 void
235 {
236  if (!OidIsValid(collation))
237  {
238  /*
239  * This typically means that the parser could not resolve a conflict
240  * of implicit collations, so report it that way.
241  */
242  ereport(ERROR,
243  (errcode(ERRCODE_INDETERMINATE_COLLATION),
244  errmsg("could not determine which collation to use for regular expression"),
245  errhint("Use the COLLATE clause to set the collation explicitly.")));
246  }
247 
248  if (lc_ctype_is_c(collation))
249  {
250  /* C/POSIX collations use this path regardless of database encoding */
252  pg_regex_locale = 0;
253  pg_regex_collation = C_COLLATION_OID;
254  }
255  else
256  {
258 
260  ereport(ERROR,
261  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
262  errmsg("nondeterministic collations are not supported for regular expressions")));
263 
264 #ifdef USE_ICU
265  if (pg_regex_locale && pg_regex_locale->provider == COLLPROVIDER_ICU)
267  else
268 #endif
269  if (GetDatabaseEncoding() == PG_UTF8)
270  {
271  if (pg_regex_locale)
272  {
273  if (pg_regex_locale->provider == COLLPROVIDER_BUILTIN)
275  else
277  }
278  else
280  }
281  else
282  {
283  if (pg_regex_locale)
285  else
287  }
288 
289  pg_regex_collation = collation;
290  }
291 }
292 
293 static int
295 {
296  switch (pg_regex_strategy)
297  {
298  case PG_REGEX_LOCALE_C:
299  return (c <= (pg_wchar) 127 &&
301  case PG_REGEX_BUILTIN:
302  return pg_u_isdigit(c, true);
304  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
305  return iswdigit((wint_t) c);
306  /* FALL THRU */
308  return (c <= (pg_wchar) UCHAR_MAX &&
309  isdigit((unsigned char) c));
311  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
312  return iswdigit_l((wint_t) c, pg_regex_locale->info.lt);
313  /* FALL THRU */
315  return (c <= (pg_wchar) UCHAR_MAX &&
316  isdigit_l((unsigned char) c, pg_regex_locale->info.lt));
317  break;
318  case PG_REGEX_LOCALE_ICU:
319 #ifdef USE_ICU
320  return u_isdigit(c);
321 #endif
322  break;
323  }
324  return 0; /* can't get here, but keep compiler quiet */
325 }
326 
327 static int
329 {
330  switch (pg_regex_strategy)
331  {
332  case PG_REGEX_LOCALE_C:
333  return (c <= (pg_wchar) 127 &&
335  case PG_REGEX_BUILTIN:
336  return pg_u_isalpha(c);
338  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
339  return iswalpha((wint_t) c);
340  /* FALL THRU */
342  return (c <= (pg_wchar) UCHAR_MAX &&
343  isalpha((unsigned char) c));
345  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
346  return iswalpha_l((wint_t) c, pg_regex_locale->info.lt);
347  /* FALL THRU */
349  return (c <= (pg_wchar) UCHAR_MAX &&
350  isalpha_l((unsigned char) c, pg_regex_locale->info.lt));
351  break;
352  case PG_REGEX_LOCALE_ICU:
353 #ifdef USE_ICU
354  return u_isalpha(c);
355 #endif
356  break;
357  }
358  return 0; /* can't get here, but keep compiler quiet */
359 }
360 
361 static int
363 {
364  switch (pg_regex_strategy)
365  {
366  case PG_REGEX_LOCALE_C:
367  return (c <= (pg_wchar) 127 &&
369  case PG_REGEX_BUILTIN:
370  return pg_u_isalnum(c, true);
372  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
373  return iswalnum((wint_t) c);
374  /* FALL THRU */
376  return (c <= (pg_wchar) UCHAR_MAX &&
377  isalnum((unsigned char) c));
379  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
380  return iswalnum_l((wint_t) c, pg_regex_locale->info.lt);
381  /* FALL THRU */
383  return (c <= (pg_wchar) UCHAR_MAX &&
384  isalnum_l((unsigned char) c, pg_regex_locale->info.lt));
385  break;
386  case PG_REGEX_LOCALE_ICU:
387 #ifdef USE_ICU
388  return u_isalnum(c);
389 #endif
390  break;
391  }
392  return 0; /* can't get here, but keep compiler quiet */
393 }
394 
395 static int
397 {
398  /* We define word characters as alnum class plus underscore */
399  if (c == CHR('_'))
400  return 1;
401  return pg_wc_isalnum(c);
402 }
403 
404 static int
406 {
407  switch (pg_regex_strategy)
408  {
409  case PG_REGEX_LOCALE_C:
410  return (c <= (pg_wchar) 127 &&
412  case PG_REGEX_BUILTIN:
413  return pg_u_isupper(c);
415  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
416  return iswupper((wint_t) c);
417  /* FALL THRU */
419  return (c <= (pg_wchar) UCHAR_MAX &&
420  isupper((unsigned char) c));
422  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
423  return iswupper_l((wint_t) c, pg_regex_locale->info.lt);
424  /* FALL THRU */
426  return (c <= (pg_wchar) UCHAR_MAX &&
427  isupper_l((unsigned char) c, pg_regex_locale->info.lt));
428  break;
429  case PG_REGEX_LOCALE_ICU:
430 #ifdef USE_ICU
431  return u_isupper(c);
432 #endif
433  break;
434  }
435  return 0; /* can't get here, but keep compiler quiet */
436 }
437 
438 static int
440 {
441  switch (pg_regex_strategy)
442  {
443  case PG_REGEX_LOCALE_C:
444  return (c <= (pg_wchar) 127 &&
446  case PG_REGEX_BUILTIN:
447  return pg_u_islower(c);
449  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
450  return iswlower((wint_t) c);
451  /* FALL THRU */
453  return (c <= (pg_wchar) UCHAR_MAX &&
454  islower((unsigned char) c));
456  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
457  return iswlower_l((wint_t) c, pg_regex_locale->info.lt);
458  /* FALL THRU */
460  return (c <= (pg_wchar) UCHAR_MAX &&
461  islower_l((unsigned char) c, pg_regex_locale->info.lt));
462  break;
463  case PG_REGEX_LOCALE_ICU:
464 #ifdef USE_ICU
465  return u_islower(c);
466 #endif
467  break;
468  }
469  return 0; /* can't get here, but keep compiler quiet */
470 }
471 
472 static int
474 {
475  switch (pg_regex_strategy)
476  {
477  case PG_REGEX_LOCALE_C:
478  return (c <= (pg_wchar) 127 &&
480  case PG_REGEX_BUILTIN:
481  return pg_u_isgraph(c);
483  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
484  return iswgraph((wint_t) c);
485  /* FALL THRU */
487  return (c <= (pg_wchar) UCHAR_MAX &&
488  isgraph((unsigned char) c));
490  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
491  return iswgraph_l((wint_t) c, pg_regex_locale->info.lt);
492  /* FALL THRU */
494  return (c <= (pg_wchar) UCHAR_MAX &&
495  isgraph_l((unsigned char) c, pg_regex_locale->info.lt));
496  break;
497  case PG_REGEX_LOCALE_ICU:
498 #ifdef USE_ICU
499  return u_isgraph(c);
500 #endif
501  break;
502  }
503  return 0; /* can't get here, but keep compiler quiet */
504 }
505 
506 static int
508 {
509  switch (pg_regex_strategy)
510  {
511  case PG_REGEX_LOCALE_C:
512  return (c <= (pg_wchar) 127 &&
514  case PG_REGEX_BUILTIN:
515  return pg_u_isprint(c);
517  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
518  return iswprint((wint_t) c);
519  /* FALL THRU */
521  return (c <= (pg_wchar) UCHAR_MAX &&
522  isprint((unsigned char) c));
524  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
525  return iswprint_l((wint_t) c, pg_regex_locale->info.lt);
526  /* FALL THRU */
528  return (c <= (pg_wchar) UCHAR_MAX &&
529  isprint_l((unsigned char) c, pg_regex_locale->info.lt));
530  break;
531  case PG_REGEX_LOCALE_ICU:
532 #ifdef USE_ICU
533  return u_isprint(c);
534 #endif
535  break;
536  }
537  return 0; /* can't get here, but keep compiler quiet */
538 }
539 
540 static int
542 {
543  switch (pg_regex_strategy)
544  {
545  case PG_REGEX_LOCALE_C:
546  return (c <= (pg_wchar) 127 &&
548  case PG_REGEX_BUILTIN:
549  return pg_u_ispunct(c, true);
551  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
552  return iswpunct((wint_t) c);
553  /* FALL THRU */
555  return (c <= (pg_wchar) UCHAR_MAX &&
556  ispunct((unsigned char) c));
558  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
559  return iswpunct_l((wint_t) c, pg_regex_locale->info.lt);
560  /* FALL THRU */
562  return (c <= (pg_wchar) UCHAR_MAX &&
563  ispunct_l((unsigned char) c, pg_regex_locale->info.lt));
564  break;
565  case PG_REGEX_LOCALE_ICU:
566 #ifdef USE_ICU
567  return u_ispunct(c);
568 #endif
569  break;
570  }
571  return 0; /* can't get here, but keep compiler quiet */
572 }
573 
574 static int
576 {
577  switch (pg_regex_strategy)
578  {
579  case PG_REGEX_LOCALE_C:
580  return (c <= (pg_wchar) 127 &&
582  case PG_REGEX_BUILTIN:
583  return pg_u_isspace(c);
585  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
586  return iswspace((wint_t) c);
587  /* FALL THRU */
589  return (c <= (pg_wchar) UCHAR_MAX &&
590  isspace((unsigned char) c));
592  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
593  return iswspace_l((wint_t) c, pg_regex_locale->info.lt);
594  /* FALL THRU */
596  return (c <= (pg_wchar) UCHAR_MAX &&
597  isspace_l((unsigned char) c, pg_regex_locale->info.lt));
598  break;
599  case PG_REGEX_LOCALE_ICU:
600 #ifdef USE_ICU
601  return u_isspace(c);
602 #endif
603  break;
604  }
605  return 0; /* can't get here, but keep compiler quiet */
606 }
607 
608 static pg_wchar
610 {
611  switch (pg_regex_strategy)
612  {
613  case PG_REGEX_LOCALE_C:
614  if (c <= (pg_wchar) 127)
615  return pg_ascii_toupper((unsigned char) c);
616  return c;
617  case PG_REGEX_BUILTIN:
618  return unicode_uppercase_simple(c);
620  /* force C behavior for ASCII characters, per comments above */
621  if (c <= (pg_wchar) 127)
622  return pg_ascii_toupper((unsigned char) c);
623  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
624  return towupper((wint_t) c);
625  /* FALL THRU */
627  /* force C behavior for ASCII characters, per comments above */
628  if (c <= (pg_wchar) 127)
629  return pg_ascii_toupper((unsigned char) c);
630  if (c <= (pg_wchar) UCHAR_MAX)
631  return toupper((unsigned char) c);
632  return c;
634  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
635  return towupper_l((wint_t) c, pg_regex_locale->info.lt);
636  /* FALL THRU */
638  if (c <= (pg_wchar) UCHAR_MAX)
639  return toupper_l((unsigned char) c, pg_regex_locale->info.lt);
640  return c;
641  case PG_REGEX_LOCALE_ICU:
642 #ifdef USE_ICU
643  return u_toupper(c);
644 #endif
645  break;
646  }
647  return 0; /* can't get here, but keep compiler quiet */
648 }
649 
650 static pg_wchar
652 {
653  switch (pg_regex_strategy)
654  {
655  case PG_REGEX_LOCALE_C:
656  if (c <= (pg_wchar) 127)
657  return pg_ascii_tolower((unsigned char) c);
658  return c;
659  case PG_REGEX_BUILTIN:
660  return unicode_lowercase_simple(c);
662  /* force C behavior for ASCII characters, per comments above */
663  if (c <= (pg_wchar) 127)
664  return pg_ascii_tolower((unsigned char) c);
665  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
666  return towlower((wint_t) c);
667  /* FALL THRU */
669  /* force C behavior for ASCII characters, per comments above */
670  if (c <= (pg_wchar) 127)
671  return pg_ascii_tolower((unsigned char) c);
672  if (c <= (pg_wchar) UCHAR_MAX)
673  return tolower((unsigned char) c);
674  return c;
676  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
677  return towlower_l((wint_t) c, pg_regex_locale->info.lt);
678  /* FALL THRU */
680  if (c <= (pg_wchar) UCHAR_MAX)
681  return tolower_l((unsigned char) c, pg_regex_locale->info.lt);
682  return c;
683  case PG_REGEX_LOCALE_ICU:
684 #ifdef USE_ICU
685  return u_tolower(c);
686 #endif
687  break;
688  }
689  return 0; /* can't get here, but keep compiler quiet */
690 }
691 
692 
693 /*
694  * These functions cache the results of probing libc's ctype behavior for
695  * all character codes of interest in a given encoding/collation. The
696  * result is provided as a "struct cvec", but notice that the representation
697  * is a touch different from a cvec created by regc_cvec.c: we allocate the
698  * chrs[] and ranges[] arrays separately from the struct so that we can
699  * realloc them larger at need. This is okay since the cvecs made here
700  * should never be freed by freecvec().
701  *
702  * We use malloc not palloc since we mustn't lose control on out-of-memory;
703  * the main regex code expects us to return a failure indication instead.
704  */
705 
706 typedef int (*pg_wc_probefunc) (pg_wchar c);
707 
708 typedef struct pg_ctype_cache
709 {
710  pg_wc_probefunc probefunc; /* pg_wc_isalpha or a sibling */
711  Oid collation; /* collation this entry is for */
712  struct cvec cv; /* cache entry contents */
713  struct pg_ctype_cache *next; /* chain link */
715 
717 
718 /*
719  * Add a chr or range to pcc->cv; return false if run out of memory
720  */
721 static bool
722 store_match(pg_ctype_cache *pcc, pg_wchar chr1, int nchrs)
723 {
724  chr *newchrs;
725 
726  if (nchrs > 1)
727  {
728  if (pcc->cv.nranges >= pcc->cv.rangespace)
729  {
730  pcc->cv.rangespace *= 2;
731  newchrs = (chr *) realloc(pcc->cv.ranges,
732  pcc->cv.rangespace * sizeof(chr) * 2);
733  if (newchrs == NULL)
734  return false;
735  pcc->cv.ranges = newchrs;
736  }
737  pcc->cv.ranges[pcc->cv.nranges * 2] = chr1;
738  pcc->cv.ranges[pcc->cv.nranges * 2 + 1] = chr1 + nchrs - 1;
739  pcc->cv.nranges++;
740  }
741  else
742  {
743  assert(nchrs == 1);
744  if (pcc->cv.nchrs >= pcc->cv.chrspace)
745  {
746  pcc->cv.chrspace *= 2;
747  newchrs = (chr *) realloc(pcc->cv.chrs,
748  pcc->cv.chrspace * sizeof(chr));
749  if (newchrs == NULL)
750  return false;
751  pcc->cv.chrs = newchrs;
752  }
753  pcc->cv.chrs[pcc->cv.nchrs++] = chr1;
754  }
755  return true;
756 }
757 
758 /*
759  * Given a probe function (e.g., pg_wc_isalpha) get a struct cvec for all
760  * chrs satisfying the probe function. The active collation is the one
761  * previously set by pg_set_regex_collation. Return NULL if out of memory.
762  *
763  * Note that the result must not be freed or modified by caller.
764  */
765 static struct cvec *
767 {
768  pg_ctype_cache *pcc;
769  pg_wchar max_chr;
770  pg_wchar cur_chr;
771  int nmatches;
772  chr *newchrs;
773 
774  /*
775  * Do we already have the answer cached?
776  */
777  for (pcc = pg_ctype_cache_list; pcc != NULL; pcc = pcc->next)
778  {
779  if (pcc->probefunc == probefunc &&
781  return &pcc->cv;
782  }
783 
784  /*
785  * Nope, so initialize some workspace ...
786  */
787  pcc = (pg_ctype_cache *) malloc(sizeof(pg_ctype_cache));
788  if (pcc == NULL)
789  return NULL;
790  pcc->probefunc = probefunc;
792  pcc->cv.nchrs = 0;
793  pcc->cv.chrspace = 128;
794  pcc->cv.chrs = (chr *) malloc(pcc->cv.chrspace * sizeof(chr));
795  pcc->cv.nranges = 0;
796  pcc->cv.rangespace = 64;
797  pcc->cv.ranges = (chr *) malloc(pcc->cv.rangespace * sizeof(chr) * 2);
798  if (pcc->cv.chrs == NULL || pcc->cv.ranges == NULL)
799  goto out_of_memory;
800  pcc->cv.cclasscode = cclasscode;
801 
802  /*
803  * Decide how many character codes we ought to look through. In general
804  * we don't go past MAX_SIMPLE_CHR; chr codes above that are handled at
805  * runtime using the "high colormap" mechanism. However, in C locale
806  * there's no need to go further than 127, and if we only have a 1-byte
807  * <ctype.h> API there's no need to go further than that can handle.
808  *
809  * If it's not MAX_SIMPLE_CHR that's constraining the search, mark the
810  * output cvec as not having any locale-dependent behavior, since there
811  * will be no need to do any run-time locale checks. (The #if's here
812  * would always be true for production values of MAX_SIMPLE_CHR, but it's
813  * useful to allow it to be small for testing purposes.)
814  */
815  switch (pg_regex_strategy)
816  {
817  case PG_REGEX_LOCALE_C:
818 #if MAX_SIMPLE_CHR >= 127
819  max_chr = (pg_wchar) 127;
820  pcc->cv.cclasscode = -1;
821 #else
822  max_chr = (pg_wchar) MAX_SIMPLE_CHR;
823 #endif
824  break;
825  case PG_REGEX_BUILTIN:
826  max_chr = (pg_wchar) MAX_SIMPLE_CHR;
827  break;
830  max_chr = (pg_wchar) MAX_SIMPLE_CHR;
831  break;
834 #if MAX_SIMPLE_CHR >= UCHAR_MAX
835  max_chr = (pg_wchar) UCHAR_MAX;
836  pcc->cv.cclasscode = -1;
837 #else
838  max_chr = (pg_wchar) MAX_SIMPLE_CHR;
839 #endif
840  break;
841  case PG_REGEX_LOCALE_ICU:
842  max_chr = (pg_wchar) MAX_SIMPLE_CHR;
843  break;
844  default:
845  Assert(false);
846  max_chr = 0; /* can't get here, but keep compiler quiet */
847  break;
848  }
849 
850  /*
851  * And scan 'em ...
852  */
853  nmatches = 0; /* number of consecutive matches */
854 
855  for (cur_chr = 0; cur_chr <= max_chr; cur_chr++)
856  {
857  if ((*probefunc) (cur_chr))
858  nmatches++;
859  else if (nmatches > 0)
860  {
861  if (!store_match(pcc, cur_chr - nmatches, nmatches))
862  goto out_of_memory;
863  nmatches = 0;
864  }
865  }
866 
867  if (nmatches > 0)
868  if (!store_match(pcc, cur_chr - nmatches, nmatches))
869  goto out_of_memory;
870 
871  /*
872  * We might have allocated more memory than needed, if so free it
873  */
874  if (pcc->cv.nchrs == 0)
875  {
876  free(pcc->cv.chrs);
877  pcc->cv.chrs = NULL;
878  pcc->cv.chrspace = 0;
879  }
880  else if (pcc->cv.nchrs < pcc->cv.chrspace)
881  {
882  newchrs = (chr *) realloc(pcc->cv.chrs,
883  pcc->cv.nchrs * sizeof(chr));
884  if (newchrs == NULL)
885  goto out_of_memory;
886  pcc->cv.chrs = newchrs;
887  pcc->cv.chrspace = pcc->cv.nchrs;
888  }
889  if (pcc->cv.nranges == 0)
890  {
891  free(pcc->cv.ranges);
892  pcc->cv.ranges = NULL;
893  pcc->cv.rangespace = 0;
894  }
895  else if (pcc->cv.nranges < pcc->cv.rangespace)
896  {
897  newchrs = (chr *) realloc(pcc->cv.ranges,
898  pcc->cv.nranges * sizeof(chr) * 2);
899  if (newchrs == NULL)
900  goto out_of_memory;
901  pcc->cv.ranges = newchrs;
902  pcc->cv.rangespace = pcc->cv.nranges;
903  }
904 
905  /*
906  * Success, link it into cache chain
907  */
908  pcc->next = pg_ctype_cache_list;
909  pg_ctype_cache_list = pcc;
910 
911  return &pcc->cv;
912 
913  /*
914  * Failure, clean up
915  */
916 out_of_memory:
917  free(pcc->cv.chrs);
918  free(pcc->cv.ranges);
919  free(pcc);
920 
921  return NULL;
922 }
#define Assert(condition)
Definition: c.h:858
#define OidIsValid(objectId)
Definition: c.h:775
int errhint(const char *fmt,...)
Definition: elog.c:1319
int errcode(int sqlerrcode)
Definition: elog.c:859
int errmsg(const char *fmt,...)
Definition: elog.c:1072
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
#define realloc(a, b)
Definition: header.h:60
#define free(a)
Definition: header.h:65
#define malloc(a)
Definition: header.h:50
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:1551
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1384
bool pg_locale_deterministic(pg_locale_t locale)
Definition: pg_locale.c:1531
@ 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:31
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)
static struct cvec * pg_ctype_get_cache(pg_wc_probefunc probefunc, int cclasscode)
#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_LOCALE_1BYTE
@ PG_REGEX_LOCALE_WIDE
@ PG_REGEX_BUILTIN
@ PG_REGEX_LOCALE_1BYTE_L
@ PG_REGEX_LOCALE_ICU
@ PG_REGEX_LOCALE_WIDE_L
@ PG_REGEX_LOCALE_C
#define PG_ISSPACE
void pg_set_regex_collation(Oid collation)
static Oid pg_regex_collation
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
struct cvec cv
locale_t lt
Definition: pg_locale.h:83
union pg_locale_struct::@151 info
pg_wchar unicode_uppercase_simple(pg_wchar code)
Definition: unicode_case.c:45
pg_wchar unicode_lowercase_simple(pg_wchar code)
Definition: unicode_case.c:29
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:426
#define iswalnum_l
Definition: win32_port.h:434
#define isgraph_l
Definition: win32_port.h:439
#define towupper_l
Definition: win32_port.h:428
#define ispunct_l
Definition: win32_port.h:443
#define isalpha_l
Definition: win32_port.h:431
#define iswgraph_l
Definition: win32_port.h:440
#define towlower_l
Definition: win32_port.h:427
#define iswspace_l
Definition: win32_port.h:446
#define isdigit_l
Definition: win32_port.h:429
#define tolower_l
Definition: win32_port.h:425
#define iswupper_l
Definition: win32_port.h:436
#define iswalpha_l
Definition: win32_port.h:432
#define isprint_l
Definition: win32_port.h:441
#define iswprint_l
Definition: win32_port.h:442
#define isupper_l
Definition: win32_port.h:435
#define isalnum_l
Definition: win32_port.h:433
#define islower_l
Definition: win32_port.h:437
#define iswlower_l
Definition: win32_port.h:438
#define iswpunct_l
Definition: win32_port.h:444
#define isspace_l
Definition: win32_port.h:445
#define iswdigit_l
Definition: win32_port.h:430