2 tre-internal.h - TRE internal definitions
4 Copyright (c) 2001-2009 Ville Laurikari <vl@iki.fi>
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
14 2. Redistributions in binary form must reproduce the above copyright
15 notice, this list of conditions and the following disclaimer in the
16 documentation and/or other materials provided with the distribution.
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
19 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #define TRE_REGEX_T_FIELD __opaque
41 typedef int reg_errcode_t;
43 typedef wchar_t tre_char_t;
45 #define DPRINT(msg) do { } while(0)
47 #define elementsof(x) ( sizeof(x) / sizeof(x[0]) )
49 #define tre_mbrtowc(pwc, s, n, ps) (mbtowc((pwc), (s), (n)))
51 /* Wide characters. */
52 typedef wint_t tre_cint_t;
53 #define TRE_CHAR_MAX 0x10ffff
55 #define tre_isalnum iswalnum
56 #define tre_isalpha iswalpha
57 #define tre_isblank iswblank
58 #define tre_iscntrl iswcntrl
59 #define tre_isdigit iswdigit
60 #define tre_isgraph iswgraph
61 #define tre_islower iswlower
62 #define tre_isprint iswprint
63 #define tre_ispunct iswpunct
64 #define tre_isspace iswspace
65 #define tre_isupper iswupper
66 #define tre_isxdigit iswxdigit
68 #define tre_tolower towlower
69 #define tre_toupper towupper
70 #define tre_strlen wcslen
72 /* Use system provided iswctype() and wctype(). */
73 typedef wctype_t tre_ctype_t;
74 #define tre_isctype iswctype
75 #define tre_ctype wctype
77 /* Returns number of bytes to add to (char *)ptr to make it
78 properly aligned for the type. */
79 #define ALIGN(ptr, type) \
80 ((((long)ptr) % sizeof(type)) \
81 ? (sizeof(type) - (((long)ptr) % sizeof(type))) \
86 #define MAX(a, b) (((a) >= (b)) ? (a) : (b))
87 #define MIN(a, b) (((a) <= (b)) ? (a) : (b))
89 /* TNFA transition type. A TNFA state is an array of transitions,
90 the terminator is a transition with NULL `state'. */
91 typedef struct tnfa_transition tre_tnfa_transition_t;
93 struct tnfa_transition {
94 /* Range of accepted characters. */
97 /* Pointer to the destination state. */
98 tre_tnfa_transition_t *state;
99 /* ID number of the destination state. */
101 /* -1 terminated array of tags (or NULL). */
103 /* Assertion bitmap. */
105 /* Assertion parameters. */
107 /* Character class assertion. */
109 /* Back reference assertion. */
112 /* Negative character class assertions. */
113 tre_ctype_t *neg_classes;
118 #define ASSERT_AT_BOL 1 /* Beginning of line. */
119 #define ASSERT_AT_EOL 2 /* End of line. */
120 #define ASSERT_CHAR_CLASS 4 /* Character class in `class'. */
121 #define ASSERT_CHAR_CLASS_NEG 8 /* Character classes in `neg_classes'. */
122 #define ASSERT_AT_BOW 16 /* Beginning of word. */
123 #define ASSERT_AT_EOW 32 /* End of word. */
124 #define ASSERT_AT_WB 64 /* Word boundary. */
125 #define ASSERT_AT_WB_NEG 128 /* Not a word boundary. */
126 #define ASSERT_BACKREF 256 /* A back reference in `backref'. */
127 #define ASSERT_LAST 256
129 /* Tag directions. */
131 TRE_TAG_MINIMIZE = 0,
133 } tre_tag_direction_t;
135 /* Instructions to compute submatch register values from tag values
136 after a successful match. */
137 struct tre_submatch_data {
138 /* Tag that gives the value for rm_so (submatch start offset). */
140 /* Tag that gives the value for rm_eo (submatch end offset). */
142 /* List of submatches this submatch is contained in. */
146 typedef struct tre_submatch_data tre_submatch_data_t;
149 /* TNFA definition. */
150 typedef struct tnfa tre_tnfa_t;
153 tre_tnfa_transition_t *transitions;
154 unsigned int num_transitions;
155 tre_tnfa_transition_t *initial;
156 tre_tnfa_transition_t *final;
157 tre_submatch_data_t *submatch_data;
158 char *firstpos_chars;
160 unsigned int num_submatches;
161 tre_tag_direction_t *tag_directions;
173 /* from tre-mem.h: */
175 #define TRE_MEM_BLOCK_SIZE 1024
177 typedef struct tre_list {
179 struct tre_list *next;
182 typedef struct tre_mem_struct {
191 #define tre_mem_new_impl __tre_mem_new_impl
192 #define tre_mem_alloc_impl __tre_mem_alloc_impl
193 #define tre_mem_destroy __tre_mem_destroy
195 tre_mem_t tre_mem_new_impl(int provided, void *provided_block);
196 void *tre_mem_alloc_impl(tre_mem_t mem, int provided, void *provided_block,
197 int zero, size_t size);
199 /* Returns a new memory allocator or NULL if out of memory. */
200 #define tre_mem_new() tre_mem_new_impl(0, NULL)
202 /* Allocates a block of `size' bytes from `mem'. Returns a pointer to the
203 allocated block or NULL if an underlying malloc() failed. */
204 #define tre_mem_alloc(mem, size) tre_mem_alloc_impl(mem, 0, NULL, 0, size)
206 /* Allocates a block of `size' bytes from `mem'. Returns a pointer to the
207 allocated block or NULL if an underlying malloc() failed. The memory
209 #define tre_mem_calloc(mem, size) tre_mem_alloc_impl(mem, 0, NULL, 1, size)
211 #ifdef TRE_USE_ALLOCA
212 /* alloca() versions. Like above, but memory is allocated with alloca()
213 instead of malloc(). */
215 #define tre_mem_newa() \
216 tre_mem_new_impl(1, alloca(sizeof(struct tre_mem_struct)))
218 #define tre_mem_alloca(mem, size) \
219 ((mem)->n >= (size) \
220 ? tre_mem_alloc_impl((mem), 1, NULL, 0, (size)) \
221 : tre_mem_alloc_impl((mem), 1, alloca(TRE_MEM_BLOCK_SIZE), 0, (size)))
222 #endif /* TRE_USE_ALLOCA */
225 /* Frees the memory allocator and all memory allocated with it. */
226 void tre_mem_destroy(tre_mem_t mem);
228 #define xmalloc malloc
229 #define xcalloc calloc
231 #define xrealloc realloc