regex: use re_malloc etc. consistently
[gnulib.git] / lib / dfa.h
bloba8d514b786298e4da8659800c24a3734808211a4
1 /* dfa.h - declarations for GNU deterministic regexp compiler
2 Copyright (C) 1988, 1998, 2007, 2009-2017 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc.,
17 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA */
19 /* Written June, 1988 by Mike Haertel */
21 #include <regex.h>
22 #include <stdbool.h>
23 #include <stddef.h>
25 #if 3 <= __GNUC__
26 # define _GL_ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
27 #else
28 # define _GL_ATTRIBUTE_MALLOC
29 #endif
31 struct localeinfo; /* See localeinfo.h. */
33 /* Element of a list of strings, at least one of which is known to
34 appear in any R.E. matching the DFA. */
35 struct dfamust
37 bool exact;
38 bool begline;
39 bool endline;
40 char *must;
43 /* The dfa structure. It is completely opaque. */
44 struct dfa;
46 /* Entry points. */
48 /* Allocate a struct dfa. The struct dfa is completely opaque.
49 The returned pointer should be passed directly to free() after
50 calling dfafree() on it. */
51 extern struct dfa *dfaalloc (void) _GL_ATTRIBUTE_MALLOC;
53 /* DFA options that can be ORed together, for dfasyntax's 4th arg. */
54 enum
56 /* ^ and $ match only the start and end of data, and do not match
57 end-of-line within data. This is always false for grep, but
58 possibly true for other apps. */
59 DFA_ANCHOR = 1 << 0,
61 /* '\0' in data is end-of-line, instead of the traditional '\n'. */
62 DFA_EOL_NUL = 1 << 1
65 /* Initialize or reinitialize a DFA. This must be called before
66 any of the routines below. The arguments are:
67 1. The DFA to operate on.
68 2. Information about the current locale.
69 3. Syntax bits described in regex.h.
70 4. Additional DFA options described above. */
71 extern void dfasyntax (struct dfa *, struct localeinfo const *,
72 reg_syntax_t, int);
74 /* Build and return the struct dfamust from the given struct dfa. */
75 extern struct dfamust *dfamust (struct dfa const *);
77 /* Free the storage held by the components of a struct dfamust. */
78 extern void dfamustfree (struct dfamust *);
80 /* Compile the given string of the given length into the given struct dfa.
81 Final argument is a flag specifying whether to build a searching or an
82 exact matcher. */
83 extern void dfacomp (char const *, size_t, struct dfa *, bool);
85 /* Search through a buffer looking for a match to the given struct dfa.
86 Find the first occurrence of a string matching the regexp in the
87 buffer, and the shortest possible version thereof. Return a pointer to
88 the first character after the match, or NULL if none is found. BEGIN
89 points to the beginning of the buffer, and END points to the first byte
90 after its end. Note however that we store a sentinel byte (usually
91 newline) in *END, so the actual buffer must be one byte longer.
92 When ALLOW_NL is true, newlines may appear in the matching string.
93 If COUNT is non-NULL, increment *COUNT once for each newline processed.
94 Finally, if BACKREF is non-NULL set *BACKREF to indicate whether we
95 encountered a back-reference. The caller can use this to decide
96 whether to fall back on a backtracking matcher. */
97 extern char *dfaexec (struct dfa *d, char const *begin, char *end,
98 bool allow_nl, size_t *count, bool *backref);
100 /* Return a superset for D. The superset matches everything that D
101 matches, along with some other strings (though the latter should be
102 rare, for efficiency reasons). Return a null pointer if no useful
103 superset is available. */
104 extern struct dfa *dfasuperset (struct dfa const *d) _GL_ATTRIBUTE_PURE;
106 /* The DFA is likely to be fast. */
107 extern bool dfaisfast (struct dfa const *) _GL_ATTRIBUTE_PURE;
109 /* Free the storage held by the components of a struct dfa. */
110 extern void dfafree (struct dfa *);
112 /* Error handling. */
114 /* dfawarn() is called by the regexp routines whenever a regex is compiled
115 that likely doesn't do what the user wanted. It takes a single
116 argument, a NUL-terminated string describing the situation. The user
117 must supply a dfawarn. */
118 extern void dfawarn (const char *);
120 /* dfaerror() is called by the regexp routines whenever an error occurs. It
121 takes a single argument, a NUL-terminated string describing the error.
122 The user must supply a dfaerror. */
123 extern _Noreturn void dfaerror (const char *);