Debian Codegraph @

internal/cre2/cre2.h

raw symbols SCIP
1 //go:build tinygo.wasm || re2_cgo
2
3 /*
4 Header file for CRE2, a C language wrapper for RE2: a regular
5 expressions library by Google.
6
7 Copyright (c) 2012, 2015 Marco Maggi <[email protected]>
8 Copyright (c) 2011 Keegan McAllister
9 All rights reserved.
10
11 For the license notice see the COPYING file.
12 */
13
14 #ifndef CRE2_H
15 #define CRE2_H 1
16
17
18 /** --------------------------------------------------------------------
19 ** Headers.
20 ** ----------------------------------------------------------------- */
21
22 #ifdef __cplusplus
23 extern "C" {
24 #else
25 #include <stdbool.h>
26 #endif
27
28 #include <stdint.h>
29
30 #ifndef cre2_decl
31 # define cre2_decl extern
32 #endif
33
34
35 /** --------------------------------------------------------------------
36 ** Regular expressions configuration options.
37 ** ----------------------------------------------------------------- */
38
39 typedef void cre2_options_t;
40
41 typedef enum cre2_encoding_t {
42 CRE2_UNKNOWN = 0, /* should never happen */
43 CRE2_UTF8 = 1,
44 CRE2_Latin1 = 2
45 } cre2_encoding_t;
46
47 cre2_decl cre2_options_t *cre2_opt_new (void);
48 cre2_decl void cre2_opt_delete (cre2_options_t *opt);
49
50 cre2_decl void cre2_opt_set_posix_syntax (cre2_options_t *opt, int flag);
51 cre2_decl void cre2_opt_set_longest_match (cre2_options_t *opt, int flag);
52 cre2_decl void cre2_opt_set_log_errors (cre2_options_t *opt, int flag);
53 cre2_decl void cre2_opt_set_literal (cre2_options_t *opt, int flag);
54 cre2_decl void cre2_opt_set_never_nl (cre2_options_t *opt, int flag);
55 cre2_decl void cre2_opt_set_dot_nl (cre2_options_t *opt, int flag);
56 cre2_decl void cre2_opt_set_never_capture (cre2_options_t *opt, int flag);
57 cre2_decl void cre2_opt_set_case_sensitive (cre2_options_t *opt, int flag);
58 cre2_decl void cre2_opt_set_perl_classes (cre2_options_t *opt, int flag);
59 cre2_decl void cre2_opt_set_word_boundary (cre2_options_t *opt, int flag);
60 cre2_decl void cre2_opt_set_one_line (cre2_options_t *opt, int flag);
61 cre2_decl void cre2_opt_set_max_mem (cre2_options_t *opt, int64_t m);
62 cre2_decl void cre2_opt_set_encoding (cre2_options_t *opt, cre2_encoding_t enc);
63 cre2_decl void cre2_opt_set_latin1_encoding (cre2_options_t *opt);
64
65 cre2_decl int cre2_opt_posix_syntax (cre2_options_t *opt);
66 cre2_decl int cre2_opt_longest_match (cre2_options_t *opt);
67 cre2_decl int cre2_opt_log_errors (cre2_options_t *opt);
68 cre2_decl int cre2_opt_literal (cre2_options_t *opt);
69 cre2_decl int cre2_opt_never_nl (cre2_options_t *opt);
70 cre2_decl int cre2_opt_dot_nl (cre2_options_t *opt);
71 cre2_decl int cre2_opt_never_capture (cre2_options_t *opt);
72 cre2_decl int cre2_opt_case_sensitive (cre2_options_t *opt);
73 cre2_decl int cre2_opt_perl_classes (cre2_options_t *opt);
74 cre2_decl int cre2_opt_word_boundary (cre2_options_t *opt);
75 cre2_decl int cre2_opt_one_line (cre2_options_t *opt);
76 cre2_decl int64_t cre2_opt_max_mem (cre2_options_t *opt);
77 cre2_decl cre2_encoding_t cre2_opt_encoding (cre2_options_t *opt);
78
79
80 /** --------------------------------------------------------------------
81 ** Precompiled regular expressions.
82 ** ----------------------------------------------------------------- */
83
84 typedef struct cre2_string_t {
85 const char * data;
86 int length;
87 } cre2_string_t;
88
89 typedef void cre2_regexp_t;
90
91 typedef struct cre2_named_groups_iter_t cre2_named_groups_iter_t;
92
93 /* This definition must be kept in sync with the definition of "enum
94 ErrorCode" in the file "re2.h" of the original RE2 distribution. */
95 typedef enum cre2_error_code_t {
96 CRE2_NO_ERROR = 0,
97 CRE2_ERROR_INTERNAL, /* unexpected error */
98 /* parse errors */
99 CRE2_ERROR_BAD_ESCAPE, /* bad escape sequence */
100 CRE2_ERROR_BAD_CHAR_CLASS, /* bad character class */
101 CRE2_ERROR_BAD_CHAR_RANGE, /* bad character class range */
102 CRE2_ERROR_MISSING_BRACKET, /* missing closing ] */
103 CRE2_ERROR_MISSING_PAREN, /* missing closing ) */
104 CRE2_ERROR_TRAILING_BACKSLASH,/* trailing \ at end of regexp */
105 CRE2_ERROR_REPEAT_ARGUMENT, /* repeat argument missing, e.g. "*" */
106 CRE2_ERROR_REPEAT_SIZE, /* bad repetition argument */
107 CRE2_ERROR_REPEAT_OP, /* bad repetition operator */
108 CRE2_ERROR_BAD_PERL_OP, /* bad perl operator */
109 CRE2_ERROR_BAD_UTF8, /* invalid UTF-8 in regexp */
110 CRE2_ERROR_BAD_NAMED_CAPTURE, /* bad named capture group */
111 CRE2_ERROR_PATTERN_TOO_LARGE, /* pattern too large (compile failed) */
112 } cre2_error_code_t;
113
114 /* construction and destruction */
115 cre2_decl cre2_regexp_t * cre2_new (const char *pattern, int pattern_len,
116 const cre2_options_t *opt);
117 cre2_decl void cre2_delete (cre2_regexp_t *re);
118
119 /* regular expression inspection */
120 cre2_decl const char * cre2_pattern (const cre2_regexp_t *re);
121 cre2_decl int cre2_error_code (const cre2_regexp_t *re);
122 cre2_decl int cre2_num_capturing_groups (const cre2_regexp_t *re);
123 cre2_decl int cre2_program_size (const cre2_regexp_t *re);
124
125 /* named capture information */
126 cre2_decl int cre2_find_named_capturing_groups (const cre2_regexp_t *re, const char *name);
127 cre2_decl cre2_named_groups_iter_t * cre2_named_groups_iter_new(const cre2_regexp_t *re);
128 cre2_decl bool cre2_named_groups_iter_next(cre2_named_groups_iter_t* iter, const char ** name, int *index);
129 cre2_decl void cre2_named_groups_iter_delete(cre2_named_groups_iter_t *iter);
130
131 /* invalidated by further re use */
132 cre2_decl const char *cre2_error_string(const cre2_regexp_t *re);
133 cre2_decl const char *cre2_error_arg(const cre2_regexp_t *re);
134
135
136 /** --------------------------------------------------------------------
137 ** Main matching functions.
138 ** ----------------------------------------------------------------- */
139
140 typedef enum cre2_anchor_t {
141 CRE2_UNANCHORED = 1,
142 CRE2_ANCHOR_START = 2,
143 CRE2_ANCHOR_BOTH = 3
144 } cre2_anchor_t;
145
146 typedef struct cre2_range_t {
147 long start; /* inclusive start index for bytevector */
148 long past; /* exclusive end index for bytevector */
149 } cre2_range_t;
150
151 cre2_decl int cre2_match (const cre2_regexp_t * re,
152 const char * text, int textlen,
153 int startpos, int endpos, cre2_anchor_t anchor,
154 cre2_string_t * match, int nmatch);
155
156 cre2_decl int cre2_easy_match (const char * pattern, int pattern_len,
157 const char * text, int text_len,
158 cre2_string_t * match, int nmatch);
159
160 cre2_decl void cre2_strings_to_ranges (const char * text, cre2_range_t * ranges,
161 cre2_string_t * strings, int nmatch);
162
163
164 /** --------------------------------------------------------------------
165 ** Other matching functions.
166 ** ----------------------------------------------------------------- */
167
168 typedef int cre2_match_stringz_fun_t (const char * pattern, const cre2_string_t * text,
169 cre2_string_t * match, int nmatch);
170
171 typedef int cre2_match_stringz2_fun_t (const char * pattern, cre2_string_t * text,
172 cre2_string_t * match, int nmatch);
173
174 typedef int cre2_match_rex_fun_t (cre2_regexp_t * rex, const cre2_string_t * text,
175 cre2_string_t * match, int nmatch);
176
177 typedef int cre2_match_rex2_fun_t (cre2_regexp_t * rex, cre2_string_t * text,
178 cre2_string_t * match, int nmatch);
179
180 cre2_decl cre2_match_stringz_fun_t cre2_full_match;
181 cre2_decl cre2_match_stringz_fun_t cre2_partial_match;
182 cre2_decl cre2_match_stringz2_fun_t cre2_consume;
183 cre2_decl cre2_match_stringz2_fun_t cre2_find_and_consume;
184
185 cre2_decl cre2_match_rex_fun_t cre2_full_match_re;
186 cre2_decl cre2_match_rex_fun_t cre2_partial_match_re;
187 cre2_decl cre2_match_rex2_fun_t cre2_consume_re;
188 cre2_decl cre2_match_rex2_fun_t cre2_find_and_consume_re;
189
190
191 /** --------------------------------------------------------------------
192 ** Problematic functions.
193 ** ----------------------------------------------------------------- */
194
195 /* Match the text in the buffer "text_and_target" against the rex in
196 "pattern" or "rex". Mutate "text_and_target" so that it references a
197 malloc'ed buffer holding the original text in which the first, and
198 only the first, match is substituted with the text in "rewrite".
199 Numeric backslash sequences (\1 to \9) in "rewrite" are substituted
200 with the portions of text matching the corresponding parenthetical
201 subexpressions.
202
203 Return 0 if no match, 1 if successful match, -1 if error allocating
204 memory. */
205 cre2_decl int cre2_replace (const char * pattern,
206 cre2_string_t * text_and_target,
207 cre2_string_t * rewrite);
208 cre2_decl int cre2_replace_re (cre2_regexp_t * rex,
209 cre2_string_t * text_and_target,
210 cre2_string_t * rewrite);
211
212 /* Match the text in the buffer "text_and_target" against the rex in
213 "pattern" or "rex". Mutate "text_and_target" so that it references a
214 malloc'ed buffer holding the original text in which the all the
215 matching substrings are substituted with the text in "rewrite".
216 Numeric backslash sequences (\1 to \9) in "rewrite" are substituted
217 with the portions of text matching the corresponding parenthetical
218 subexpressions.
219
220 Return 0 if no match, positive integer representing the number of
221 substitutions performed if successful match, -1 if error allocating
222 memory. */
223 cre2_decl int cre2_global_replace (const char * pattern,
224 cre2_string_t * text_and_target,
225 cre2_string_t * rewrite);
226 cre2_decl int cre2_global_replace_re (cre2_regexp_t * rex,
227 cre2_string_t * text_and_target,
228 cre2_string_t * rewrite);
229
230 /* Match the text in the buffer "text" against the rex in "pattern" or
231 "rex". Mutate "target" so that it references a malloc'ed buffer
232 holding a copy of the text in "rewrite"; numeric backslash sequences
233 (\1 to \9) in "rewrite" are substituted with the portions of text
234 matching the corresponding parenthetical subexpressions.
235
236 Non-matching text in "text" is ignored.
237
238 Return 0 if no match, 1 if successful match, -1 if error allocating
239 memory. */
240 cre2_decl int cre2_extract (const char * pattern,
241 cre2_string_t * text,
242 cre2_string_t * rewrite,
243 cre2_string_t * target);
244
245 cre2_decl int cre2_extract_re (cre2_regexp_t * rex,
246 cre2_string_t * text,
247 cre2_string_t * rewrite,
248 cre2_string_t * target);
249
250 /* ------------------------------------------------------------------ */
251
252 /* Allocate a zero-terminated malloc'ed buffer and fill it with the text
253 from "original" having all the regexp meta characters quoted with
254 single backslashes. Return 0 if successful, return -1 if an error
255 allocating memory occurs. */
256 cre2_decl int cre2_quote_meta (cre2_string_t * quoted, cre2_string_t * original);
257
258 /* Compute a "minimum" string and a "maximum" string matching the given
259 regular expression. The min and max can in some cases be arbitrarily
260 precise, so the caller gets to specify "maxlen" begin the maximum
261 desired length of string returned.
262
263 Assuming the call returns successfully, any string S that is an
264 anchored match for this regexp satisfies:
265
266 min <= S && S <= max.
267
268 Note that this function will only consider the first copy of an
269 infinitely repeated element (i.e., any regexp element followed by a
270 '*' or '+' operator). Regexps with "{N}" constructions are not
271 affected, as those do not compile down to infinite repetitions.
272
273 "min_" and "max_" are mutated to reference zero-terminated malloc'ed
274 buffers holding the min and max strings.
275
276 Return 0 if failure, return 1 if successful, return -1 if an error
277 allocating memory occurs. */
278 cre2_decl int cre2_possible_match_range (cre2_regexp_t * rex,
279 cre2_string_t * min_, cre2_string_t * max_,
280 int maxlen);
281
282 /* Check that the given rewrite string is suitable for use with this
283 regular expression. It checks that:
284
285 * The regular expression has enough parenthesized subexpressions to
286 satisfy all of the \N tokens in rewrite
287
288 * The rewrite string doesn't have any syntax errors. E.g., '\'
289 followed by anything other than a digit or '\'.
290
291 A true return value guarantees that the replace and extract functions
292 won't fail because of a bad rewrite string.
293
294 In case of error: "errmsg" is mutated to reference a zero-terminated
295 malloc'ed string describing the problem.
296
297 Return 1 if the string is correct, return 0 if the string is
298 incorrect, return -1 if an error occurred allocating memory. */
299 cre2_decl int cre2_check_rewrite_string (cre2_regexp_t * rex,
300 cre2_string_t * rewrite, cre2_string_t * errmsg);
301
302
303 /** --------------------------------------------------------------------
304 ** Set match.
305 ** ----------------------------------------------------------------- */
306
307 /* Struct used to represent an RE2::Set object. */
308 struct cre2_set;
309 typedef struct cre2_set cre2_set;
310
311 /* RE2::Set constructor */
312 cre2_decl cre2_set *cre2_set_new(cre2_options_t *opt, cre2_anchor_t anchor);
313 /* RE2::Set destructor */
314 cre2_decl void cre2_set_delete(cre2_set *set);
315
316 // Add a regex to the set. If invalid: return error message that must be freed. If valid, return "ok".
317 cre2_decl char* cre2_set_add(cre2_set *set, const char *pattern, size_t pattern_len);
318
319 /* Add pattern without NULL byte. Discard error message.
320 * Returns the index associated to this regex, -1 on error */
321 cre2_decl int cre2_set_add_simple(cre2_set *set, const char *pattern);
322
323 /* Compile the regex set into a DFA. Must be called after add and before match.
324 * Returns 1 on success, 0 on error */
325 cre2_decl int cre2_set_compile(cre2_set *set);
326
327 /* Match the set of regex against text and store indices of matching regexes in match array.
328 * Returns the number of regexes which match. */
329 cre2_decl size_t cre2_set_match(cre2_set *set, const char *text, size_t text_len,
330 int *match, size_t match_len);
331
332
333 /** --------------------------------------------------------------------
334 ** Done.
335 ** ----------------------------------------------------------------- */
336
337 #ifdef __cplusplus
338 } // extern "C"
339 #endif
340
341 #endif /* CRE2_H */
342
343 /* end of file */