Coverage Report

Created: 2025-10-09 06:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/postgres/src/backend/regex/regc_pg_locale.c
Line
Count
Source
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-2025, 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 "common/unicode_case.h"
20
#include "common/unicode_category.h"
21
#include "utils/pg_locale.h"
22
23
static pg_locale_t pg_regex_locale;
24
25
static struct pg_locale_struct dummy_c_locale = {
26
  .collate_is_c = true,
27
  .ctype_is_c = true,
28
};
29
30
/*
31
 * Hard-wired character properties for C locale
32
 */
33
0
#define PG_ISDIGIT  0x01
34
0
#define PG_ISALPHA  0x02
35
0
#define PG_ISALNUM  (PG_ISDIGIT | PG_ISALPHA)
36
0
#define PG_ISUPPER  0x04
37
0
#define PG_ISLOWER  0x08
38
0
#define PG_ISGRAPH  0x10
39
0
#define PG_ISPRINT  0x20
40
0
#define PG_ISPUNCT  0x40
41
0
#define PG_ISSPACE  0x80
42
43
static const unsigned char pg_char_properties[128] = {
44
   /* NUL */ 0,
45
   /* ^A */ 0,
46
   /* ^B */ 0,
47
   /* ^C */ 0,
48
   /* ^D */ 0,
49
   /* ^E */ 0,
50
   /* ^F */ 0,
51
   /* ^G */ 0,
52
   /* ^H */ 0,
53
   /* ^I */ PG_ISSPACE,
54
   /* ^J */ PG_ISSPACE,
55
   /* ^K */ PG_ISSPACE,
56
   /* ^L */ PG_ISSPACE,
57
   /* ^M */ PG_ISSPACE,
58
   /* ^N */ 0,
59
   /* ^O */ 0,
60
   /* ^P */ 0,
61
   /* ^Q */ 0,
62
   /* ^R */ 0,
63
   /* ^S */ 0,
64
   /* ^T */ 0,
65
   /* ^U */ 0,
66
   /* ^V */ 0,
67
   /* ^W */ 0,
68
   /* ^X */ 0,
69
   /* ^Y */ 0,
70
   /* ^Z */ 0,
71
   /* ^[ */ 0,
72
   /* ^\ */ 0,
73
   /* ^] */ 0,
74
   /* ^^ */ 0,
75
   /* ^_ */ 0,
76
   /* */ PG_ISPRINT | PG_ISSPACE,
77
   /* !  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
78
   /* "  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
79
   /* #  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
80
   /* $  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
81
   /* %  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
82
   /* &  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
83
   /* '  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
84
   /* (  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
85
   /* )  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
86
   /* *  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
87
   /* +  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
88
   /* ,  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
89
   /* -  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
90
   /* .  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
91
   /* /  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
92
   /* 0  */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
93
   /* 1  */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
94
   /* 2  */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
95
   /* 3  */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
96
   /* 4  */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
97
   /* 5  */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
98
   /* 6  */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
99
   /* 7  */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
100
   /* 8  */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
101
   /* 9  */ PG_ISDIGIT | PG_ISGRAPH | PG_ISPRINT,
102
   /* :  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
103
   /* ;  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
104
   /* <  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
105
   /* =  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
106
   /* >  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
107
   /* ?  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
108
   /* @  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
109
   /* A  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
110
   /* B  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
111
   /* C  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
112
   /* D  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
113
   /* E  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
114
   /* F  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
115
   /* G  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
116
   /* H  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
117
   /* I  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
118
   /* J  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
119
   /* K  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
120
   /* L  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
121
   /* M  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
122
   /* N  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
123
   /* O  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
124
   /* P  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
125
   /* Q  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
126
   /* R  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
127
   /* S  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
128
   /* T  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
129
   /* U  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
130
   /* V  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
131
   /* W  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
132
   /* X  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
133
   /* Y  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
134
   /* Z  */ PG_ISALPHA | PG_ISUPPER | PG_ISGRAPH | PG_ISPRINT,
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
   /* a  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
142
   /* b  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
143
   /* c  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
144
   /* d  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
145
   /* e  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
146
   /* f  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
147
   /* g  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
148
   /* h  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
149
   /* i  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
150
   /* j  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
151
   /* k  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
152
   /* l  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
153
   /* m  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
154
   /* n  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
155
   /* o  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
156
   /* p  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
157
   /* q  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
158
   /* r  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
159
   /* s  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
160
   /* t  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
161
   /* u  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
162
   /* v  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
163
   /* w  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
164
   /* x  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
165
   /* y  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
166
   /* z  */ PG_ISALPHA | PG_ISLOWER | PG_ISGRAPH | PG_ISPRINT,
167
   /* {  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
168
   /* |  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
169
   /* }  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
170
   /* ~  */ PG_ISGRAPH | PG_ISPRINT | PG_ISPUNCT,
171
   /* DEL */ 0
172
};
173
174
175
/*
176
 * pg_set_regex_collation: set collation for these functions to obey
177
 *
178
 * This is called when beginning compilation or execution of a regexp.
179
 * Since there's no need for reentrancy of regexp operations, it's okay
180
 * to store the results in static variables.
181
 */
182
void
183
pg_set_regex_collation(Oid collation)
184
0
{
185
0
  pg_locale_t locale = 0;
186
187
0
  if (!OidIsValid(collation))
188
0
  {
189
    /*
190
     * This typically means that the parser could not resolve a conflict
191
     * of implicit collations, so report it that way.
192
     */
193
0
    ereport(ERROR,
194
0
        (errcode(ERRCODE_INDETERMINATE_COLLATION),
195
0
         errmsg("could not determine which collation to use for regular expression"),
196
0
         errhint("Use the COLLATE clause to set the collation explicitly.")));
197
0
  }
198
199
0
  if (collation == C_COLLATION_OID)
200
0
  {
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
     */
206
0
    locale = &dummy_c_locale;
207
0
  }
208
0
  else
209
0
  {
210
0
    locale = pg_newlocale_from_collation(collation);
211
212
0
    if (!locale->deterministic)
213
0
      ereport(ERROR,
214
0
          (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
215
0
           errmsg("nondeterministic collations are not supported for regular expressions")));
216
217
0
    if (locale->ctype_is_c)
218
0
    {
219
      /*
220
       * C/POSIX collations use this path regardless of database
221
       * encoding
222
       */
223
0
      locale = &dummy_c_locale;
224
0
    }
225
0
  }
226
227
0
  pg_regex_locale = locale;
228
0
}
229
230
static int
231
pg_wc_isdigit(pg_wchar c)
232
0
{
233
0
  if (pg_regex_locale->ctype_is_c)
234
0
    return (c <= (pg_wchar) 127 &&
235
0
        (pg_char_properties[c] & PG_ISDIGIT));
236
0
  else
237
0
    return pg_regex_locale->ctype->wc_isdigit(c, pg_regex_locale);
238
0
}
239
240
static int
241
pg_wc_isalpha(pg_wchar c)
242
0
{
243
0
  if (pg_regex_locale->ctype_is_c)
244
0
    return (c <= (pg_wchar) 127 &&
245
0
        (pg_char_properties[c] & PG_ISALPHA));
246
0
  else
247
0
    return pg_regex_locale->ctype->wc_isalpha(c, pg_regex_locale);
248
0
}
249
250
static int
251
pg_wc_isalnum(pg_wchar c)
252
0
{
253
0
  if (pg_regex_locale->ctype_is_c)
254
0
    return (c <= (pg_wchar) 127 &&
255
0
        (pg_char_properties[c] & PG_ISALNUM));
256
0
  else
257
0
    return pg_regex_locale->ctype->wc_isalnum(c, pg_regex_locale);
258
0
}
259
260
static int
261
pg_wc_isword(pg_wchar c)
262
0
{
263
  /* We define word characters as alnum class plus underscore */
264
0
  if (c == CHR('_'))
265
0
    return 1;
266
0
  return pg_wc_isalnum(c);
267
0
}
268
269
static int
270
pg_wc_isupper(pg_wchar c)
271
0
{
272
0
  if (pg_regex_locale->ctype_is_c)
273
0
    return (c <= (pg_wchar) 127 &&
274
0
        (pg_char_properties[c] & PG_ISUPPER));
275
0
  else
276
0
    return pg_regex_locale->ctype->wc_isupper(c, pg_regex_locale);
277
0
}
278
279
static int
280
pg_wc_islower(pg_wchar c)
281
0
{
282
0
  if (pg_regex_locale->ctype_is_c)
283
0
    return (c <= (pg_wchar) 127 &&
284
0
        (pg_char_properties[c] & PG_ISLOWER));
285
0
  else
286
0
    return pg_regex_locale->ctype->wc_islower(c, pg_regex_locale);
287
0
}
288
289
static int
290
pg_wc_isgraph(pg_wchar c)
291
0
{
292
0
  if (pg_regex_locale->ctype_is_c)
293
0
    return (c <= (pg_wchar) 127 &&
294
0
        (pg_char_properties[c] & PG_ISGRAPH));
295
0
  else
296
0
    return pg_regex_locale->ctype->wc_isgraph(c, pg_regex_locale);
297
0
}
298
299
static int
300
pg_wc_isprint(pg_wchar c)
301
0
{
302
0
  if (pg_regex_locale->ctype_is_c)
303
0
    return (c <= (pg_wchar) 127 &&
304
0
        (pg_char_properties[c] & PG_ISPRINT));
305
0
  else
306
0
    return pg_regex_locale->ctype->wc_isprint(c, pg_regex_locale);
307
0
}
308
309
static int
310
pg_wc_ispunct(pg_wchar c)
311
0
{
312
0
  if (pg_regex_locale->ctype_is_c)
313
0
    return (c <= (pg_wchar) 127 &&
314
0
        (pg_char_properties[c] & PG_ISPUNCT));
315
0
  else
316
0
    return pg_regex_locale->ctype->wc_ispunct(c, pg_regex_locale);
317
0
}
318
319
static int
320
pg_wc_isspace(pg_wchar c)
321
0
{
322
0
  if (pg_regex_locale->ctype_is_c)
323
0
    return (c <= (pg_wchar) 127 &&
324
0
        (pg_char_properties[c] & PG_ISSPACE));
325
0
  else
326
0
    return pg_regex_locale->ctype->wc_isspace(c, pg_regex_locale);
327
0
}
328
329
static pg_wchar
330
pg_wc_toupper(pg_wchar c)
331
0
{
332
0
  if (pg_regex_locale->ctype_is_c)
333
0
  {
334
0
    if (c <= (pg_wchar) 127)
335
0
      return pg_ascii_toupper((unsigned char) c);
336
0
    return c;
337
0
  }
338
0
  else
339
0
    return pg_regex_locale->ctype->wc_toupper(c, pg_regex_locale);
340
0
}
341
342
static pg_wchar
343
pg_wc_tolower(pg_wchar c)
344
0
{
345
0
  if (pg_regex_locale->ctype_is_c)
346
0
  {
347
0
    if (c <= (pg_wchar) 127)
348
0
      return pg_ascii_tolower((unsigned char) c);
349
0
    return c;
350
0
  }
351
0
  else
352
0
    return pg_regex_locale->ctype->wc_tolower(c, pg_regex_locale);
353
0
}
354
355
356
/*
357
 * These functions cache the results of probing libc's ctype behavior for
358
 * all character codes of interest in a given encoding/collation.  The
359
 * result is provided as a "struct cvec", but notice that the representation
360
 * is a touch different from a cvec created by regc_cvec.c: we allocate the
361
 * chrs[] and ranges[] arrays separately from the struct so that we can
362
 * realloc them larger at need.  This is okay since the cvecs made here
363
 * should never be freed by freecvec().
364
 *
365
 * We use malloc not palloc since we mustn't lose control on out-of-memory;
366
 * the main regex code expects us to return a failure indication instead.
367
 */
368
369
typedef int (*pg_wc_probefunc) (pg_wchar c);
370
371
typedef struct pg_ctype_cache
372
{
373
  pg_wc_probefunc probefunc;  /* pg_wc_isalpha or a sibling */
374
  pg_locale_t locale;     /* locale this entry is for */
375
  struct cvec cv;       /* cache entry contents */
376
  struct pg_ctype_cache *next;  /* chain link */
377
} pg_ctype_cache;
378
379
static pg_ctype_cache *pg_ctype_cache_list = NULL;
380
381
/*
382
 * Add a chr or range to pcc->cv; return false if run out of memory
383
 */
384
static bool
385
store_match(pg_ctype_cache *pcc, pg_wchar chr1, int nchrs)
386
0
{
387
0
  chr      *newchrs;
388
389
0
  if (nchrs > 1)
390
0
  {
391
0
    if (pcc->cv.nranges >= pcc->cv.rangespace)
392
0
    {
393
0
      pcc->cv.rangespace *= 2;
394
0
      newchrs = (chr *) realloc(pcc->cv.ranges,
395
0
                    pcc->cv.rangespace * sizeof(chr) * 2);
396
0
      if (newchrs == NULL)
397
0
        return false;
398
0
      pcc->cv.ranges = newchrs;
399
0
    }
400
0
    pcc->cv.ranges[pcc->cv.nranges * 2] = chr1;
401
0
    pcc->cv.ranges[pcc->cv.nranges * 2 + 1] = chr1 + nchrs - 1;
402
0
    pcc->cv.nranges++;
403
0
  }
404
0
  else
405
0
  {
406
0
    assert(nchrs == 1);
407
0
    if (pcc->cv.nchrs >= pcc->cv.chrspace)
408
0
    {
409
0
      pcc->cv.chrspace *= 2;
410
0
      newchrs = (chr *) realloc(pcc->cv.chrs,
411
0
                    pcc->cv.chrspace * sizeof(chr));
412
0
      if (newchrs == NULL)
413
0
        return false;
414
0
      pcc->cv.chrs = newchrs;
415
0
    }
416
0
    pcc->cv.chrs[pcc->cv.nchrs++] = chr1;
417
0
  }
418
0
  return true;
419
0
}
420
421
/*
422
 * Given a probe function (e.g., pg_wc_isalpha) get a struct cvec for all
423
 * chrs satisfying the probe function.  The active collation is the one
424
 * previously set by pg_set_regex_collation.  Return NULL if out of memory.
425
 *
426
 * Note that the result must not be freed or modified by caller.
427
 */
428
static struct cvec *
429
pg_ctype_get_cache(pg_wc_probefunc probefunc, int cclasscode)
430
0
{
431
0
  pg_ctype_cache *pcc;
432
0
  pg_wchar  max_chr;
433
0
  pg_wchar  cur_chr;
434
0
  int     nmatches;
435
0
  chr      *newchrs;
436
437
  /*
438
   * Do we already have the answer cached?
439
   */
440
0
  for (pcc = pg_ctype_cache_list; pcc != NULL; pcc = pcc->next)
441
0
  {
442
0
    if (pcc->probefunc == probefunc &&
443
0
      pcc->locale == pg_regex_locale)
444
0
      return &pcc->cv;
445
0
  }
446
447
  /*
448
   * Nope, so initialize some workspace ...
449
   */
450
0
  pcc = (pg_ctype_cache *) malloc(sizeof(pg_ctype_cache));
451
0
  if (pcc == NULL)
452
0
    return NULL;
453
0
  pcc->probefunc = probefunc;
454
0
  pcc->locale = pg_regex_locale;
455
0
  pcc->cv.nchrs = 0;
456
0
  pcc->cv.chrspace = 128;
457
0
  pcc->cv.chrs = (chr *) malloc(pcc->cv.chrspace * sizeof(chr));
458
0
  pcc->cv.nranges = 0;
459
0
  pcc->cv.rangespace = 64;
460
0
  pcc->cv.ranges = (chr *) malloc(pcc->cv.rangespace * sizeof(chr) * 2);
461
0
  if (pcc->cv.chrs == NULL || pcc->cv.ranges == NULL)
462
0
    goto out_of_memory;
463
0
  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
   */
478
0
  if (pg_regex_locale->ctype_is_c)
479
0
  {
480
0
#if MAX_SIMPLE_CHR >= 127
481
0
    max_chr = (pg_wchar) 127;
482
0
    pcc->cv.cclasscode = -1;
483
#else
484
    max_chr = (pg_wchar) MAX_SIMPLE_CHR;
485
#endif
486
0
  }
487
0
  else
488
0
  {
489
0
    if (pg_regex_locale->ctype->max_chr != 0 &&
490
0
      pg_regex_locale->ctype->max_chr <= MAX_SIMPLE_CHR)
491
0
    {
492
0
      max_chr = pg_regex_locale->ctype->max_chr;
493
0
      pcc->cv.cclasscode = -1;
494
0
    }
495
0
    else
496
0
      max_chr = (pg_wchar) MAX_SIMPLE_CHR;
497
0
  }
498
499
  /*
500
   * And scan 'em ...
501
   */
502
0
  nmatches = 0;       /* number of consecutive matches */
503
504
0
  for (cur_chr = 0; cur_chr <= max_chr; cur_chr++)
505
0
  {
506
0
    if ((*probefunc) (cur_chr))
507
0
      nmatches++;
508
0
    else if (nmatches > 0)
509
0
    {
510
0
      if (!store_match(pcc, cur_chr - nmatches, nmatches))
511
0
        goto out_of_memory;
512
0
      nmatches = 0;
513
0
    }
514
0
  }
515
516
0
  if (nmatches > 0)
517
0
    if (!store_match(pcc, cur_chr - nmatches, nmatches))
518
0
      goto out_of_memory;
519
520
  /*
521
   * We might have allocated more memory than needed, if so free it
522
   */
523
0
  if (pcc->cv.nchrs == 0)
524
0
  {
525
0
    free(pcc->cv.chrs);
526
0
    pcc->cv.chrs = NULL;
527
0
    pcc->cv.chrspace = 0;
528
0
  }
529
0
  else if (pcc->cv.nchrs < pcc->cv.chrspace)
530
0
  {
531
0
    newchrs = (chr *) realloc(pcc->cv.chrs,
532
0
                  pcc->cv.nchrs * sizeof(chr));
533
0
    if (newchrs == NULL)
534
0
      goto out_of_memory;
535
0
    pcc->cv.chrs = newchrs;
536
0
    pcc->cv.chrspace = pcc->cv.nchrs;
537
0
  }
538
0
  if (pcc->cv.nranges == 0)
539
0
  {
540
0
    free(pcc->cv.ranges);
541
0
    pcc->cv.ranges = NULL;
542
0
    pcc->cv.rangespace = 0;
543
0
  }
544
0
  else if (pcc->cv.nranges < pcc->cv.rangespace)
545
0
  {
546
0
    newchrs = (chr *) realloc(pcc->cv.ranges,
547
0
                  pcc->cv.nranges * sizeof(chr) * 2);
548
0
    if (newchrs == NULL)
549
0
      goto out_of_memory;
550
0
    pcc->cv.ranges = newchrs;
551
0
    pcc->cv.rangespace = pcc->cv.nranges;
552
0
  }
553
554
  /*
555
   * Success, link it into cache chain
556
   */
557
0
  pcc->next = pg_ctype_cache_list;
558
0
  pg_ctype_cache_list = pcc;
559
560
0
  return &pcc->cv;
561
562
  /*
563
   * Failure, clean up
564
   */
565
0
out_of_memory:
566
0
  free(pcc->cv.chrs);
567
0
  free(pcc->cv.ranges);
568
0
  free(pcc);
569
570
  return NULL;
571
0
}