1 /* Test for UTF-8 regular expression optimizations.
2 Copyright (C) 2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Jakub Jelinek <jakub@redhat.com>, 2003.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 #include <sys/types.h>
29 #define RE_NO_INTERNAL_PROTOTYPES 1
30 #include "regex_internal.h"
39 /* \xc3\x84 LATIN CAPITAL LETTER A WITH DIAERESIS
40 \xc3\x96 LATIN CAPITAL LETTER O WITH DIAERESIS
41 \xc3\xa4 LATIN SMALL LETTER A WITH DIAERESIS
42 \xc3\xb6 LATIN SMALL LETTER O WITH DIAERESIS
43 \xe2\x80\x94 EM DASH */
44 /* Should be optimized. */
45 {RE_SYNTAX_POSIX_BASIC, "foo", "b\xc3\xa4rfoob\xc3\xa4z", 4, 1},
46 {RE_SYNTAX_POSIX_BASIC, "^x\\|xy*z$", "\xc3\xb6xyyz", 2, 1},
47 {RE_SYNTAX_POSIX_BASIC, "^x\\\\y\\{6\\}z\\+", "x\\yyyyyyzz\xc3\xb6", 0, 1},
48 {RE_SYNTAX_POSIX_BASIC, "^x\\\\y\\{2,36\\}z\\+", "x\\yzz\xc3\xb6", -1, 1},
49 {RE_SYNTAX_POSIX_BASIC, "^x\\\\y\\{,3\\}z\\+", "x\\yyyzz\xc3\xb6", 0, 1},
50 {RE_SYNTAX_POSIX_BASIC, "x[C]y", "axCy", 1, 1},
51 {RE_SYNTAX_POSIX_BASIC, "x[ABC]y", "axCy", 1, 1},
52 {RE_SYNTAX_POSIX_BASIC, "\\`x\\|z\\'", "x\xe2\x80\x94", 0, 1},
53 {RE_SYNTAX_POSIX_BASIC, "\\(xy\\)z\\1a\\1", "\xe2\x80\x94xyzxyaxy\xc3\x84", 3, 1},
54 {RE_SYNTAX_POSIX_BASIC, "xy\\?z", "\xc3\x84xz\xc3\xb6", 2, 1},
55 {RE_SYNTAX_POSIX_EXTENDED, "foo", "b\xc3\xa4rfoob\xc3\xa4z", 4, 1},
56 {RE_SYNTAX_POSIX_EXTENDED, "^x|xy*z$", "\xc3\xb6xyyz", 2, 1},
57 {RE_SYNTAX_POSIX_EXTENDED, "^x\\\\y{6}z+", "x\\yyyyyyzz\xc3\xb6", 0, 1},
58 {RE_SYNTAX_POSIX_EXTENDED, "^x\\\\y{2,36}z+", "x\\yzz\xc3\xb6", -1, 1},
59 {RE_SYNTAX_POSIX_EXTENDED, "^x\\\\y{,3}z+", "x\\yyyzz\xc3\xb6", 0, 1},
60 {RE_SYNTAX_POSIX_EXTENDED, "x[C]y", "axCy", 1, 1},
61 {RE_SYNTAX_POSIX_EXTENDED, "x[ABC]y", "axCy", 1, 1},
62 {RE_SYNTAX_POSIX_EXTENDED, "\\`x|z\\'", "x\xe2\x80\x94", 0, 1},
63 {RE_SYNTAX_POSIX_EXTENDED, "(xy)z\\1a\\1", "\xe2\x80\x94xyzxyaxy\xc3\x84", 3, 1},
64 {RE_SYNTAX_POSIX_EXTENDED, "xy?z", "\xc3\x84xz\xc3\xb6", 2, 1},
65 /* Should not be optimized. */
66 {RE_SYNTAX_POSIX_BASIC, "x.y", "ax\xe2\x80\x94yz", 1, 0},
67 {RE_SYNTAX_POSIX_BASIC, "x\xc3\x96*y", "ax\xc3\x96\xc3\x96yz", 1, 0},
68 {RE_SYNTAX_POSIX_BASIC, "x[\xc3\x84\xc3\xa4]y", "ax\xc3\xa4y", 1, 0},
69 {RE_SYNTAX_POSIX_BASIC, "x[A-Z,]y", "axCy", 1, 0},
70 {RE_SYNTAX_POSIX_BASIC, "x[^y]z", "ax\xe2\x80\x94z", 1, 0},
71 {RE_SYNTAX_POSIX_BASIC, "x[[:alnum:]]z", "ax\xc3\x96z", 1, 0},
72 {RE_SYNTAX_POSIX_BASIC, "x[[=A=]]z", "axAz", 1, 0},
73 {RE_SYNTAX_POSIX_BASIC, "x[[=\xc3\x84=]]z", "ax\xc3\x84z", 1, 0},
74 {RE_SYNTAX_POSIX_BASIC, "\\<g", "\xe2\x80\x94g", 3, 0},
75 {RE_SYNTAX_POSIX_BASIC, "\\bg\\b", "\xe2\x80\x94g", 3, 0},
76 {RE_SYNTAX_POSIX_BASIC, "\\Bg\\B", "\xc3\xa4g\xc3\xa4", 2, 0},
77 {RE_SYNTAX_POSIX_BASIC, "a\\wz", "a\xc3\x84z", 0, 0},
78 {RE_SYNTAX_POSIX_BASIC, "x\\Wz", "\xc3\x96x\xe2\x80\x94z", 2, 0},
79 {RE_SYNTAX_POSIX_EXTENDED, "x.y", "ax\xe2\x80\x94yz", 1, 0},
80 {RE_SYNTAX_POSIX_EXTENDED, "x\xc3\x96*y", "ax\xc3\x96\xc3\x96yz", 1, 0},
81 {RE_SYNTAX_POSIX_EXTENDED, "x[\xc3\x84\xc3\xa4]y", "ax\xc3\xa4y", 1, 0},
82 {RE_SYNTAX_POSIX_EXTENDED, "x[A-Z,]y", "axCy", 1, 0},
83 {RE_SYNTAX_POSIX_EXTENDED, "x[^y]z", "ax\xe2\x80\x94z", 1, 0},
84 {RE_SYNTAX_POSIX_EXTENDED, "x[[:alnum:]]z", "ax\xc3\x96z", 1, 0},
85 {RE_SYNTAX_POSIX_EXTENDED, "x[[=A=]]z", "axAz", 1, 0},
86 {RE_SYNTAX_POSIX_EXTENDED, "x[[=\xc3\x84=]]z", "ax\xc3\x84z", 1, 0},
87 {RE_SYNTAX_POSIX_EXTENDED, "\\<g", "\xe2\x80\x94g", 3, 0},
88 {RE_SYNTAX_POSIX_EXTENDED, "\\bg\\b", "\xe2\x80\x94g", 3, 0},
89 {RE_SYNTAX_POSIX_EXTENDED, "\\Bg\\B", "\xc3\xa4g\xc3\xa4", 2, 0},
90 {RE_SYNTAX_POSIX_EXTENDED, "a\\wz", "a\xc3\x84z", 0, 0},
91 {RE_SYNTAX_POSIX_EXTENDED, "x\\Wz", "\xc3\x96x\xe2\x80\x94z", 2, 0},
97 struct re_pattern_buffer regbuf;
104 setlocale (LC_ALL, "de_DE.UTF-8");
105 for (i = 0; i < sizeof (tests) / sizeof (tests[0]); ++i)
109 re_set_syntax (tests[i].syntax);
110 memset (®buf, '\0', sizeof (regbuf));
111 err = re_compile_pattern (tests[i].pattern, strlen (tests[i].pattern),
115 printf ("re_compile_pattern failed: %s\n", err);
120 /* Check if re_search will be done as multi-byte or single-byte. */
121 optimized = ((re_dfa_t *) regbuf.buffer)->mb_cur_max == 1;
122 if (optimized != tests[i].optimize)
124 printf ("pattern %zd %soptimized while it should%s be\n",
125 i, optimized ? "" : "not ", tests[i].optimize ? "" : " not");
129 res = re_search (®buf, tests[i].string, strlen (tests[i].string), 0,
130 strlen (tests[i].string), NULL);
131 if (res != tests[i].res)
133 printf ("re_search %zd failed: %d\n", i, res);
140 re_set_syntax (tests[i].syntax | RE_ICASE);
141 memset (®buf, '\0', sizeof (regbuf));
142 err = re_compile_pattern (tests[i].pattern, strlen (tests[i].pattern),
146 printf ("re_compile_pattern failed: %s\n", err);
151 /* Check if re_search will be done as multi-byte or single-byte. */
152 optimized = ((re_dfa_t *) regbuf.buffer)->mb_cur_max == 1;
155 printf ("pattern %zd optimized while it should not be when case insensitive\n",
160 res = re_search (®buf, tests[i].string, strlen (tests[i].string), 0,
161 strlen (tests[i].string), NULL);
162 if (res != tests[i].res)
164 printf ("re_search %zd failed: %d\n", i, res);