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