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 "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. Here, we use the locale_t-extended forms of the <wctype.h> and <ctype.h>
48  * functions, under exactly the same cases as #2.
49  *
50  * There is one notable difference between cases 2 and 3: in the "default"
51  * collation we force ASCII letters to follow ASCII upcase/downcase rules,
52  * while in a non-default collation we just let the library functions do what
53  * they will. The case where this matters is treatment of I/i in Turkish,
54  * and the behavior is meant to match the upper()/lower() SQL functions.
55  *
56  * We store the active collation setting in static variables. In principle
57  * it could be passed down to here via the regex library's "struct vars" data
58  * structure; but that would require somewhat invasive changes in the regex
59  * library, and right now there's no real benefit to be gained from that.
60  *
61  * NB: the coding here assumes pg_wchar is an unsigned type.
62  */
63 
64 typedef enum
65 {
66  PG_REGEX_LOCALE_C, /* C locale (encoding independent) */
67  PG_REGEX_LOCALE_WIDE, /* Use <wctype.h> functions */
68  PG_REGEX_LOCALE_1BYTE, /* Use <ctype.h> functions */
69  PG_REGEX_LOCALE_WIDE_L, /* Use locale_t <wctype.h> functions */
70  PG_REGEX_LOCALE_1BYTE_L, /* Use locale_t <ctype.h> functions */
71  PG_REGEX_LOCALE_ICU, /* Use ICU uchar.h functions */
73 
77 
78 /*
79  * Hard-wired character properties for C locale
80  */
81 #define PG_ISDIGIT 0x01
82 #define PG_ISALPHA 0x02
83 #define PG_ISALNUM (PG_ISDIGIT | PG_ISALPHA)
84 #define PG_ISUPPER 0x04
85 #define PG_ISLOWER 0x08
86 #define PG_ISGRAPH 0x10
87 #define PG_ISPRINT 0x20
88 #define PG_ISPUNCT 0x40
89 #define PG_ISSPACE 0x80
90 
91 static const unsigned char pg_char_properties[128] = {
92  /* NUL */ 0,
93  /* ^A */ 0,
94  /* ^B */ 0,
95  /* ^C */ 0,
96  /* ^D */ 0,
97  /* ^E */ 0,
98  /* ^F */ 0,
99  /* ^G */ 0,
100  /* ^H */ 0,
101  /* ^I */ PG_ISSPACE,
102  /* ^J */ PG_ISSPACE,
103  /* ^K */ PG_ISSPACE,
104  /* ^L */ PG_ISSPACE,
105  /* ^M */ PG_ISSPACE,
106  /* ^N */ 0,
107  /* ^O */ 0,
108  /* ^P */ 0,
109  /* ^Q */ 0,
110  /* ^R */ 0,
111  /* ^S */ 0,
112  /* ^T */ 0,
113  /* ^U */ 0,
114  /* ^V */ 0,
115  /* ^W */ 0,
116  /* ^X */ 0,
117  /* ^Y */ 0,
118  /* ^Z */ 0,
119  /* ^[ */ 0,
120  /* ^\ */ 0,
121  /* ^] */ 0,
122  /* ^^ */ 0,
123  /* ^_ */ 0,
124  /* */ PG_ISPRINT | PG_ISSPACE,
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  /* - */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
138  /* . */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
139  /* / */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
140  /* 0 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
141  /* 1 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
142  /* 2 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
143  /* 3 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
144  /* 4 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
145  /* 5 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
146  /* 6 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
147  /* 7 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
148  /* 8 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
149  /* 9 */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
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,
154  /* > */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
155  /* ? */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
156  /* @ */ 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,
186  /* ^ */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
187  /* _ */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
188  /* ` */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
215  /* { */ 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  /* DEL */ 0
220 };
221 
222 
223 /*
224  * pg_set_regex_collation: set collation for these functions to obey
225  *
226  * This is called when beginning compilation or execution of a regexp.
227  * Since there's no need for reentrancy of regexp operations, it's okay
228  * to store the results in static variables.
229  */
230 void
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  */
239  ereport(ERROR,
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 (lc_ctype_is_c(collation))
246  {
247  /* C/POSIX collations use this path regardless of database encoding */
249  pg_regex_locale = 0;
250  pg_regex_collation = C_COLLATION_OID;
251  }
252  else
253  {
255 
257  ereport(ERROR,
258  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
259  errmsg("nondeterministic collations are not supported for regular expressions")));
260 
261 #ifdef USE_ICU
262  if (pg_regex_locale && pg_regex_locale->provider == COLLPROVIDER_ICU)
264  else
265 #endif
266  if (GetDatabaseEncoding() == PG_UTF8)
267  {
268  if (pg_regex_locale)
270  else
272  }
273  else
274  {
275  if (pg_regex_locale)
277  else
279  }
280 
281  pg_regex_collation = collation;
282  }
283 }
284 
285 static int
287 {
288  switch (pg_regex_strategy)
289  {
290  case PG_REGEX_LOCALE_C:
291  return (c <= (pg_wchar) 127 &&
294  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
295  return iswdigit((wint_t) c);
296  /* FALL THRU */
298  return (c <= (pg_wchar) UCHAR_MAX &&
299  isdigit((unsigned char) c));
301  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
302  return iswdigit_l((wint_t) c, pg_regex_locale->info.lt);
303  /* FALL THRU */
305  return (c <= (pg_wchar) UCHAR_MAX &&
306  isdigit_l((unsigned char) c, pg_regex_locale->info.lt));
307  break;
308  case PG_REGEX_LOCALE_ICU:
309 #ifdef USE_ICU
310  return u_isdigit(c);
311 #endif
312  break;
313  }
314  return 0; /* can't get here, but keep compiler quiet */
315 }
316 
317 static int
319 {
320  switch (pg_regex_strategy)
321  {
322  case PG_REGEX_LOCALE_C:
323  return (c <= (pg_wchar) 127 &&
326  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
327  return iswalpha((wint_t) c);
328  /* FALL THRU */
330  return (c <= (pg_wchar) UCHAR_MAX &&
331  isalpha((unsigned char) c));
333  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
334  return iswalpha_l((wint_t) c, pg_regex_locale->info.lt);
335  /* FALL THRU */
337  return (c <= (pg_wchar) UCHAR_MAX &&
338  isalpha_l((unsigned char) c, pg_regex_locale->info.lt));
339  break;
340  case PG_REGEX_LOCALE_ICU:
341 #ifdef USE_ICU
342  return u_isalpha(c);
343 #endif
344  break;
345  }
346  return 0; /* can't get here, but keep compiler quiet */
347 }
348 
349 static int
351 {
352  switch (pg_regex_strategy)
353  {
354  case PG_REGEX_LOCALE_C:
355  return (c <= (pg_wchar) 127 &&
358  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
359  return iswalnum((wint_t) c);
360  /* FALL THRU */
362  return (c <= (pg_wchar) UCHAR_MAX &&
363  isalnum((unsigned char) c));
365  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
366  return iswalnum_l((wint_t) c, pg_regex_locale->info.lt);
367  /* FALL THRU */
369  return (c <= (pg_wchar) UCHAR_MAX &&
370  isalnum_l((unsigned char) c, pg_regex_locale->info.lt));
371  break;
372  case PG_REGEX_LOCALE_ICU:
373 #ifdef USE_ICU
374  return u_isalnum(c);
375 #endif
376  break;
377  }
378  return 0; /* can't get here, but keep compiler quiet */
379 }
380 
381 static int
383 {
384  /* We define word characters as alnum class plus underscore */
385  if (c == CHR('_'))
386  return 1;
387  return pg_wc_isalnum(c);
388 }
389 
390 static int
392 {
393  switch (pg_regex_strategy)
394  {
395  case PG_REGEX_LOCALE_C:
396  return (c <= (pg_wchar) 127 &&
399  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
400  return iswupper((wint_t) c);
401  /* FALL THRU */
403  return (c <= (pg_wchar) UCHAR_MAX &&
404  isupper((unsigned char) c));
406  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
407  return iswupper_l((wint_t) c, pg_regex_locale->info.lt);
408  /* FALL THRU */
410  return (c <= (pg_wchar) UCHAR_MAX &&
411  isupper_l((unsigned char) c, pg_regex_locale->info.lt));
412  break;
413  case PG_REGEX_LOCALE_ICU:
414 #ifdef USE_ICU
415  return u_isupper(c);
416 #endif
417  break;
418  }
419  return 0; /* can't get here, but keep compiler quiet */
420 }
421 
422 static int
424 {
425  switch (pg_regex_strategy)
426  {
427  case PG_REGEX_LOCALE_C:
428  return (c <= (pg_wchar) 127 &&
431  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
432  return iswlower((wint_t) c);
433  /* FALL THRU */
435  return (c <= (pg_wchar) UCHAR_MAX &&
436  islower((unsigned char) c));
438  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
439  return iswlower_l((wint_t) c, pg_regex_locale->info.lt);
440  /* FALL THRU */
442  return (c <= (pg_wchar) UCHAR_MAX &&
443  islower_l((unsigned char) c, pg_regex_locale->info.lt));
444  break;
445  case PG_REGEX_LOCALE_ICU:
446 #ifdef USE_ICU
447  return u_islower(c);
448 #endif
449  break;
450  }
451  return 0; /* can't get here, but keep compiler quiet */
452 }
453 
454 static int
456 {
457  switch (pg_regex_strategy)
458  {
459  case PG_REGEX_LOCALE_C:
460  return (c <= (pg_wchar) 127 &&
463  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
464  return iswgraph((wint_t) c);
465  /* FALL THRU */
467  return (c <= (pg_wchar) UCHAR_MAX &&
468  isgraph((unsigned char) c));
470  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
471  return iswgraph_l((wint_t) c, pg_regex_locale->info.lt);
472  /* FALL THRU */
474  return (c <= (pg_wchar) UCHAR_MAX &&
475  isgraph_l((unsigned char) c, pg_regex_locale->info.lt));
476  break;
477  case PG_REGEX_LOCALE_ICU:
478 #ifdef USE_ICU
479  return u_isgraph(c);
480 #endif
481  break;
482  }
483  return 0; /* can't get here, but keep compiler quiet */
484 }
485 
486 static int
488 {
489  switch (pg_regex_strategy)
490  {
491  case PG_REGEX_LOCALE_C:
492  return (c <= (pg_wchar) 127 &&
495  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
496  return iswprint((wint_t) c);
497  /* FALL THRU */
499  return (c <= (pg_wchar) UCHAR_MAX &&
500  isprint((unsigned char) c));
502  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
503  return iswprint_l((wint_t) c, pg_regex_locale->info.lt);
504  /* FALL THRU */
506  return (c <= (pg_wchar) UCHAR_MAX &&
507  isprint_l((unsigned char) c, pg_regex_locale->info.lt));
508  break;
509  case PG_REGEX_LOCALE_ICU:
510 #ifdef USE_ICU
511  return u_isprint(c);
512 #endif
513  break;
514  }
515  return 0; /* can't get here, but keep compiler quiet */
516 }
517 
518 static int
520 {
521  switch (pg_regex_strategy)
522  {
523  case PG_REGEX_LOCALE_C:
524  return (c <= (pg_wchar) 127 &&
527  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
528  return iswpunct((wint_t) c);
529  /* FALL THRU */
531  return (c <= (pg_wchar) UCHAR_MAX &&
532  ispunct((unsigned char) c));
534  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
535  return iswpunct_l((wint_t) c, pg_regex_locale->info.lt);
536  /* FALL THRU */
538  return (c <= (pg_wchar) UCHAR_MAX &&
539  ispunct_l((unsigned char) c, pg_regex_locale->info.lt));
540  break;
541  case PG_REGEX_LOCALE_ICU:
542 #ifdef USE_ICU
543  return u_ispunct(c);
544 #endif
545  break;
546  }
547  return 0; /* can't get here, but keep compiler quiet */
548 }
549 
550 static int
552 {
553  switch (pg_regex_strategy)
554  {
555  case PG_REGEX_LOCALE_C:
556  return (c <= (pg_wchar) 127 &&
559  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
560  return iswspace((wint_t) c);
561  /* FALL THRU */
563  return (c <= (pg_wchar) UCHAR_MAX &&
564  isspace((unsigned char) c));
566  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
567  return iswspace_l((wint_t) c, pg_regex_locale->info.lt);
568  /* FALL THRU */
570  return (c <= (pg_wchar) UCHAR_MAX &&
571  isspace_l((unsigned char) c, pg_regex_locale->info.lt));
572  break;
573  case PG_REGEX_LOCALE_ICU:
574 #ifdef USE_ICU
575  return u_isspace(c);
576 #endif
577  break;
578  }
579  return 0; /* can't get here, but keep compiler quiet */
580 }
581 
582 static pg_wchar
584 {
585  switch (pg_regex_strategy)
586  {
587  case PG_REGEX_LOCALE_C:
588  if (c <= (pg_wchar) 127)
589  return pg_ascii_toupper((unsigned char) c);
590  return c;
592  /* force C behavior for ASCII characters, per comments above */
593  if (c <= (pg_wchar) 127)
594  return pg_ascii_toupper((unsigned char) c);
595  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
596  return towupper((wint_t) c);
597  /* FALL THRU */
599  /* force C behavior for ASCII characters, per comments above */
600  if (c <= (pg_wchar) 127)
601  return pg_ascii_toupper((unsigned char) c);
602  if (c <= (pg_wchar) UCHAR_MAX)
603  return toupper((unsigned char) c);
604  return c;
606  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
607  return towupper_l((wint_t) c, pg_regex_locale->info.lt);
608  /* FALL THRU */
610  if (c <= (pg_wchar) UCHAR_MAX)
611  return toupper_l((unsigned char) c, pg_regex_locale->info.lt);
612  return c;
613  case PG_REGEX_LOCALE_ICU:
614 #ifdef USE_ICU
615  return u_toupper(c);
616 #endif
617  break;
618  }
619  return 0; /* can't get here, but keep compiler quiet */
620 }
621 
622 static pg_wchar
624 {
625  switch (pg_regex_strategy)
626  {
627  case PG_REGEX_LOCALE_C:
628  if (c <= (pg_wchar) 127)
629  return pg_ascii_tolower((unsigned char) c);
630  return c;
632  /* force C behavior for ASCII characters, per comments above */
633  if (c <= (pg_wchar) 127)
634  return pg_ascii_tolower((unsigned char) c);
635  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
636  return towlower((wint_t) c);
637  /* FALL THRU */
639  /* force C behavior for ASCII characters, per comments above */
640  if (c <= (pg_wchar) 127)
641  return pg_ascii_tolower((unsigned char) c);
642  if (c <= (pg_wchar) UCHAR_MAX)
643  return tolower((unsigned char) c);
644  return c;
646  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
647  return towlower_l((wint_t) c, pg_regex_locale->info.lt);
648  /* FALL THRU */
650  if (c <= (pg_wchar) UCHAR_MAX)
651  return tolower_l((unsigned char) c, pg_regex_locale->info.lt);
652  return c;
653  case PG_REGEX_LOCALE_ICU:
654 #ifdef USE_ICU
655  return u_tolower(c);
656 #endif
657  break;
658  }
659  return 0; /* can't get here, but keep compiler quiet */
660 }
661 
662 
663 /*
664  * These functions cache the results of probing libc's ctype behavior for
665  * all character codes of interest in a given encoding/collation. The
666  * result is provided as a "struct cvec", but notice that the representation
667  * is a touch different from a cvec created by regc_cvec.c: we allocate the
668  * chrs[] and ranges[] arrays separately from the struct so that we can
669  * realloc them larger at need. This is okay since the cvecs made here
670  * should never be freed by freecvec().
671  *
672  * We use malloc not palloc since we mustn't lose control on out-of-memory;
673  * the main regex code expects us to return a failure indication instead.
674  */
675 
676 typedef int (*pg_wc_probefunc) (pg_wchar c);
677 
678 typedef struct pg_ctype_cache
679 {
680  pg_wc_probefunc probefunc; /* pg_wc_isalpha or a sibling */
681  Oid collation; /* collation this entry is for */
682  struct cvec cv; /* cache entry contents */
683  struct pg_ctype_cache *next; /* chain link */
685 
687 
688 /*
689  * Add a chr or range to pcc->cv; return false if run out of memory
690  */
691 static bool
692 store_match(pg_ctype_cache *pcc, pg_wchar chr1, int nchrs)
693 {
694  chr *newchrs;
695 
696  if (nchrs > 1)
697  {
698  if (pcc->cv.nranges >= pcc->cv.rangespace)
699  {
700  pcc->cv.rangespace *= 2;
701  newchrs = (chr *) realloc(pcc->cv.ranges,
702  pcc->cv.rangespace * sizeof(chr) * 2);
703  if (newchrs == NULL)
704  return false;
705  pcc->cv.ranges = newchrs;
706  }
707  pcc->cv.ranges[pcc->cv.nranges * 2] = chr1;
708  pcc->cv.ranges[pcc->cv.nranges * 2 + 1] = chr1 + nchrs - 1;
709  pcc->cv.nranges++;
710  }
711  else
712  {
713  assert(nchrs == 1);
714  if (pcc->cv.nchrs >= pcc->cv.chrspace)
715  {
716  pcc->cv.chrspace *= 2;
717  newchrs = (chr *) realloc(pcc->cv.chrs,
718  pcc->cv.chrspace * sizeof(chr));
719  if (newchrs == NULL)
720  return false;
721  pcc->cv.chrs = newchrs;
722  }
723  pcc->cv.chrs[pcc->cv.nchrs++] = chr1;
724  }
725  return true;
726 }
727 
728 /*
729  * Given a probe function (e.g., pg_wc_isalpha) get a struct cvec for all
730  * chrs satisfying the probe function. The active collation is the one
731  * previously set by pg_set_regex_collation. Return NULL if out of memory.
732  *
733  * Note that the result must not be freed or modified by caller.
734  */
735 static struct cvec *
737 {
738  pg_ctype_cache *pcc;
739  pg_wchar max_chr;
740  pg_wchar cur_chr;
741  int nmatches;
742  chr *newchrs;
743 
744  /*
745  * Do we already have the answer cached?
746  */
747  for (pcc = pg_ctype_cache_list; pcc != NULL; pcc = pcc->next)
748  {
749  if (pcc->probefunc == probefunc &&
751  return &pcc->cv;
752  }
753 
754  /*
755  * Nope, so initialize some workspace ...
756  */
757  pcc = (pg_ctype_cache *) malloc(sizeof(pg_ctype_cache));
758  if (pcc == NULL)
759  return NULL;
760  pcc->probefunc = probefunc;
762  pcc->cv.nchrs = 0;
763  pcc->cv.chrspace = 128;
764  pcc->cv.chrs = (chr *) malloc(pcc->cv.chrspace * sizeof(chr));
765  pcc->cv.nranges = 0;
766  pcc->cv.rangespace = 64;
767  pcc->cv.ranges = (chr *) malloc(pcc->cv.rangespace * sizeof(chr) * 2);
768  if (pcc->cv.chrs == NULL || pcc->cv.ranges == NULL)
769  goto out_of_memory;
770  pcc->cv.cclasscode = cclasscode;
771 
772  /*
773  * Decide how many character codes we ought to look through. In general
774  * we don't go past MAX_SIMPLE_CHR; chr codes above that are handled at
775  * runtime using the "high colormap" mechanism. However, in C locale
776  * there's no need to go further than 127, and if we only have a 1-byte
777  * <ctype.h> API there's no need to go further than that can handle.
778  *
779  * If it's not MAX_SIMPLE_CHR that's constraining the search, mark the
780  * output cvec as not having any locale-dependent behavior, since there
781  * will be no need to do any run-time locale checks. (The #if's here
782  * would always be true for production values of MAX_SIMPLE_CHR, but it's
783  * useful to allow it to be small for testing purposes.)
784  */
785  switch (pg_regex_strategy)
786  {
787  case PG_REGEX_LOCALE_C:
788 #if MAX_SIMPLE_CHR >= 127
789  max_chr = (pg_wchar) 127;
790  pcc->cv.cclasscode = -1;
791 #else
792  max_chr = (pg_wchar) MAX_SIMPLE_CHR;
793 #endif
794  break;
797  max_chr = (pg_wchar) MAX_SIMPLE_CHR;
798  break;
801 #if MAX_SIMPLE_CHR >= UCHAR_MAX
802  max_chr = (pg_wchar) UCHAR_MAX;
803  pcc->cv.cclasscode = -1;
804 #else
805  max_chr = (pg_wchar) MAX_SIMPLE_CHR;
806 #endif
807  break;
808  case PG_REGEX_LOCALE_ICU:
809  max_chr = (pg_wchar) MAX_SIMPLE_CHR;
810  break;
811  default:
812  max_chr = 0; /* can't get here, but keep compiler quiet */
813  break;
814  }
815 
816  /*
817  * And scan 'em ...
818  */
819  nmatches = 0; /* number of consecutive matches */
820 
821  for (cur_chr = 0; cur_chr <= max_chr; cur_chr++)
822  {
823  if ((*probefunc) (cur_chr))
824  nmatches++;
825  else if (nmatches > 0)
826  {
827  if (!store_match(pcc, cur_chr - nmatches, nmatches))
828  goto out_of_memory;
829  nmatches = 0;
830  }
831  }
832 
833  if (nmatches > 0)
834  if (!store_match(pcc, cur_chr - nmatches, nmatches))
835  goto out_of_memory;
836 
837  /*
838  * We might have allocated more memory than needed, if so free it
839  */
840  if (pcc->cv.nchrs == 0)
841  {
842  free(pcc->cv.chrs);
843  pcc->cv.chrs = NULL;
844  pcc->cv.chrspace = 0;
845  }
846  else if (pcc->cv.nchrs < pcc->cv.chrspace)
847  {
848  newchrs = (chr *) realloc(pcc->cv.chrs,
849  pcc->cv.nchrs * sizeof(chr));
850  if (newchrs == NULL)
851  goto out_of_memory;
852  pcc->cv.chrs = newchrs;
853  pcc->cv.chrspace = pcc->cv.nchrs;
854  }
855  if (pcc->cv.nranges == 0)
856  {
857  free(pcc->cv.ranges);
858  pcc->cv.ranges = NULL;
859  pcc->cv.rangespace = 0;
860  }
861  else if (pcc->cv.nranges < pcc->cv.rangespace)
862  {
863  newchrs = (chr *) realloc(pcc->cv.ranges,
864  pcc->cv.nranges * sizeof(chr) * 2);
865  if (newchrs == NULL)
866  goto out_of_memory;
867  pcc->cv.ranges = newchrs;
868  pcc->cv.rangespace = pcc->cv.nranges;
869  }
870 
871  /*
872  * Success, link it into cache chain
873  */
874  pcc->next = pg_ctype_cache_list;
875  pg_ctype_cache_list = pcc;
876 
877  return &pcc->cv;
878 
879  /*
880  * Failure, clean up
881  */
882 out_of_memory:
883  free(pcc->cv.chrs);
884  free(pcc->cv.ranges);
885  free(pcc);
886 
887  return NULL;
888 }
#define OidIsValid(objectId)
Definition: c.h:762
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:1545
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1378
bool pg_locale_deterministic(pg_locale_t locale)
Definition: pg_locale.c:1525
@ 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: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::@146 info
#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