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 */
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 re-entrancy of regexp operations, it's okay
228  * to store the results in static variables.
229  */
230 void
232 {
233  if (lc_ctype_is_c(collation))
234  {
235  /* C/POSIX collations use this path regardless of database encoding */
237  pg_regex_locale = 0;
239  }
240  else
241  {
242  if (collation == DEFAULT_COLLATION_OID)
243  pg_regex_locale = 0;
244  else if (OidIsValid(collation))
245  {
246  /*
247  * NB: pg_newlocale_from_collation will fail if not HAVE_LOCALE_T;
248  * the case of pg_regex_locale != 0 but not HAVE_LOCALE_T does not
249  * have to be considered below.
250  */
252  }
253  else
254  {
255  /*
256  * This typically means that the parser could not resolve a
257  * conflict of implicit collations, so report it that way.
258  */
259  ereport(ERROR,
260  (errcode(ERRCODE_INDETERMINATE_COLLATION),
261  errmsg("could not determine which collation to use for regular expression"),
262  errhint("Use the COLLATE clause to set the collation explicitly.")));
263  }
264 
265 #ifdef USE_WIDE_UPPER_LOWER
266  if (GetDatabaseEncoding() == PG_UTF8)
267  {
268  if (pg_regex_locale)
270  else
272  }
273  else
274 #endif /* USE_WIDE_UPPER_LOWER */
275  {
276  if (pg_regex_locale)
278  else
280  }
281 
282  pg_regex_collation = collation;
283  }
284 }
285 
286 static int
288 {
289  switch (pg_regex_strategy)
290  {
291  case PG_REGEX_LOCALE_C:
292  return (c <= (pg_wchar) 127 &&
295 #ifdef USE_WIDE_UPPER_LOWER
296  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
297  return iswdigit((wint_t) c);
298 #endif
299  /* FALL THRU */
301  return (c <= (pg_wchar) UCHAR_MAX &&
302  isdigit((unsigned char) c));
304 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
305  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
306  return iswdigit_l((wint_t) c, pg_regex_locale);
307 #endif
308  /* FALL THRU */
310 #ifdef HAVE_LOCALE_T
311  return (c <= (pg_wchar) UCHAR_MAX &&
312  isdigit_l((unsigned char) c, pg_regex_locale));
313 #endif
314  break;
315  }
316  return 0; /* can't get here, but keep compiler quiet */
317 }
318 
319 static int
321 {
322  switch (pg_regex_strategy)
323  {
324  case PG_REGEX_LOCALE_C:
325  return (c <= (pg_wchar) 127 &&
328 #ifdef USE_WIDE_UPPER_LOWER
329  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
330  return iswalpha((wint_t) c);
331 #endif
332  /* FALL THRU */
334  return (c <= (pg_wchar) UCHAR_MAX &&
335  isalpha((unsigned char) c));
337 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
338  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
339  return iswalpha_l((wint_t) c, pg_regex_locale);
340 #endif
341  /* FALL THRU */
343 #ifdef HAVE_LOCALE_T
344  return (c <= (pg_wchar) UCHAR_MAX &&
345  isalpha_l((unsigned char) c, pg_regex_locale));
346 #endif
347  break;
348  }
349  return 0; /* can't get here, but keep compiler quiet */
350 }
351 
352 static int
354 {
355  switch (pg_regex_strategy)
356  {
357  case PG_REGEX_LOCALE_C:
358  return (c <= (pg_wchar) 127 &&
361 #ifdef USE_WIDE_UPPER_LOWER
362  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
363  return iswalnum((wint_t) c);
364 #endif
365  /* FALL THRU */
367  return (c <= (pg_wchar) UCHAR_MAX &&
368  isalnum((unsigned char) c));
370 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
371  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
372  return iswalnum_l((wint_t) c, pg_regex_locale);
373 #endif
374  /* FALL THRU */
376 #ifdef HAVE_LOCALE_T
377  return (c <= (pg_wchar) UCHAR_MAX &&
378  isalnum_l((unsigned char) c, pg_regex_locale));
379 #endif
380  break;
381  }
382  return 0; /* can't get here, but keep compiler quiet */
383 }
384 
385 static int
387 {
388  switch (pg_regex_strategy)
389  {
390  case PG_REGEX_LOCALE_C:
391  return (c <= (pg_wchar) 127 &&
394 #ifdef USE_WIDE_UPPER_LOWER
395  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
396  return iswupper((wint_t) c);
397 #endif
398  /* FALL THRU */
400  return (c <= (pg_wchar) UCHAR_MAX &&
401  isupper((unsigned char) c));
403 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
404  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
405  return iswupper_l((wint_t) c, pg_regex_locale);
406 #endif
407  /* FALL THRU */
409 #ifdef HAVE_LOCALE_T
410  return (c <= (pg_wchar) UCHAR_MAX &&
411  isupper_l((unsigned char) c, pg_regex_locale));
412 #endif
413  break;
414  }
415  return 0; /* can't get here, but keep compiler quiet */
416 }
417 
418 static int
420 {
421  switch (pg_regex_strategy)
422  {
423  case PG_REGEX_LOCALE_C:
424  return (c <= (pg_wchar) 127 &&
427 #ifdef USE_WIDE_UPPER_LOWER
428  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
429  return iswlower((wint_t) c);
430 #endif
431  /* FALL THRU */
433  return (c <= (pg_wchar) UCHAR_MAX &&
434  islower((unsigned char) c));
436 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
437  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
438  return iswlower_l((wint_t) c, pg_regex_locale);
439 #endif
440  /* FALL THRU */
442 #ifdef HAVE_LOCALE_T
443  return (c <= (pg_wchar) UCHAR_MAX &&
444  islower_l((unsigned char) c, pg_regex_locale));
445 #endif
446  break;
447  }
448  return 0; /* can't get here, but keep compiler quiet */
449 }
450 
451 static int
453 {
454  switch (pg_regex_strategy)
455  {
456  case PG_REGEX_LOCALE_C:
457  return (c <= (pg_wchar) 127 &&
460 #ifdef USE_WIDE_UPPER_LOWER
461  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
462  return iswgraph((wint_t) c);
463 #endif
464  /* FALL THRU */
466  return (c <= (pg_wchar) UCHAR_MAX &&
467  isgraph((unsigned char) c));
469 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
470  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
471  return iswgraph_l((wint_t) c, pg_regex_locale);
472 #endif
473  /* FALL THRU */
475 #ifdef HAVE_LOCALE_T
476  return (c <= (pg_wchar) UCHAR_MAX &&
477  isgraph_l((unsigned char) c, pg_regex_locale));
478 #endif
479  break;
480  }
481  return 0; /* can't get here, but keep compiler quiet */
482 }
483 
484 static int
486 {
487  switch (pg_regex_strategy)
488  {
489  case PG_REGEX_LOCALE_C:
490  return (c <= (pg_wchar) 127 &&
493 #ifdef USE_WIDE_UPPER_LOWER
494  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
495  return iswprint((wint_t) c);
496 #endif
497  /* FALL THRU */
499  return (c <= (pg_wchar) UCHAR_MAX &&
500  isprint((unsigned char) c));
502 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
503  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
504  return iswprint_l((wint_t) c, pg_regex_locale);
505 #endif
506  /* FALL THRU */
508 #ifdef HAVE_LOCALE_T
509  return (c <= (pg_wchar) UCHAR_MAX &&
510  isprint_l((unsigned char) c, pg_regex_locale));
511 #endif
512  break;
513  }
514  return 0; /* can't get here, but keep compiler quiet */
515 }
516 
517 static int
519 {
520  switch (pg_regex_strategy)
521  {
522  case PG_REGEX_LOCALE_C:
523  return (c <= (pg_wchar) 127 &&
526 #ifdef USE_WIDE_UPPER_LOWER
527  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
528  return iswpunct((wint_t) c);
529 #endif
530  /* FALL THRU */
532  return (c <= (pg_wchar) UCHAR_MAX &&
533  ispunct((unsigned char) c));
535 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
536  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
537  return iswpunct_l((wint_t) c, pg_regex_locale);
538 #endif
539  /* FALL THRU */
541 #ifdef HAVE_LOCALE_T
542  return (c <= (pg_wchar) UCHAR_MAX &&
543  ispunct_l((unsigned char) c, pg_regex_locale));
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 #ifdef USE_WIDE_UPPER_LOWER
560  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
561  return iswspace((wint_t) c);
562 #endif
563  /* FALL THRU */
565  return (c <= (pg_wchar) UCHAR_MAX &&
566  isspace((unsigned char) c));
568 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
569  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
570  return iswspace_l((wint_t) c, pg_regex_locale);
571 #endif
572  /* FALL THRU */
574 #ifdef HAVE_LOCALE_T
575  return (c <= (pg_wchar) UCHAR_MAX &&
576  isspace_l((unsigned char) c, pg_regex_locale));
577 #endif
578  break;
579  }
580  return 0; /* can't get here, but keep compiler quiet */
581 }
582 
583 static pg_wchar
585 {
586  switch (pg_regex_strategy)
587  {
588  case PG_REGEX_LOCALE_C:
589  if (c <= (pg_wchar) 127)
590  return pg_ascii_toupper((unsigned char) c);
591  return c;
593  /* force C behavior for ASCII characters, per comments above */
594  if (c <= (pg_wchar) 127)
595  return pg_ascii_toupper((unsigned char) c);
596 #ifdef USE_WIDE_UPPER_LOWER
597  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
598  return towupper((wint_t) c);
599 #endif
600  /* FALL THRU */
602  /* force C behavior for ASCII characters, per comments above */
603  if (c <= (pg_wchar) 127)
604  return pg_ascii_toupper((unsigned char) c);
605  if (c <= (pg_wchar) UCHAR_MAX)
606  return toupper((unsigned char) c);
607  return c;
609 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
610  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
611  return towupper_l((wint_t) c, pg_regex_locale);
612 #endif
613  /* FALL THRU */
615 #ifdef HAVE_LOCALE_T
616  if (c <= (pg_wchar) UCHAR_MAX)
617  return toupper_l((unsigned char) c, pg_regex_locale);
618 #endif
619  return c;
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_tolower((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_tolower((unsigned char) c);
637 #ifdef USE_WIDE_UPPER_LOWER
638  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
639  return towlower((wint_t) c);
640 #endif
641  /* FALL THRU */
643  /* force C behavior for ASCII characters, per comments above */
644  if (c <= (pg_wchar) 127)
645  return pg_ascii_tolower((unsigned char) c);
646  if (c <= (pg_wchar) UCHAR_MAX)
647  return tolower((unsigned char) c);
648  return c;
650 #if defined(HAVE_LOCALE_T) && defined(USE_WIDE_UPPER_LOWER)
651  if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
652  return towlower_l((wint_t) c, pg_regex_locale);
653 #endif
654  /* FALL THRU */
656 #ifdef HAVE_LOCALE_T
657  if (c <= (pg_wchar) UCHAR_MAX)
658  return tolower_l((unsigned char) c, pg_regex_locale);
659 #endif
660  return c;
661  }
662  return 0; /* can't get here, but keep compiler quiet */
663 }
664 
665 
666 /*
667  * These functions cache the results of probing libc's ctype behavior for
668  * all character codes of interest in a given encoding/collation. The
669  * result is provided as a "struct cvec", but notice that the representation
670  * is a touch different from a cvec created by regc_cvec.c: we allocate the
671  * chrs[] and ranges[] arrays separately from the struct so that we can
672  * realloc them larger at need. This is okay since the cvecs made here
673  * should never be freed by freecvec().
674  *
675  * We use malloc not palloc since we mustn't lose control on out-of-memory;
676  * the main regex code expects us to return a failure indication instead.
677  */
678 
679 typedef int (*pg_wc_probefunc) (pg_wchar c);
680 
681 typedef struct pg_ctype_cache
682 {
683  pg_wc_probefunc probefunc; /* pg_wc_isalpha or a sibling */
684  Oid collation; /* collation this entry is for */
685  struct cvec cv; /* cache entry contents */
686  struct pg_ctype_cache *next; /* chain link */
688 
690 
691 /*
692  * Add a chr or range to pcc->cv; return false if run out of memory
693  */
694 static bool
695 store_match(pg_ctype_cache *pcc, pg_wchar chr1, int nchrs)
696 {
697  chr *newchrs;
698 
699  if (nchrs > 1)
700  {
701  if (pcc->cv.nranges >= pcc->cv.rangespace)
702  {
703  pcc->cv.rangespace *= 2;
704  newchrs = (chr *) realloc(pcc->cv.ranges,
705  pcc->cv.rangespace * sizeof(chr) * 2);
706  if (newchrs == NULL)
707  return false;
708  pcc->cv.ranges = newchrs;
709  }
710  pcc->cv.ranges[pcc->cv.nranges * 2] = chr1;
711  pcc->cv.ranges[pcc->cv.nranges * 2 + 1] = chr1 + nchrs - 1;
712  pcc->cv.nranges++;
713  }
714  else
715  {
716  assert(nchrs == 1);
717  if (pcc->cv.nchrs >= pcc->cv.chrspace)
718  {
719  pcc->cv.chrspace *= 2;
720  newchrs = (chr *) realloc(pcc->cv.chrs,
721  pcc->cv.chrspace * sizeof(chr));
722  if (newchrs == NULL)
723  return false;
724  pcc->cv.chrs = newchrs;
725  }
726  pcc->cv.chrs[pcc->cv.nchrs++] = chr1;
727  }
728  return true;
729 }
730 
731 /*
732  * Given a probe function (e.g., pg_wc_isalpha) get a struct cvec for all
733  * chrs satisfying the probe function. The active collation is the one
734  * previously set by pg_set_regex_collation. Return NULL if out of memory.
735  *
736  * Note that the result must not be freed or modified by caller.
737  */
738 static struct cvec *
740 {
741  pg_ctype_cache *pcc;
742  pg_wchar max_chr;
743  pg_wchar cur_chr;
744  int nmatches;
745  chr *newchrs;
746 
747  /*
748  * Do we already have the answer cached?
749  */
750  for (pcc = pg_ctype_cache_list; pcc != NULL; pcc = pcc->next)
751  {
752  if (pcc->probefunc == probefunc &&
754  return &pcc->cv;
755  }
756 
757  /*
758  * Nope, so initialize some workspace ...
759  */
760  pcc = (pg_ctype_cache *) malloc(sizeof(pg_ctype_cache));
761  if (pcc == NULL)
762  return NULL;
763  pcc->probefunc = probefunc;
765  pcc->cv.nchrs = 0;
766  pcc->cv.chrspace = 128;
767  pcc->cv.chrs = (chr *) malloc(pcc->cv.chrspace * sizeof(chr));
768  pcc->cv.nranges = 0;
769  pcc->cv.rangespace = 64;
770  pcc->cv.ranges = (chr *) malloc(pcc->cv.rangespace * sizeof(chr) * 2);
771  if (pcc->cv.chrs == NULL || pcc->cv.ranges == NULL)
772  goto out_of_memory;
773  pcc->cv.cclasscode = cclasscode;
774 
775  /*
776  * Decide how many character codes we ought to look through. In general
777  * we don't go past MAX_SIMPLE_CHR; chr codes above that are handled at
778  * runtime using the "high colormap" mechanism. However, in C locale
779  * there's no need to go further than 127, and if we only have a 1-byte
780  * <ctype.h> API there's no need to go further than that can handle.
781  *
782  * If it's not MAX_SIMPLE_CHR that's constraining the search, mark the
783  * output cvec as not having any locale-dependent behavior, since there
784  * will be no need to do any run-time locale checks. (The #if's here
785  * would always be true for production values of MAX_SIMPLE_CHR, but it's
786  * useful to allow it to be small for testing purposes.)
787  */
788  switch (pg_regex_strategy)
789  {
790  case PG_REGEX_LOCALE_C:
791 #if MAX_SIMPLE_CHR >= 127
792  max_chr = (pg_wchar) 127;
793  pcc->cv.cclasscode = -1;
794 #else
795  max_chr = (pg_wchar) MAX_SIMPLE_CHR;
796 #endif
797  break;
800  max_chr = (pg_wchar) MAX_SIMPLE_CHR;
801  break;
804 #if MAX_SIMPLE_CHR >= UCHAR_MAX
805  max_chr = (pg_wchar) UCHAR_MAX;
806  pcc->cv.cclasscode = -1;
807 #else
808  max_chr = (pg_wchar) MAX_SIMPLE_CHR;
809 #endif
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  if (pcc->cv.chrs)
884  free(pcc->cv.chrs);
885  if (pcc->cv.ranges)
886  free(pcc->cv.ranges);
887  free(pcc);
888 
889  return NULL;
890 }
#define ispunct_l
Definition: win32.h:352
static int pg_wc_ispunct(pg_wchar c)
#define PG_ISALPHA
#define isspace_l
Definition: win32.h:354
#define PG_ISPRINT
int errhint(const char *fmt,...)
Definition: elog.c:987
struct cvec cv
#define toupper_l
Definition: win32.h:335
#define iswalnum_l
Definition: win32.h:343
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:351
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:350
#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:338
unsigned int Oid
Definition: postgres_ext.h:31
int chrspace
Definition: regguts.h:259
#define OidIsValid(objectId)
Definition: c.h:533
#define iswupper_l
Definition: win32.h:345
int pg_locale_t
Definition: pg_locale.h:71
#define malloc(a)
Definition: header.h:45
#define iswalpha_l
Definition: win32.h:341
#define isupper_l
Definition: win32.h:344
#define iswpunct_l
Definition: win32.h:353
pg_wchar chr
Definition: regcustom.h:59
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:339
char * c
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:68
#define assert(TEST)
Definition: imath.c:37
struct pg_ctype_cache * next
#define isalnum_l
Definition: win32.h:342
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:349
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:347
static pg_ctype_cache * pg_ctype_cache_list
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1260
#define PG_ISPUNCT
#define iswspace_l
Definition: win32.h:355
#define islower_l
Definition: win32.h:346
int GetDatabaseEncoding(void)
Definition: mbutils.c:1015
static int pg_wc_isdigit(pg_wchar c)
#define free(a)
Definition: header.h:60
chr * chrs
Definition: regguts.h:260
#define NULL
Definition: c.h:226
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:71
#define realloc(a, b)
Definition: header.h:55
#define tolower_l
Definition: win32.h:334
#define isalpha_l
Definition: win32.h:340
#define towlower_l
Definition: win32.h:336
static int pg_wc_isprint(pg_wchar c)
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define isgraph_l
Definition: win32.h:348
#define PG_ISDIGIT
bool lc_ctype_is_c(Oid collation)
Definition: pg_locale.c:1173
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:337
#define MAX_SIMPLE_CHR
Definition: regcustom.h:88