annotate lib/regex-quote.c @ 14409:ee532a615968

regex-quote: Fix creation of POSIX extended regular expressions. * lib/regex-quote.c (ere_special): Add grouping and alternation operators.
author Bruno Haible <bruno@clisp.org>
date Sun, 06 Mar 2011 14:25:49 +0100
parents 97fc9a21a8fb
children 0a972f366396
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.
14079
97fc9a21a8fb maint: update almost all copyright ranges to include 2011
Jim Meyering <meyering@redhat.com>
parents: 13728
diff changeset
2 Copyright (C) 1995, 2010-2011 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
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 size_t
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 regex_quote_length (const char *string, int cflags)
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 {
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 const char *special = (cflags != 0 ? ere_special : bre_special);
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 size_t length;
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 mbui_iterator_t iter;
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 length = 0;
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 for (mbui_init (iter, string); mbui_avail (iter); mbui_advance (iter))
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 {
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 /* We know that special contains only ASCII characters. */
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 if (mb_len (mbui_cur (iter)) == 1
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 && strchr (special, * mbui_cur_ptr (iter)))
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 length += 1;
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 length += mb_len (mbui_cur (iter));
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 }
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 return length;
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 }
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 /* Copies the quoted string to p and returns the incremented p.
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 There must be room for regex_quote_length (string, cflags) + 1 bytes at p.
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 */
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 char *
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 regex_quote_copy (char *p, const char *string, int cflags)
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 {
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 const char *special = (cflags != 0 ? ere_special : bre_special);
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 mbui_iterator_t iter;
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 for (mbui_init (iter, string); mbui_avail (iter); mbui_advance (iter))
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 {
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 /* We know that special contains only ASCII characters. */
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 if (mb_len (mbui_cur (iter)) == 1
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 && strchr (special, * mbui_cur_ptr (iter)))
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 *p++ = '\\';
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 memcpy (p, mbui_cur_ptr (iter), mb_len (mbui_cur (iter)));
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 p += mb_len (mbui_cur (iter));
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 }
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 return p;
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 }
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 /* Returns the freshly allocated quoted string. */
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 char *
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 regex_quote (const char *string, int cflags)
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 {
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78 size_t length = regex_quote_length (string, cflags);
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 char *result = XNMALLOC (length + 1, char);
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 char *p;
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 p = result;
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 p = regex_quote_copy (p, string, cflags);
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 *p = '\0';
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 return result;
e67e8c083d6d New module 'regex-quote'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 }