Debian Codegraph @

vendor/onigmo-source/testconvu.rb

raw
1 #!/usr/local/bin/ruby
2 # testconvu.rb
3 # Copyright (C) 2004-2006 K.Kosako (sndgk393 AT ybb DOT ne DOT jp)
4
5 WINDOWS = (ARGV.size > 0 && /^-win/i =~ ARGV[0])
6 ARGV.shift if WINDOWS
7
8 BIG_ENDIAN = 1
9 LITTLE_ENDIAN = 2
10
11 def eucjp_char_pos(s, byte_pos)
12 pos = 0
13 i = 0
14 while (i < byte_pos)
15 x = s[i].ord
16 if ((x >= 0xa1 && x <= 0xfe) || x == 0x8e)
17 i += 2
18 elsif (x == 0x8f)
19 i += 3
20 else
21 i += 1
22 end
23 pos += 1
24 end
25 return pos
26 end
27
28 def utf16_byte_pos(endian, s, char_pos)
29 i = 0
30 while (char_pos > 0)
31 x = (endian == BIG_ENDIAN ? s[i] : s[i+1]).ord
32 if (x >= 0xd8 && x <= 0xdb)
33 i += 4
34 else
35 i += 2
36 end
37 char_pos -= 1
38 end
39 return i
40 end
41
42 def s_escape(s)
43 q = ''
44 s.each_byte { |b|
45 if (b < 0x20 || b >= 0x7f || b == 0x22 || b == 0x5c)
46 q << sprintf("\\%03o", b)
47 else
48 q << b.chr
49 end
50 }
51 q
52 end
53
54 def conv_to_utf16(endian, s)
55 begin
56 if (endian == BIG_ENDIAN)
57 q = s.encode('UTF-16BE', 'EUC-JP')
58 else
59 q = s.encode('UTF-16LE', 'EUC-JP')
60 end
61 q.force_encoding('ASCII-8BIT')
62 rescue Encoding::InvalidByteSequenceError
63 q = 'Invalid character'
64 rescue Encoding::UndefinedConversionError
65 STDERR.printf("Encoding::UndefinedConversionError: [%s]\n", s)
66 return ''
67 end
68
69 q << "\000\000"
70 s_escape(q)
71 end
72
73 def conv_reg(endian, s)
74 s = s.gsub(/\\([0-7]{2,3})\\([0-7]{2,3})/) {
75 $1.to_i(8).chr + $2.to_i(8).chr
76 }
77
78 s = s.gsub(/\\x([0-9A-Fa-f]{2})\\x([0-9A-Fa-f]{2})/) {
79 $1.to_i(16).chr + $2.to_i(16).chr
80 }
81
82 if (endian == BIG_ENDIAN)
83 s = s.gsub(/(\\[0-7]{2,3})/) { "\\000" + $1 }
84 s = s.gsub(/(\\x[0-9A-Fa-f]{2})/) { "\\x00" + $1 }
85 else
86 s = s.gsub(/(\\[0-7]{2,3})/) { $1 + "\\000" }
87 s = s.gsub(/(\\x[0-9A-Fa-f]{2})/) { $1 + "\\x00" }
88 end
89
90 s = s.gsub(/\\/, '\\\\') #'
91
92 if (WINDOWS)
93 s = s.gsub(/\?\?/, '?\\?') # escape ANSI trigraph
94 end
95 conv_to_utf16(endian, s)
96 end
97
98 def conv_str(endian, s, from, to)
99 if (s[0] == ?')
100 s = s[1..-2]
101 q = s.gsub(/\\/, '\\\\') #'
102 else
103 q = s[1..-2]
104 q.gsub!(/\\n/, "\x0a")
105 q.gsub!(/\\t/, "\x09")
106 q.gsub!(/\\v/, "\x0b")
107 q.gsub!(/\\r/, "\x0d")
108 q.gsub!(/\\f/, "\x0c")
109 q.gsub!(/\\a/, "\x07")
110 q.gsub!(/\\e/, "\x1b")
111
112 q.gsub!(/\\([0-7]{2,3})/) { $1.to_i(8).chr }
113 q.gsub!(/\\x([0-9A-Fa-f]{2})/) { $1.to_i(16).chr }
114 end
115
116 q.force_encoding('ASCII-8BIT')
117 from = from.to_i
118 to = to.to_i
119 eucjp_from = eucjp_char_pos(q, from)
120 eucjp_to = eucjp_char_pos(q, to)
121
122 s = conv_to_utf16(endian, q)
123
124 from = utf16_byte_pos(endian, s, eucjp_from)
125 to = utf16_byte_pos(endian, s, eucjp_to)
126 return s, from, to
127 end
128
129 print(<<"EOS")
130 /*
131 * This program was generated by testconv.rb.
132 */
133 #include<stdio.h>
134
135 #ifdef POSIX_TEST
136 #include "onigmoposix.h"
137 #else
138 #include "onigmo.h"
139 #endif
140
141 static int nsucc = 0;
142 static int nfail = 0;
143 static int nerror = 0;
144
145 static FILE* err_file;
146
147 #ifndef POSIX_TEST
148 static OnigRegion* region;
149 static OnigEncoding ENC;
150 #endif
151
152 #define ulen(p) onigenc_str_bytelen_null(ENC, (UChar* )p)
153
154 static void uconv(char* from, char* to, int len)
155 {
156 int i;
157 unsigned char c;
158 char *q;
159
160 q = to;
161
162 for (i = 0; i < len; i += 2) {
163 c = (unsigned char )from[i];
164 if (c == 0) {
165 c = (unsigned char )from[i+1];
166 if (c < 0x20 || c >= 0x7f || c == 0x5c || c == 0x22) {
167 sprintf(q, "\\\\%03o", c);
168 q += 4;
169 }
170 else {
171 sprintf(q, "%c", c);
172 q++;
173 }
174 }
175 else {
176 sprintf(q, "\\\\%03o", c);
177 q += 4;
178 c = (unsigned char )from[i+1];
179 sprintf(q, "\\\\%03o", c);
180 q += 4;
181 }
182 }
183
184 *q = 0;
185 }
186
187 static void xx(char* pattern, char* str, int from, int to, int mem, int not)
188 {
189 int r;
190 char cpat[4000], cstr[4000];
191
192 #ifdef POSIX_TEST
193 regex_t reg;
194 char buf[200];
195 regmatch_t pmatch[20];
196
197 uconv(pattern, cpat, ulen(pattern));
198 uconv(str, cstr, ulen(str));
199
200 r = regcomp(&reg, pattern, REG_EXTENDED | REG_NEWLINE);
201 if (r) {
202 regerror(r, &reg, buf, sizeof(buf));
203 fprintf(err_file, "ERROR: %s\\n", buf);
204 nerror++;
205 return ;
206 }
207
208 r = regexec(&reg, str, reg.re_nsub + 1, pmatch, 0);
209 if (r != 0 && r != REG_NOMATCH) {
210 regerror(r, &reg, buf, sizeof(buf));
211 fprintf(err_file, "ERROR: %s\\n", buf);
212 nerror++;
213 return ;
214 }
215
216 if (r == REG_NOMATCH) {
217 if (not) {
218 fprintf(stdout, "OK(N): /%s/ '%s'\\n", cpat, cstr);
219 nsucc++;
220 }
221 else {
222 fprintf(stdout, "FAIL: /%s/ '%s'\\n", cpat, cstr);
223 nfail++;
224 }
225 }
226 else {
227 if (not) {
228 fprintf(stdout, "FAIL(N): /%s/ '%s'\\n", cpat, cstr);
229 nfail++;
230 }
231 else {
232 if (pmatch[mem].rm_so == from && pmatch[mem].rm_eo == to) {
233 fprintf(stdout, "OK: /%s/ '%s'\\n", cpat, cstr);
234 nsucc++;
235 }
236 else {
237 fprintf(stdout, "FAIL: /%s/ '%s' %d-%d : %d-%d\\n", cpat, cstr,
238 (int)from, (int)to, (int)pmatch[mem].rm_so, (int)pmatch[mem].rm_eo);
239 nfail++;
240 }
241 }
242 }
243 regfree(&reg);
244
245 #else
246 regex_t* reg;
247 OnigCompileInfo ci;
248 OnigErrorInfo einfo;
249 OnigSyntaxType syn = *ONIG_SYNTAX_DEFAULT;
250
251 /* ONIG_OPTION_OFF(syn.options, ONIG_OPTION_ASCII_RANGE); */
252
253 uconv(pattern, cpat, ulen(pattern));
254 uconv(str, cstr, ulen(str));
255
256 #if 0
257 r = onig_new(&reg, (UChar* )pattern, (UChar* )(pattern + ulen(pattern)),
258 ONIG_OPTION_DEFAULT, ENC, &syn, &einfo);
259 #else
260 ci.num_of_elements = 5;
261 ci.pattern_enc = ENC;
262 ci.target_enc = ENC;
263 ci.syntax = &syn;
264 ci.option = ONIG_OPTION_DEFAULT;
265 ci.case_fold_flag = ONIGENC_CASE_FOLD_DEFAULT;
266
267 r = onig_new_deluxe(&reg, (UChar* )pattern,
268 (UChar* )(pattern + ulen(pattern)),
269 &ci, &einfo);
270 #endif
271
272 if (r) {
273 char s[ONIG_MAX_ERROR_MESSAGE_LEN];
274 onig_error_code_to_str((UChar* )s, r, &einfo);
275 fprintf(err_file, "ERROR: %s\\n", s);
276 nerror++;
277 return ;
278 }
279
280 r = onig_search(reg, (UChar* )str, (UChar* )(str + ulen(str)),
281 (UChar* )str, (UChar* )(str + ulen(str)),
282 region, ONIG_OPTION_NONE);
283 if (r < ONIG_MISMATCH) {
284 char s[ONIG_MAX_ERROR_MESSAGE_LEN];
285 onig_error_code_to_str((UChar* )s, r);
286 fprintf(err_file, "ERROR: %s\\n", s);
287 nerror++;
288 return ;
289 }
290
291 if (r == ONIG_MISMATCH) {
292 if (not) {
293 fprintf(stdout, "OK(N): /%s/ '%s'\\n", cpat, cstr);
294 nsucc++;
295 }
296 else {
297 fprintf(stdout, "FAIL: /%s/ '%s'\\n", cpat, cstr);
298 nfail++;
299 }
300 }
301 else {
302 if (not) {
303 fprintf(stdout, "FAIL(N): /%s/ '%s'\\n", cpat, cstr);
304 nfail++;
305 }
306 else {
307 if (region->beg[mem] == from && region->end[mem] == to) {
308 fprintf(stdout, "OK: /%s/ '%s'\\n", cpat, cstr);
309 nsucc++;
310 }
311 else {
312 fprintf(stdout, "FAIL: /%s/ '%s' %d-%d : %d-%d\\n", cpat, cstr,
313 (int)from, (int)to, (int)region->beg[mem], (int)region->end[mem]);
314 nfail++;
315 }
316 }
317 }
318 onig_free(reg);
319 #endif
320 }
321
322 static void x2(char* pattern, char* str, int from, int to)
323 {
324 xx(pattern, str, from, to, 0, 0);
325 }
326
327 static void x3(char* pattern, char* str, int from, int to, int mem)
328 {
329 xx(pattern, str, from, to, mem, 0);
330 }
331
332 static void n(char* pattern, char* str)
333 {
334 xx(pattern, str, 0, 0, 0, 1);
335 }
336
337 extern int main(int argc, char* argv[])
338 {
339 err_file = stdout;
340
341 #ifndef POSIX_TEST
342 region = onig_region_new();
343 #endif
344 EOS
345
346
347 PAT = '\\/([^\\\\\\/]*(?:\\\\.[^\\\\\\/]*)*)\\/'
348 CM = /\s*,\s*/
349 RX2 = %r{\Ax\(#{PAT}#{CM}('[^']*'|"[^"]*")#{CM}(\S+)#{CM}(\S+)\)}
350 RI2 = %r{\Ai\(#{PAT}#{CM}('[^']*'|"[^"]*")#{CM}(\S+)#{CM}(\S+)\)}
351 RX3 = %r{\Ax\(#{PAT}#{CM}('[^']*'|"[^"]*")#{CM}(\S+)#{CM}(\S+)#{CM}(\S+)\)}
352 RN = %r{\An\(#{PAT}#{CM}('[^']*'|"[^"]*")\)} #'
353
354 def convert(endian, fp)
355
356 if (endian == BIG_ENDIAN)
357 se = 'BE'
358 else
359 se = 'LE'
360 end
361
362 print(<<"EOS")
363 #ifdef POSIX_TEST
364 reg_set_encoding(REG_POSIX_ENCODING_UTF16_#{se});
365 #else
366 ENC = ONIG_ENCODING_UTF16_#{se};
367 #endif
368 EOS
369
370 while line = fp.gets()
371 if (m = RX2.match(line))
372 reg = conv_reg(endian, m[1])
373 str, from, to = conv_str(endian, m[2], m[3], m[4])
374 printf(" x2(\"%s\", \"%s\", %s, %s);\n", reg, str, from, to)
375 elsif (m = RI2.match(line))
376 reg = conv_reg(endian, m[1])
377 str, from, to = conv_str(endian, m[2], m[3], m[4])
378 printf(" x2(\"%s\", \"%s\", %s, %s);\n", reg, str, from, to)
379 elsif (m = RX3.match(line))
380 reg = conv_reg(endian, m[1])
381 str, from, to = conv_str(endian, m[2], m[3], m[4])
382 printf(" x3(\"%s\", \"%s\", %s, %s, %s);\n", reg, str, from, to, m[5])
383 elsif (m = RN.match(line))
384 reg = conv_reg(endian, m[1])
385 str, from, to = conv_str(endian, m[2], 0, 0)
386 printf(" n(\"%s\", \"%s\");\n", reg, str)
387 else
388 end
389 end
390 end
391
392 File::open(ARGV[0]) { |fp|
393 convert(BIG_ENDIAN, fp)
394 }
395
396 #File::open(ARGV[0]) { |fp|
397 # convert(LITTLE_ENDIAN, fp)
398 #}
399
400 print(<<'EOS')
401 fprintf(stdout,
402 "\nRESULT SUCC: %d, FAIL: %d, ERROR: %d (by Onigmo %s)\n",
403 nsucc, nfail, nerror, onig_version());
404
405 #ifndef POSIX_TEST
406 onig_region_free(region, 1);
407 onig_end();
408 #endif
409
410 return ((nfail == 0 && nerror == 0) ? 0 : -1);
411 }
412 EOS
413
414 # END OF SCRIPT