annotate lib/regex-quote.c @ 17426:90f3d53e01f5

sig2str: port to C++ * lib/sig2str.h (sig2str, str2sig): Declare as extern "C". Reported by Daniel J Sebald in <http://lists.gnu.org/archive/html/bug-gnulib/2013-06/msg00000.html>.
author Paul Eggert <eggert@cs.ucla.edu>
date Sun, 02 Jun 2013 11:52:41 -0700
parents e542fd46ad6f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
13728
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Construct a regular expression from a literal string.
17249
e542fd46ad6f maint: update all copyright year number ranges
Eric Blake <eblake@redhat.com>
parents: 16201
diff changeset
2 Copyright (C) 1995, 2010-2013 Free Software Foundation, Inc.
13728
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <haible@clisp.cons.org>, 2010.
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 3 of the License, or
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 (at your option) any later version.
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18 #include <config.h>
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20 /* Specification. */
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #include "regex-quote.h"
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include <string.h>
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include "mbuiter.h"
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include "xalloc.h"
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 /* Characters that are special in a BRE. */
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 static const char bre_special[] = "$^.*[]\\";
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 /* Characters that are special in an ERE. */
14409
ee532a615968 regex-quote: Fix creation of POSIX extended regular expressions.
Bruno Haible <bruno@clisp.org>
parents: 14079
diff changeset
32 static const char ere_special[] = "$^.*[]\\+?{}()|";
13728
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33
14410
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
34 struct regex_quote_spec
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
35 regex_quote_spec_posix (int cflags, bool anchored)
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
36 {
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
37 struct regex_quote_spec result;
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
38
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
39 strcpy (result.special, cflags != 0 ? ere_special : bre_special);
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
40 result.multibyte = true;
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
41 result.anchored = anchored;
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
42
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
43 return result;
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
44 }
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
45
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
46 /* Syntax bit values, defined in GNU <regex.h>. We don't include it here,
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
47 otherwise this module would need to depend on gnulib module 'regex'. */
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
48 #define RE_BK_PLUS_QM 0x00000002
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
49 #define RE_INTERVALS 0x00000200
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
50 #define RE_LIMITED_OPS 0x00000400
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
51 #define RE_NEWLINE_ALT 0x00000800
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
52 #define RE_NO_BK_BRACES 0x00001000
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
53 #define RE_NO_BK_PARENS 0x00002000
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
54 #define RE_NO_BK_VBAR 0x00008000
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
55
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
56 struct regex_quote_spec
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
57 regex_quote_spec_gnu (unsigned long /*reg_syntax_t*/ syntax, bool anchored)
13728
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 {
14410
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
59 struct regex_quote_spec result;
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
60 char *p;
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
61
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
62 p = result.special;
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
63 memcpy (p, bre_special, sizeof (bre_special) - 1);
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
64 p += sizeof (bre_special) - 1;
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
65 if ((syntax & RE_LIMITED_OPS) == 0 && (syntax & RE_BK_PLUS_QM) == 0)
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
66 {
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
67 *p++ = '+';
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
68 *p++ = '?';
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
69 }
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
70 if ((syntax & RE_INTERVALS) != 0 && (syntax & RE_NO_BK_BRACES) != 0)
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
71 {
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
72 *p++ = '{';
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
73 *p++ = '}';
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
74 }
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
75 if ((syntax & RE_NO_BK_PARENS) != 0)
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
76 {
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
77 *p++ = '(';
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
78 *p++ = ')';
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
79 }
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
80 if ((syntax & RE_LIMITED_OPS) == 0 && (syntax & RE_NO_BK_VBAR) != 0)
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
81 *p++ = '|';
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
82 if ((syntax & RE_NEWLINE_ALT) != 0)
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
83 *p++ = '\n';
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
84 *p = '\0';
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
85
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
86 result.multibyte = true;
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
87 result.anchored = anchored;
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
88
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
89 return result;
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
90 }
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
91
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
92 /* Characters that are special in a PCRE. */
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
93 static const char pcre_special[] = "$^.*[]\\+?{}()|";
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
94
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
95 /* Options bit values, defined in <pcre.h>. We don't include it here, because
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
96 it is not a standard header. */
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
97 #define PCRE_ANCHORED 0x00000010
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
98 #define PCRE_EXTENDED 0x00000008
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
99
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
100 struct regex_quote_spec
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
101 regex_quote_spec_pcre (int options, bool anchored)
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
102 {
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
103 struct regex_quote_spec result;
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
104 char *p;
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
105
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
106 p = result.special;
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
107 memcpy (p, bre_special, sizeof (pcre_special) - 1);
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
108 p += sizeof (pcre_special) - 1;
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
109 if (options & PCRE_EXTENDED)
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
110 {
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
111 *p++ = ' ';
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
112 *p++ = '\t';
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
113 *p++ = '\n';
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
114 *p++ = '\v';
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
115 *p++ = '\f';
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
116 *p++ = '\r';
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
117 *p++ = '#';
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
118 }
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
119 *p = '\0';
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
120
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
121 /* PCRE regular expressions consist of UTF-8 characters of options contains
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
122 PCRE_UTF8 and of single bytes otherwise. */
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
123 result.multibyte = false;
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
124 /* If options contains PCRE_ANCHORED, the anchoring is implicit. */
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
125 result.anchored = (options & PCRE_ANCHORED ? 0 : anchored);
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
126
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
127 return result;
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
128 }
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
129
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
130 size_t
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
131 regex_quote_length (const char *string, const struct regex_quote_spec *spec)
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
132 {
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
133 const char *special = spec->special;
13728
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134 size_t length;
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
136 length = 0;
14410
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
137 if (spec->anchored)
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
138 length += 2; /* for '^' at the beginning and '$' at the end */
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
139 if (spec->multibyte)
13728
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140 {
14410
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
141 mbui_iterator_t iter;
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
142
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
143 for (mbui_init (iter, string); mbui_avail (iter); mbui_advance (iter))
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
144 {
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
145 /* We know that special contains only ASCII characters. */
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
146 if (mb_len (mbui_cur (iter)) == 1
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
147 && strchr (special, * mbui_cur_ptr (iter)))
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
148 length += 1;
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
149 length += mb_len (mbui_cur (iter));
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
150 }
13728
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
151 }
14410
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
152 else
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
153 {
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
154 const char *iter;
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
155
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
156 for (iter = string; *iter != '\0'; iter++)
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
157 {
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
158 if (strchr (special, *iter))
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
159 length += 1;
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
160 length += 1;
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
161 }
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
162 }
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
163
13728
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
164 return length;
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
165 }
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
166
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
167 char *
14410
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
168 regex_quote_copy (char *p, const char *string, const struct regex_quote_spec *spec)
13728
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
169 {
14410
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
170 const char *special = spec->special;
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
171
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
172 if (spec->anchored)
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
173 *p++ = '^';
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
174 if (spec->multibyte)
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
175 {
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
176 mbui_iterator_t iter;
13728
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
177
14410
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
178 for (mbui_init (iter, string); mbui_avail (iter); mbui_advance (iter))
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
179 {
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
180 /* We know that special contains only ASCII characters. */
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
181 if (mb_len (mbui_cur (iter)) == 1
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
182 && strchr (special, * mbui_cur_ptr (iter)))
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
183 *p++ = '\\';
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
184 memcpy (p, mbui_cur_ptr (iter), mb_len (mbui_cur (iter)));
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
185 p += mb_len (mbui_cur (iter));
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
186 }
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
187 }
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
188 else
13728
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
189 {
14410
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
190 const char *iter;
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
191
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
192 for (iter = string; *iter != '\0'; iter++)
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
193 {
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
194 if (strchr (special, *iter))
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
195 *p++ = '\\';
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
196 *p++ = *iter++;
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
197 }
13728
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
198 }
14410
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
199 if (spec->anchored)
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
200 *p++ = '$';
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
201
13728
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
202 return p;
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
203 }
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
204
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
205 char *
14410
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
206 regex_quote (const char *string, const struct regex_quote_spec *spec)
13728
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
207 {
14410
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
208 size_t length = regex_quote_length (string, spec);
13728
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
209 char *result = XNMALLOC (length + 1, char);
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
210 char *p;
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
211
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
212 p = result;
14410
0a972f366396 regex-quote: New API.
Bruno Haible <bruno@clisp.org>
parents: 14409
diff changeset
213 p = regex_quote_copy (p, string, spec);
13728
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
214 *p = '\0';
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
215 return result;
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
216 }