Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/pcre2/src/pcre2_maketables.c
22057 views
1
/*************************************************
2
* Perl-Compatible Regular Expressions *
3
*************************************************/
4
5
/* PCRE is a library of functions to support regular expressions whose syntax
6
and semantics are as close as possible to those of the Perl 5 language.
7
8
Written by Philip Hazel
9
Original API code Copyright (c) 1997-2012 University of Cambridge
10
New API code Copyright (c) 2016-2024 University of Cambridge
11
12
-----------------------------------------------------------------------------
13
Redistribution and use in source and binary forms, with or without
14
modification, are permitted provided that the following conditions are met:
15
16
* Redistributions of source code must retain the above copyright notice,
17
this list of conditions and the following disclaimer.
18
19
* Redistributions in binary form must reproduce the above copyright
20
notice, this list of conditions and the following disclaimer in the
21
documentation and/or other materials provided with the distribution.
22
23
* Neither the name of the University of Cambridge nor the names of its
24
contributors may be used to endorse or promote products derived from
25
this software without specific prior written permission.
26
27
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37
POSSIBILITY OF SUCH DAMAGE.
38
-----------------------------------------------------------------------------
39
*/
40
41
42
/* This module contains the external function pcre2_maketables(), which builds
43
character tables for PCRE2 in the current locale. The file is compiled on its
44
own as part of the PCRE2 library. It is also included in the compilation of
45
pcre2_dftables.c as a freestanding program, in which case the macro
46
PCRE2_DFTABLES is defined. */
47
48
49
#ifndef PCRE2_DFTABLES /* Compiling the library */
50
#include "pcre2_internal.h"
51
#endif
52
53
54
55
/*************************************************
56
* Create PCRE2 character tables *
57
*************************************************/
58
59
/* This function builds a set of character tables for use by PCRE2 and returns
60
a pointer to them. They are build using the ctype functions, and consequently
61
their contents will depend upon the current locale setting. When compiled as
62
part of the library, the store is obtained via a general context malloc, if
63
supplied, but when PCRE2_DFTABLES is defined (when compiling the pcre2_dftables
64
freestanding auxiliary program) malloc() is used, and the function has a
65
different name so as not to clash with the prototype in pcre2.h.
66
67
Arguments: pointers to character-transforming functions when PCRE2_DFTABLES is
68
defined;
69
else a PCRE2 general context or NULL
70
Returns: pointer to the contiguous block of data;
71
else NULL if memory allocation failed
72
*/
73
74
#ifdef PCRE2_DFTABLES /* Included in freestanding pcre2_dftables program */
75
static const uint8_t *maketables(int (*charfn_to)(int), int (*charfn_from)(int))
76
{
77
uint8_t *yield = (uint8_t *)malloc(TABLES_LENGTH);
78
79
#else /* Not PCRE2_DFTABLES, that is, compiling the library */
80
PCRE2_EXP_DEFN const uint8_t * PCRE2_CALL_CONVENTION
81
pcre2_maketables(pcre2_general_context *gcontext)
82
{
83
uint8_t *yield = (uint8_t *)((gcontext != NULL)?
84
gcontext->memctl.malloc(TABLES_LENGTH, gcontext->memctl.memory_data) :
85
malloc(TABLES_LENGTH));
86
87
#define charfn_to(c) (c)
88
#define charfn_from(c) (c)
89
#endif /* PCRE2_DFTABLES */
90
91
int i;
92
uint8_t *p;
93
94
if (yield == NULL) return NULL;
95
p = yield;
96
97
/* First comes the lower casing table */
98
99
for (i = 0; i < 256; i++)
100
{
101
int c = charfn_from(tolower(charfn_to(i)));
102
*p++ = (c < 256)? c : i;
103
}
104
105
/* Next the case-flipping table */
106
107
for (i = 0; i < 256; i++)
108
{
109
int c = charfn_from(islower(charfn_to(i))? toupper(charfn_to(i))
110
: tolower(charfn_to(i)));
111
*p++ = (c < 256)? c : i;
112
}
113
114
/* Then the character class tables. Don't try to be clever and save effort on
115
exclusive ones - in some locales things may be different.
116
117
Note that the table for "space" includes everything "isspace" gives, including
118
VT in the default locale. This makes it work for the POSIX class [:space:].
119
From PCRE1 release 8.34 and for all PCRE2 releases it is also correct for Perl
120
space, because Perl added VT at release 5.18.
121
122
Note also that it is possible for a character to be alnum or alpha without
123
being lower or upper, such as "male and female ordinals" (\xAA and \xBA) in the
124
fr_FR locale (at least under Debian Linux's locales as of 12/2005). So we must
125
test for alnum specially. */
126
127
memset(p, 0, cbit_length);
128
for (i = 0; i < 256; i++)
129
{
130
if (isdigit(charfn_to(i))) p[cbit_digit + i/8] |= 1u << (i&7);
131
if (isupper(charfn_to(i))) p[cbit_upper + i/8] |= 1u << (i&7);
132
if (islower(charfn_to(i))) p[cbit_lower + i/8] |= 1u << (i&7);
133
if (isalnum(charfn_to(i))) p[cbit_word + i/8] |= 1u << (i&7);
134
if (i == CHAR_UNDERSCORE) p[cbit_word + i/8] |= 1u << (i&7);
135
if (isspace(charfn_to(i))) p[cbit_space + i/8] |= 1u << (i&7);
136
if (isxdigit(charfn_to(i))) p[cbit_xdigit + i/8] |= 1u << (i&7);
137
if (isgraph(charfn_to(i))) p[cbit_graph + i/8] |= 1u << (i&7);
138
if (isprint(charfn_to(i))) p[cbit_print + i/8] |= 1u << (i&7);
139
if (ispunct(charfn_to(i))) p[cbit_punct + i/8] |= 1u << (i&7);
140
if (iscntrl(charfn_to(i))) p[cbit_cntrl + i/8] |= 1u << (i&7);
141
}
142
p += cbit_length;
143
144
/* Finally, the character type table. In this, we used to exclude VT from the
145
white space chars, because Perl didn't recognize it as such for \s and for
146
comments within regexes. However, Perl changed at release 5.18, so PCRE1
147
changed at release 8.34 and it's always been this way for PCRE2. */
148
149
for (i = 0; i < 256; i++)
150
{
151
int x = 0;
152
if (isspace(charfn_to(i))) x += ctype_space;
153
if (isalpha(charfn_to(i))) x += ctype_letter;
154
if (islower(charfn_to(i))) x += ctype_lcletter;
155
if (isdigit(charfn_to(i))) x += ctype_digit;
156
if (isalnum(charfn_to(i)) || i == CHAR_UNDERSCORE) x += ctype_word;
157
*p++ = x;
158
}
159
160
return yield;
161
}
162
163
#ifndef PCRE2_DFTABLES /* Compiling the library */
164
#undef charfn_to
165
#undef charfn_from
166
167
PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
168
pcre2_maketables_free(pcre2_general_context *gcontext, const uint8_t *tables)
169
{
170
if (gcontext != NULL)
171
gcontext->memctl.free((void *)tables, gcontext->memctl.memory_data);
172
else
173
free((void *)tables);
174
}
175
#endif
176
177
/* End of pcre2_maketables.c */
178
179