PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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-2017, 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 if
33  * available. This assumes that every platform uses Unicode codepoints
34  * directly 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, or on machines that lack <wctype.h>, we use
38  * the <ctype.h> functions for pg_wchar values up to 255, and punt for values
39  * above that. This is only 100% correct in single-byte encodings such as
40  * LATINn. However, non-Unicode multibyte encodings are mostly Far Eastern
41  * character sets for which the properties being tested here aren't very
42  * relevant for higher code values anyway. The difficulty with using the
43  * <wctype.h> functions with non-Unicode multibyte encodings is that we can
44  * have no certainty that the platform's wchar_t representation matches
45  * what we do in pg_wchar 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 (lc_ctype_is_c(collation))
235  {
236  /* C/POSIX collations use this path regardless of database encoding */
238  pg_regex_locale = 0;
240  }
241  else
242  {
243  if (collation == DEFAULT_COLLATION_OID)
244  pg_regex_locale = 0;
245  else if (OidIsValid(collation))
246  {
247  /*
248  * NB: pg_newlocale_from_collation will fail if not HAVE_LOCALE_T;
249  * the case of pg_regex_locale != 0 but not HAVE_LOCALE_T does not
250  * have to be considered below.
251  */
252  pg_regex_locale = pg_newlocale_from_collation(collation);
253  }
254  else
255  {
256  /*
257  * This typically means that the parser could not resolve a
258  * conflict of implicit collations, so report it that way.
259  */
260  ereport(ERROR,
261  (errcode(ERRCODE_INDETERMINATE_COLLATION),
262  errmsg("could not determine which collation to use for regular expression"),
263  errhint("Use the COLLATE clause to set the collation explicitly.")));
264  }
265 
266 #ifdef USE_ICU
267  if (pg_regex_locale && pg_regex_locale->provider == COLLPROVIDER_ICU)
269  else
270 #endif
271 #ifdef USE_WIDE_UPPER_LOWER
272  if (GetDatabaseEncoding() == PG_UTF8)
273  {
274  if (pg_regex_locale)
276  else
278  }
279  else
280 #endif /* USE_WIDE_UPPER_LOWER */
281  {
282  if (pg_regex_locale)
284  else
286  }
287 
288  pg_regex_collation = collation;
289  }
290 }
291 
292 static int
294 {
295  switch (pg_regex_strategy)
296  {
297  case PG_REGEX_LOCALE_C:
298  return (c <= (pg_wchar) 127 &&
301 #ifdef USE_WIDE_UPPER_LOWER
302  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
303  return iswdigit((wint_t) c);
304 #endif
305  /* FALL THRU */
307  return (c <= (pg_wchar) UCHAR_MAX &&
308  isdigit((unsigned char) c));
310 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
311  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
312  return iswdigit_l((wint_t) c, pg_regex_locale->info.lt);
313 #endif
314  /* FALL THRU */
316 #ifdef HAVE_LOCALE_T
317  return (c <= (pg_wchar) UCHAR_MAX &&
318  isdigit_l((unsigned char) c, pg_regex_locale->info.lt));
319 #endif
320  break;
321  case PG_REGEX_LOCALE_ICU:
322 #ifdef USE_ICU
323  return u_isdigit(c);
324 #endif
325  break;
326  }
327  return 0; /* can't get here, but keep compiler quiet */
328 }
329 
330 static int
332 {
333  switch (pg_regex_strategy)
334  {
335  case PG_REGEX_LOCALE_C:
336  return (c <= (pg_wchar) 127 &&
339 #ifdef USE_WIDE_UPPER_LOWER
340  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
341  return iswalpha((wint_t) c);
342 #endif
343  /* FALL THRU */
345  return (c <= (pg_wchar) UCHAR_MAX &&
346  isalpha((unsigned char) c));
348 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
349  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
350  return iswalpha_l((wint_t) c, pg_regex_locale->info.lt);
351 #endif
352  /* FALL THRU */
354 #ifdef HAVE_LOCALE_T
355  return (c <= (pg_wchar) UCHAR_MAX &&
356  isalpha_l((unsigned char) c, pg_regex_locale->info.lt));
357 #endif
358  break;
359  case PG_REGEX_LOCALE_ICU:
360 #ifdef USE_ICU
361  return u_isalpha(c);
362 #endif
363  break;
364  }
365  return 0; /* can't get here, but keep compiler quiet */
366 }
367 
368 static int
370 {
371  switch (pg_regex_strategy)
372  {
373  case PG_REGEX_LOCALE_C:
374  return (c <= (pg_wchar) 127 &&
377 #ifdef USE_WIDE_UPPER_LOWER
378  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
379  return iswalnum((wint_t) c);
380 #endif
381  /* FALL THRU */
383  return (c <= (pg_wchar) UCHAR_MAX &&
384  isalnum((unsigned char) c));
386 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
387  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
388  return iswalnum_l((wint_t) c, pg_regex_locale->info.lt);
389 #endif
390  /* FALL THRU */
392 #ifdef HAVE_LOCALE_T
393  return (c <= (pg_wchar) UCHAR_MAX &&
394  isalnum_l((unsigned char) c, pg_regex_locale->info.lt));
395 #endif
396  break;
397  case PG_REGEX_LOCALE_ICU:
398 #ifdef USE_ICU
399  return u_isalnum(c);
400 #endif
401  break;
402  }
403  return 0; /* can't get here, but keep compiler quiet */
404 }
405 
406 static int
408 {
409  switch (pg_regex_strategy)
410  {
411  case PG_REGEX_LOCALE_C:
412  return (c <= (pg_wchar) 127 &&
415 #ifdef USE_WIDE_UPPER_LOWER
416  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
417  return iswupper((wint_t) c);
418 #endif
419  /* FALL THRU */
421  return (c <= (pg_wchar) UCHAR_MAX &&
422  isupper((unsigned char) c));
424 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
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 #ifdef USE_WIDE_UPPER_LOWER
454  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
455  return iswlower((wint_t) c);
456 #endif
457  /* FALL THRU */
459  return (c <= (pg_wchar) UCHAR_MAX &&
460  islower((unsigned char) c));
462 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
463  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
464  return iswlower_l((wint_t) c, pg_regex_locale->info.lt);
465 #endif
466  /* FALL THRU */
468 #ifdef HAVE_LOCALE_T
469  return (c <= (pg_wchar) UCHAR_MAX &&
470  islower_l((unsigned char) c, pg_regex_locale->info.lt));
471 #endif
472  break;
473  case PG_REGEX_LOCALE_ICU:
474 #ifdef USE_ICU
475  return u_islower(c);
476 #endif
477  break;
478  }
479  return 0; /* can't get here, but keep compiler quiet */
480 }
481 
482 static int
484 {
485  switch (pg_regex_strategy)
486  {
487  case PG_REGEX_LOCALE_C:
488  return (c <= (pg_wchar) 127 &&
491 #ifdef USE_WIDE_UPPER_LOWER
492  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
493  return iswgraph((wint_t) c);
494 #endif
495  /* FALL THRU */
497  return (c <= (pg_wchar) UCHAR_MAX &&
498  isgraph((unsigned char) c));
500 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
501  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
502  return iswgraph_l((wint_t) c, pg_regex_locale->info.lt);
503 #endif
504  /* FALL THRU */
506 #ifdef HAVE_LOCALE_T
507  return (c <= (pg_wchar) UCHAR_MAX &&
508  isgraph_l((unsigned char) c, pg_regex_locale->info.lt));
509 #endif
510  break;
511  case PG_REGEX_LOCALE_ICU:
512 #ifdef USE_ICU
513  return u_isgraph(c);
514 #endif
515  break;
516  }
517  return 0; /* can't get here, but keep compiler quiet */
518 }
519 
520 static int
522 {
523  switch (pg_regex_strategy)
524  {
525  case PG_REGEX_LOCALE_C:
526  return (c <= (pg_wchar) 127 &&
529 #ifdef USE_WIDE_UPPER_LOWER
530  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
531  return iswprint((wint_t) c);
532 #endif
533  /* FALL THRU */
535  return (c <= (pg_wchar) UCHAR_MAX &&
536  isprint((unsigned char) c));
538 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
539  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
540  return iswprint_l((wint_t) c, pg_regex_locale->info.lt);
541 #endif
542  /* FALL THRU */
544 #ifdef HAVE_LOCALE_T
545  return (c <= (pg_wchar) UCHAR_MAX &&
546  isprint_l((unsigned char) c, pg_regex_locale->info.lt));
547 #endif
548  break;
549  case PG_REGEX_LOCALE_ICU:
550 #ifdef USE_ICU
551  return u_isprint(c);
552 #endif
553  break;
554  }
555  return 0; /* can't get here, but keep compiler quiet */
556 }
557 
558 static int
560 {
561  switch (pg_regex_strategy)
562  {
563  case PG_REGEX_LOCALE_C:
564  return (c <= (pg_wchar) 127 &&
567 #ifdef USE_WIDE_UPPER_LOWER
568  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
569  return iswpunct((wint_t) c);
570 #endif
571  /* FALL THRU */
573  return (c <= (pg_wchar) UCHAR_MAX &&
574  ispunct((unsigned char) c));
576 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
577  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
578  return iswpunct_l((wint_t) c, pg_regex_locale->info.lt);
579 #endif
580  /* FALL THRU */
582 #ifdef HAVE_LOCALE_T
583  return (c <= (pg_wchar) UCHAR_MAX &&
584  ispunct_l((unsigned char) c, pg_regex_locale->info.lt));
585 #endif
586  break;
587  case PG_REGEX_LOCALE_ICU:
588 #ifdef USE_ICU
589  return u_ispunct(c);
590 #endif
591  break;
592  }
593  return 0; /* can't get here, but keep compiler quiet */
594 }
595 
596 static int
598 {
599  switch (pg_regex_strategy)
600  {
601  case PG_REGEX_LOCALE_C:
602  return (c <= (pg_wchar) 127 &&
605 #ifdef USE_WIDE_UPPER_LOWER
606  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
607  return iswspace((wint_t) c);
608 #endif
609  /* FALL THRU */
611  return (c <= (pg_wchar) UCHAR_MAX &&
612  isspace((unsigned char) c));
614 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
615  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
616  return iswspace_l((wint_t) c, pg_regex_locale->info.lt);
617 #endif
618  /* FALL THRU */
620 #ifdef HAVE_LOCALE_T
621  return (c <= (pg_wchar) UCHAR_MAX &&
622  isspace_l((unsigned char) c, pg_regex_locale->info.lt));
623 #endif
624  break;
625  case PG_REGEX_LOCALE_ICU:
626 #ifdef USE_ICU
627  return u_isspace(c);
628 #endif
629  break;
630  }
631  return 0; /* can't get here, but keep compiler quiet */
632 }
633 
634 static pg_wchar
636 {
637  switch (pg_regex_strategy)
638  {
639  case PG_REGEX_LOCALE_C:
640  if (c <= (pg_wchar) 127)
641  return pg_ascii_toupper((unsigned char) c);
642  return c;
644  /* force C behavior for ASCII characters, per comments above */
645  if (c <= (pg_wchar) 127)
646  return pg_ascii_toupper((unsigned char) c);
647 #ifdef USE_WIDE_UPPER_LOWER
648  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
649  return towupper((wint_t) c);
650 #endif
651  /* FALL THRU */
653  /* force C behavior for ASCII characters, per comments above */
654  if (c <= (pg_wchar) 127)
655  return pg_ascii_toupper((unsigned char) c);
656  if (c <= (pg_wchar) UCHAR_MAX)
657  return toupper((unsigned char) c);
658  return c;
660 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
661  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
662  return towupper_l((wint_t) c, pg_regex_locale->info.lt);
663 #endif
664  /* FALL THRU */
666 #ifdef HAVE_LOCALE_T
667  if (c <= (pg_wchar) UCHAR_MAX)
668  return toupper_l((unsigned char) c, pg_regex_locale->info.lt);
669 #endif
670  return c;
671  case PG_REGEX_LOCALE_ICU:
672 #ifdef USE_ICU
673  return u_toupper(c);
674 #endif
675  break;
676  }
677  return 0; /* can't get here, but keep compiler quiet */
678 }
679 
680 static pg_wchar
682 {
683  switch (pg_regex_strategy)
684  {
685  case PG_REGEX_LOCALE_C:
686  if (c <= (pg_wchar) 127)
687  return pg_ascii_tolower((unsigned char) c);
688  return c;
690  /* force C behavior for ASCII characters, per comments above */
691  if (c <= (pg_wchar) 127)
692  return pg_ascii_tolower((unsigned char) c);
693 #ifdef USE_WIDE_UPPER_LOWER
694  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
695  return towlower((wint_t) c);
696 #endif
697  /* FALL THRU */
699  /* force C behavior for ASCII characters, per comments above */
700  if (c <= (pg_wchar) 127)
701  return pg_ascii_tolower((unsigned char) c);
702  if (c <= (pg_wchar) UCHAR_MAX)
703  return tolower((unsigned char) c);
704  return c;
706 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
707  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
708  return towlower_l((wint_t) c, pg_regex_locale->info.lt);
709 #endif
710  /* FALL THRU */
712 #ifdef HAVE_LOCALE_T
713  if (c <= (pg_wchar) UCHAR_MAX)
714  return tolower_l((unsigned char) c, pg_regex_locale->info.lt);
715 #endif
716  return c;
717  case PG_REGEX_LOCALE_ICU:
718 #ifdef USE_ICU
719  return u_tolower(c);
720 #endif
721  break;
722  }
723  return 0; /* can't get here, but keep compiler quiet */
724 }
725 
726 
727 /*
728  * These functions cache the results of probing libc's ctype behavior for
729  * all character codes of interest in a given encoding/collation. The
730  * result is provided as a "struct cvec", but notice that the representation
731  * is a touch different from a cvec created by regc_cvec.c: we allocate the
732  * chrs[] and ranges[] arrays separately from the struct so that we can
733  * realloc them larger at need. This is okay since the cvecs made here
734  * should never be freed by freecvec().
735  *
736  * We use malloc not palloc since we mustn't lose control on out-of-memory;
737  * the main regex code expects us to return a failure indication instead.
738  */
739 
740 typedef int (*pg_wc_probefunc) (pg_wchar c);
741 
742 typedef struct pg_ctype_cache
743 {
744  pg_wc_probefunc probefunc; /* pg_wc_isalpha or a sibling */
745  Oid collation; /* collation this entry is for */
746  struct cvec cv; /* cache entry contents */
747  struct pg_ctype_cache *next; /* chain link */
749 
751 
752 /*
753  * Add a chr or range to pcc->cv; return false if run out of memory
754  */
755 static bool
756 store_match(pg_ctype_cache *pcc, pg_wchar chr1, int nchrs)
757 {
758  chr *newchrs;
759 
760  if (nchrs > 1)
761  {
762  if (pcc->cv.nranges >= pcc->cv.rangespace)
763  {
764  pcc->cv.rangespace *= 2;
765  newchrs = (chr *) realloc(pcc->cv.ranges,
766  pcc->cv.rangespace * sizeof(chr) * 2);
767  if (newchrs == NULL)
768  return false;
769  pcc->cv.ranges = newchrs;
770  }
771  pcc->cv.ranges[pcc->cv.nranges * 2] = chr1;
772  pcc->cv.ranges[pcc->cv.nranges * 2 + 1] = chr1 + nchrs - 1;
773  pcc->cv.nranges++;
774  }
775  else
776  {
777  assert(nchrs == 1);
778  if (pcc->cv.nchrs >= pcc->cv.chrspace)
779  {
780  pcc->cv.chrspace *= 2;
781  newchrs = (chr *) realloc(pcc->cv.chrs,
782  pcc->cv.chrspace * sizeof(chr));
783  if (newchrs == NULL)
784  return false;
785  pcc->cv.chrs = newchrs;
786  }
787  pcc->cv.chrs[pcc->cv.nchrs++] = chr1;
788  }
789  return true;
790 }
791 
792 /*
793  * Given a probe function (e.g., pg_wc_isalpha) get a struct cvec for all
794  * chrs satisfying the probe function. The active collation is the one
795  * previously set by pg_set_regex_collation. Return NULL if out of memory.
796  *
797  * Note that the result must not be freed or modified by caller.
798  */
799 static struct cvec *
801 {
802  pg_ctype_cache *pcc;
803  pg_wchar max_chr;
804  pg_wchar cur_chr;
805  int nmatches;
806  chr *newchrs;
807 
808  /*
809  * Do we already have the answer cached?
810  */
811  for (pcc = pg_ctype_cache_list; pcc != NULL; pcc = pcc->next)
812  {
813  if (pcc->probefunc == probefunc &&
815  return &pcc->cv;
816  }
817 
818  /*
819  * Nope, so initialize some workspace ...
820  */
821  pcc = (pg_ctype_cache *) malloc(sizeof(pg_ctype_cache));
822  if (pcc == NULL)
823  return NULL;
824  pcc->probefunc = probefunc;
826  pcc->cv.nchrs = 0;
827  pcc->cv.chrspace = 128;
828  pcc->cv.chrs = (chr *) malloc(pcc->cv.chrspace * sizeof(chr));
829  pcc->cv.nranges = 0;
830  pcc->cv.rangespace = 64;
831  pcc->cv.ranges = (chr *) malloc(pcc->cv.rangespace * sizeof(chr) * 2);
832  if (pcc->cv.chrs == NULL || pcc->cv.ranges == NULL)
833  goto out_of_memory;
834  pcc->cv.cclasscode = cclasscode;
835 
836  /*
837  * Decide how many character codes we ought to look through. In general
838  * we don't go past MAX_SIMPLE_CHR; chr codes above that are handled at
839  * runtime using the "high colormap" mechanism. However, in C locale
840  * there's no need to go further than 127, and if we only have a 1-byte
841  * <ctype.h> API there's no need to go further than that can handle.
842  *
843  * If it's not MAX_SIMPLE_CHR that's constraining the search, mark the
844  * output cvec as not having any locale-dependent behavior, since there
845  * will be no need to do any run-time locale checks. (The #if's here
846  * would always be true for production values of MAX_SIMPLE_CHR, but it's
847  * useful to allow it to be small for testing purposes.)
848  */
849  switch (pg_regex_strategy)
850  {
851  case PG_REGEX_LOCALE_C:
852 #if MAX_SIMPLE_CHR >= 127
853  max_chr = (pg_wchar) 127;
854  pcc->cv.cclasscode = -1;
855 #else
856  max_chr = (pg_wchar) MAX_SIMPLE_CHR;
857 #endif
858  break;
861  max_chr = (pg_wchar) MAX_SIMPLE_CHR;
862  break;
865 #if MAX_SIMPLE_CHR >= UCHAR_MAX
866  max_chr = (pg_wchar) UCHAR_MAX;
867  pcc->cv.cclasscode = -1;
868 #else
869  max_chr = (pg_wchar) MAX_SIMPLE_CHR;
870 #endif
871  break;
872  case PG_REGEX_LOCALE_ICU:
873  max_chr = (pg_wchar) MAX_SIMPLE_CHR;
874  break;
875  default:
876  max_chr = 0; /* can't get here, but keep compiler quiet */
877  break;
878  }
879 
880  /*
881  * And scan 'em ...
882  */
883  nmatches = 0; /* number of consecutive matches */
884 
885  for (cur_chr = 0; cur_chr <= max_chr; cur_chr++)
886  {
887  if ((*probefunc) (cur_chr))
888  nmatches++;
889  else if (nmatches > 0)
890  {
891  if (!store_match(pcc, cur_chr - nmatches, nmatches))
892  goto out_of_memory;
893  nmatches = 0;
894  }
895  }
896 
897  if (nmatches > 0)
898  if (!store_match(pcc, cur_chr - nmatches, nmatches))
899  goto out_of_memory;
900 
901  /*
902  * We might have allocated more memory than needed, if so free it
903  */
904  if (pcc->cv.nchrs == 0)
905  {
906  free(pcc->cv.chrs);
907  pcc->cv.chrs = NULL;
908  pcc->cv.chrspace = 0;
909  }
910  else if (pcc->cv.nchrs < pcc->cv.chrspace)
911  {
912  newchrs = (chr *) realloc(pcc->cv.chrs,
913  pcc->cv.nchrs * sizeof(chr));
914  if (newchrs == NULL)
915  goto out_of_memory;
916  pcc->cv.chrs = newchrs;
917  pcc->cv.chrspace = pcc->cv.nchrs;
918  }
919  if (pcc->cv.nranges == 0)
920  {
921  free(pcc->cv.ranges);
922  pcc->cv.ranges = NULL;
923  pcc->cv.rangespace = 0;
924  }
925  else if (pcc->cv.nranges < pcc->cv.rangespace)
926  {
927  newchrs = (chr *) realloc(pcc->cv.ranges,
928  pcc->cv.nranges * sizeof(chr) * 2);
929  if (newchrs == NULL)
930  goto out_of_memory;
931  pcc->cv.ranges = newchrs;
932  pcc->cv.rangespace = pcc->cv.nranges;
933  }
934 
935  /*
936  * Success, link it into cache chain
937  */
938  pcc->next = pg_ctype_cache_list;
939  pg_ctype_cache_list = pcc;
940 
941  return &pcc->cv;
942 
943  /*
944  * Failure, clean up
945  */
946 out_of_memory:
947  if (pcc->cv.chrs)
948  free(pcc->cv.chrs);
949  if (pcc->cv.ranges)
950  free(pcc->cv.ranges);
951  free(pcc);
952 
953  return NULL;
954 }
#define ispunct_l
Definition: win32.h:342
#define COLLPROVIDER_ICU
Definition: pg_collation.h:85
static int pg_wc_ispunct(pg_wchar c)
#define PG_ISALPHA
#define isspace_l
Definition: win32.h:344
#define PG_ISPRINT
int errhint(const char *fmt,...)
Definition: elog.c:987
struct cvec cv
#define toupper_l
Definition: win32.h:325
#define iswalnum_l
Definition: win32.h:333
static const unsigned char pg_char_properties[128]
int(* pg_wc_probefunc)(pg_wchar c)
static pg_locale_t pg_regex_locale
#define iswprint_l
Definition: win32.h:341
pg_wc_probefunc probefunc
static Oid pg_regex_collation
static PG_Locale_Strategy pg_regex_strategy
unsigned char pg_ascii_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:146
#define isprint_l
Definition: win32.h:340
union pg_locale_struct::@120 info
#define PG_ISGRAPH
int errcode(int sqlerrcode)
Definition: elog.c:575
unsigned char pg_ascii_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:135
static pg_wchar pg_wc_toupper(pg_wchar c)
#define isdigit_l
Definition: win32.h:328
unsigned int Oid
Definition: postgres_ext.h:31
int chrspace
Definition: regguts.h:259
#define OidIsValid(objectId)
Definition: c.h:538
#define iswupper_l
Definition: win32.h:335
#define malloc(a)
Definition: header.h:50
#define iswalpha_l
Definition: win32.h:331
#define isupper_l
Definition: win32.h:334
#define iswpunct_l
Definition: win32.h:343
pg_wchar chr
Definition: regcustom.h:68
chr * ranges
Definition: regguts.h:263
#define PG_ISSPACE
#define ERROR
Definition: elog.h:43
static int pg_wc_isalpha(pg_wchar c)
int nchrs
Definition: regguts.h:258
int cclasscode
Definition: regguts.h:264
#define PG_ISUPPER
static int pg_wc_isspace(pg_wchar c)
#define PG_ISALNUM
PG_Locale_Strategy
#define iswdigit_l
Definition: win32.h:329
char * c
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:75
#define assert(TEST)
Definition: imath.c:37
struct pg_ctype_cache * next
#define isalnum_l
Definition: win32.h:332
static struct cvec * pg_ctype_get_cache(pg_wc_probefunc probefunc, int cclasscode)
#define ereport(elevel, rest)
Definition: elog.h:122
unsigned int pg_wchar
Definition: mbprint.c:31
static int pg_wc_islower(pg_wchar c)
#define iswgraph_l
Definition: win32.h:339
Definition: regguts.h:256
static pg_wchar pg_wc_tolower(pg_wchar c)
static int pg_wc_isalnum(pg_wchar c)
#define iswlower_l
Definition: win32.h:337
static pg_ctype_cache * pg_ctype_cache_list
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1275
#define PG_ISPUNCT
#define iswspace_l
Definition: win32.h:345
#define islower_l
Definition: win32.h:336
int GetDatabaseEncoding(void)
Definition: mbutils.c:1015
static int pg_wc_isdigit(pg_wchar c)
#define free(a)
Definition: header.h:65
chr * chrs
Definition: regguts.h:260
#define NULL
Definition: c.h:229
void pg_set_regex_collation(Oid collation)
#define PG_ISLOWER
int rangespace
Definition: regguts.h:262
static bool store_match(pg_ctype_cache *pcc, pg_wchar chr1, int nchrs)
#define C_COLLATION_OID
Definition: pg_collation.h:78
#define realloc(a, b)
Definition: header.h:60
#define tolower_l
Definition: win32.h:324
#define isalpha_l
Definition: win32.h:330
#define towlower_l
Definition: win32.h:326
static int pg_wc_isprint(pg_wchar c)
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define isgraph_l
Definition: win32.h:338
#define PG_ISDIGIT
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1178
struct pg_ctype_cache pg_ctype_cache
static int pg_wc_isupper(pg_wchar c)
static int pg_wc_isgraph(pg_wchar c)
int nranges
Definition: regguts.h:261
#define towupper_l
Definition: win32.h:327
#define MAX_SIMPLE_CHR
Definition: regcustom.h:97