22 #define errcpy(err, msg) strlcpy((err), (msg), ONIG_MAX_ERROR_MESSAGE_LEN) 24 #define BEG(no) (regs->beg[(no)]) 25 #define END(no) (regs->end[(no)]) 28 static const char casetable[] = {
29 '\000',
'\001',
'\002',
'\003',
'\004',
'\005',
'\006',
'\007',
30 '\010',
'\011',
'\012',
'\013',
'\014',
'\015',
'\016',
'\017',
31 '\020',
'\021',
'\022',
'\023',
'\024',
'\025',
'\026',
'\027',
32 '\030',
'\031',
'\032',
'\033',
'\034',
'\035',
'\036',
'\037',
34 '\040',
'\041',
'\042',
'\043',
'\044',
'\045',
'\046',
'\047',
36 '\050',
'\051',
'\052',
'\053',
'\054',
'\055',
'\056',
'\057',
38 '\060',
'\061',
'\062',
'\063',
'\064',
'\065',
'\066',
'\067',
40 '\070',
'\071',
'\072',
'\073',
'\074',
'\075',
'\076',
'\077',
42 '\100',
'\141',
'\142',
'\143',
'\144',
'\145',
'\146',
'\147',
44 '\150',
'\151',
'\152',
'\153',
'\154',
'\155',
'\156',
'\157',
46 '\160',
'\161',
'\162',
'\163',
'\164',
'\165',
'\166',
'\167',
48 '\170',
'\171',
'\172',
'\133',
'\134',
'\135',
'\136',
'\137',
50 '\140',
'\141',
'\142',
'\143',
'\144',
'\145',
'\146',
'\147',
52 '\150',
'\151',
'\152',
'\153',
'\154',
'\155',
'\156',
'\157',
54 '\160',
'\161',
'\162',
'\163',
'\164',
'\165',
'\166',
'\167',
56 '\170',
'\171',
'\172',
'\173',
'\174',
'\175',
'\176',
'\177',
57 '\200',
'\201',
'\202',
'\203',
'\204',
'\205',
'\206',
'\207',
58 '\210',
'\211',
'\212',
'\213',
'\214',
'\215',
'\216',
'\217',
59 '\220',
'\221',
'\222',
'\223',
'\224',
'\225',
'\226',
'\227',
60 '\230',
'\231',
'\232',
'\233',
'\234',
'\235',
'\236',
'\237',
61 '\240',
'\241',
'\242',
'\243',
'\244',
'\245',
'\246',
'\247',
62 '\250',
'\251',
'\252',
'\253',
'\254',
'\255',
'\256',
'\257',
63 '\260',
'\261',
'\262',
'\263',
'\264',
'\265',
'\266',
'\267',
64 '\270',
'\271',
'\272',
'\273',
'\274',
'\275',
'\276',
'\277',
65 '\300',
'\301',
'\302',
'\303',
'\304',
'\305',
'\306',
'\307',
66 '\310',
'\311',
'\312',
'\313',
'\314',
'\315',
'\316',
'\317',
67 '\320',
'\321',
'\322',
'\323',
'\324',
'\325',
'\326',
'\327',
68 '\330',
'\331',
'\332',
'\333',
'\334',
'\335',
'\336',
'\337',
69 '\340',
'\341',
'\342',
'\343',
'\344',
'\345',
'\346',
'\347',
70 '\350',
'\351',
'\352',
'\353',
'\354',
'\355',
'\356',
'\357',
71 '\360',
'\361',
'\362',
'\363',
'\364',
'\365',
'\366',
'\367',
72 '\370',
'\371',
'\372',
'\373',
'\374',
'\375',
'\376',
'\377',
75 # error >>> "You lose. You will need a translation table for your character set." <<< 81 const unsigned char *p1 = x, *p2 = y;
85 if ((tmp = casetable[(
unsigned)*p1++] - casetable[(
unsigned)*p2++]))
93 rb_memsearch_ss(
const unsigned char *xs,
long m,
const unsigned char *ys,
long n)
95 const unsigned char *y;
97 if (y = memmem(ys, n, xs, m))
104 rb_memsearch_ss(
const unsigned char *xs,
long m,
const unsigned char *ys,
long n)
106 const unsigned char *x = xs, *xe = xs + m;
107 const unsigned char *y = ys, *ye = ys + n;
109 # if SIZEOF_VALUE == 8 110 # define VALUE_MAX 0xFFFFFFFFFFFFFFFFULL 111 # elif SIZEOF_VALUE == 4 112 # define VALUE_MAX 0xFFFFFFFFUL 118 rb_bug(
"!!too long pattern string!!");
120 if (!(y = memchr(y, *x, n - m + 1)))
124 for (hx = *x++, hy = *y++; x < xe; ++x, ++y) {
144 rb_memsearch_qs(
const unsigned char *xs,
long m,
const unsigned char *ys,
long n)
146 const unsigned char *x = xs, *xe = xs + m;
147 const unsigned char *y = ys;
148 VALUE i, qstable[256];
151 for (i = 0; i < 256; ++i)
154 qstable[*x] = xe - x;
156 for (; y + m <= ys + n; y += *(qstable + y[m])) {
157 if (*xs == *y &&
memcmp(xs, y, m) == 0)
163 static inline unsigned int 164 rb_memsearch_qs_utf8_hash(
const unsigned char *x)
166 register const unsigned int mix = 8353;
167 register unsigned int h = *x;
192 return (
unsigned char)h;
196 rb_memsearch_qs_utf8(
const unsigned char *xs,
long m,
const unsigned char *ys,
long n)
198 const unsigned char *x = xs, *xe = xs + m;
199 const unsigned char *y = ys;
200 VALUE i, qstable[512];
203 for (i = 0; i < 512; ++i) {
206 for (; x < xe; ++x) {
207 qstable[rb_memsearch_qs_utf8_hash(x)] = xe - x;
210 for (; y + m <= ys + n; y += qstable[rb_memsearch_qs_utf8_hash(y+m)]) {
211 if (*xs == *y &&
memcmp(xs, y, m) == 0)
218 rb_memsearch_wchar(
const unsigned char *xs,
long m,
const unsigned char *ys,
long n)
220 const unsigned char *x = xs, x0 = *xs, *y = ys;
224 if (x0 == *y &&
memcmp(x+1, y+1, m-1) == 0)
231 rb_memsearch_qchar(
const unsigned char *xs,
long m,
const unsigned char *ys,
long n)
233 const unsigned char *x = xs, x0 = *xs, *y = ys;
237 if (x0 == *y &&
memcmp(x+1, y+1, m-1) == 0)
246 const unsigned char *x = x0, *y = y0;
248 if (m > n)
return -1;
250 return memcmp(x0, y0, m) == 0 ? 0 : -1;
256 const unsigned char *ys = memchr(y, *x, n);
265 return rb_memsearch_ss(x0, m, y0, n);
268 return rb_memsearch_qs_utf8(x0, m, y0, n);
272 return rb_memsearch_wchar(x0, m, y0, n);
275 return rb_memsearch_qchar(x0, m, y0, n);
277 return rb_memsearch_qs(x0, m, y0, n);
280 #define REG_LITERAL FL_USER5 281 #define REG_ENCODING_NONE FL_USER6 283 #define KCODE_FIXED FL_USER4 285 #define ARG_REG_OPTION_MASK \ 286 (ONIG_OPTION_IGNORECASE|ONIG_OPTION_MULTILINE|ONIG_OPTION_EXTEND) 287 #define ARG_ENCODING_FIXED 16 288 #define ARG_ENCODING_NONE 32 291 char_to_option(
int c)
313 option_to_str(
char str[4],
int options)
343 return (*option = char_to_option(c));
350 rb_reg_check(
VALUE re)
358 rb_reg_expr_str(
VALUE str,
const char *s,
long len,
361 const char *p, *pend;
366 p = s; pend = p +
len;
373 p +=
mbclen(p, pend, enc);
401 if (c ==
'\\' && p+clen < pend) {
402 int n = clen +
mbclen(p+clen, pend, enc);
415 c = (
unsigned char)*p;
435 snprintf(b,
sizeof(b),
"\\x%02X", c);
447 rb_reg_desc(
const char *s,
long len,
VALUE re)
460 rb_reg_expr_str(str, s,
len, enc, resenc);
465 if (*option_to_str(opts,
RREGEXP_PTR(re)->options))
490 rb_reg_source(
VALUE re)
513 rb_reg_inspect(
VALUE re)
543 rb_reg_to_s(
VALUE re)
560 if (len >= 4 && ptr[0] ==
'(' && ptr[1] ==
'?') {
563 if ((len -= 2) > 0) {
565 opt = char_to_option((
int )*ptr);
575 if (len > 1 && *ptr ==
'-') {
579 opt = char_to_option((
int )*ptr);
594 if (*ptr ==
':' && ptr[len-1] ==
')') {
601 err =
onig_new(&rp, ptr, ptr + len, options,
615 if ((options & embeddable) != embeddable) {
617 option_to_str(optbuf + 1, ~options);
623 rb_reg_expr_str(str, (
char*)ptr, len, enc,
NULL);
643 rb_reg_expr_str(str, (
char*)ptr, len, enc,
NULL);
653 rb_reg_raise(
const char *s,
long len,
const char *
err,
VALUE re)
655 VALUE desc = rb_reg_desc(s,
len, re);
661 rb_enc_reg_error_desc(
const char *s,
long len,
rb_encoding *enc,
int options,
const char *
err)
670 rb_reg_expr_str(desc, s,
len, enc, resenc);
672 option_to_str(opts + 1, options);
678 rb_enc_reg_raise(
const char *s,
long len,
rb_encoding *enc,
int options,
const char *
err)
684 rb_reg_error_desc(
VALUE str,
int options,
const char *
err)
691 rb_reg_raise_str(
VALUE str,
int options,
const char *
err)
709 rb_reg_casefold_p(
VALUE re)
741 rb_reg_options_m(
VALUE re)
749 int back_num,
int *back_refs,
OnigRegex regex,
void *arg)
773 rb_reg_names(
VALUE re)
784 int back_num,
int *back_refs,
OnigRegex regex,
void *arg)
790 for (i = 0; i < back_num; i++)
821 rb_reg_named_captures(
VALUE re)
830 onig_new_with_source(
regex_t** reg,
const UChar* pattern,
const UChar* pattern_end,
832 OnigErrorInfo* einfo,
const char *sourcefile,
int sourceline)
842 r =
onig_compile_ruby(*reg, pattern, pattern_end, einfo, sourcefile, sourceline);
853 const char *sourcefile,
int sourceline)
866 r = onig_new_with_source(&rp, (
UChar*)s, (
UChar*)(s +
len), flags,
891 match_alloc(
VALUE klass)
920 pair_byte_cmp(
const void *pair1,
const void *pair2)
922 long diff = ((
pair_t*)pair1)->byte_pos - ((
pair_t*)pair2)->byte_pos;
923 #if SIZEOF_LONG > SIZEOF_INT 924 return diff ? diff > 0 ? 1 : -1 : 0;
931 update_char_offset(
VALUE match)
970 qsort(pairs, num_pos,
sizeof(
pair_t), pair_byte_cmp);
974 for (i = 0; i < num_pos; i++) {
990 found = bsearch(&key, pairs, num_pos,
sizeof(
pair_t), pair_byte_cmp);
994 found = bsearch(&key, pairs, num_pos,
sizeof(
pair_t), pair_byte_cmp);
1002 match_check(
VALUE match)
1004 if (!
RMATCH(match)->regexp) {
1020 rm =
RMATCH(obj)->rmatch;
1053 match_regexp(
VALUE match)
1057 regexp =
RMATCH(match)->regexp;
1058 if (
NIL_P(regexp)) {
1061 RMATCH(match)->regexp = regexp;
1081 match_names(
VALUE match)
1086 return rb_reg_names(
RMATCH(match)->regexp);
1102 match_size(
VALUE match)
1108 static int name_to_backref_number(
struct re_registers *,
VALUE,
const char*,
const char*);
1111 match_backref_number(
VALUE match,
VALUE backref)
1128 num = name_to_backref_number(regs, regexp, name, name +
strlen(name));
1140 return match_backref_number(match, backref);
1164 int i = match_backref_number(match, n);
1174 update_char_offset(match);
1200 int i = match_backref_number(match, n);
1210 update_char_offset(match);
1235 int i = match_backref_number(match, n);
1245 update_char_offset(match);
1249 #define MATCH_BUSY FL_USER2 1261 if (
NIL_P(match))
return -1;
1263 if (!regs)
return -1;
1273 if (!regs)
return FALSE;
1279 if (nth <= 0)
return FALSE;
1281 return (
BEG(nth) != -1);
1290 match->
str = string;
1306 match_set_string(match,
string, pos, len);
1340 rb_reg_fixed_encoding_p(
VALUE re)
1349 rb_reg_preprocess(
const char *p,
const char *end,
rb_encoding *enc,
1357 "incompatible encoding regexp match (%s regexp with %s string)",
1363 str_coderange(
VALUE str)
1373 rb_reg_prepare_enc(
VALUE re,
VALUE str,
int warn)
1376 int cr = str_coderange(str);
1380 "invalid byte sequence in %s",
1393 reg_enc_error(re, str);
1395 else if (rb_reg_fixed_encoding_p(re)) {
1398 reg_enc_error(re, str);
1405 rb_warn(
"historical binary regexp match /.../n against %s string",
1417 const char *pattern;
1420 rb_encoding *enc = rb_reg_prepare_enc(re, str, 1);
1422 if (reg->
enc == enc)
return reg;
1428 unescaped = rb_reg_preprocess(
1432 if (unescaped ==
Qnil) {
1463 enc = rb_reg_prepare_enc(re, str, 0);
1506 if (!tmpreg)
RREGEXP(re)->usecnt++;
1509 if (!
NIL_P(match)) {
1529 if (!tmpreg)
RREGEXP(re)->usecnt--;
1563 if (set_backref_str) {
1568 RMATCH(match)->regexp = re;
1569 RMATCH(match)->rmatch->char_offset_updated = 0;
1595 if (!tmpreg)
RREGEXP(re)->usecnt++;
1598 if (!
NIL_P(match)) {
1614 if (!tmpreg)
RREGEXP(re)->usecnt--;
1651 RMATCH(match)->regexp = re;
1652 RMATCH(match)->rmatch->char_offset_updated = 0;
1672 if (nth <= 0)
return Qnil;
1693 if (nth <= 0)
return Qnil;
1696 if (start == -1)
return Qnil;
1731 if (
BEG(0) == -1)
return Qnil;
1759 if (
BEG(0) == -1)
return Qnil;
1760 str =
RMATCH(match)->str;
1776 if (
BEG(0) == -1)
return Qnil;
1778 for (i=regs->
num_regs-1;
BEG(i) == -1 && i > 0; i--)
1780 if (i == 0)
return Qnil;
1785 last_match_getter(
void)
1791 prematch_getter(
void)
1797 postmatch_getter(
void)
1803 last_paren_match_getter(
void)
1809 match_array(
VALUE match,
int start)
1820 target =
RMATCH(match)->str;
1822 for (i=start; i<regs->
num_regs; i++) {
1823 if (regs->
beg[i] == -1) {
1859 match_to_a(
VALUE match)
1861 return match_array(match, 0);
1878 match_captures(
VALUE match)
1880 return match_array(match, 1);
1884 name_to_backref_number(
struct re_registers *regs,
VALUE regexp,
const char*
name,
const char* name_end)
1887 (
const unsigned char *)
name, (
const unsigned char *)name_end, regs);
1898 #define NAME_TO_NUMBER(regs, re, name, name_ptr, name_end) \ 1900 !rb_enc_compatible(RREGEXP_SRC(re), (name)) ? 0 : \ 1901 name_to_backref_number((regs), (re), (name_ptr), (name_end))) 1917 name_to_backref_error(name);
1928 if (len == 0)
return result;
1930 for (j =
beg; j <
end; j++) {
1933 if (
beg + len > j) {
1954 return match_ary_subseq(match, beg, len, result);
1993 if (
NIL_P(length)) {
1998 int num = namev_to_backref_number(
RMATCH_REGS(match),
RMATCH(match)->regexp, idx);
2003 return match_ary_aref(match, idx,
Qnil);
2016 if (beg < 0)
return Qnil;
2018 else if (beg > num_regs) {
2021 else if (beg+len > num_regs) {
2022 len = num_regs -
beg;
2024 return match_ary_subseq(match, beg, len,
Qnil);
2054 for (i=0; i<
argc; i++) {
2064 match_ary_aref(match,
argv[i], result);
2083 match_to_s(
VALUE match)
2096 int back_num,
int *back_refs,
OnigRegex regex,
void *arg) {
2107 for (i = 0; i < back_num; i++) {
2146 match_named_captures(
VALUE match)
2174 match_string(
VALUE match)
2177 return RMATCH(match)->str;
2187 int back_num,
int *back_refs,
OnigRegex regex,
void *arg0)
2192 for (i = 0; i < back_num; i++) {
2194 arg[back_refs[i]].
len = name_end -
name;
2220 match_inspect(
VALUE match)
2233 else if (
NIL_P(regexp)) {
2242 match_inspect_name_iter, names);
2247 for (i = 0; i < num_regs; i++) {
2274 const char *p = *pp;
2276 int meta_prefix = 0, ctrl_prefix = 0;
2279 if (p == end || *p++ !=
'\\') {
2280 errcpy(err,
"too short escaped multibyte character");
2286 errcpy(err,
"too short escape sequence");
2290 case '\\': code =
'\\';
break;
2291 case 'n': code =
'\n';
break;
2292 case 't': code =
'\t';
break;
2293 case 'r': code =
'\r';
break;
2294 case 'f': code =
'\f';
break;
2295 case 'v': code =
'\013';
break;
2296 case 'a': code =
'\007';
break;
2297 case 'e': code =
'\033';
break;
2300 case '0':
case '1':
case '2':
case '3':
2301 case '4':
case '5':
case '6':
case '7':
2303 code =
scan_oct(p, end < p+3 ? end-p : 3, &len);
2308 code =
scan_hex(p, end < p+2 ? end-p : 2, &len);
2310 errcpy(err,
"invalid hex escape");
2318 errcpy(err,
"duplicate meta escape");
2322 if (p+1 < end && *p++ ==
'-' && (*p & 0x80) == 0) {
2332 errcpy(err,
"too short meta escape");
2336 if (p == end || *p++ !=
'-') {
2337 errcpy(err,
"too short control escape");
2342 errcpy(err,
"duplicate control escape");
2346 if (p < end && (*p & 0x80) == 0) {
2356 errcpy(err,
"too short control escape");
2360 errcpy(err,
"unexpected escape sequence");
2363 if (code < 0 || 0xff < code) {
2364 errcpy(err,
"invalid escape code");
2378 unescape_escaped_nonascii(
const char **pp,
const char *end,
rb_encoding *enc,
2381 const char *p = *pp;
2383 char *chbuf =
ALLOCA_N(
char, chmaxlen);
2388 memset(chbuf, 0, chmaxlen);
2390 byte = read_escaped_byte(&p, end, err);
2395 chbuf[chlen++] = byte;
2396 while (chlen < chmaxlen &&
2398 byte = read_escaped_byte(&p, end, err);
2402 chbuf[chlen++] = byte;
2407 errcpy(err,
"invalid multibyte escape");
2410 if (1 < chlen || (chbuf[0] & 0x80)) {
2415 else if (*encp != enc) {
2416 errcpy(err,
"escaped non ASCII character in UTF-8 regexp");
2422 snprintf(escbuf,
sizeof(escbuf),
"\\x%02X", chbuf[0]&0xff);
2432 if ((0xd800 <= code && code <= 0xdfff) ||
2434 errcpy(err,
"invalid Unicode range");
2441 append_utf8(
unsigned long uv,
2444 if (check_unicode_range(uv, err) != 0)
2448 snprintf(escbuf,
sizeof(escbuf),
"\\x%02X", (
int)uv);
2460 errcpy(err,
"UTF-8 character in non UTF-8 regexp");
2468 unescape_unicode_list(
const char **pp,
const char *end,
2471 const char *p = *pp;
2472 int has_unicode = 0;
2476 while (p < end &&
ISSPACE(*p)) p++;
2483 errcpy(err,
"invalid Unicode range");
2487 if (append_utf8(code,
buf, encp, err) != 0)
2491 while (p < end &&
ISSPACE(*p)) p++;
2494 if (has_unicode == 0) {
2495 errcpy(err,
"invalid Unicode list");
2505 unescape_unicode_bmp(
const char **pp,
const char *end,
2508 const char *p = *pp;
2513 errcpy(err,
"invalid Unicode escape");
2518 errcpy(err,
"invalid Unicode escape");
2521 if (append_utf8(code,
buf, encp, err) != 0)
2528 unescape_nonascii(
const char *p,
const char *end,
rb_encoding *enc,
2538 errcpy(err,
"invalid multibyte character");
2542 if (1 < chlen || (*p & 0x80)) {
2547 else if (*encp != enc) {
2548 errcpy(err,
"non ASCII character in UTF-8 regexp");
2557 errcpy(err,
"too short escape sequence");
2561 case '1':
case '2':
case '3':
2562 case '4':
case '5':
case '6':
case '7':
2564 size_t len = end-(p-1), octlen;
2565 if (
ruby_scan_oct(p-1, len < 3 ? len : 3, &octlen) <= 0177) {
2582 const char *pbeg = p;
2583 c = read_escaped_byte(&p, end, err);
2584 if (c == (
char)-1)
return -1;
2588 if (unescape_escaped_nonascii(&p, end, enc,
buf, encp, err) != 0)
2595 errcpy(err,
"too short escape sequence");
2601 if (unescape_unicode_list(&p, end,
buf, encp, err) != 0)
2603 if (p == end || *p++ !=
'}') {
2604 errcpy(err,
"invalid Unicode list");
2611 if (unescape_unicode_bmp(&p, end,
buf, encp, err) != 0)
2642 rb_reg_preprocess(
const char *p,
const char *end,
rb_encoding *enc,
2646 int has_property = 0;
2657 if (unescape_nonascii(p, end, enc, buf, fixed_enc, &has_property, err) != 0)
2660 if (has_property && !*fixed_enc) {
2685 buf = rb_reg_preprocess(p, end, enc, &fixed_enc, err);
2689 return rb_reg_error_desc(str, 0, err);
2695 rb_reg_preprocess_dregexp(
VALUE ary,
int options)
2716 src_enc != ascii8bit) {
2720 src_enc = ascii8bit;
2727 buf = rb_reg_preprocess(p, end, src_enc, &fixed_enc, err);
2732 if (fixed_enc != 0) {
2733 if (regexp_enc != 0 && regexp_enc != fixed_enc) {
2737 regexp_enc = fixed_enc;
2755 const char *sourcefile,
int sourceline)
2770 errcpy(
err,
"can't make regexp with dummy encoding");
2774 unescaped = rb_reg_preprocess(s, s+
len, enc, &fixed_enc,
err);
2775 if (unescaped ==
Qnil)
2781 errcpy(
err,
"incompatible character encoding");
2784 if (fixed_enc != a_enc) {
2794 if ((options & ARG_ENCODING_FIXED) || fixed_enc) {
2803 sourcefile, sourceline);
2804 if (!re->ptr)
return -1;
2813 if (regenc != enc) {
2821 const char *sourcefile,
int sourceline)
2827 if (enc != ascii8bit) {
2829 errcpy(
err,
"/.../n has a non escaped non ASCII character in non ASCII-8BIT script");
2836 options,
err, sourcefile, sourceline);
2838 if (ret == 0) reg_set_source(obj, str, str_enc);
2843 rb_reg_s_alloc(
VALUE klass)
2871 if (rb_reg_initialize_str(re, s, options, err,
NULL, 0) != 0) {
2872 rb_reg_raise_str(s, options, err);
2884 enc, options, err,
NULL, 0) != 0) {
2885 rb_reg_raise_str(s, options, err);
2887 reg_set_source(re, s, enc);
2904 if (rb_reg_initialize(re, s, len, enc, options, err,
NULL, 0) != 0) {
2905 rb_enc_reg_raise(s, len, enc, options, err);
2925 if (rb_reg_initialize_str(re, str, options, err, sourcefile, sourceline) != 0) {
2933 static VALUE reg_cache;
2957 rb_reg_hash(
VALUE re)
2993 if (re1 == re2)
return Qtrue;
2995 rb_reg_check(re1); rb_reg_check(re2);
3017 match_hash(
VALUE match)
3024 hashval =
rb_hash_uint(hashval, reg_hash(match_regexp(match)));
3047 if (match1 == match2)
return Qtrue;
3051 if (!rb_reg_equal(match_regexp(match1), match_regexp(match2)))
return Qfalse;
3061 reg_operand(
VALUE s,
int check)
3072 reg_match_pos(
VALUE re,
VALUE *strp,
long pos)
3080 *strp = str = reg_operand(str,
TRUE);
3145 long pos = reg_match_pos(re, &str, 0);
3146 if (pos < 0)
return Qnil;
3177 str = reg_operand(str,
FALSE);
3256 VALUE result, str, initpos;
3266 pos = reg_match_pos(re, &str, pos);
3316 if (pos < 0)
return Qfalse;
3327 if (!tmpreg)
RREGEXP(re)->usecnt++;
3330 result =
onig_search(reg, start, end, start + pos, end,
3332 if (!tmpreg)
RREGEXP(re)->usecnt--;
3407 if (kcode[0] ==
'n' || kcode[0] ==
'N') {
3412 rb_warn(
"encoding option is ignored - %s", kcode);
3418 rb_reg_init_str_enc(
self, str, enc, flags);
3438 s +=
mbclen(s, send, enc);
3442 case '[':
case ']':
case '{':
case '}':
3443 case '(':
case ')':
case '|':
case '-':
3444 case '*':
case '.':
case '\\':
3445 case '?':
case '+':
case '^':
case '$':
3447 case '\t':
case '\f':
case '\v':
case '\n':
case '\r':
3474 int n =
mbclen(s, send, enc);
3482 case '[':
case ']':
case '{':
case '}':
3483 case '(':
case ')':
case '|':
case '-':
3484 case '*':
case '.':
case '\\':
3485 case '?':
case '+':
case '^':
case '$':
3593 else if (argc == 1) {
3600 quoted = rb_reg_s_quote(
Qnil, arg);
3609 int has_asciionly = 0;
3613 for (i = 0; i <
argc; i++) {
3624 if (!has_ascii_incompat)
3625 has_ascii_incompat = enc;
3626 else if (has_ascii_incompat != enc)
3630 else if (rb_reg_fixed_encoding_p(v)) {
3631 if (!has_ascii_compat_fixed)
3632 has_ascii_compat_fixed = enc;
3633 else if (has_ascii_compat_fixed != enc)
3647 if (!has_ascii_incompat)
3648 has_ascii_incompat = enc;
3649 else if (has_ascii_incompat != enc)
3657 if (!has_ascii_compat_fixed)
3658 has_ascii_compat_fixed = enc;
3659 else if (has_ascii_compat_fixed != enc)
3663 v = rb_reg_s_quote(
Qnil, e);
3665 if (has_ascii_incompat) {
3666 if (has_asciionly) {
3670 if (has_ascii_compat_fixed) {
3682 if (has_ascii_incompat) {
3683 result_enc = has_ascii_incompat;
3685 else if (has_ascii_compat_fixed) {
3686 result_enc = has_ascii_compat_fixed;
3724 return rb_reg_s_union(
self, v);
3726 return rb_reg_s_union(
self, args);
3747 #define ASCGET(s,e,cl) (acompat ? (*(cl)=1,ISASCII((s)[0])?(s)[0]:-1) : rb_enc_ascget((s), (e), (cl), str_enc)) 3753 int c =
ASCGET(s, e, &clen);
3757 s +=
mbclen(s, e, str_enc);
3763 if (c !=
'\\' || s == e)
continue;
3772 s +=
mbclen(s, e, str_enc);
3781 case '1':
case '2':
case '3':
case '4':
3782 case '5':
case '6':
case '7':
case '8':
case '9':
3792 if (s < e &&
ASCGET(s, e, &clen) ==
'<') {
3793 char *
name, *name_end;
3795 name_end = name = s + clen;
3796 while (name_end < e) {
3797 c =
ASCGET(name_end, e, &clen);
3798 if (c ==
'>')
break;
3799 name_end += c == -1 ?
mbclen(name_end, e, str_enc) : clen;
3803 (
long)(name_end - name));
3804 if ((no =
NAME_TO_NUMBER(regs, regexp, n, name, name_end)) < 1) {
3805 name_to_backref_error(n);
3807 p = s = name_end + clen;
3833 while (
BEG(no) == -1 && no > 0) no--;
3834 if (no == 0)
continue;
3847 if (no >= regs->
num_regs)
continue;
3848 if (
BEG(no) == -1)
continue;
3853 if (!val)
return str;
3864 rb_warn(
"variable $KCODE is no longer effective");
3871 rb_warn(
"variable $KCODE is no longer effective; ignored");
3875 ignorecase_getter(
void)
3877 rb_warn(
"variable $= is no longer effective");
3884 rb_warn(
"variable $= is no longer effective; ignored");
3943 n = match_backref_number(match, nth);
3946 return match_getter();
3950 re_warn(
const char *s)
unsigned int OnigOptionType
VALUE rb_reg_match(VALUE re, VALUE str)
ONIG_EXTERN int onig_foreach_name(OnigRegex reg, int(*func)(const OnigUChar *, const OnigUChar *, int, int *, OnigRegex, void *), void *arg)
ONIG_EXTERN int onigenc_set_default_encoding(OnigEncoding enc)
int onig_compile_ruby(regex_t *reg, const UChar *pattern, const UChar *pattern_end, OnigErrorInfo *einfo, const char *sourcefile, int sourceline)
#define MBCLEN_CHARFOUND_P(ret)
#define ONIGENC_MBC_MAXLEN(enc)
void rb_warn(const char *fmt,...)
void rb_bug(const char *fmt,...)
ONIG_EXTERN OnigUChar * onigenc_get_right_adjust_char_head(OnigEncoding enc, const OnigUChar *start, const OnigUChar *s, const OnigUChar *end)
VALUE rb_str_length(VALUE)
VALUE rb_ary_entry(VALUE ary, long offset)
#define MBCLEN_CHARFOUND_LEN(ret)
#define rb_enc_mbc_to_codepoint(p, e, enc)
VALUE rb_ary_new_capa(long capa)
void rb_enc_copy(VALUE obj1, VALUE obj2)
VALUE rb_str_equal(VALUE str1, VALUE str2)
long rb_str_coderange_scan_restartable(const char *, const char *, rb_encoding *, int *)
size_t strlen(const char *)
#define scan_oct(s, l, e)
void rb_backref_set(VALUE)
void rb_define_virtual_variable(const char *, VALUE(*)(ANYARGS), void(*)(ANYARGS))
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
int rb_block_given_p(void)
Determines if the current method is given a block.
void rb_raise(VALUE exc, const char *fmt,...)
ONIG_EXTERN int onig_number_of_names(const OnigRegexType *reg)
ONIG_EXTERN int onig_new(OnigRegex *, const OnigUChar *pattern, const OnigUChar *pattern_end, OnigOptionType option, OnigEncoding enc, const OnigSyntaxType *syntax, OnigErrorInfo *einfo)
st_index_t rb_hash_end(st_index_t)
#define ARG_ENCODING_FIXED
#define OBJ_INIT_COPY(obj, orig)
#define ONIGENC_CASE_FOLD_DEFAULT
int rb_enc_dummy_p(rb_encoding *enc)
regex_t * rb_reg_prepare_re0(VALUE re, VALUE str, onig_errmsg_buffer err)
#define RGENGC_WB_PROTECTED_REGEXP
#define REG_ENCODING_NONE
VALUE rb_enc_from_encoding(rb_encoding *encoding)
#define ONIG_ENCODING_ASCII
rb_encoding * rb_default_internal_encoding(void)
VALUE rb_ary_push(VALUE ary, VALUE item)
VALUE rb_str_buf_new2(const char *)
st_index_t rb_memhash(const void *ptr, long len)
ONIG_EXTERN void onig_region_free(OnigRegion *region, int free_self)
int rb_reg_region_copy(struct re_registers *to, const struct re_registers *from)
VALUE rb_backref_get(void)
int rb_enc_str_coderange(VALUE)
VALUE rb_enc_associate(VALUE obj, rb_encoding *enc)
VALUE rb_reg_regsub(VALUE str, VALUE src, struct re_registers *regs, VALUE regexp)
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
NORETURN(static void name_to_backref_error(VALUE name))
ONIG_EXTERN void onig_free(OnigRegex)
VALUE rb_check_convert_type(VALUE, int, const char *, const char *)
Tries to convert an object into another type.
int rb_reg_backref_number(VALUE match, VALUE backref)
st_index_t rb_str_hash(VALUE)
char * rb_str_subpos(VALUE, long, long *)
#define ONIG_OPTION_IGNORECASE
VALUE rb_range_beg_len(VALUE, long *, long *, long, int)
#define rb_enc_mbmaxlen(enc)
int char_offset_num_allocated
#define MEMO_NEW(a, b, c)
VALUE rb_hash_new_with_size(st_index_t size)
rb_encoding * rb_utf8_encoding(void)
#define ONIGENC_LEFT_ADJUST_CHAR_HEAD(enc, start, s, end)
void rb_undef_method(VALUE klass, const char *name)
VALUE rb_str_buf_append(VALUE, VALUE)
long rb_reg_search0(VALUE re, VALUE str, long pos, int reverse, int set_backref_str)
#define ENC_CODERANGE_7BIT
ONIG_EXTERN int onig_error_code_to_str(OnigUChar *s, OnigPosition err_code,...)
VALUE rb_reg_init_str(VALUE re, VALUE s, int options)
ONIG_EXTERN int onig_noname_group_capture_is_active(const OnigRegexType *reg)
VALUE rb_str_buf_cat(VALUE, const char *, long)
#define NEWOBJ_OF(obj, type, klass, flags)
long rb_reg_search(VALUE re, VALUE str, long pos, int reverse)
void rb_global_variable(VALUE *var)
VALUE rb_reg_new_str(VALUE s, int options)
ONIG_EXTERN int onig_reg_init(OnigRegex reg, OnigOptionType option, OnigCaseFoldType case_fold_flag, OnigEncoding enc, const OnigSyntaxType *syntax)
VALUE rb_obj_class(VALUE)
call-seq: obj.class -> class
#define RB_TYPE_P(obj, type)
VALUE rb_reg_last_match(VALUE match)
#define MEMZERO(p, type, n)
VALUE rb_lastline_get(void)
rb_encoding * rb_default_external_encoding(void)
VALUE rb_reg_match_post(VALUE match)
regex_t * rb_reg_prepare_re(VALUE re, VALUE str)
#define scan_hex(s, l, e)
int rb_match_nth_defined(int nth, VALUE match)
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
ONIG_EXTERN void onig_set_verb_warn_func(OnigWarnFunc f)
#define RREGEXP_SRC_PTR(r)
RUBY_EXTERN VALUE rb_cObject
VALUE rb_reg_new_ary(VALUE ary, int opt)
#define MBCLEN_NEEDMORE_P(ret)
void rb_set_errinfo(VALUE err)
Sets the current exception ($!) to the given value.
#define mbclen(p, e, enc)
VALUE rb_str_buf_cat2(VALUE, const char *)
ONIG_EXTERN int onig_name_to_backref_number(OnigRegex reg, const OnigUChar *name, const OnigUChar *name_end, const OnigRegion *region)
int rb_ascii8bit_encindex(void)
VALUE rb_any_to_s(VALUE)
call-seq: obj.to_s -> string
#define ONIG_MAX_ERROR_MESSAGE_LEN
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
VALUE rb_reg_nth_match(int nth, VALUE match)
#define ENC_CODERANGE_CLEAN_P(cr)
void rb_define_const(VALUE, const char *, VALUE)
void rb_ary_store(VALUE ary, long idx, VALUE val)
#define ALLOCA_N(type, n)
int rb_uv_to_utf8(char[6], unsigned long)
bool rb_reg_start_with_p(VALUE re, VALUE str)
#define range(low, item, hi)
#define ENC_CODERANGE_UNKNOWN
#define rb_enc_isprint(c, enc)
#define MEMCPY(p1, p2, type, n)
#define ENC_CODERANGE_BROKEN
void rb_match_busy(VALUE match)
#define rb_enc_mbminlen(enc)
VALUE rb_check_regexp_type(VALUE re)
#define ONIG_OPTION_EXTEND
VALUE rb_reg_match2(VALUE re)
VALUE rb_str_resize(VALUE, long)
long rb_str_offset(VALUE, long)
int rb_reg_options(VALUE re)
int rb_enc_ascget(const char *p, const char *e, int *len, rb_encoding *enc)
long rb_memsearch(const void *x0, long m, const void *y0, long n, rb_encoding *enc)
VALUE rb_str_subseq(VALUE, long, long)
#define NAME_TO_NUMBER(regs, re, name, name_ptr, name_end)
struct re_pattern_buffer * ptr
#define REALLOC_N(var, type, n)
VALUE rb_sprintf(const char *format,...)
#define rb_enc_isspace(c, enc)
int rb_enc_precise_mbclen(const char *p, const char *e, rb_encoding *enc)
int rb_enc_unicode_p(rb_encoding *enc)
VALUE rb_class_path(VALUE)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
unsigned char buf[MIME_BUF_SIZE]
VALUE rb_assoc_new(VALUE car, VALUE cdr)
rb_encoding * rb_usascii_encoding(void)
VALUE rb_reg_eqq(VALUE re, VALUE str)
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
ONIG_EXTERN int onig_region_resize(OnigRegion *region, int n)
ONIG_EXTERN OnigPosition onig_search(OnigRegex, const OnigUChar *str, const OnigUChar *end, const OnigUChar *start, const OnigUChar *range, OnigRegion *region, OnigOptionType option)
int rb_utf8_encindex(void)
VALUE rb_enc_reg_new(const char *s, long len, rb_encoding *enc, int options)
int rb_match_count(VALUE match)
VALUE rb_obj_encoding(VALUE obj)
#define rb_enc_asciicompat(enc)
int memcmp(const void *s1, const void *s2, size_t len)
int rb_memcicmp(const void *x, const void *y, long len)
register unsigned int len
#define StringValueCStr(v)
#define ONIG_OPTION_MULTILINE
#define RB_OBJ_WRITE(a, slot, b)
VALUE rb_ary_resize(VALUE ary, long len)
expands or shrinks ary to len elements.
#define ENCODING_GET(obj)
rb_encoding * rb_enc_get(VALUE obj)
int rb_str_buf_cat_escaped_char(VALUE result, unsigned int c, int unicode_p)
char onig_errmsg_buffer[ONIG_MAX_ERROR_MESSAGE_LEN]
VALUE rb_reg_quote(VALUE str)
long rb_enc_strlen(const char *, const char *, rb_encoding *)
VALUE rb_reg_match_p(VALUE re, VALUE str, long pos)
#define MBCLEN_INVALID_P(ret)
#define RARRAY_AREF(a, i)
VALUE rb_str_buf_cat_ascii(VALUE, const char *)
void rb_backref_set_string(VALUE string, long pos, long len)
VALUE rb_check_array_type(VALUE ary)
long rb_reg_adjust_startpos(VALUE re, VALUE str, long pos, int reverse)
typedefRUBY_SYMBOL_EXPORT_BEGIN struct re_pattern_buffer Regexp
#define ENCINDEX_Windows_31J
VALUE rb_str_catf(VALUE str, const char *format,...)
#define ENC_CODERANGE(obj)
VALUE rb_check_string_type(VALUE)
VALUE rb_reg_compile(VALUE str, int options, const char *sourcefile, int sourceline)
VALUE rb_class_new_instance(int, const VALUE *, VALUE)
Allocates and initializes an instance of klass.
st_index_t rb_hash_uint(st_index_t, st_index_t)
#define char_size(c2, c1)
VALUE rb_reg_match_last(VALUE match)
#define ARG_REG_OPTION_MASK
long rb_str_sublen(VALUE, long)
#define rb_enc_left_char_head(s, p, e, enc)
VALUE rb_str_inspect(VALUE)
int rb_char_to_option_kcode(int c, int *option, int *kcode)
VALUE rb_enc_str_new(const char *, long, rb_encoding *)
struct rmatch_offset * char_offset
ONIG_EXTERN const OnigSyntaxType * OnigDefaultSyntax
unsigned long ruby_scan_hex(const char *, size_t, size_t *)
#define RREGEXP_SRC_LEN(r)
ONIG_EXTERN void onig_region_copy(OnigRegion *to, const OnigRegion *from)
#define StringValuePtr(v)
ONIG_EXTERN OnigPosition onig_match(OnigRegex, const OnigUChar *str, const OnigUChar *end, const OnigUChar *at, OnigRegion *region, OnigOptionType option)
rb_encoding * rb_ascii8bit_encoding(void)
#define rb_check_frozen(obj)
ONIG_EXTERN void onig_set_warn_func(OnigWarnFunc f)
VALUE rb_str_encode(VALUE str, VALUE to, int ecflags, VALUE ecopts)
VALUE rb_reg_nth_defined(int nth, VALUE match)
#define ARG_ENCODING_NONE
#define rb_enc_mbcput(c, buf, enc)
int rb_enc_str_asciionly_p(VALUE)
VALUE rb_reg_new(const char *s, long len, int options)
VALUE rb_str_buf_new(long)
VALUE rb_reg_regcomp(VALUE str)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
VALUE rb_str_append(VALUE, VALUE)
VALUE rb_reg_match_pre(VALUE match)
VALUE rb_str_to_str(VALUE)
st_index_t rb_hash_start(st_index_t)
unsigned long ruby_scan_oct(const char *, size_t, size_t *)
VALUE rb_reg_check_preprocess(VALUE str)
VALUE rb_enc_str_buf_cat(VALUE str, const char *ptr, long len, rb_encoding *enc)
VALUE rb_str_new(const char *, long)