Debian Codegraph @

vendor/onigmo-source/sample/crnl.c

raw
1 /*
2 * crnl.c 2007/05/30 K.Kosako
3 *
4 * !!! You should enable USE_CRNL_AS_LINE_TERMINATOR. !!!
5 *
6 * USE_CRNL_AS_LINE_TERMINATOR config test program.
7 */
8 #include <stdio.h>
9 #include <string.h>
10 #include "onigmo.h"
11
12 /* #define USE_UNICODE_ALL_LINE_TERMINATORS */
13
14 static int nfail = 0;
15
16 static void result(int no, int from, int to,
17 int expected_from, int expected_to)
18 {
19 fprintf(stderr, "%3d: ", no);
20 if (from == expected_from && to == expected_to) {
21 fprintf(stderr, "Success\n");
22 }
23 else {
24 fprintf(stderr, "Fail: expected: (%d-%d), result: (%d-%d)\n",
25 expected_from, expected_to, from, to);
26
27 nfail++;
28 }
29 }
30
31 static int
32 x0(int no, char* pattern_arg, char* str_arg,
33 int start_offset, int expected_from, int expected_to, int backward)
34 {
35 int r;
36 unsigned char *start, *range, *end;
37 regex_t* reg;
38 OnigErrorInfo einfo;
39 OnigRegion *region;
40 UChar *pattern, *str;
41
42 pattern = (UChar* )pattern_arg;
43 str = (UChar* )str_arg;
44
45 r = onig_new(&reg, pattern, pattern + strlen((char* )pattern),
46 ONIG_OPTION_NEWLINE_CRLF, ONIG_ENCODING_UTF8, ONIG_SYNTAX_DEFAULT, &einfo);
47 if (r != ONIG_NORMAL) {
48 OnigUChar s[ONIG_MAX_ERROR_MESSAGE_LEN];
49 onig_error_code_to_str(s, r, &einfo);
50 fprintf(stderr, "ERROR: %s\n", s);
51 return -1;
52 }
53
54 region = onig_region_new();
55
56 end = str + strlen((char* )str);
57 if (backward) {
58 start = end + start_offset;
59 range = str;
60 }
61 else {
62 start = str + start_offset;
63 range = end;
64 }
65 r = onig_search(reg, str, end, start, range, region, ONIG_OPTION_NONE);
66 if (r >= 0) {
67 result(no, region->beg[0], region->end[0], expected_from, expected_to);
68 }
69 else if (r == ONIG_MISMATCH) {
70 result(no, r, -1, expected_from, expected_to);
71 }
72 else { /* error */
73 OnigUChar s[ONIG_MAX_ERROR_MESSAGE_LEN];
74 onig_error_code_to_str(s, r);
75 fprintf(stderr, "ERROR: %s\n", s);
76 return -1;
77 }
78
79 onig_region_free(region, 1 /* 1:free self, 0:free contents only */);
80 onig_free(reg);
81 return 0;
82 }
83
84 static int
85 x(int no, char* pattern_arg, char* str_arg,
86 int expected_from, int expected_to)
87 {
88 return x0(no, pattern_arg, str_arg, 0, expected_from, expected_to, 0);
89 }
90
91 static int
92 f0(int no, char* pattern_arg, char* str_arg, int start_offset, int backward)
93 {
94 return x0(no, pattern_arg, str_arg, start_offset, -1, -1, backward);
95 }
96
97 static int
98 f(int no, char* pattern_arg, char* str_arg)
99 {
100 return x(no, pattern_arg, str_arg, -1, -1);
101 }
102
103 extern int main(int argc, char* argv[])
104 {
105 x( 1, "", "\r\n", 0, 0);
106 /* x( 2, ".", "\r\n", 0, 1); */
107 f( 2, ".", "\r\n");
108 f( 3, "..", "\r\n");
109 x( 4, "^", "\r\n", 0, 0);
110 x( 5, "\\n^", "\r\nf", 1, 2);
111 x( 6, "\\n^a", "\r\na", 1, 3);
112 x( 7, "$", "\r\n", 0, 0);
113 x( 8, "T$", "T\r\n", 0, 1);
114 #ifdef USE_UNICODE_ALL_LINE_TERMINATORS
115 x( 9, "T$", "T\raT\r\n", 0, 1);
116 #else
117 x( 9, "T$", "T\raT\r\n", 3, 4);
118 #endif
119 x(10, "\\z", "\r\n", 2, 2);
120 f(11, "a\\z", "a\r\n");
121 x(12, "\\Z", "\r\n", 0, 0);
122 x(13, "\\Z", "\r\na", 3, 3);
123 x(14, "\\Z", "\r\n\r\n\n", 4, 4);
124 x(15, "\\Z", "\r\n\r\nX", 5, 5);
125 x(16, "a\\Z", "a\r\n", 0, 1);
126 x(17, "aaaaaaaaaaaaaaa\\Z", "aaaaaaaaaaaaaaa\r\n", 0, 15);
127 x(18, "a|$", "b\r\n", 1, 1);
128 #ifdef USE_UNICODE_ALL_LINE_TERMINATORS
129 x(19, "$|b", "\rb", 0, 0);
130 #else
131 x(19, "$|b", "\rb", 1, 2);
132 #endif
133 x(20, "a$|ab$", "\r\nab\r\n", 2, 4);
134
135 x(21, "a|\\Z", "b\r\n", 1, 1);
136 #ifdef USE_UNICODE_ALL_LINE_TERMINATORS
137 x(22, "\\Z|b", "\rb", 0, 0);
138 #else
139 x(22, "\\Z|b", "\rb", 1, 2);
140 #endif
141 x(23, "a\\Z|ab\\Z", "\r\nab\r\n", 2, 4);
142 x(24, "(?=a$).", "a\r\n", 0, 1);
143 #ifdef USE_UNICODE_ALL_LINE_TERMINATORS
144 x(25, "(?=a$).", "a\r", 0, 1);
145 f(26, "(?!a$)..", "a\r");
146 #else
147 f(25, "(?=a$).", "a\r");
148 x(26, "(?!a$)..", "a\r", 0, 2);
149 #endif
150 /* x(27, "(?<=a$).\\n", "a\r\n", 1, 3); */
151 x(27, "(?<=a$)\\r\\n", "a\r\n", 1, 3);
152 /* f(28, "(?<!a$).\\n", "a\r\n"); */
153 f(28, "(?<!a$)\\r\\n", "a\r\n");
154 x(29, "(?=a\\Z).", "a\r\n", 0, 1);
155 #ifdef USE_UNICODE_ALL_LINE_TERMINATORS
156 x(30, "(?=a\\Z).", "a\r", 0, 1);
157 f(31, "(?!a\\Z)..", "a\r");
158 #else
159 f(30, "(?=a\\Z).", "a\r");
160 x(31, "(?!a\\Z)..", "a\r", 0, 2);
161 #endif
162
163 x(32, ".*$", "aa\r\n", 0, 2);
164 #ifdef USE_UNICODE_ALL_LINE_TERMINATORS
165 x(33, ".*$", "aa\r", 0, 2);
166 #else
167 x(33, ".*$", "aa\r", 0, 3);
168 #endif
169 x(34, "\\R{3}", "\r\r\n\n", 0, 4);
170 x(35, "$", "\n", 0, 0);
171 x(36, "T$", "T\n", 0, 1);
172 x(37, "(?m).", "\r\n", 0, 1);
173 x(38, "(?m)..", "\r\n", 0, 2);
174 x0(39, "^", "\n.", 1, 1, 1, 0);
175 x0(40, "^", "\r\n.", 1, 2, 2, 0);
176 x0(41, "^", "\r\n.", 2, 2, 2, 0);
177 x0(42, "$", "\n\n", 1, 1, 1, 0);
178 x0(43, "$", "\r\n\n", 1, 2, 2, 0);
179 x0(44, "$", "\r\n\n", 2, 2, 2, 0);
180 #ifdef USE_UNICODE_ALL_LINE_TERMINATORS
181 x0(45, "^$", "\n\r", 1, 1, 1, 0);
182 #else
183 f0(45, "^$", "\n\r", 1, 0);
184 #endif
185 x0(46, "^$", "\n\r\n", 1, 1, 1, 0);
186 x0(47, "^$", "\r\n\n", 1, 2, 2, 0);
187 x0(48, "\\Z", "\r\n\n", 1, 2, 2, 0);
188 f0(49, ".(?=\\Z)", "\r\n", 1, 0);
189 x0(50, "(?=\\Z)", "\r\n", 1, 2, 2, 0);
190 x0(51, "(?<=^).", "\r\n.", 0, 2, 3, 0);
191 x0(52, "(?<=^).", "\r\n.", 1, 2, 3, 0);
192 x0(53, "(?<=^).", "\r\n.", 2, 2, 3, 0);
193 x0(54, "^a", "\r\na", 0, 2, 3, 0);
194 x0(55, "^a", "\r\na", 1, 2, 3, 0);
195 x0(56, "(?m)$.{1,2}a", "\r\na", 0, 0, 3, 0);
196 f0(57, "(?m)$.{1,2}a", "\r\na", 1, 0);
197 x0(58, ".*b", "\r\naaab\r\n", 1, 2, 6, 0);
198
199 /* backward search */
200 /* x0(59, "$", "\n\n", 0, 1, 1, 1); */ /* BUG? */
201 x0(60, "$", "\n\n", -1, 1, 1, 1);
202 x0(61, "$", "\n\r\n", -1, 1, 1, 1);
203 x0(62, "$", "\n\r\n", -2, 1, 1, 1);
204 x0(63, "^$", "\n\r\n", -1, 1, 1, 1);
205 x0(64, "^$", "\n\r\n", 0, 1, 1, 1);
206 x0(65, "^$", "\r\n\n", 0, 2, 2, 1);
207 x0(66, "^a", "\r\na", 0, 2, 3, 1);
208 /* x0(67, "^a", "\r\na", -1, 2, 3, 1); */
209 f0(67, "^a", "\r\na", -1, 1); /* This should not match after the fix of Issue #22. */
210 f0(68, "^a", "\r\na", -2, 1);
211
212 onig_end();
213
214 if (nfail > 0) {
215 fprintf(stderr, "\n");
216 fprintf(stderr, "!!! You have to enable USE_CRNL_AS_LINE_TERMINATOR\n");
217 fprintf(stderr, "!!! in regenc.h for this test program.\n");
218 fprintf(stderr, "\n");
219 }
220
221 return nfail;
222 }