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-2018, 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 */
73 } PG_Locale_Strategy;
74
75 static PG_Locale_Strategy pg_regex_strategy;
76 static pg_locale_t pg_regex_locale;
77 static Oid pg_regex_collation;
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,
158 /* A */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
159 /* B */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
160 /* C */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
161 /* D */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
162 /* E */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
163 /* F */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
164 /* G */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
165 /* H */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
166 /* I */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
167 /* J */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
168 /* K */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
169 /* L */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
170 /* M */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
171 /* N */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
172 /* O */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
173 /* P */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
174 /* Q */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
175 /* R */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
176 /* S */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
177 /* T */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
178 /* U */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
179 /* V */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
180 /* W */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
181 /* X */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
182 /* Y */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
183 /* Z */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
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,
190 /* a */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
191 /* b */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
192 /* c */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
193 /* d */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
194 /* e */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
195 /* f */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
196 /* g */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
197 /* h */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
198 /* i */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
199 /* j */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
200 /* k */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
201 /* l */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
202 /* m */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
203 /* n */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
204 /* o */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
205 /* p */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
206 /* q */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
207 /* r */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
208 /* s */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
209 /* t */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
210 /* u */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
211 /* v */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
212 /* w */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
213 /* x */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
214 /* y */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
215 /* z */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
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
pg_set_regex_collation(Oid collation)232 pg_set_regex_collation(Oid collation)
233 {
234 if (lc_ctype_is_c(collation))
235 {
236 /* C/POSIX collations use this path regardless of database encoding */
237 pg_regex_strategy = PG_REGEX_LOCALE_C;
238 pg_regex_locale = 0;
239 pg_regex_collation = C_COLLATION_OID;
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)
268 pg_regex_strategy = PG_REGEX_LOCALE_ICU;
269 else
270 #endif
271 if (GetDatabaseEncoding() == PG_UTF8)
272 {
273 if (pg_regex_locale)
274 pg_regex_strategy = PG_REGEX_LOCALE_WIDE_L;
275 else
276 pg_regex_strategy = PG_REGEX_LOCALE_WIDE;
277 }
278 else
279 {
280 if (pg_regex_locale)
281 pg_regex_strategy = PG_REGEX_LOCALE_1BYTE_L;
282 else
283 pg_regex_strategy = PG_REGEX_LOCALE_1BYTE;
284 }
285
286 pg_regex_collation = collation;
287 }
288 }
289
290 static int
pg_wc_isdigit(pg_wchar c)291 pg_wc_isdigit(pg_wchar c)
292 {
293 switch (pg_regex_strategy)
294 {
295 case PG_REGEX_LOCALE_C:
296 return (c <= (pg_wchar) 127 &&
297 (pg_char_properties[c] & PG_ISDIGIT));
298 case PG_REGEX_LOCALE_WIDE:
299 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
300 return iswdigit((wint_t) c);
301 /* FALL THRU */
302 case PG_REGEX_LOCALE_1BYTE:
303 return (c <= (pg_wchar) UCHAR_MAX &&
304 isdigit((unsigned char) c));
305 case PG_REGEX_LOCALE_WIDE_L:
306 #ifdef HAVE_LOCALE_T
307 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
308 return iswdigit_l((wint_t) c, pg_regex_locale->info.lt);
309 #endif
310 /* FALL THRU */
311 case PG_REGEX_LOCALE_1BYTE_L:
312 #ifdef HAVE_LOCALE_T
313 return (c <= (pg_wchar) UCHAR_MAX &&
314 isdigit_l((unsigned char) c, pg_regex_locale->info.lt));
315 #endif
316 break;
317 case PG_REGEX_LOCALE_ICU:
318 #ifdef USE_ICU
319 return u_isdigit(c);
320 #endif
321 break;
322 }
323 return 0; /* can't get here, but keep compiler quiet */
324 }
325
326 static int
pg_wc_isalpha(pg_wchar c)327 pg_wc_isalpha(pg_wchar c)
328 {
329 switch (pg_regex_strategy)
330 {
331 case PG_REGEX_LOCALE_C:
332 return (c <= (pg_wchar) 127 &&
333 (pg_char_properties[c] & PG_ISALPHA));
334 case PG_REGEX_LOCALE_WIDE:
335 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
336 return iswalpha((wint_t) c);
337 /* FALL THRU */
338 case PG_REGEX_LOCALE_1BYTE:
339 return (c <= (pg_wchar) UCHAR_MAX &&
340 isalpha((unsigned char) c));
341 case PG_REGEX_LOCALE_WIDE_L:
342 #ifdef HAVE_LOCALE_T
343 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
344 return iswalpha_l((wint_t) c, pg_regex_locale->info.lt);
345 #endif
346 /* FALL THRU */
347 case PG_REGEX_LOCALE_1BYTE_L:
348 #ifdef HAVE_LOCALE_T
349 return (c <= (pg_wchar) UCHAR_MAX &&
350 isalpha_l((unsigned char) c, pg_regex_locale->info.lt));
351 #endif
352 break;
353 case PG_REGEX_LOCALE_ICU:
354 #ifdef USE_ICU
355 return u_isalpha(c);
356 #endif
357 break;
358 }
359 return 0; /* can't get here, but keep compiler quiet */
360 }
361
362 static int
pg_wc_isalnum(pg_wchar c)363 pg_wc_isalnum(pg_wchar c)
364 {
365 switch (pg_regex_strategy)
366 {
367 case PG_REGEX_LOCALE_C:
368 return (c <= (pg_wchar) 127 &&
369 (pg_char_properties[c] & PG_ISALNUM));
370 case PG_REGEX_LOCALE_WIDE:
371 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
372 return iswalnum((wint_t) c);
373 /* FALL THRU */
374 case PG_REGEX_LOCALE_1BYTE:
375 return (c <= (pg_wchar) UCHAR_MAX &&
376 isalnum((unsigned char) c));
377 case PG_REGEX_LOCALE_WIDE_L:
378 #ifdef HAVE_LOCALE_T
379 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
380 return iswalnum_l((wint_t) c, pg_regex_locale->info.lt);
381 #endif
382 /* FALL THRU */
383 case PG_REGEX_LOCALE_1BYTE_L:
384 #ifdef HAVE_LOCALE_T
385 return (c <= (pg_wchar) UCHAR_MAX &&
386 isalnum_l((unsigned char) c, pg_regex_locale->info.lt));
387 #endif
388 break;
389 case PG_REGEX_LOCALE_ICU:
390 #ifdef USE_ICU
391 return u_isalnum(c);
392 #endif
393 break;
394 }
395 return 0; /* can't get here, but keep compiler quiet */
396 }
397
398 static int
pg_wc_isupper(pg_wchar c)399 pg_wc_isupper(pg_wchar c)
400 {
401 switch (pg_regex_strategy)
402 {
403 case PG_REGEX_LOCALE_C:
404 return (c <= (pg_wchar) 127 &&
405 (pg_char_properties[c] & PG_ISUPPER));
406 case PG_REGEX_LOCALE_WIDE:
407 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
408 return iswupper((wint_t) c);
409 /* FALL THRU */
410 case PG_REGEX_LOCALE_1BYTE:
411 return (c <= (pg_wchar) UCHAR_MAX &&
412 isupper((unsigned char) c));
413 case PG_REGEX_LOCALE_WIDE_L:
414 #ifdef HAVE_LOCALE_T
415 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
416 return iswupper_l((wint_t) c, pg_regex_locale->info.lt);
417 #endif
418 /* FALL THRU */
419 case PG_REGEX_LOCALE_1BYTE_L:
420 #ifdef HAVE_LOCALE_T
421 return (c <= (pg_wchar) UCHAR_MAX &&
422 isupper_l((unsigned char) c, pg_regex_locale->info.lt));
423 #endif
424 break;
425 case PG_REGEX_LOCALE_ICU:
426 #ifdef USE_ICU
427 return u_isupper(c);
428 #endif
429 break;
430 }
431 return 0; /* can't get here, but keep compiler quiet */
432 }
433
434 static int
pg_wc_islower(pg_wchar c)435 pg_wc_islower(pg_wchar c)
436 {
437 switch (pg_regex_strategy)
438 {
439 case PG_REGEX_LOCALE_C:
440 return (c <= (pg_wchar) 127 &&
441 (pg_char_properties[c] & PG_ISLOWER));
442 case PG_REGEX_LOCALE_WIDE:
443 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
444 return iswlower((wint_t) c);
445 /* FALL THRU */
446 case PG_REGEX_LOCALE_1BYTE:
447 return (c <= (pg_wchar) UCHAR_MAX &&
448 islower((unsigned char) c));
449 case PG_REGEX_LOCALE_WIDE_L:
450 #ifdef HAVE_LOCALE_T
451 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
452 return iswlower_l((wint_t) c, pg_regex_locale->info.lt);
453 #endif
454 /* FALL THRU */
455 case PG_REGEX_LOCALE_1BYTE_L:
456 #ifdef HAVE_LOCALE_T
457 return (c <= (pg_wchar) UCHAR_MAX &&
458 islower_l((unsigned char) c, pg_regex_locale->info.lt));
459 #endif
460 break;
461 case PG_REGEX_LOCALE_ICU:
462 #ifdef USE_ICU
463 return u_islower(c);
464 #endif
465 break;
466 }
467 return 0; /* can't get here, but keep compiler quiet */
468 }
469
470 static int
pg_wc_isgraph(pg_wchar c)471 pg_wc_isgraph(pg_wchar c)
472 {
473 switch (pg_regex_strategy)
474 {
475 case PG_REGEX_LOCALE_C:
476 return (c <= (pg_wchar) 127 &&
477 (pg_char_properties[c] & PG_ISGRAPH));
478 case PG_REGEX_LOCALE_WIDE:
479 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
480 return iswgraph((wint_t) c);
481 /* FALL THRU */
482 case PG_REGEX_LOCALE_1BYTE:
483 return (c <= (pg_wchar) UCHAR_MAX &&
484 isgraph((unsigned char) c));
485 case PG_REGEX_LOCALE_WIDE_L:
486 #ifdef HAVE_LOCALE_T
487 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
488 return iswgraph_l((wint_t) c, pg_regex_locale->info.lt);
489 #endif
490 /* FALL THRU */
491 case PG_REGEX_LOCALE_1BYTE_L:
492 #ifdef HAVE_LOCALE_T
493 return (c <= (pg_wchar) UCHAR_MAX &&
494 isgraph_l((unsigned char) c, pg_regex_locale->info.lt));
495 #endif
496 break;
497 case PG_REGEX_LOCALE_ICU:
498 #ifdef USE_ICU
499 return u_isgraph(c);
500 #endif
501 break;
502 }
503 return 0; /* can't get here, but keep compiler quiet */
504 }
505
506 static int
pg_wc_isprint(pg_wchar c)507 pg_wc_isprint(pg_wchar c)
508 {
509 switch (pg_regex_strategy)
510 {
511 case PG_REGEX_LOCALE_C:
512 return (c <= (pg_wchar) 127 &&
513 (pg_char_properties[c] & PG_ISPRINT));
514 case PG_REGEX_LOCALE_WIDE:
515 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
516 return iswprint((wint_t) c);
517 /* FALL THRU */
518 case PG_REGEX_LOCALE_1BYTE:
519 return (c <= (pg_wchar) UCHAR_MAX &&
520 isprint((unsigned char) c));
521 case PG_REGEX_LOCALE_WIDE_L:
522 #ifdef HAVE_LOCALE_T
523 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
524 return iswprint_l((wint_t) c, pg_regex_locale->info.lt);
525 #endif
526 /* FALL THRU */
527 case PG_REGEX_LOCALE_1BYTE_L:
528 #ifdef HAVE_LOCALE_T
529 return (c <= (pg_wchar) UCHAR_MAX &&
530 isprint_l((unsigned char) c, pg_regex_locale->info.lt));
531 #endif
532 break;
533 case PG_REGEX_LOCALE_ICU:
534 #ifdef USE_ICU
535 return u_isprint(c);
536 #endif
537 break;
538 }
539 return 0; /* can't get here, but keep compiler quiet */
540 }
541
542 static int
pg_wc_ispunct(pg_wchar c)543 pg_wc_ispunct(pg_wchar c)
544 {
545 switch (pg_regex_strategy)
546 {
547 case PG_REGEX_LOCALE_C:
548 return (c <= (pg_wchar) 127 &&
549 (pg_char_properties[c] & PG_ISPUNCT));
550 case PG_REGEX_LOCALE_WIDE:
551 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
552 return iswpunct((wint_t) c);
553 /* FALL THRU */
554 case PG_REGEX_LOCALE_1BYTE:
555 return (c <= (pg_wchar) UCHAR_MAX &&
556 ispunct((unsigned char) c));
557 case PG_REGEX_LOCALE_WIDE_L:
558 #ifdef HAVE_LOCALE_T
559 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
560 return iswpunct_l((wint_t) c, pg_regex_locale->info.lt);
561 #endif
562 /* FALL THRU */
563 case PG_REGEX_LOCALE_1BYTE_L:
564 #ifdef HAVE_LOCALE_T
565 return (c <= (pg_wchar) UCHAR_MAX &&
566 ispunct_l((unsigned char) c, pg_regex_locale->info.lt));
567 #endif
568 break;
569 case PG_REGEX_LOCALE_ICU:
570 #ifdef USE_ICU
571 return u_ispunct(c);
572 #endif
573 break;
574 }
575 return 0; /* can't get here, but keep compiler quiet */
576 }
577
578 static int
pg_wc_isspace(pg_wchar c)579 pg_wc_isspace(pg_wchar c)
580 {
581 switch (pg_regex_strategy)
582 {
583 case PG_REGEX_LOCALE_C:
584 return (c <= (pg_wchar) 127 &&
585 (pg_char_properties[c] & PG_ISSPACE));
586 case PG_REGEX_LOCALE_WIDE:
587 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
588 return iswspace((wint_t) c);
589 /* FALL THRU */
590 case PG_REGEX_LOCALE_1BYTE:
591 return (c <= (pg_wchar) UCHAR_MAX &&
592 isspace((unsigned char) c));
593 case PG_REGEX_LOCALE_WIDE_L:
594 #ifdef HAVE_LOCALE_T
595 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
596 return iswspace_l((wint_t) c, pg_regex_locale->info.lt);
597 #endif
598 /* FALL THRU */
599 case PG_REGEX_LOCALE_1BYTE_L:
600 #ifdef HAVE_LOCALE_T
601 return (c <= (pg_wchar) UCHAR_MAX &&
602 isspace_l((unsigned char) c, pg_regex_locale->info.lt));
603 #endif
604 break;
605 case PG_REGEX_LOCALE_ICU:
606 #ifdef USE_ICU
607 return u_isspace(c);
608 #endif
609 break;
610 }
611 return 0; /* can't get here, but keep compiler quiet */
612 }
613
614 static pg_wchar
pg_wc_toupper(pg_wchar c)615 pg_wc_toupper(pg_wchar c)
616 {
617 switch (pg_regex_strategy)
618 {
619 case PG_REGEX_LOCALE_C:
620 if (c <= (pg_wchar) 127)
621 return pg_ascii_toupper((unsigned char) c);
622 return c;
623 case PG_REGEX_LOCALE_WIDE:
624 /* force C behavior for ASCII characters, per comments above */
625 if (c <= (pg_wchar) 127)
626 return pg_ascii_toupper((unsigned char) c);
627 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
628 return towupper((wint_t) c);
629 /* FALL THRU */
630 case PG_REGEX_LOCALE_1BYTE:
631 /* force C behavior for ASCII characters, per comments above */
632 if (c <= (pg_wchar) 127)
633 return pg_ascii_toupper((unsigned char) c);
634 if (c <= (pg_wchar) UCHAR_MAX)
635 return toupper((unsigned char) c);
636 return c;
637 case PG_REGEX_LOCALE_WIDE_L:
638 #ifdef HAVE_LOCALE_T
639 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
640 return towupper_l((wint_t) c, pg_regex_locale->info.lt);
641 #endif
642 /* FALL THRU */
643 case PG_REGEX_LOCALE_1BYTE_L:
644 #ifdef HAVE_LOCALE_T
645 if (c <= (pg_wchar) UCHAR_MAX)
646 return toupper_l((unsigned char) c, pg_regex_locale->info.lt);
647 #endif
648 return c;
649 case PG_REGEX_LOCALE_ICU:
650 #ifdef USE_ICU
651 return u_toupper(c);
652 #endif
653 break;
654 }
655 return 0; /* can't get here, but keep compiler quiet */
656 }
657
658 static pg_wchar
pg_wc_tolower(pg_wchar c)659 pg_wc_tolower(pg_wchar c)
660 {
661 switch (pg_regex_strategy)
662 {
663 case PG_REGEX_LOCALE_C:
664 if (c <= (pg_wchar) 127)
665 return pg_ascii_tolower((unsigned char) c);
666 return c;
667 case PG_REGEX_LOCALE_WIDE:
668 /* force C behavior for ASCII characters, per comments above */
669 if (c <= (pg_wchar) 127)
670 return pg_ascii_tolower((unsigned char) c);
671 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
672 return towlower((wint_t) c);
673 /* FALL THRU */
674 case PG_REGEX_LOCALE_1BYTE:
675 /* force C behavior for ASCII characters, per comments above */
676 if (c <= (pg_wchar) 127)
677 return pg_ascii_tolower((unsigned char) c);
678 if (c <= (pg_wchar) UCHAR_MAX)
679 return tolower((unsigned char) c);
680 return c;
681 case PG_REGEX_LOCALE_WIDE_L:
682 #ifdef HAVE_LOCALE_T
683 if (sizeof(wchar_t) >= 4 || c <= (pg_wchar) 0xFFFF)
684 return towlower_l((wint_t) c, pg_regex_locale->info.lt);
685 #endif
686 /* FALL THRU */
687 case PG_REGEX_LOCALE_1BYTE_L:
688 #ifdef HAVE_LOCALE_T
689 if (c <= (pg_wchar) UCHAR_MAX)
690 return tolower_l((unsigned char) c, pg_regex_locale->info.lt);
691 #endif
692 return c;
693 case PG_REGEX_LOCALE_ICU:
694 #ifdef USE_ICU
695 return u_tolower(c);
696 #endif
697 break;
698 }
699 return 0; /* can't get here, but keep compiler quiet */
700 }
701
702
703 /*
704 * These functions cache the results of probing libc's ctype behavior for
705 * all character codes of interest in a given encoding/collation. The
706 * result is provided as a "struct cvec", but notice that the representation
707 * is a touch different from a cvec created by regc_cvec.c: we allocate the
708 * chrs[] and ranges[] arrays separately from the struct so that we can
709 * realloc them larger at need. This is okay since the cvecs made here
710 * should never be freed by freecvec().
711 *
712 * We use malloc not palloc since we mustn't lose control on out-of-memory;
713 * the main regex code expects us to return a failure indication instead.
714 */
715
716 typedef int (*pg_wc_probefunc) (pg_wchar c);
717
718 typedef struct pg_ctype_cache
719 {
720 pg_wc_probefunc probefunc; /* pg_wc_isalpha or a sibling */
721 Oid collation; /* collation this entry is for */
722 struct cvec cv; /* cache entry contents */
723 struct pg_ctype_cache *next; /* chain link */
724 } pg_ctype_cache;
725
726 static pg_ctype_cache *pg_ctype_cache_list = NULL;
727
728 /*
729 * Add a chr or range to pcc->cv; return false if run out of memory
730 */
731 static bool
store_match(pg_ctype_cache * pcc,pg_wchar chr1,int nchrs)732 store_match(pg_ctype_cache *pcc, pg_wchar chr1, int nchrs)
733 {
734 chr *newchrs;
735
736 if (nchrs > 1)
737 {
738 if (pcc->cv.nranges >= pcc->cv.rangespace)
739 {
740 pcc->cv.rangespace *= 2;
741 newchrs = (chr *) realloc(pcc->cv.ranges,
742 pcc->cv.rangespace * sizeof(chr) * 2);
743 if (newchrs == NULL)
744 return false;
745 pcc->cv.ranges = newchrs;
746 }
747 pcc->cv.ranges[pcc->cv.nranges * 2] = chr1;
748 pcc->cv.ranges[pcc->cv.nranges * 2 + 1] = chr1 + nchrs - 1;
749 pcc->cv.nranges++;
750 }
751 else
752 {
753 assert(nchrs == 1);
754 if (pcc->cv.nchrs >= pcc->cv.chrspace)
755 {
756 pcc->cv.chrspace *= 2;
757 newchrs = (chr *) realloc(pcc->cv.chrs,
758 pcc->cv.chrspace * sizeof(chr));
759 if (newchrs == NULL)
760 return false;
761 pcc->cv.chrs = newchrs;
762 }
763 pcc->cv.chrs[pcc->cv.nchrs++] = chr1;
764 }
765 return true;
766 }
767
768 /*
769 * Given a probe function (e.g., pg_wc_isalpha) get a struct cvec for all
770 * chrs satisfying the probe function. The active collation is the one
771 * previously set by pg_set_regex_collation. Return NULL if out of memory.
772 *
773 * Note that the result must not be freed or modified by caller.
774 */
775 static struct cvec *
pg_ctype_get_cache(pg_wc_probefunc probefunc,int cclasscode)776 pg_ctype_get_cache(pg_wc_probefunc probefunc, int cclasscode)
777 {
778 pg_ctype_cache *pcc;
779 pg_wchar max_chr;
780 pg_wchar cur_chr;
781 int nmatches;
782 chr *newchrs;
783
784 /*
785 * Do we already have the answer cached?
786 */
787 for (pcc = pg_ctype_cache_list; pcc != NULL; pcc = pcc->next)
788 {
789 if (pcc->probefunc == probefunc &&
790 pcc->collation == pg_regex_collation)
791 return &pcc->cv;
792 }
793
794 /*
795 * Nope, so initialize some workspace ...
796 */
797 pcc = (pg_ctype_cache *) malloc(sizeof(pg_ctype_cache));
798 if (pcc == NULL)
799 return NULL;
800 pcc->probefunc = probefunc;
801 pcc->collation = pg_regex_collation;
802 pcc->cv.nchrs = 0;
803 pcc->cv.chrspace = 128;
804 pcc->cv.chrs = (chr *) malloc(pcc->cv.chrspace * sizeof(chr));
805 pcc->cv.nranges = 0;
806 pcc->cv.rangespace = 64;
807 pcc->cv.ranges = (chr *) malloc(pcc->cv.rangespace * sizeof(chr) * 2);
808 if (pcc->cv.chrs == NULL || pcc->cv.ranges == NULL)
809 goto out_of_memory;
810 pcc->cv.cclasscode = cclasscode;
811
812 /*
813 * Decide how many character codes we ought to look through. In general
814 * we don't go past MAX_SIMPLE_CHR; chr codes above that are handled at
815 * runtime using the "high colormap" mechanism. However, in C locale
816 * there's no need to go further than 127, and if we only have a 1-byte
817 * <ctype.h> API there's no need to go further than that can handle.
818 *
819 * If it's not MAX_SIMPLE_CHR that's constraining the search, mark the
820 * output cvec as not having any locale-dependent behavior, since there
821 * will be no need to do any run-time locale checks. (The #if's here
822 * would always be true for production values of MAX_SIMPLE_CHR, but it's
823 * useful to allow it to be small for testing purposes.)
824 */
825 switch (pg_regex_strategy)
826 {
827 case PG_REGEX_LOCALE_C:
828 #if MAX_SIMPLE_CHR >= 127
829 max_chr = (pg_wchar) 127;
830 pcc->cv.cclasscode = -1;
831 #else
832 max_chr = (pg_wchar) MAX_SIMPLE_CHR;
833 #endif
834 break;
835 case PG_REGEX_LOCALE_WIDE:
836 case PG_REGEX_LOCALE_WIDE_L:
837 max_chr = (pg_wchar) MAX_SIMPLE_CHR;
838 break;
839 case PG_REGEX_LOCALE_1BYTE:
840 case PG_REGEX_LOCALE_1BYTE_L:
841 #if MAX_SIMPLE_CHR >= UCHAR_MAX
842 max_chr = (pg_wchar) UCHAR_MAX;
843 pcc->cv.cclasscode = -1;
844 #else
845 max_chr = (pg_wchar) MAX_SIMPLE_CHR;
846 #endif
847 break;
848 case PG_REGEX_LOCALE_ICU:
849 max_chr = (pg_wchar) MAX_SIMPLE_CHR;
850 break;
851 default:
852 max_chr = 0; /* can't get here, but keep compiler quiet */
853 break;
854 }
855
856 /*
857 * And scan 'em ...
858 */
859 nmatches = 0; /* number of consecutive matches */
860
861 for (cur_chr = 0; cur_chr <= max_chr; cur_chr++)
862 {
863 if ((*probefunc) (cur_chr))
864 nmatches++;
865 else if (nmatches > 0)
866 {
867 if (!store_match(pcc, cur_chr - nmatches, nmatches))
868 goto out_of_memory;
869 nmatches = 0;
870 }
871 }
872
873 if (nmatches > 0)
874 if (!store_match(pcc, cur_chr - nmatches, nmatches))
875 goto out_of_memory;
876
877 /*
878 * We might have allocated more memory than needed, if so free it
879 */
880 if (pcc->cv.nchrs == 0)
881 {
882 free(pcc->cv.chrs);
883 pcc->cv.chrs = NULL;
884 pcc->cv.chrspace = 0;
885 }
886 else if (pcc->cv.nchrs < pcc->cv.chrspace)
887 {
888 newchrs = (chr *) realloc(pcc->cv.chrs,
889 pcc->cv.nchrs * sizeof(chr));
890 if (newchrs == NULL)
891 goto out_of_memory;
892 pcc->cv.chrs = newchrs;
893 pcc->cv.chrspace = pcc->cv.nchrs;
894 }
895 if (pcc->cv.nranges == 0)
896 {
897 free(pcc->cv.ranges);
898 pcc->cv.ranges = NULL;
899 pcc->cv.rangespace = 0;
900 }
901 else if (pcc->cv.nranges < pcc->cv.rangespace)
902 {
903 newchrs = (chr *) realloc(pcc->cv.ranges,
904 pcc->cv.nranges * sizeof(chr) * 2);
905 if (newchrs == NULL)
906 goto out_of_memory;
907 pcc->cv.ranges = newchrs;
908 pcc->cv.rangespace = pcc->cv.nranges;
909 }
910
911 /*
912 * Success, link it into cache chain
913 */
914 pcc->next = pg_ctype_cache_list;
915 pg_ctype_cache_list = pcc;
916
917 return &pcc->cv;
918
919 /*
920 * Failure, clean up
921 */
922 out_of_memory:
923 if (pcc->cv.chrs)
924 free(pcc->cv.chrs);
925 if (pcc->cv.ranges)
926 free(pcc->cv.ranges);
927 free(pcc);
928
929 return NULL;
930 }
931