xref: /aosp_15_r20/external/musl/src/regex/tre.h (revision c9945492fdd68bbe62686c5b452b4dc1be3f8453)
1*c9945492SAndroid Build Coastguard Worker /*
2*c9945492SAndroid Build Coastguard Worker   tre-internal.h - TRE internal definitions
3*c9945492SAndroid Build Coastguard Worker 
4*c9945492SAndroid Build Coastguard Worker   Copyright (c) 2001-2009 Ville Laurikari <[email protected]>
5*c9945492SAndroid Build Coastguard Worker   All rights reserved.
6*c9945492SAndroid Build Coastguard Worker 
7*c9945492SAndroid Build Coastguard Worker   Redistribution and use in source and binary forms, with or without
8*c9945492SAndroid Build Coastguard Worker   modification, are permitted provided that the following conditions
9*c9945492SAndroid Build Coastguard Worker   are met:
10*c9945492SAndroid Build Coastguard Worker 
11*c9945492SAndroid Build Coastguard Worker     1. Redistributions of source code must retain the above copyright
12*c9945492SAndroid Build Coastguard Worker        notice, this list of conditions and the following disclaimer.
13*c9945492SAndroid Build Coastguard Worker 
14*c9945492SAndroid Build Coastguard Worker     2. Redistributions in binary form must reproduce the above copyright
15*c9945492SAndroid Build Coastguard Worker        notice, this list of conditions and the following disclaimer in the
16*c9945492SAndroid Build Coastguard Worker        documentation and/or other materials provided with the distribution.
17*c9945492SAndroid Build Coastguard Worker 
18*c9945492SAndroid Build Coastguard Worker   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
19*c9945492SAndroid Build Coastguard Worker   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20*c9945492SAndroid Build Coastguard Worker   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21*c9945492SAndroid Build Coastguard Worker   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
22*c9945492SAndroid Build Coastguard Worker   HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23*c9945492SAndroid Build Coastguard Worker   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24*c9945492SAndroid Build Coastguard Worker   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25*c9945492SAndroid Build Coastguard Worker   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26*c9945492SAndroid Build Coastguard Worker   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27*c9945492SAndroid Build Coastguard Worker   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28*c9945492SAndroid Build Coastguard Worker   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29*c9945492SAndroid Build Coastguard Worker 
30*c9945492SAndroid Build Coastguard Worker */
31*c9945492SAndroid Build Coastguard Worker 
32*c9945492SAndroid Build Coastguard Worker #include <regex.h>
33*c9945492SAndroid Build Coastguard Worker #include <wchar.h>
34*c9945492SAndroid Build Coastguard Worker #include <wctype.h>
35*c9945492SAndroid Build Coastguard Worker 
36*c9945492SAndroid Build Coastguard Worker #undef  TRE_MBSTATE
37*c9945492SAndroid Build Coastguard Worker 
38*c9945492SAndroid Build Coastguard Worker #define NDEBUG
39*c9945492SAndroid Build Coastguard Worker 
40*c9945492SAndroid Build Coastguard Worker #define TRE_REGEX_T_FIELD __opaque
41*c9945492SAndroid Build Coastguard Worker typedef int reg_errcode_t;
42*c9945492SAndroid Build Coastguard Worker 
43*c9945492SAndroid Build Coastguard Worker typedef wchar_t tre_char_t;
44*c9945492SAndroid Build Coastguard Worker 
45*c9945492SAndroid Build Coastguard Worker #define DPRINT(msg) do { } while(0)
46*c9945492SAndroid Build Coastguard Worker 
47*c9945492SAndroid Build Coastguard Worker #define elementsof(x)	( sizeof(x) / sizeof(x[0]) )
48*c9945492SAndroid Build Coastguard Worker 
49*c9945492SAndroid Build Coastguard Worker #define tre_mbrtowc(pwc, s, n, ps) (mbtowc((pwc), (s), (n)))
50*c9945492SAndroid Build Coastguard Worker 
51*c9945492SAndroid Build Coastguard Worker /* Wide characters. */
52*c9945492SAndroid Build Coastguard Worker typedef wint_t tre_cint_t;
53*c9945492SAndroid Build Coastguard Worker #define TRE_CHAR_MAX 0x10ffff
54*c9945492SAndroid Build Coastguard Worker 
55*c9945492SAndroid Build Coastguard Worker #define tre_isalnum iswalnum
56*c9945492SAndroid Build Coastguard Worker #define tre_isalpha iswalpha
57*c9945492SAndroid Build Coastguard Worker #define tre_isblank iswblank
58*c9945492SAndroid Build Coastguard Worker #define tre_iscntrl iswcntrl
59*c9945492SAndroid Build Coastguard Worker #define tre_isdigit iswdigit
60*c9945492SAndroid Build Coastguard Worker #define tre_isgraph iswgraph
61*c9945492SAndroid Build Coastguard Worker #define tre_islower iswlower
62*c9945492SAndroid Build Coastguard Worker #define tre_isprint iswprint
63*c9945492SAndroid Build Coastguard Worker #define tre_ispunct iswpunct
64*c9945492SAndroid Build Coastguard Worker #define tre_isspace iswspace
65*c9945492SAndroid Build Coastguard Worker #define tre_isupper iswupper
66*c9945492SAndroid Build Coastguard Worker #define tre_isxdigit iswxdigit
67*c9945492SAndroid Build Coastguard Worker 
68*c9945492SAndroid Build Coastguard Worker #define tre_tolower towlower
69*c9945492SAndroid Build Coastguard Worker #define tre_toupper towupper
70*c9945492SAndroid Build Coastguard Worker #define tre_strlen  wcslen
71*c9945492SAndroid Build Coastguard Worker 
72*c9945492SAndroid Build Coastguard Worker /* Use system provided iswctype() and wctype(). */
73*c9945492SAndroid Build Coastguard Worker typedef wctype_t tre_ctype_t;
74*c9945492SAndroid Build Coastguard Worker #define tre_isctype iswctype
75*c9945492SAndroid Build Coastguard Worker #define tre_ctype   wctype
76*c9945492SAndroid Build Coastguard Worker 
77*c9945492SAndroid Build Coastguard Worker /* Returns number of bytes to add to (char *)ptr to make it
78*c9945492SAndroid Build Coastguard Worker    properly aligned for the type. */
79*c9945492SAndroid Build Coastguard Worker #define ALIGN(ptr, type) \
80*c9945492SAndroid Build Coastguard Worker   ((((long)ptr) % sizeof(type)) \
81*c9945492SAndroid Build Coastguard Worker    ? (sizeof(type) - (((long)ptr) % sizeof(type))) \
82*c9945492SAndroid Build Coastguard Worker    : 0)
83*c9945492SAndroid Build Coastguard Worker 
84*c9945492SAndroid Build Coastguard Worker #undef MAX
85*c9945492SAndroid Build Coastguard Worker #undef MIN
86*c9945492SAndroid Build Coastguard Worker #define MAX(a, b) (((a) >= (b)) ? (a) : (b))
87*c9945492SAndroid Build Coastguard Worker #define MIN(a, b) (((a) <= (b)) ? (a) : (b))
88*c9945492SAndroid Build Coastguard Worker 
89*c9945492SAndroid Build Coastguard Worker /* TNFA transition type. A TNFA state is an array of transitions,
90*c9945492SAndroid Build Coastguard Worker    the terminator is a transition with NULL `state'. */
91*c9945492SAndroid Build Coastguard Worker typedef struct tnfa_transition tre_tnfa_transition_t;
92*c9945492SAndroid Build Coastguard Worker 
93*c9945492SAndroid Build Coastguard Worker struct tnfa_transition {
94*c9945492SAndroid Build Coastguard Worker   /* Range of accepted characters. */
95*c9945492SAndroid Build Coastguard Worker   tre_cint_t code_min;
96*c9945492SAndroid Build Coastguard Worker   tre_cint_t code_max;
97*c9945492SAndroid Build Coastguard Worker   /* Pointer to the destination state. */
98*c9945492SAndroid Build Coastguard Worker   tre_tnfa_transition_t *state;
99*c9945492SAndroid Build Coastguard Worker   /* ID number of the destination state. */
100*c9945492SAndroid Build Coastguard Worker   int state_id;
101*c9945492SAndroid Build Coastguard Worker   /* -1 terminated array of tags (or NULL). */
102*c9945492SAndroid Build Coastguard Worker   int *tags;
103*c9945492SAndroid Build Coastguard Worker   /* Assertion bitmap. */
104*c9945492SAndroid Build Coastguard Worker   int assertions;
105*c9945492SAndroid Build Coastguard Worker   /* Assertion parameters. */
106*c9945492SAndroid Build Coastguard Worker   union {
107*c9945492SAndroid Build Coastguard Worker     /* Character class assertion. */
108*c9945492SAndroid Build Coastguard Worker     tre_ctype_t class;
109*c9945492SAndroid Build Coastguard Worker     /* Back reference assertion. */
110*c9945492SAndroid Build Coastguard Worker     int backref;
111*c9945492SAndroid Build Coastguard Worker   } u;
112*c9945492SAndroid Build Coastguard Worker   /* Negative character class assertions. */
113*c9945492SAndroid Build Coastguard Worker   tre_ctype_t *neg_classes;
114*c9945492SAndroid Build Coastguard Worker };
115*c9945492SAndroid Build Coastguard Worker 
116*c9945492SAndroid Build Coastguard Worker 
117*c9945492SAndroid Build Coastguard Worker /* Assertions. */
118*c9945492SAndroid Build Coastguard Worker #define ASSERT_AT_BOL		  1   /* Beginning of line. */
119*c9945492SAndroid Build Coastguard Worker #define ASSERT_AT_EOL		  2   /* End of line. */
120*c9945492SAndroid Build Coastguard Worker #define ASSERT_CHAR_CLASS	  4   /* Character class in `class'. */
121*c9945492SAndroid Build Coastguard Worker #define ASSERT_CHAR_CLASS_NEG	  8   /* Character classes in `neg_classes'. */
122*c9945492SAndroid Build Coastguard Worker #define ASSERT_AT_BOW		 16   /* Beginning of word. */
123*c9945492SAndroid Build Coastguard Worker #define ASSERT_AT_EOW		 32   /* End of word. */
124*c9945492SAndroid Build Coastguard Worker #define ASSERT_AT_WB		 64   /* Word boundary. */
125*c9945492SAndroid Build Coastguard Worker #define ASSERT_AT_WB_NEG	128   /* Not a word boundary. */
126*c9945492SAndroid Build Coastguard Worker #define ASSERT_BACKREF		256   /* A back reference in `backref'. */
127*c9945492SAndroid Build Coastguard Worker #define ASSERT_LAST		256
128*c9945492SAndroid Build Coastguard Worker 
129*c9945492SAndroid Build Coastguard Worker /* Tag directions. */
130*c9945492SAndroid Build Coastguard Worker typedef enum {
131*c9945492SAndroid Build Coastguard Worker   TRE_TAG_MINIMIZE = 0,
132*c9945492SAndroid Build Coastguard Worker   TRE_TAG_MAXIMIZE = 1
133*c9945492SAndroid Build Coastguard Worker } tre_tag_direction_t;
134*c9945492SAndroid Build Coastguard Worker 
135*c9945492SAndroid Build Coastguard Worker /* Instructions to compute submatch register values from tag values
136*c9945492SAndroid Build Coastguard Worker    after a successful match.  */
137*c9945492SAndroid Build Coastguard Worker struct tre_submatch_data {
138*c9945492SAndroid Build Coastguard Worker   /* Tag that gives the value for rm_so (submatch start offset). */
139*c9945492SAndroid Build Coastguard Worker   int so_tag;
140*c9945492SAndroid Build Coastguard Worker   /* Tag that gives the value for rm_eo (submatch end offset). */
141*c9945492SAndroid Build Coastguard Worker   int eo_tag;
142*c9945492SAndroid Build Coastguard Worker   /* List of submatches this submatch is contained in. */
143*c9945492SAndroid Build Coastguard Worker   int *parents;
144*c9945492SAndroid Build Coastguard Worker };
145*c9945492SAndroid Build Coastguard Worker 
146*c9945492SAndroid Build Coastguard Worker typedef struct tre_submatch_data tre_submatch_data_t;
147*c9945492SAndroid Build Coastguard Worker 
148*c9945492SAndroid Build Coastguard Worker 
149*c9945492SAndroid Build Coastguard Worker /* TNFA definition. */
150*c9945492SAndroid Build Coastguard Worker typedef struct tnfa tre_tnfa_t;
151*c9945492SAndroid Build Coastguard Worker 
152*c9945492SAndroid Build Coastguard Worker struct tnfa {
153*c9945492SAndroid Build Coastguard Worker   tre_tnfa_transition_t *transitions;
154*c9945492SAndroid Build Coastguard Worker   unsigned int num_transitions;
155*c9945492SAndroid Build Coastguard Worker   tre_tnfa_transition_t *initial;
156*c9945492SAndroid Build Coastguard Worker   tre_tnfa_transition_t *final;
157*c9945492SAndroid Build Coastguard Worker   tre_submatch_data_t *submatch_data;
158*c9945492SAndroid Build Coastguard Worker   char *firstpos_chars;
159*c9945492SAndroid Build Coastguard Worker   int first_char;
160*c9945492SAndroid Build Coastguard Worker   unsigned int num_submatches;
161*c9945492SAndroid Build Coastguard Worker   tre_tag_direction_t *tag_directions;
162*c9945492SAndroid Build Coastguard Worker   int *minimal_tags;
163*c9945492SAndroid Build Coastguard Worker   int num_tags;
164*c9945492SAndroid Build Coastguard Worker   int num_minimals;
165*c9945492SAndroid Build Coastguard Worker   int end_tag;
166*c9945492SAndroid Build Coastguard Worker   int num_states;
167*c9945492SAndroid Build Coastguard Worker   int cflags;
168*c9945492SAndroid Build Coastguard Worker   int have_backrefs;
169*c9945492SAndroid Build Coastguard Worker   int have_approx;
170*c9945492SAndroid Build Coastguard Worker };
171*c9945492SAndroid Build Coastguard Worker 
172*c9945492SAndroid Build Coastguard Worker /* from tre-mem.h: */
173*c9945492SAndroid Build Coastguard Worker 
174*c9945492SAndroid Build Coastguard Worker #define TRE_MEM_BLOCK_SIZE 1024
175*c9945492SAndroid Build Coastguard Worker 
176*c9945492SAndroid Build Coastguard Worker typedef struct tre_list {
177*c9945492SAndroid Build Coastguard Worker   void *data;
178*c9945492SAndroid Build Coastguard Worker   struct tre_list *next;
179*c9945492SAndroid Build Coastguard Worker } tre_list_t;
180*c9945492SAndroid Build Coastguard Worker 
181*c9945492SAndroid Build Coastguard Worker typedef struct tre_mem_struct {
182*c9945492SAndroid Build Coastguard Worker   tre_list_t *blocks;
183*c9945492SAndroid Build Coastguard Worker   tre_list_t *current;
184*c9945492SAndroid Build Coastguard Worker   char *ptr;
185*c9945492SAndroid Build Coastguard Worker   size_t n;
186*c9945492SAndroid Build Coastguard Worker   int failed;
187*c9945492SAndroid Build Coastguard Worker   void **provided;
188*c9945492SAndroid Build Coastguard Worker } *tre_mem_t;
189*c9945492SAndroid Build Coastguard Worker 
190*c9945492SAndroid Build Coastguard Worker #define tre_mem_new_impl   __tre_mem_new_impl
191*c9945492SAndroid Build Coastguard Worker #define tre_mem_alloc_impl __tre_mem_alloc_impl
192*c9945492SAndroid Build Coastguard Worker #define tre_mem_destroy    __tre_mem_destroy
193*c9945492SAndroid Build Coastguard Worker 
194*c9945492SAndroid Build Coastguard Worker hidden tre_mem_t tre_mem_new_impl(int provided, void *provided_block);
195*c9945492SAndroid Build Coastguard Worker hidden void *tre_mem_alloc_impl(tre_mem_t mem, int provided, void *provided_block,
196*c9945492SAndroid Build Coastguard Worker                                 int zero, size_t size);
197*c9945492SAndroid Build Coastguard Worker 
198*c9945492SAndroid Build Coastguard Worker /* Returns a new memory allocator or NULL if out of memory. */
199*c9945492SAndroid Build Coastguard Worker #define tre_mem_new()  tre_mem_new_impl(0, NULL)
200*c9945492SAndroid Build Coastguard Worker 
201*c9945492SAndroid Build Coastguard Worker /* Allocates a block of `size' bytes from `mem'.  Returns a pointer to the
202*c9945492SAndroid Build Coastguard Worker    allocated block or NULL if an underlying malloc() failed. */
203*c9945492SAndroid Build Coastguard Worker #define tre_mem_alloc(mem, size) tre_mem_alloc_impl(mem, 0, NULL, 0, size)
204*c9945492SAndroid Build Coastguard Worker 
205*c9945492SAndroid Build Coastguard Worker /* Allocates a block of `size' bytes from `mem'.  Returns a pointer to the
206*c9945492SAndroid Build Coastguard Worker    allocated block or NULL if an underlying malloc() failed.  The memory
207*c9945492SAndroid Build Coastguard Worker    is set to zero. */
208*c9945492SAndroid Build Coastguard Worker #define tre_mem_calloc(mem, size) tre_mem_alloc_impl(mem, 0, NULL, 1, size)
209*c9945492SAndroid Build Coastguard Worker 
210*c9945492SAndroid Build Coastguard Worker #ifdef TRE_USE_ALLOCA
211*c9945492SAndroid Build Coastguard Worker /* alloca() versions.  Like above, but memory is allocated with alloca()
212*c9945492SAndroid Build Coastguard Worker    instead of malloc(). */
213*c9945492SAndroid Build Coastguard Worker 
214*c9945492SAndroid Build Coastguard Worker #define tre_mem_newa() \
215*c9945492SAndroid Build Coastguard Worker   tre_mem_new_impl(1, alloca(sizeof(struct tre_mem_struct)))
216*c9945492SAndroid Build Coastguard Worker 
217*c9945492SAndroid Build Coastguard Worker #define tre_mem_alloca(mem, size)					      \
218*c9945492SAndroid Build Coastguard Worker   ((mem)->n >= (size)							      \
219*c9945492SAndroid Build Coastguard Worker    ? tre_mem_alloc_impl((mem), 1, NULL, 0, (size))			      \
220*c9945492SAndroid Build Coastguard Worker    : tre_mem_alloc_impl((mem), 1, alloca(TRE_MEM_BLOCK_SIZE), 0, (size)))
221*c9945492SAndroid Build Coastguard Worker #endif /* TRE_USE_ALLOCA */
222*c9945492SAndroid Build Coastguard Worker 
223*c9945492SAndroid Build Coastguard Worker 
224*c9945492SAndroid Build Coastguard Worker /* Frees the memory allocator and all memory allocated with it. */
225*c9945492SAndroid Build Coastguard Worker hidden void tre_mem_destroy(tre_mem_t mem);
226*c9945492SAndroid Build Coastguard Worker 
227*c9945492SAndroid Build Coastguard Worker #define xmalloc malloc
228*c9945492SAndroid Build Coastguard Worker #define xcalloc calloc
229*c9945492SAndroid Build Coastguard Worker #define xfree free
230*c9945492SAndroid Build Coastguard Worker #define xrealloc realloc
231*c9945492SAndroid Build Coastguard Worker 
232