| 1 |
#!/usr/local/bin/ruby -Ke |
| 2 |
# testconv.rb |
| 3 |
# Copyright (C) 2003-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 |
if WINDOWS |
| 9 |
REGENC = 'ONIG_ENCODING_SJIS' |
| 10 |
else |
| 11 |
REGENC = 'ONIG_ENCODING_EUC_JP' |
| 12 |
end |
| 13 |
|
| 14 |
def conv_reg(s) |
| 15 |
s.gsub!(/\\/, '\\\\\\\\') #' |
| 16 |
s.gsub!(/\?\?/, '?\\\\?') # escape ANSI trigraph (??) |
| 17 |
s |
| 18 |
end |
| 19 |
|
| 20 |
def conv_str(s) |
| 21 |
if (s[0] == ?') |
| 22 |
s = s[1..-2] |
| 23 |
return s.gsub(/\\/, '\\\\\\\\') #' |
| 24 |
else |
| 25 |
return s[1..-2] |
| 26 |
end |
| 27 |
end |
| 28 |
|
| 29 |
print(<<"EOS") |
| 30 |
/* |
| 31 |
* This program was generated by testconv.rb. |
| 32 |
*/ |
| 33 |
#include "config.h" |
| 34 |
#ifdef ONIG_ESCAPE_UCHAR_COLLISION |
| 35 |
#undef ONIG_ESCAPE_UCHAR_COLLISION |
| 36 |
#endif |
| 37 |
#include <stdio.h> |
| 38 |
|
| 39 |
#ifdef POSIX_TEST |
| 40 |
#include "onigmoposix.h" |
| 41 |
#else |
| 42 |
#include "onigmo.h" |
| 43 |
#endif |
| 44 |
|
| 45 |
#include <string.h> |
| 46 |
|
| 47 |
#define SLEN(s) strlen(s) |
| 48 |
|
| 49 |
static int nsucc = 0; |
| 50 |
static int nfail = 0; |
| 51 |
static int nerror = 0; |
| 52 |
|
| 53 |
static FILE* err_file; |
| 54 |
|
| 55 |
#ifndef POSIX_TEST |
| 56 |
static OnigRegion* region; |
| 57 |
#endif |
| 58 |
|
| 59 |
static void xx(char* pattern, char* str, int from, int to, int mem, int not) |
| 60 |
{ |
| 61 |
int r; |
| 62 |
|
| 63 |
#ifdef POSIX_TEST |
| 64 |
regex_t reg; |
| 65 |
char buf[200]; |
| 66 |
regmatch_t pmatch[25]; |
| 67 |
|
| 68 |
r = regcomp(®, pattern, REG_EXTENDED | REG_NEWLINE); |
| 69 |
if (r) { |
| 70 |
regerror(r, ®, buf, sizeof(buf)); |
| 71 |
fprintf(err_file, "ERROR: %s\\n", buf); |
| 72 |
nerror++; |
| 73 |
return ; |
| 74 |
} |
| 75 |
|
| 76 |
r = regexec(®, str, reg.re_nsub + 1, pmatch, 0); |
| 77 |
if (r != 0 && r != REG_NOMATCH) { |
| 78 |
regerror(r, ®, buf, sizeof(buf)); |
| 79 |
fprintf(err_file, "ERROR: %s\\n", buf); |
| 80 |
nerror++; |
| 81 |
return ; |
| 82 |
} |
| 83 |
|
| 84 |
if (r == REG_NOMATCH) { |
| 85 |
if (not) { |
| 86 |
fprintf(stdout, "OK(N): /%s/ '%s'\\n", pattern, str); |
| 87 |
nsucc++; |
| 88 |
} |
| 89 |
else { |
| 90 |
fprintf(stdout, "FAIL: /%s/ '%s'\\n", pattern, str); |
| 91 |
nfail++; |
| 92 |
} |
| 93 |
} |
| 94 |
else { |
| 95 |
if (not) { |
| 96 |
fprintf(stdout, "FAIL(N): /%s/ '%s'\\n", pattern, str); |
| 97 |
nfail++; |
| 98 |
} |
| 99 |
else { |
| 100 |
if (pmatch[mem].rm_so == from && pmatch[mem].rm_eo == to) { |
| 101 |
fprintf(stdout, "OK: /%s/ '%s'\\n", pattern, str); |
| 102 |
nsucc++; |
| 103 |
} |
| 104 |
else { |
| 105 |
fprintf(stdout, "FAIL: /%s/ '%s' %d-%d : %d-%d\\n", pattern, str, |
| 106 |
(int)from, (int)to, (int)pmatch[mem].rm_so, (int)pmatch[mem].rm_eo); |
| 107 |
nfail++; |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
regfree(®); |
| 112 |
|
| 113 |
#else |
| 114 |
regex_t* reg; |
| 115 |
OnigErrorInfo einfo; |
| 116 |
OnigSyntaxType syn = *ONIG_SYNTAX_DEFAULT; |
| 117 |
|
| 118 |
/* ONIG_OPTION_OFF(syn.options, ONIG_OPTION_ASCII_RANGE); */ |
| 119 |
|
| 120 |
r = onig_new(®, (UChar* )pattern, (UChar* )(pattern + SLEN(pattern)), |
| 121 |
ONIG_OPTION_DEFAULT, #{REGENC}, &syn, &einfo); |
| 122 |
if (r) { |
| 123 |
char s[ONIG_MAX_ERROR_MESSAGE_LEN]; |
| 124 |
onig_error_code_to_str((UChar* )s, r, &einfo); |
| 125 |
fprintf(err_file, "ERROR: %s\\n", s); |
| 126 |
nerror++; |
| 127 |
return ; |
| 128 |
} |
| 129 |
|
| 130 |
r = onig_search(reg, (UChar* )str, (UChar* )(str + SLEN(str)), |
| 131 |
(UChar* )str, (UChar* )(str + SLEN(str)), |
| 132 |
region, ONIG_OPTION_NONE); |
| 133 |
if (r < ONIG_MISMATCH) { |
| 134 |
char s[ONIG_MAX_ERROR_MESSAGE_LEN]; |
| 135 |
onig_error_code_to_str((UChar* )s, r); |
| 136 |
fprintf(err_file, "ERROR: %s\\n", s); |
| 137 |
nerror++; |
| 138 |
return ; |
| 139 |
} |
| 140 |
|
| 141 |
if (r == ONIG_MISMATCH) { |
| 142 |
if (not) { |
| 143 |
fprintf(stdout, "OK(N): /%s/ '%s'\\n", pattern, str); |
| 144 |
nsucc++; |
| 145 |
} |
| 146 |
else { |
| 147 |
fprintf(stdout, "FAIL: /%s/ '%s'\\n", pattern, str); |
| 148 |
nfail++; |
| 149 |
} |
| 150 |
} |
| 151 |
else { |
| 152 |
if (not) { |
| 153 |
fprintf(stdout, "FAIL(N): /%s/ '%s'\\n", pattern, str); |
| 154 |
nfail++; |
| 155 |
} |
| 156 |
else { |
| 157 |
if (region->beg[mem] == from && region->end[mem] == to) { |
| 158 |
fprintf(stdout, "OK: /%s/ '%s'\\n", pattern, str); |
| 159 |
nsucc++; |
| 160 |
} |
| 161 |
else { |
| 162 |
fprintf(stdout, "FAIL: /%s/ '%s' %d-%d : %d-%d\\n", pattern, str, |
| 163 |
(int)from, (int)to, (int)region->beg[mem], (int)region->end[mem]); |
| 164 |
nfail++; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
onig_free(reg); |
| 169 |
#endif |
| 170 |
} |
| 171 |
|
| 172 |
static void x2(char* pattern, char* str, int from, int to) |
| 173 |
{ |
| 174 |
xx(pattern, str, from, to, 0, 0); |
| 175 |
} |
| 176 |
|
| 177 |
static void x3(char* pattern, char* str, int from, int to, int mem) |
| 178 |
{ |
| 179 |
xx(pattern, str, from, to, mem, 0); |
| 180 |
} |
| 181 |
|
| 182 |
static void n(char* pattern, char* str) |
| 183 |
{ |
| 184 |
xx(pattern, str, 0, 0, 0, 1); |
| 185 |
} |
| 186 |
|
| 187 |
extern int main(int argc, char* argv[]) |
| 188 |
{ |
| 189 |
err_file = stdout; |
| 190 |
|
| 191 |
#ifdef POSIX_TEST |
| 192 |
reg_set_encoding(#{REGENC.sub(/\AONIG_/, 'REG_POSIX_')}); |
| 193 |
#else |
| 194 |
region = onig_region_new(); |
| 195 |
#endif |
| 196 |
|
| 197 |
EOS |
| 198 |
|
| 199 |
PAT = '\\/([^\\\\\\/]*(?:\\\\.[^\\\\\\/]*)*)\\/' |
| 200 |
CM = '\s*,\s*' |
| 201 |
RX2 = %r{\Ax\(#{PAT}#{CM}('[^']*'|"[^"]*")#{CM}(\S+)#{CM}(\S+)\)} |
| 202 |
RI2 = %r{\Ai\(#{PAT}#{CM}('[^']*'|"[^"]*")#{CM}(\S+)#{CM}(\S+)\)} |
| 203 |
RX3 = %r{\Ax\(#{PAT}#{CM}('[^']*'|"[^"]*")#{CM}(\S+)#{CM}(\S+)#{CM}(\S+)\)} |
| 204 |
RN = %r{\An\(#{PAT}#{CM}('[^']*'|"[^"]*")\)} #' |
| 205 |
|
| 206 |
while line = gets() |
| 207 |
if (m = RX2.match(line)) |
| 208 |
reg = conv_reg(m[1]) |
| 209 |
str = conv_str(m[2]) |
| 210 |
printf(" x2(\"%s\", \"%s\", %s, %s);\n", reg, str, m[3], m[4]) |
| 211 |
elsif (m = RI2.match(line)) |
| 212 |
reg = conv_reg(m[1]) |
| 213 |
str = conv_str(m[2]) |
| 214 |
printf(" x2(\"%s\", \"%s\", %s, %s);\n", reg, str, m[3], m[4]) |
| 215 |
elsif (m = RX3.match(line)) |
| 216 |
reg = conv_reg(m[1]) |
| 217 |
str = conv_str(m[2]) |
| 218 |
printf(" x3(\"%s\", \"%s\", %s, %s, %s);\n", reg, str, m[3], m[4], m[5]) |
| 219 |
elsif (m = RN.match(line)) |
| 220 |
reg = conv_reg(m[1]) |
| 221 |
str = conv_str(m[2]) |
| 222 |
printf(" n(\"%s\", \"%s\");\n", reg, str) |
| 223 |
else |
| 224 |
|
| 225 |
end |
| 226 |
end |
| 227 |
|
| 228 |
print(<<'EOS') |
| 229 |
fprintf(stdout, |
| 230 |
"\nRESULT SUCC: %d, FAIL: %d, ERROR: %d (by Onigmo %s)\n", |
| 231 |
nsucc, nfail, nerror, onig_version()); |
| 232 |
|
| 233 |
#ifndef POSIX_TEST |
| 234 |
onig_region_free(region, 1); |
| 235 |
onig_end(); |
| 236 |
#endif |
| 237 |
|
| 238 |
return ((nfail == 0 && nerror == 0) ? 0 : -1); |
| 239 |
} |
| 240 |
EOS |
| 241 |
|
| 242 |
# END OF SCRIPT |