exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / regex-quote.h
blob017915ec013b2b9a3319bac3f4704eda1ce1a1fe
1 /* Construct a regular expression from a literal string.
2 Copyright (C) 1995, 2010-2024 Free Software Foundation, Inc.
3 Written by Bruno Haible <haible@clisp.cons.org>, 2010.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 #ifndef _REGEX_QUOTE_H
19 #define _REGEX_QUOTE_H
21 /* This file uses _GL_ATTRIBUTE_MALLOC, _GL_ATTRIBUTE_RETURNS_NONNULL. */
22 #if !_GL_CONFIG_H_INCLUDED
23 #error "Please include config.h first."
24 #endif
26 #include <stdlib.h>
28 /* Specifies a quotation task for converting a fixed string to a regular
29 expression pattern. */
30 struct regex_quote_spec
32 /* True if the regular expression pattern consists of multibyte characters
33 (in the encoding given by the LC_CTYPE category of the locale),
34 false if it consists of single bytes or UTF-8 characters. */
35 unsigned int /*bool*/ multibyte : 1;
36 /* True if the regular expression pattern shall match only entire lines. */
37 unsigned int /*bool*/ anchored : 1;
38 /* Set of characters that need to be escaped (all ASCII), as a
39 NUL-terminated string. */
40 char special[30 + 1];
44 /* Creates a quotation task that produces a POSIX regular expression, that is,
45 a pattern that can be compiled with regcomp().
46 CFLAGS can be 0 or REG_EXTENDED.
47 If it is 0, the result is a Basic Regular Expression (BRE)
48 <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_03>.
49 If it is REG_EXTENDED, the result is an Extended Regular Expression (ERE)
50 <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap09.html#tag_09_04>.
51 If ANCHORED is false, the regular expression will match substrings of lines.
52 If ANCHORED is true, it will match only complete lines, */
53 extern struct regex_quote_spec
54 regex_quote_spec_posix (int cflags, bool anchored);
56 /* Creates a quotation task that produces a regular expression that can be
57 compiled with the GNU API function re_compile_pattern().
58 SYNTAX describes the syntax of the regular expression (such as
59 RE_SYNTAX_POSIX_BASIC, RE_SYNTAX_POSIX_EXTENDED, RE_SYNTAX_EMACS, all
60 defined in <regex.h>). It must be the same value as 're_syntax_options'
61 at the moment of the re_compile_pattern() call.
62 If ANCHORED is false, the regular expression will match substrings of lines.
63 If ANCHORED is true, it will match only complete lines, */
64 extern struct regex_quote_spec
65 regex_quote_spec_gnu (unsigned long /*reg_syntax_t*/ syntax, bool anchored);
67 /* Creates a quotation task that produces a PCRE regular expression, that is,
68 a pattern that can be compiled with pcre_compile().
69 OPTIONS is the same value as the second argument passed to pcre_compile().
70 If ANCHORED is false, the regular expression will match substrings of lines.
71 If ANCHORED is true, it will match only complete lines, */
72 extern struct regex_quote_spec
73 regex_quote_spec_pcre (int options, bool anchored);
76 /* Returns the number of bytes needed for the quoted string. */
77 extern size_t
78 regex_quote_length (const char *string, const struct regex_quote_spec *spec);
80 /* Copies the quoted string to p and returns the incremented p.
81 There must be room for regex_quote_length (string, spec) + 1 bytes at p. */
82 extern char *
83 regex_quote_copy (char *restrict p,
84 const char *string, const struct regex_quote_spec *spec);
86 /* Returns the freshly allocated quoted string. */
87 extern char *
88 regex_quote (const char *string, const struct regex_quote_spec *spec)
89 _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_DEALLOC_FREE
90 _GL_ATTRIBUTE_RETURNS_NONNULL;
93 #endif /* _REGEX_QUOTE_H */