1 | /* Test re.translate != NULL. |
2 | Copyright (C) 2004-2022 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library 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 GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <ctype.h> |
20 | #include <locale.h> |
21 | #include <regex.h> |
22 | #include <stdio.h> |
23 | #include <string.h> |
24 | |
25 | int |
26 | main (void) |
27 | { |
28 | struct re_pattern_buffer re; |
29 | char trans[256]; |
30 | int i, result = 0; |
31 | const char *s; |
32 | |
33 | setlocale (LC_ALL, "de_DE.ISO-8859-1" ); |
34 | |
35 | for (i = 0; i < 256; ++i) |
36 | trans[i] = tolower (i); |
37 | |
38 | re_set_syntax (RE_SYNTAX_POSIX_EGREP); |
39 | |
40 | memset (&re, 0, sizeof (re)); |
41 | re.translate = (unsigned char *) trans; |
42 | s = re_compile_pattern (pattern: "\\W" , length: 2, buffer: &re); |
43 | |
44 | if (s != NULL) |
45 | { |
46 | printf (format: "failed to compile pattern \"\\W\": %s\n" , s); |
47 | result = 1; |
48 | } |
49 | else |
50 | { |
51 | int ret = re_search (buffer: &re, String: "abc.de" , length: 6, start: 0, range: 6, NULL); |
52 | if (ret != 3) |
53 | { |
54 | printf (format: "1st re_search returned %d\n" , ret); |
55 | result = 1; |
56 | } |
57 | |
58 | ret = re_search (buffer: &re, String: "\xc4\xd6\xae\xf7" , length: 4, start: 0, range: 4, NULL); |
59 | if (ret != 2) |
60 | { |
61 | printf (format: "2nd re_search returned %d\n" , ret); |
62 | result = 1; |
63 | } |
64 | re.translate = NULL; |
65 | regfree (preg: &re); |
66 | } |
67 | |
68 | memset (&re, 0, sizeof (re)); |
69 | re.translate = (unsigned char *) trans; |
70 | s = re_compile_pattern (pattern: "\\w" , length: 2, buffer: &re); |
71 | |
72 | if (s != NULL) |
73 | { |
74 | printf (format: "failed to compile pattern \"\\w\": %s\n" , s); |
75 | result = 1; |
76 | } |
77 | else |
78 | { |
79 | int ret = re_search (buffer: &re, String: ".,!abc" , length: 6, start: 0, range: 6, NULL); |
80 | if (ret != 3) |
81 | { |
82 | printf (format: "3rd re_search returned %d\n" , ret); |
83 | result = 1; |
84 | } |
85 | |
86 | ret = re_search (buffer: &re, String: "\xae\xf7\xc4\xd6" , length: 4, start: 0, range: 4, NULL); |
87 | if (ret != 2) |
88 | { |
89 | printf (format: "4th re_search returned %d\n" , ret); |
90 | result = 1; |
91 | } |
92 | re.translate = NULL; |
93 | regfree (preg: &re); |
94 | } |
95 | |
96 | memset (&re, 0, sizeof (re)); |
97 | re.translate = (unsigned char *) trans; |
98 | s = re_compile_pattern (pattern: "[[:DIGIT:]]" , length: 11, buffer: &re); |
99 | if (s == NULL) |
100 | { |
101 | puts (s: "compilation of \"[[:DIGIT:]]\" pattern unexpectedly succeeded: " |
102 | "length 11" ); |
103 | result = 1; |
104 | } |
105 | |
106 | memset (&re, 0, sizeof (re)); |
107 | re.translate = (unsigned char *) trans; |
108 | s = re_compile_pattern (pattern: "[[:DIGIT:]]" , length: 2, buffer: &re); |
109 | if (s == NULL) |
110 | { |
111 | puts (s: "compilation of \"[[:DIGIT:]]\" pattern unexpectedly succeeded: " |
112 | "length 2" ); |
113 | result = 1; |
114 | } |
115 | |
116 | return result; |
117 | } |
118 | |