Thanks to visit codestin.com
Credit goes to doxygen.postgresql.org

PostgreSQL Source Code git master
regc_pg_locale.c File Reference
Include dependency graph for regc_pg_locale.c:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  pg_ctype_cache
 

Macros

#define PG_ISDIGIT   0x01
 
#define PG_ISALPHA   0x02
 
#define PG_ISALNUM   (PG_ISDIGIT | PG_ISALPHA)
 
#define PG_ISUPPER   0x04
 
#define PG_ISLOWER   0x08
 
#define PG_ISGRAPH   0x10
 
#define PG_ISPRINT   0x20
 
#define PG_ISPUNCT   0x40
 
#define PG_ISSPACE   0x80
 

Typedefs

typedef int(* pg_wc_probefunc) (pg_wchar c)
 
typedef struct pg_ctype_cache pg_ctype_cache
 

Functions

void pg_set_regex_collation (Oid collation)
 
static int pg_wc_isdigit (pg_wchar c)
 
static int pg_wc_isalpha (pg_wchar c)
 
static int pg_wc_isalnum (pg_wchar c)
 
static int pg_wc_isword (pg_wchar c)
 
static int pg_wc_isupper (pg_wchar c)
 
static int pg_wc_islower (pg_wchar c)
 
static int pg_wc_isgraph (pg_wchar c)
 
static int pg_wc_isprint (pg_wchar c)
 
static int pg_wc_ispunct (pg_wchar c)
 
static int pg_wc_isspace (pg_wchar c)
 
static pg_wchar pg_wc_toupper (pg_wchar c)
 
static pg_wchar pg_wc_tolower (pg_wchar c)
 
static bool store_match (pg_ctype_cache *pcc, pg_wchar chr1, int nchrs)
 
static struct cvecpg_ctype_get_cache (pg_wc_probefunc probefunc, int cclasscode)
 

Variables

static pg_locale_t pg_regex_locale
 
static struct pg_locale_struct dummy_c_locale
 
static const unsigned char pg_char_properties [128]
 
static pg_ctype_cachepg_ctype_cache_list = NULL
 

Macro Definition Documentation

◆ PG_ISALNUM

#define PG_ISALNUM   (PG_ISDIGIT | PG_ISALPHA)

Definition at line 35 of file regc_pg_locale.c.

◆ PG_ISALPHA

#define PG_ISALPHA   0x02

Definition at line 34 of file regc_pg_locale.c.

◆ PG_ISDIGIT

#define PG_ISDIGIT   0x01

Definition at line 33 of file regc_pg_locale.c.

◆ PG_ISGRAPH

#define PG_ISGRAPH   0x10

Definition at line 38 of file regc_pg_locale.c.

◆ PG_ISLOWER

#define PG_ISLOWER   0x08

Definition at line 37 of file regc_pg_locale.c.

◆ PG_ISPRINT

#define PG_ISPRINT   0x20

Definition at line 39 of file regc_pg_locale.c.

◆ PG_ISPUNCT

#define PG_ISPUNCT   0x40

Definition at line 40 of file regc_pg_locale.c.

◆ PG_ISSPACE

#define PG_ISSPACE   0x80

Definition at line 41 of file regc_pg_locale.c.

◆ PG_ISUPPER

#define PG_ISUPPER   0x04

Definition at line 36 of file regc_pg_locale.c.

Typedef Documentation

◆ pg_ctype_cache

◆ pg_wc_probefunc

typedef int(* pg_wc_probefunc) (pg_wchar c)

Definition at line 369 of file regc_pg_locale.c.

Function Documentation

◆ pg_ctype_get_cache()

static struct cvec * pg_ctype_get_cache ( pg_wc_probefunc  probefunc,
int  cclasscode 
)
static

Definition at line 429 of file regc_pg_locale.c.

430{
431 pg_ctype_cache *pcc;
432 pg_wchar max_chr;
433 pg_wchar cur_chr;
434 int nmatches;
435 chr *newchrs;
436
437 /*
438 * Do we already have the answer cached?
439 */
440 for (pcc = pg_ctype_cache_list; pcc != NULL; pcc = pcc->next)
441 {
442 if (pcc->probefunc == probefunc &&
443 pcc->locale == pg_regex_locale)
444 return &pcc->cv;
445 }
446
447 /*
448 * Nope, so initialize some workspace ...
449 */
450 pcc = (pg_ctype_cache *) malloc(sizeof(pg_ctype_cache));
451 if (pcc == NULL)
452 return NULL;
453 pcc->probefunc = probefunc;
454 pcc->locale = pg_regex_locale;
455 pcc->cv.nchrs = 0;
456 pcc->cv.chrspace = 128;
457 pcc->cv.chrs = (chr *) malloc(pcc->cv.chrspace * sizeof(chr));
458 pcc->cv.nranges = 0;
459 pcc->cv.rangespace = 64;
460 pcc->cv.ranges = (chr *) malloc(pcc->cv.rangespace * sizeof(chr) * 2);
461 if (pcc->cv.chrs == NULL || pcc->cv.ranges == NULL)
462 goto out_of_memory;
463 pcc->cv.cclasscode = cclasscode;
464
465 /*
466 * Decide how many character codes we ought to look through. In general
467 * we don't go past MAX_SIMPLE_CHR; chr codes above that are handled at
468 * runtime using the "high colormap" mechanism. However, in C locale
469 * there's no need to go further than 127, and if we only have a 1-byte
470 * <ctype.h> API there's no need to go further than that can handle.
471 *
472 * If it's not MAX_SIMPLE_CHR that's constraining the search, mark the
473 * output cvec as not having any locale-dependent behavior, since there
474 * will be no need to do any run-time locale checks. (The #if's here
475 * would always be true for production values of MAX_SIMPLE_CHR, but it's
476 * useful to allow it to be small for testing purposes.)
477 */
479 {
480#if MAX_SIMPLE_CHR >= 127
481 max_chr = (pg_wchar) 127;
482 pcc->cv.cclasscode = -1;
483#else
484 max_chr = (pg_wchar) MAX_SIMPLE_CHR;
485#endif
486 }
487 else
488 {
489 if (pg_regex_locale->ctype->max_chr != 0 &&
491 {
492 max_chr = pg_regex_locale->ctype->max_chr;
493 pcc->cv.cclasscode = -1;
494 }
495 else
496 max_chr = (pg_wchar) MAX_SIMPLE_CHR;
497 }
498
499 /*
500 * And scan 'em ...
501 */
502 nmatches = 0; /* number of consecutive matches */
503
504 for (cur_chr = 0; cur_chr <= max_chr; cur_chr++)
505 {
506 if ((*probefunc) (cur_chr))
507 nmatches++;
508 else if (nmatches > 0)
509 {
510 if (!store_match(pcc, cur_chr - nmatches, nmatches))
511 goto out_of_memory;
512 nmatches = 0;
513 }
514 }
515
516 if (nmatches > 0)
517 if (!store_match(pcc, cur_chr - nmatches, nmatches))
518 goto out_of_memory;
519
520 /*
521 * We might have allocated more memory than needed, if so free it
522 */
523 if (pcc->cv.nchrs == 0)
524 {
525 free(pcc->cv.chrs);
526 pcc->cv.chrs = NULL;
527 pcc->cv.chrspace = 0;
528 }
529 else if (pcc->cv.nchrs < pcc->cv.chrspace)
530 {
531 newchrs = (chr *) realloc(pcc->cv.chrs,
532 pcc->cv.nchrs * sizeof(chr));
533 if (newchrs == NULL)
534 goto out_of_memory;
535 pcc->cv.chrs = newchrs;
536 pcc->cv.chrspace = pcc->cv.nchrs;
537 }
538 if (pcc->cv.nranges == 0)
539 {
540 free(pcc->cv.ranges);
541 pcc->cv.ranges = NULL;
542 pcc->cv.rangespace = 0;
543 }
544 else if (pcc->cv.nranges < pcc->cv.rangespace)
545 {
546 newchrs = (chr *) realloc(pcc->cv.ranges,
547 pcc->cv.nranges * sizeof(chr) * 2);
548 if (newchrs == NULL)
549 goto out_of_memory;
550 pcc->cv.ranges = newchrs;
551 pcc->cv.rangespace = pcc->cv.nranges;
552 }
553
554 /*
555 * Success, link it into cache chain
556 */
559
560 return &pcc->cv;
561
562 /*
563 * Failure, clean up
564 */
565out_of_memory:
566 free(pcc->cv.chrs);
567 free(pcc->cv.ranges);
568 free(pcc);
569
570 return NULL;
571}
#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
static pg_ctype_cache * pg_ctype_cache_list
static bool store_match(pg_ctype_cache *pcc, pg_wchar chr1, int nchrs)
static pg_locale_t pg_regex_locale
#define MAX_SIMPLE_CHR
Definition: regcustom.h:87
pg_wchar chr
Definition: regcustom.h:59
pg_wchar max_chr
Definition: pg_locale.h:130
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
pg_locale_t locale
struct cvec cv
const struct ctype_methods * ctype
Definition: pg_locale.h:157

References cvec::cclasscode, cvec::chrs, cvec::chrspace, pg_locale_struct::ctype, pg_locale_struct::ctype_is_c, pg_ctype_cache::cv, free, pg_ctype_cache::locale, malloc, ctype_methods::max_chr, MAX_SIMPLE_CHR, cvec::nchrs, pg_ctype_cache::next, cvec::nranges, pg_ctype_cache_list, pg_regex_locale, pg_ctype_cache::probefunc, cvec::ranges, cvec::rangespace, realloc, and store_match().

Referenced by cclasscvec().

◆ pg_set_regex_collation()

void pg_set_regex_collation ( Oid  collation)

Definition at line 183 of file regc_pg_locale.c.

184{
186
187 if (!OidIsValid(collation))
188 {
189 /*
190 * This typically means that the parser could not resolve a conflict
191 * of implicit collations, so report it that way.
192 */
194 (errcode(ERRCODE_INDETERMINATE_COLLATION),
195 errmsg("could not determine which collation to use for regular expression"),
196 errhint("Use the COLLATE clause to set the collation explicitly.")));
197 }
198
199 if (collation == C_COLLATION_OID)
200 {
201 /*
202 * Some callers expect regexes to work for C_COLLATION_OID before
203 * catalog access is available, so we can't call
204 * pg_newlocale_from_collation().
205 */
207 }
208 else
209 {
211
212 if (!locale->deterministic)
214 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
215 errmsg("nondeterministic collations are not supported for regular expressions")));
216
217 if (locale->ctype_is_c)
218 {
219 /*
220 * C/POSIX collations use this path regardless of database
221 * encoding
222 */
224 }
225 }
226
228}
#define OidIsValid(objectId)
Definition: c.h:775
int errhint(const char *fmt,...)
Definition: elog.c:1321
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:150
static char * locale
Definition: initdb.c:140
pg_locale_t pg_newlocale_from_collation(Oid collid)
Definition: pg_locale.c:1166
static struct pg_locale_struct dummy_c_locale

References dummy_c_locale, ereport, errcode(), errhint(), errmsg(), ERROR, locale, OidIsValid, pg_newlocale_from_collation(), and pg_regex_locale.

Referenced by pg_regcomp(), pg_regexec(), and pg_regprefix().

◆ pg_wc_isalnum()

static int pg_wc_isalnum ( pg_wchar  c)
static

Definition at line 251 of file regc_pg_locale.c.

252{
254 return (c <= (pg_wchar) 127 &&
256 else
258}
char * c
#define PG_ISALNUM
static const unsigned char pg_char_properties[128]
bool(* wc_isalnum)(pg_wchar wc, pg_locale_t locale)
Definition: pg_locale.h:106

References pg_locale_struct::ctype, pg_locale_struct::ctype_is_c, pg_char_properties, PG_ISALNUM, pg_regex_locale, and ctype_methods::wc_isalnum.

Referenced by cclass_column_index(), cclasscvec(), and pg_wc_isword().

◆ pg_wc_isalpha()

static int pg_wc_isalpha ( pg_wchar  c)
static

Definition at line 241 of file regc_pg_locale.c.

242{
244 return (c <= (pg_wchar) 127 &&
246 else
248}
#define PG_ISALPHA
bool(* wc_isalpha)(pg_wchar wc, pg_locale_t locale)
Definition: pg_locale.h:105

References pg_locale_struct::ctype, pg_locale_struct::ctype_is_c, pg_char_properties, PG_ISALPHA, pg_regex_locale, and ctype_methods::wc_isalpha.

Referenced by cclass_column_index(), and cclasscvec().

◆ pg_wc_isdigit()

static int pg_wc_isdigit ( pg_wchar  c)
static

Definition at line 231 of file regc_pg_locale.c.

232{
234 return (c <= (pg_wchar) 127 &&
236 else
238}
#define PG_ISDIGIT
bool(* wc_isdigit)(pg_wchar wc, pg_locale_t locale)
Definition: pg_locale.h:104

References pg_locale_struct::ctype, pg_locale_struct::ctype_is_c, pg_char_properties, PG_ISDIGIT, pg_regex_locale, and ctype_methods::wc_isdigit.

Referenced by cclass_column_index(), and cclasscvec().

◆ pg_wc_isgraph()

static int pg_wc_isgraph ( pg_wchar  c)
static

Definition at line 290 of file regc_pg_locale.c.

291{
293 return (c <= (pg_wchar) 127 &&
295 else
297}
#define PG_ISGRAPH
bool(* wc_isgraph)(pg_wchar wc, pg_locale_t locale)
Definition: pg_locale.h:109

References pg_locale_struct::ctype, pg_locale_struct::ctype_is_c, pg_char_properties, PG_ISGRAPH, pg_regex_locale, and ctype_methods::wc_isgraph.

Referenced by cclass_column_index(), and cclasscvec().

◆ pg_wc_islower()

static int pg_wc_islower ( pg_wchar  c)
static

Definition at line 280 of file regc_pg_locale.c.

281{
283 return (c <= (pg_wchar) 127 &&
285 else
287}
#define PG_ISLOWER
bool(* wc_islower)(pg_wchar wc, pg_locale_t locale)
Definition: pg_locale.h:108

References pg_locale_struct::ctype, pg_locale_struct::ctype_is_c, pg_char_properties, PG_ISLOWER, pg_regex_locale, and ctype_methods::wc_islower.

Referenced by cclass_column_index(), and cclasscvec().

◆ pg_wc_isprint()

static int pg_wc_isprint ( pg_wchar  c)
static

Definition at line 300 of file regc_pg_locale.c.

301{
303 return (c <= (pg_wchar) 127 &&
305 else
307}
#define PG_ISPRINT
bool(* wc_isprint)(pg_wchar wc, pg_locale_t locale)
Definition: pg_locale.h:110

References pg_locale_struct::ctype, pg_locale_struct::ctype_is_c, pg_char_properties, PG_ISPRINT, pg_regex_locale, and ctype_methods::wc_isprint.

Referenced by cclass_column_index(), and cclasscvec().

◆ pg_wc_ispunct()

static int pg_wc_ispunct ( pg_wchar  c)
static

Definition at line 310 of file regc_pg_locale.c.

311{
313 return (c <= (pg_wchar) 127 &&
315 else
317}
#define PG_ISPUNCT
bool(* wc_ispunct)(pg_wchar wc, pg_locale_t locale)
Definition: pg_locale.h:111

References pg_locale_struct::ctype, pg_locale_struct::ctype_is_c, pg_char_properties, PG_ISPUNCT, pg_regex_locale, and ctype_methods::wc_ispunct.

Referenced by cclass_column_index(), and cclasscvec().

◆ pg_wc_isspace()

static int pg_wc_isspace ( pg_wchar  c)
static

Definition at line 320 of file regc_pg_locale.c.

321{
323 return (c <= (pg_wchar) 127 &&
325 else
327}
#define PG_ISSPACE
bool(* wc_isspace)(pg_wchar wc, pg_locale_t locale)
Definition: pg_locale.h:112

References pg_locale_struct::ctype, pg_locale_struct::ctype_is_c, pg_char_properties, PG_ISSPACE, pg_regex_locale, and ctype_methods::wc_isspace.

Referenced by cclass_column_index(), and cclasscvec().

◆ pg_wc_isupper()

static int pg_wc_isupper ( pg_wchar  c)
static

Definition at line 270 of file regc_pg_locale.c.

271{
273 return (c <= (pg_wchar) 127 &&
275 else
277}
#define PG_ISUPPER
bool(* wc_isupper)(pg_wchar wc, pg_locale_t locale)
Definition: pg_locale.h:107

References pg_locale_struct::ctype, pg_locale_struct::ctype_is_c, pg_char_properties, PG_ISUPPER, pg_regex_locale, and ctype_methods::wc_isupper.

Referenced by cclass_column_index(), and cclasscvec().

◆ pg_wc_isword()

static int pg_wc_isword ( pg_wchar  c)
static

Definition at line 261 of file regc_pg_locale.c.

262{
263 /* We define word characters as alnum class plus underscore */
264 if (c == CHR('_'))
265 return 1;
266 return pg_wc_isalnum(c);
267}
static int pg_wc_isalnum(pg_wchar c)
#define CHR(c)
Definition: regcustom.h:62

References CHR, and pg_wc_isalnum().

Referenced by cclass_column_index(), and cclasscvec().

◆ pg_wc_tolower()

static pg_wchar pg_wc_tolower ( pg_wchar  c)
static

Definition at line 343 of file regc_pg_locale.c.

344{
346 {
347 if (c <= (pg_wchar) 127)
348 return pg_ascii_tolower((unsigned char) c);
349 return c;
350 }
351 else
353}
unsigned char pg_ascii_tolower(unsigned char ch)
Definition: pgstrcasecmp.c:146
pg_wchar(* wc_tolower)(pg_wchar wc, pg_locale_t locale)
Definition: pg_locale.h:114

References pg_locale_struct::ctype, pg_locale_struct::ctype_is_c, pg_ascii_tolower(), pg_regex_locale, and ctype_methods::wc_tolower.

Referenced by allcases(), casecmp(), and range().

◆ pg_wc_toupper()

static pg_wchar pg_wc_toupper ( pg_wchar  c)
static

Definition at line 330 of file regc_pg_locale.c.

331{
333 {
334 if (c <= (pg_wchar) 127)
335 return pg_ascii_toupper((unsigned char) c);
336 return c;
337 }
338 else
340}
unsigned char pg_ascii_toupper(unsigned char ch)
Definition: pgstrcasecmp.c:135
pg_wchar(* wc_toupper)(pg_wchar wc, pg_locale_t locale)
Definition: pg_locale.h:113

References pg_locale_struct::ctype, pg_locale_struct::ctype_is_c, pg_ascii_toupper(), pg_regex_locale, and ctype_methods::wc_toupper.

Referenced by allcases(), and range().

◆ store_match()

static bool store_match ( pg_ctype_cache pcc,
pg_wchar  chr1,
int  nchrs 
)
static

Definition at line 385 of file regc_pg_locale.c.

386{
387 chr *newchrs;
388
389 if (nchrs > 1)
390 {
391 if (pcc->cv.nranges >= pcc->cv.rangespace)
392 {
393 pcc->cv.rangespace *= 2;
394 newchrs = (chr *) realloc(pcc->cv.ranges,
395 pcc->cv.rangespace * sizeof(chr) * 2);
396 if (newchrs == NULL)
397 return false;
398 pcc->cv.ranges = newchrs;
399 }
400 pcc->cv.ranges[pcc->cv.nranges * 2] = chr1;
401 pcc->cv.ranges[pcc->cv.nranges * 2 + 1] = chr1 + nchrs - 1;
402 pcc->cv.nranges++;
403 }
404 else
405 {
406 assert(nchrs == 1);
407 if (pcc->cv.nchrs >= pcc->cv.chrspace)
408 {
409 pcc->cv.chrspace *= 2;
410 newchrs = (chr *) realloc(pcc->cv.chrs,
411 pcc->cv.chrspace * sizeof(chr));
412 if (newchrs == NULL)
413 return false;
414 pcc->cv.chrs = newchrs;
415 }
416 pcc->cv.chrs[pcc->cv.nchrs++] = chr1;
417 }
418 return true;
419}
#define assert(x)
Definition: regcustom.h:56

References assert, cvec::chrs, cvec::chrspace, pg_ctype_cache::cv, cvec::nchrs, cvec::nranges, cvec::ranges, cvec::rangespace, and realloc.

Referenced by pg_ctype_get_cache().

Variable Documentation

◆ dummy_c_locale

struct pg_locale_struct dummy_c_locale
static
Initial value:
= {
.collate_is_c = true,
.ctype_is_c = true,
}

Definition at line 25 of file regc_pg_locale.c.

Referenced by pg_set_regex_collation().

◆ pg_char_properties

const unsigned char pg_char_properties[128]
static

◆ pg_ctype_cache_list

pg_ctype_cache* pg_ctype_cache_list = NULL
static

Definition at line 379 of file regc_pg_locale.c.

Referenced by pg_ctype_get_cache().

◆ pg_regex_locale