1 #include <stdio.h>
2 #include <ctype.h>
3 #include <errno.h>
4 #include <locale.h>
5 #include <regex.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <string.h>
9
10 /* lowercase chars mapped to uppercase */
11 static const char casetable[] = {
12 '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
13 '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
14 '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
15 '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
16 /* ' ' '!' '"' '#' '$' '%' '&' ''' */
17 '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
18 /* '(' ')' '*' '+' ',' '-' '.' '/' */
19 '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
20 /* '0' '1' '2' '3' '4' '5' '6' '7' */
21 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
22 /* '8' '9' ':' ';' '<' '=' '>' '?' */
23 '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
24 /* '@' 'A' 'B' 'C' 'D' 'E' 'F' 'G' */
25 '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
26 /* 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O' */
27 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
28 /* 'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' */
29 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
30 /* 'X' 'Y' 'Z' '[' '\' ']' '^' '_' */
31 '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
32 /* '`' 'a' 'b' 'c' 'd' 'e' 'f' 'g' */
33 '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
34 /* 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' */
35 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
36 /* 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' */
37 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
38 /* 'x' 'y' 'z' '{' '|' '}' '~' */
39 '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
40
41 /* Latin 1: */
42 '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
43 '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
44 '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
45 '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
46 '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
47 '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
48 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
49 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
50 '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
51 '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
52 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\327',
53 '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\337',
54 '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
55 '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
56 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
57 '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
58 };
59
60
61 static int
run_test(const char * pattern,struct re_registers * regs)62 run_test (const char *pattern, struct re_registers *regs)
63 {
64 static char text[] = "1111AAAA2222bbbb";
65
66 struct re_pattern_buffer pat;
67
68 const char *err;
69 int res;
70 int start2;
71
72 memset (&pat, '\0', sizeof (pat));
73 memset (regs, '\0', 2 * sizeof (regs[0]));
74 pat.allocated = 0; /* regex will allocate the buffer */
75 pat.fastmap = (char *) malloc (256);
76 if (pat.fastmap == NULL)
77 {
78 puts ("out of memory");
79 exit (1);
80 }
81
82 pat.translate = (unsigned char *) casetable;
83
84 err = re_compile_pattern (pattern, strlen (pattern), &pat);
85 if (err != NULL)
86 {
87 fprintf (stderr, "/%s/: %s\n", pattern, err);
88 exit (1);
89 }
90 res = re_search (&pat, text, strlen (text), 0, strlen (text), ®s[0]);
91 if (res < 0)
92 printf ("search 1: res = %d\n", res);
93 else
94 printf ("search 1: res = %d, start = %d, end = %d\n",
95 res, regs[0].start[0], regs[0].end[0]);
96
97 if (regs[0].end == NULL)
98 start2 = 8;
99 else
100 start2 = regs[0].end[0] + 1;
101 regs[1] = regs[0];
102 res = re_search (&pat, text, strlen (text), start2, strlen (text), ®s[1]);
103 if (res < 0)
104 printf ("search 2: res = %d\n", res);
105 else
106 printf ("search 2: res = %d, start = %d, end = %d\n",
107 res, regs[1].start[0], regs[1].end[0]);
108
109 return res < 0 ? 1 : 0;
110 }
111
112
113 static int
do_test(void)114 do_test (void)
115 {
116 static const char lower[] = "[[:lower:]]+";
117 static const char upper[] = "[[:upper:]]+";
118 struct re_registers regs[4];
119 int result = 0;
120
121 #define CHECK(exp) \
122 if (exp) { puts (#exp); result = 1; }
123
124 printf ("INFO: Checking C.\n");
125 setlocale (LC_ALL, "C");
126
127 (void) re_set_syntax (RE_SYNTAX_GNU_AWK);
128
129 result |= run_test (lower, regs);
130 result |= run_test (upper, ®s[2]);
131 if (! result)
132 {
133 CHECK (regs[0].start[0] != regs[2].start[0]);
134 CHECK (regs[0].end[0] != regs[2].end[0]);
135 CHECK (regs[1].start[0] != regs[3].start[0]);
136 CHECK (regs[1].end[0] != regs[3].end[0]);
137 }
138
139 printf ("INFO: Checking C.UTF-8.\n");
140 setlocale (LC_ALL, "C.UTF-8");
141
142 (void) re_set_syntax (RE_SYNTAX_GNU_AWK);
143
144 result |= run_test (lower, regs);
145 result |= run_test (upper, ®s[2]);
146 if (! result)
147 {
148 CHECK (regs[0].start[0] != regs[2].start[0]);
149 CHECK (regs[0].end[0] != regs[2].end[0]);
150 CHECK (regs[1].start[0] != regs[3].start[0]);
151 CHECK (regs[1].end[0] != regs[3].end[0]);
152 }
153
154 return result;
155 }
156
157 #define TEST_FUNCTION do_test ()
158 #include "../test-skeleton.c"
159