[ruby/win32ole] Undefine allocator of WIN32OLE_VARIABLE to get rid of warning
[ruby-80x24.org.git] / re.c
blobd91909a743542a9c080900a34f8ed0bbff263083
1 /**********************************************************************
3 re.c -
5 $Author$
6 created at: Mon Aug 9 18:24:49 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10 **********************************************************************/
12 #include "ruby/internal/config.h"
14 #include <ctype.h>
16 #include "encindex.h"
17 #include "internal.h"
18 #include "internal/hash.h"
19 #include "internal/imemo.h"
20 #include "internal/re.h"
21 #include "internal/string.h"
22 #include "internal/variable.h"
23 #include "regint.h"
24 #include "ruby/encoding.h"
25 #include "ruby/re.h"
26 #include "ruby/util.h"
28 VALUE rb_eRegexpError;
30 typedef char onig_errmsg_buffer[ONIG_MAX_ERROR_MESSAGE_LEN];
31 #define errcpy(err, msg) strlcpy((err), (msg), ONIG_MAX_ERROR_MESSAGE_LEN)
33 #define BEG(no) (regs->beg[(no)])
34 #define END(no) (regs->end[(no)])
36 #if 'a' == 97 /* it's ascii */
37 static const char casetable[] = {
38 '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
39 '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
40 '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
41 '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
42 /* ' ' '!' '"' '#' '$' '%' '&' ''' */
43 '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
44 /* '(' ')' '*' '+' ',' '-' '.' '/' */
45 '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
46 /* '0' '1' '2' '3' '4' '5' '6' '7' */
47 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
48 /* '8' '9' ':' ';' '<' '=' '>' '?' */
49 '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
50 /* '@' 'A' 'B' 'C' 'D' 'E' 'F' 'G' */
51 '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
52 /* 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O' */
53 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
54 /* 'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' */
55 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
56 /* 'X' 'Y' 'Z' '[' '\' ']' '^' '_' */
57 '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
58 /* '`' 'a' 'b' 'c' 'd' 'e' 'f' 'g' */
59 '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
60 /* 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' */
61 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
62 /* 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' */
63 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
64 /* 'x' 'y' 'z' '{' '|' '}' '~' */
65 '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
66 '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
67 '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
68 '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
69 '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
70 '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
71 '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
72 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
73 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
74 '\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
75 '\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317',
76 '\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327',
77 '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
78 '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
79 '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
80 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
81 '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
83 #else
84 # error >>> "You lose. You will need a translation table for your character set." <<<
85 #endif
87 int
88 rb_memcicmp(const void *x, const void *y, long len)
90 const unsigned char *p1 = x, *p2 = y;
91 int tmp;
93 while (len--) {
94 if ((tmp = casetable[(unsigned)*p1++] - casetable[(unsigned)*p2++]))
95 return tmp;
97 return 0;
100 #ifdef HAVE_MEMMEM
101 static inline long
102 rb_memsearch_ss(const unsigned char *xs, long m, const unsigned char *ys, long n)
104 const unsigned char *y;
106 if ((y = memmem(ys, n, xs, m)) != NULL)
107 return y - ys;
108 else
109 return -1;
111 #else
112 static inline long
113 rb_memsearch_ss(const unsigned char *xs, long m, const unsigned char *ys, long n)
115 const unsigned char *x = xs, *xe = xs + m;
116 const unsigned char *y = ys, *ye = ys + n;
117 #define VALUE_MAX ((VALUE)~(VALUE)0)
118 VALUE hx, hy, mask = VALUE_MAX >> ((SIZEOF_VALUE - m) * CHAR_BIT);
120 if (m > SIZEOF_VALUE)
121 rb_bug("!!too long pattern string!!");
123 if (!(y = memchr(y, *x, n - m + 1)))
124 return -1;
126 /* Prepare hash value */
127 for (hx = *x++, hy = *y++; x < xe; ++x, ++y) {
128 hx <<= CHAR_BIT;
129 hy <<= CHAR_BIT;
130 hx |= *x;
131 hy |= *y;
133 /* Searching */
134 while (hx != hy) {
135 if (y == ye)
136 return -1;
137 hy <<= CHAR_BIT;
138 hy |= *y;
139 hy &= mask;
140 y++;
142 return y - ys - m;
144 #endif
146 static inline long
147 rb_memsearch_qs(const unsigned char *xs, long m, const unsigned char *ys, long n)
149 const unsigned char *x = xs, *xe = xs + m;
150 const unsigned char *y = ys;
151 VALUE i, qstable[256];
153 /* Preprocessing */
154 for (i = 0; i < 256; ++i)
155 qstable[i] = m + 1;
156 for (; x < xe; ++x)
157 qstable[*x] = xe - x;
158 /* Searching */
159 for (; y + m <= ys + n; y += *(qstable + y[m])) {
160 if (*xs == *y && memcmp(xs, y, m) == 0)
161 return y - ys;
163 return -1;
166 static inline unsigned int
167 rb_memsearch_qs_utf8_hash(const unsigned char *x)
169 register const unsigned int mix = 8353;
170 register unsigned int h = *x;
171 if (h < 0xC0) {
172 return h + 256;
174 else if (h < 0xE0) {
175 h *= mix;
176 h += x[1];
178 else if (h < 0xF0) {
179 h *= mix;
180 h += x[1];
181 h *= mix;
182 h += x[2];
184 else if (h < 0xF5) {
185 h *= mix;
186 h += x[1];
187 h *= mix;
188 h += x[2];
189 h *= mix;
190 h += x[3];
192 else {
193 return h + 256;
195 return (unsigned char)h;
198 static inline long
199 rb_memsearch_qs_utf8(const unsigned char *xs, long m, const unsigned char *ys, long n)
201 const unsigned char *x = xs, *xe = xs + m;
202 const unsigned char *y = ys;
203 VALUE i, qstable[512];
205 /* Preprocessing */
206 for (i = 0; i < 512; ++i) {
207 qstable[i] = m + 1;
209 for (; x < xe; ++x) {
210 qstable[rb_memsearch_qs_utf8_hash(x)] = xe - x;
212 /* Searching */
213 for (; y + m <= ys + n; y += qstable[rb_memsearch_qs_utf8_hash(y+m)]) {
214 if (*xs == *y && memcmp(xs, y, m) == 0)
215 return y - ys;
217 return -1;
220 static inline long
221 rb_memsearch_wchar(const unsigned char *xs, long m, const unsigned char *ys, long n)
223 const unsigned char *x = xs, x0 = *xs, *y = ys;
224 enum {char_size = 2};
226 for (n -= m; n >= 0; n -= char_size, y += char_size) {
227 if (x0 == *y && memcmp(x+1, y+1, m-1) == 0)
228 return y - ys;
230 return -1;
233 static inline long
234 rb_memsearch_qchar(const unsigned char *xs, long m, const unsigned char *ys, long n)
236 const unsigned char *x = xs, x0 = *xs, *y = ys;
237 enum {char_size = 4};
239 for (n -= m; n >= 0; n -= char_size, y += char_size) {
240 if (x0 == *y && memcmp(x+1, y+1, m-1) == 0)
241 return y - ys;
243 return -1;
246 long
247 rb_memsearch(const void *x0, long m, const void *y0, long n, rb_encoding *enc)
249 const unsigned char *x = x0, *y = y0;
251 if (m > n) return -1;
252 else if (m == n) {
253 return memcmp(x0, y0, m) == 0 ? 0 : -1;
255 else if (m < 1) {
256 return 0;
258 else if (m == 1) {
259 const unsigned char *ys = memchr(y, *x, n);
261 if (ys)
262 return ys - y;
263 else
264 return -1;
266 else if (LIKELY(rb_enc_mbminlen(enc) == 1)) {
267 if (m <= SIZEOF_VALUE) {
268 return rb_memsearch_ss(x0, m, y0, n);
270 else if (enc == rb_utf8_encoding()){
271 return rb_memsearch_qs_utf8(x0, m, y0, n);
274 else if (LIKELY(rb_enc_mbminlen(enc) == 2)) {
275 return rb_memsearch_wchar(x0, m, y0, n);
277 else if (LIKELY(rb_enc_mbminlen(enc) == 4)) {
278 return rb_memsearch_qchar(x0, m, y0, n);
280 return rb_memsearch_qs(x0, m, y0, n);
283 #define REG_LITERAL FL_USER5
284 #define REG_ENCODING_NONE FL_USER6
286 #define KCODE_FIXED FL_USER4
288 #define ARG_REG_OPTION_MASK \
289 (ONIG_OPTION_IGNORECASE|ONIG_OPTION_MULTILINE|ONIG_OPTION_EXTEND)
290 #define ARG_ENCODING_FIXED 16
291 #define ARG_ENCODING_NONE 32
293 static int
294 char_to_option(int c)
296 int val;
298 switch (c) {
299 case 'i':
300 val = ONIG_OPTION_IGNORECASE;
301 break;
302 case 'x':
303 val = ONIG_OPTION_EXTEND;
304 break;
305 case 'm':
306 val = ONIG_OPTION_MULTILINE;
307 break;
308 default:
309 val = 0;
310 break;
312 return val;
315 enum { OPTBUF_SIZE = 4 };
317 static char *
318 option_to_str(char str[OPTBUF_SIZE], int options)
320 char *p = str;
321 if (options & ONIG_OPTION_MULTILINE) *p++ = 'm';
322 if (options & ONIG_OPTION_IGNORECASE) *p++ = 'i';
323 if (options & ONIG_OPTION_EXTEND) *p++ = 'x';
324 *p = 0;
325 return str;
328 extern int
329 rb_char_to_option_kcode(int c, int *option, int *kcode)
331 *option = 0;
333 switch (c) {
334 case 'n':
335 *kcode = rb_ascii8bit_encindex();
336 return (*option = ARG_ENCODING_NONE);
337 case 'e':
338 *kcode = ENCINDEX_EUC_JP;
339 break;
340 case 's':
341 *kcode = ENCINDEX_Windows_31J;
342 break;
343 case 'u':
344 *kcode = rb_utf8_encindex();
345 break;
346 default:
347 *kcode = -1;
348 return (*option = char_to_option(c));
350 *option = ARG_ENCODING_FIXED;
351 return 1;
354 static void
355 rb_reg_check(VALUE re)
357 if (!RREGEXP_PTR(re) || !RREGEXP_SRC(re) || !RREGEXP_SRC_PTR(re)) {
358 rb_raise(rb_eTypeError, "uninitialized Regexp");
362 static void
363 rb_reg_expr_str(VALUE str, const char *s, long len,
364 rb_encoding *enc, rb_encoding *resenc, int term)
366 const char *p, *pend;
367 int cr = ENC_CODERANGE_UNKNOWN;
368 int need_escape = 0;
369 int c, clen;
371 p = s; pend = p + len;
372 rb_str_coderange_scan_restartable(p, pend, enc, &cr);
373 if (rb_enc_asciicompat(enc) && ENC_CODERANGE_CLEAN_P(cr)) {
374 while (p < pend) {
375 c = rb_enc_ascget(p, pend, &clen, enc);
376 if (c == -1) {
377 if (enc == resenc) {
378 p += mbclen(p, pend, enc);
380 else {
381 need_escape = 1;
382 break;
385 else if (c != term && rb_enc_isprint(c, enc)) {
386 p += clen;
388 else {
389 need_escape = 1;
390 break;
394 else {
395 need_escape = 1;
398 if (!need_escape) {
399 rb_str_buf_cat(str, s, len);
401 else {
402 int unicode_p = rb_enc_unicode_p(enc);
403 p = s;
404 while (p<pend) {
405 c = rb_enc_ascget(p, pend, &clen, enc);
406 if (c == '\\' && p+clen < pend) {
407 int n = clen + mbclen(p+clen, pend, enc);
408 rb_str_buf_cat(str, p, n);
409 p += n;
410 continue;
412 else if (c == -1) {
413 clen = rb_enc_precise_mbclen(p, pend, enc);
414 if (!MBCLEN_CHARFOUND_P(clen)) {
415 c = (unsigned char)*p;
416 clen = 1;
417 goto hex;
419 if (resenc) {
420 unsigned int c = rb_enc_mbc_to_codepoint(p, pend, enc);
421 rb_str_buf_cat_escaped_char(str, c, unicode_p);
423 else {
424 clen = MBCLEN_CHARFOUND_LEN(clen);
425 rb_str_buf_cat(str, p, clen);
428 else if (c == term) {
429 char c = '\\';
430 rb_str_buf_cat(str, &c, 1);
431 rb_str_buf_cat(str, p, clen);
433 else if (rb_enc_isprint(c, enc)) {
434 rb_str_buf_cat(str, p, clen);
436 else if (!rb_enc_isspace(c, enc)) {
437 char b[8];
439 hex:
440 snprintf(b, sizeof(b), "\\x%02X", c);
441 rb_str_buf_cat(str, b, 4);
443 else {
444 rb_str_buf_cat(str, p, clen);
446 p += clen;
451 static VALUE
452 rb_reg_desc(const char *s, long len, VALUE re)
454 rb_encoding *enc = rb_enc_get(re);
455 VALUE str = rb_str_buf_new2("/");
456 rb_encoding *resenc = rb_default_internal_encoding();
457 if (resenc == NULL) resenc = rb_default_external_encoding();
459 if (re && rb_enc_asciicompat(enc)) {
460 rb_enc_copy(str, re);
462 else {
463 rb_enc_associate(str, rb_usascii_encoding());
465 rb_reg_expr_str(str, s, len, enc, resenc, '/');
466 rb_str_buf_cat2(str, "/");
467 if (re) {
468 char opts[OPTBUF_SIZE];
469 rb_reg_check(re);
470 if (*option_to_str(opts, RREGEXP_PTR(re)->options))
471 rb_str_buf_cat2(str, opts);
472 if (RBASIC(re)->flags & REG_ENCODING_NONE)
473 rb_str_buf_cat2(str, "n");
475 return str;
480 * call-seq:
481 * rxp.source -> str
483 * Returns the original string of the pattern.
485 * /ab+c/ix.source #=> "ab+c"
487 * Note that escape sequences are retained as is.
489 * /\x20\+/.source #=> "\\x20\\+"
493 static VALUE
494 rb_reg_source(VALUE re)
496 VALUE str;
498 rb_reg_check(re);
499 str = rb_str_dup(RREGEXP_SRC(re));
500 return str;
504 * call-seq:
505 * rxp.inspect -> string
507 * Produce a nicely formatted string-version of _rxp_. Perhaps surprisingly,
508 * <code>#inspect</code> actually produces the more natural version of
509 * the string than <code>#to_s</code>.
511 * /ab+c/ix.inspect #=> "/ab+c/ix"
515 static VALUE
516 rb_reg_inspect(VALUE re)
518 if (!RREGEXP_PTR(re) || !RREGEXP_SRC(re) || !RREGEXP_SRC_PTR(re)) {
519 return rb_any_to_s(re);
521 return rb_reg_desc(RREGEXP_SRC_PTR(re), RREGEXP_SRC_LEN(re), re);
524 static VALUE rb_reg_str_with_term(VALUE re, int term);
527 * call-seq:
528 * rxp.to_s -> str
530 * Returns a string containing the regular expression and its options (using the
531 * <code>(?opts:source)</code> notation. This string can be fed back in to
532 * Regexp::new to a regular expression with the same semantics as the
533 * original. (However, <code>Regexp#==</code> may not return true
534 * when comparing the two, as the source of the regular expression
535 * itself may differ, as the example shows). Regexp#inspect produces
536 * a generally more readable version of <i>rxp</i>.
538 * r1 = /ab+c/ix #=> /ab+c/ix
539 * s1 = r1.to_s #=> "(?ix-m:ab+c)"
540 * r2 = Regexp.new(s1) #=> /(?ix-m:ab+c)/
541 * r1 == r2 #=> false
542 * r1.source #=> "ab+c"
543 * r2.source #=> "(?ix-m:ab+c)"
546 static VALUE
547 rb_reg_to_s(VALUE re)
549 return rb_reg_str_with_term(re, '/');
552 static VALUE
553 rb_reg_str_with_term(VALUE re, int term)
555 int options, opt;
556 const int embeddable = ONIG_OPTION_MULTILINE|ONIG_OPTION_IGNORECASE|ONIG_OPTION_EXTEND;
557 long len;
558 const UChar* ptr;
559 VALUE str = rb_str_buf_new2("(?");
560 char optbuf[OPTBUF_SIZE + 1]; /* for '-' */
561 rb_encoding *enc = rb_enc_get(re);
563 rb_reg_check(re);
565 rb_enc_copy(str, re);
566 options = RREGEXP_PTR(re)->options;
567 ptr = (UChar*)RREGEXP_SRC_PTR(re);
568 len = RREGEXP_SRC_LEN(re);
569 again:
570 if (len >= 4 && ptr[0] == '(' && ptr[1] == '?') {
571 int err = 1;
572 ptr += 2;
573 if ((len -= 2) > 0) {
574 do {
575 opt = char_to_option((int )*ptr);
576 if (opt != 0) {
577 options |= opt;
579 else {
580 break;
582 ++ptr;
583 } while (--len > 0);
585 if (len > 1 && *ptr == '-') {
586 ++ptr;
587 --len;
588 do {
589 opt = char_to_option((int )*ptr);
590 if (opt != 0) {
591 options &= ~opt;
593 else {
594 break;
596 ++ptr;
597 } while (--len > 0);
599 if (*ptr == ')') {
600 --len;
601 ++ptr;
602 goto again;
604 if (*ptr == ':' && ptr[len-1] == ')') {
605 Regexp *rp;
606 VALUE verbose = ruby_verbose;
607 ruby_verbose = Qfalse;
609 ++ptr;
610 len -= 2;
611 err = onig_new(&rp, ptr, ptr + len, options,
612 enc, OnigDefaultSyntax, NULL);
613 onig_free(rp);
614 ruby_verbose = verbose;
616 if (err) {
617 options = RREGEXP_PTR(re)->options;
618 ptr = (UChar*)RREGEXP_SRC_PTR(re);
619 len = RREGEXP_SRC_LEN(re);
623 if (*option_to_str(optbuf, options)) rb_str_buf_cat2(str, optbuf);
625 if ((options & embeddable) != embeddable) {
626 optbuf[0] = '-';
627 option_to_str(optbuf + 1, ~options);
628 rb_str_buf_cat2(str, optbuf);
631 rb_str_buf_cat2(str, ":");
632 if (rb_enc_asciicompat(enc)) {
633 rb_reg_expr_str(str, (char*)ptr, len, enc, NULL, term);
634 rb_str_buf_cat2(str, ")");
636 else {
637 const char *s, *e;
638 char *paren;
639 ptrdiff_t n;
640 rb_str_buf_cat2(str, ")");
641 rb_enc_associate(str, rb_usascii_encoding());
642 str = rb_str_encode(str, rb_enc_from_encoding(enc), 0, Qnil);
644 /* backup encoded ")" to paren */
645 s = RSTRING_PTR(str);
646 e = RSTRING_END(str);
647 s = rb_enc_left_char_head(s, e-1, e, enc);
648 n = e - s;
649 paren = ALLOCA_N(char, n);
650 memcpy(paren, s, n);
651 rb_str_resize(str, RSTRING_LEN(str) - n);
653 rb_reg_expr_str(str, (char*)ptr, len, enc, NULL, term);
654 rb_str_buf_cat(str, paren, n);
656 rb_enc_copy(str, re);
658 return str;
661 NORETURN(static void rb_reg_raise(const char *s, long len, const char *err, VALUE re));
663 static void
664 rb_reg_raise(const char *s, long len, const char *err, VALUE re)
666 VALUE desc = rb_reg_desc(s, len, re);
668 rb_raise(rb_eRegexpError, "%s: %"PRIsVALUE, err, desc);
671 static VALUE
672 rb_enc_reg_error_desc(const char *s, long len, rb_encoding *enc, int options, const char *err)
674 char opts[OPTBUF_SIZE + 1]; /* for '/' */
675 VALUE desc = rb_str_buf_new2(err);
676 rb_encoding *resenc = rb_default_internal_encoding();
677 if (resenc == NULL) resenc = rb_default_external_encoding();
679 rb_enc_associate(desc, enc);
680 rb_str_buf_cat2(desc, ": /");
681 rb_reg_expr_str(desc, s, len, enc, resenc, '/');
682 opts[0] = '/';
683 option_to_str(opts + 1, options);
684 rb_str_buf_cat2(desc, opts);
685 return rb_exc_new3(rb_eRegexpError, desc);
688 NORETURN(static void rb_enc_reg_raise(const char *s, long len, rb_encoding *enc, int options, const char *err));
690 static void
691 rb_enc_reg_raise(const char *s, long len, rb_encoding *enc, int options, const char *err)
693 rb_exc_raise(rb_enc_reg_error_desc(s, len, enc, options, err));
696 static VALUE
697 rb_reg_error_desc(VALUE str, int options, const char *err)
699 return rb_enc_reg_error_desc(RSTRING_PTR(str), RSTRING_LEN(str),
700 rb_enc_get(str), options, err);
703 NORETURN(static void rb_reg_raise_str(VALUE str, int options, const char *err));
705 static void
706 rb_reg_raise_str(VALUE str, int options, const char *err)
708 rb_exc_raise(rb_reg_error_desc(str, options, err));
713 * call-seq:
714 * rxp.casefold? -> true or false
716 * Returns the value of the case-insensitive flag.
718 * /a/.casefold? #=> false
719 * /a/i.casefold? #=> true
720 * /(?i:a)/.casefold? #=> false
723 static VALUE
724 rb_reg_casefold_p(VALUE re)
726 rb_reg_check(re);
727 return RBOOL(RREGEXP_PTR(re)->options & ONIG_OPTION_IGNORECASE);
732 * call-seq:
733 * rxp.options -> integer
735 * Returns the set of bits corresponding to the options used when
736 * creating this Regexp (see Regexp::new for details. Note that
737 * additional bits may be set in the returned options: these are used
738 * internally by the regular expression code. These extra bits are
739 * ignored if the options are passed to Regexp::new.
741 * Regexp::IGNORECASE #=> 1
742 * Regexp::EXTENDED #=> 2
743 * Regexp::MULTILINE #=> 4
745 * /cat/.options #=> 0
746 * /cat/ix.options #=> 3
747 * Regexp.new('cat', true).options #=> 1
748 * /\xa1\xa2/e.options #=> 16
750 * r = /cat/ix
751 * Regexp.new(r.source, r.options) #=> /cat/ix
754 static VALUE
755 rb_reg_options_m(VALUE re)
757 int options = rb_reg_options(re);
758 return INT2NUM(options);
761 static int
762 reg_names_iter(const OnigUChar *name, const OnigUChar *name_end,
763 int back_num, int *back_refs, OnigRegex regex, void *arg)
765 VALUE ary = (VALUE)arg;
766 rb_ary_push(ary, rb_enc_str_new((const char *)name, name_end-name, regex->enc));
767 return 0;
771 * call-seq:
772 * rxp.names -> [name1, name2, ...]
774 * Returns a list of names of captures as an array of strings.
776 * /(?<foo>.)(?<bar>.)(?<baz>.)/.names
777 * #=> ["foo", "bar", "baz"]
779 * /(?<foo>.)(?<foo>.)/.names
780 * #=> ["foo"]
782 * /(.)(.)/.names
783 * #=> []
786 static VALUE
787 rb_reg_names(VALUE re)
789 VALUE ary;
790 rb_reg_check(re);
791 ary = rb_ary_new_capa(onig_number_of_names(RREGEXP_PTR(re)));
792 onig_foreach_name(RREGEXP_PTR(re), reg_names_iter, (void*)ary);
793 return ary;
796 static int
797 reg_named_captures_iter(const OnigUChar *name, const OnigUChar *name_end,
798 int back_num, int *back_refs, OnigRegex regex, void *arg)
800 VALUE hash = (VALUE)arg;
801 VALUE ary = rb_ary_new2(back_num);
802 int i;
804 for (i = 0; i < back_num; i++)
805 rb_ary_store(ary, i, INT2NUM(back_refs[i]));
807 rb_hash_aset(hash, rb_str_new((const char*)name, name_end-name),ary);
809 return 0;
813 * call-seq:
814 * rxp.named_captures -> hash
816 * Returns a hash representing information about named captures of <i>rxp</i>.
818 * A key of the hash is a name of the named captures.
819 * A value of the hash is an array which is list of indexes of corresponding
820 * named captures.
822 * /(?<foo>.)(?<bar>.)/.named_captures
823 * #=> {"foo"=>[1], "bar"=>[2]}
825 * /(?<foo>.)(?<foo>.)/.named_captures
826 * #=> {"foo"=>[1, 2]}
828 * If there are no named captures, an empty hash is returned.
830 * /(.)(.)/.named_captures
831 * #=> {}
834 static VALUE
835 rb_reg_named_captures(VALUE re)
837 regex_t *reg = (rb_reg_check(re), RREGEXP_PTR(re));
838 VALUE hash = rb_hash_new_with_size(onig_number_of_names(reg));
839 onig_foreach_name(reg, reg_named_captures_iter, (void*)hash);
840 return hash;
843 static int
844 onig_new_with_source(regex_t** reg, const UChar* pattern, const UChar* pattern_end,
845 OnigOptionType option, OnigEncoding enc, const OnigSyntaxType* syntax,
846 OnigErrorInfo* einfo, const char *sourcefile, int sourceline)
848 int r;
850 *reg = (regex_t* )malloc(sizeof(regex_t));
851 if (IS_NULL(*reg)) return ONIGERR_MEMORY;
853 r = onig_reg_init(*reg, option, ONIGENC_CASE_FOLD_DEFAULT, enc, syntax);
854 if (r) goto err;
856 r = onig_compile_ruby(*reg, pattern, pattern_end, einfo, sourcefile, sourceline);
857 if (r) {
858 err:
859 onig_free(*reg);
860 *reg = NULL;
862 return r;
865 static Regexp*
866 make_regexp(const char *s, long len, rb_encoding *enc, int flags, onig_errmsg_buffer err,
867 const char *sourcefile, int sourceline)
869 Regexp *rp;
870 int r;
871 OnigErrorInfo einfo;
873 /* Handle escaped characters first. */
875 /* Build a copy of the string (in dest) with the
876 escaped characters translated, and generate the regex
877 from that.
880 r = onig_new_with_source(&rp, (UChar*)s, (UChar*)(s + len), flags,
881 enc, OnigDefaultSyntax, &einfo, sourcefile, sourceline);
882 if (r) {
883 onig_error_code_to_str((UChar*)err, r, &einfo);
884 return 0;
886 return rp;
891 * Document-class: MatchData
893 * MatchData encapsulates the result of matching a Regexp against
894 * string. It is returned by Regexp#match and String#match, and also
895 * stored in a global variable returned by Regexp.last_match.
897 * Usage:
899 * url = 'https://docs.ruby-lang.org/en/2.5.0/MatchData.html'
900 * m = url.match(/(\d\.?)+/) # => #<MatchData "2.5.0" 1:"0">
901 * m.string # => "https://docs.ruby-lang.org/en/2.5.0/MatchData.html"
902 * m.regexp # => /(\d\.?)+/
903 * # entire matched substring:
904 * m[0] # => "2.5.0"
906 * # Working with unnamed captures
907 * m = url.match(%r{([^/]+)/([^/]+)\.html$})
908 * m.captures # => ["2.5.0", "MatchData"]
909 * m[1] # => "2.5.0"
910 * m.values_at(1, 2) # => ["2.5.0", "MatchData"]
912 * # Working with named captures
913 * m = url.match(%r{(?<version>[^/]+)/(?<module>[^/]+)\.html$})
914 * m.captures # => ["2.5.0", "MatchData"]
915 * m.named_captures # => {"version"=>"2.5.0", "module"=>"MatchData"}
916 * m[:version] # => "2.5.0"
917 * m.values_at(:version, :module)
918 * # => ["2.5.0", "MatchData"]
919 * # Numerical indexes are working, too
920 * m[1] # => "2.5.0"
921 * m.values_at(1, 2) # => ["2.5.0", "MatchData"]
923 * == Global variables equivalence
925 * Parts of last MatchData (returned by Regexp.last_match) are also
926 * aliased as global variables:
928 * * <code>$~</code> is Regexp.last_match;
929 * * <code>$&</code> is Regexp.last_match<code>[ 0 ]</code>;
930 * * <code>$1</code>, <code>$2</code>, and so on are
931 * Regexp.last_match<code>[ i ]</code> (captures by number);
932 * * <code>$`</code> is Regexp.last_match<code>.pre_match</code>;
933 * * <code>$'</code> is Regexp.last_match<code>.post_match</code>;
934 * * <code>$+</code> is Regexp.last_match<code>[ -1 ]</code> (the last capture).
936 * See also "Special global variables" section in Regexp documentation.
939 VALUE rb_cMatch;
941 static VALUE
942 match_alloc(VALUE klass)
944 NEWOBJ_OF(match, struct RMatch, klass, T_MATCH);
946 match->str = 0;
947 match->rmatch = 0;
948 match->regexp = 0;
949 match->rmatch = ZALLOC(struct rmatch);
951 return (VALUE)match;
955 rb_reg_region_copy(struct re_registers *to, const struct re_registers *from)
957 onig_region_copy(to, (OnigRegion *)from);
958 if (to->allocated) return 0;
959 rb_gc();
960 onig_region_copy(to, (OnigRegion *)from);
961 if (to->allocated) return 0;
962 return ONIGERR_MEMORY;
965 typedef struct {
966 long byte_pos;
967 long char_pos;
968 } pair_t;
970 static int
971 pair_byte_cmp(const void *pair1, const void *pair2)
973 long diff = ((pair_t*)pair1)->byte_pos - ((pair_t*)pair2)->byte_pos;
974 #if SIZEOF_LONG > SIZEOF_INT
975 return diff ? diff > 0 ? 1 : -1 : 0;
976 #else
977 return (int)diff;
978 #endif
981 static void
982 update_char_offset(VALUE match)
984 struct rmatch *rm = RMATCH(match)->rmatch;
985 struct re_registers *regs;
986 int i, num_regs, num_pos;
987 long c;
988 char *s, *p, *q;
989 rb_encoding *enc;
990 pair_t *pairs;
992 if (rm->char_offset_num_allocated)
993 return;
995 regs = &rm->regs;
996 num_regs = rm->regs.num_regs;
998 if (rm->char_offset_num_allocated < num_regs) {
999 REALLOC_N(rm->char_offset, struct rmatch_offset, num_regs);
1000 rm->char_offset_num_allocated = num_regs;
1003 enc = rb_enc_get(RMATCH(match)->str);
1004 if (rb_enc_mbmaxlen(enc) == 1) {
1005 for (i = 0; i < num_regs; i++) {
1006 rm->char_offset[i].beg = BEG(i);
1007 rm->char_offset[i].end = END(i);
1009 return;
1012 pairs = ALLOCA_N(pair_t, num_regs*2);
1013 num_pos = 0;
1014 for (i = 0; i < num_regs; i++) {
1015 if (BEG(i) < 0)
1016 continue;
1017 pairs[num_pos++].byte_pos = BEG(i);
1018 pairs[num_pos++].byte_pos = END(i);
1020 qsort(pairs, num_pos, sizeof(pair_t), pair_byte_cmp);
1022 s = p = RSTRING_PTR(RMATCH(match)->str);
1023 c = 0;
1024 for (i = 0; i < num_pos; i++) {
1025 q = s + pairs[i].byte_pos;
1026 c += rb_enc_strlen(p, q, enc);
1027 pairs[i].char_pos = c;
1028 p = q;
1031 for (i = 0; i < num_regs; i++) {
1032 pair_t key, *found;
1033 if (BEG(i) < 0) {
1034 rm->char_offset[i].beg = -1;
1035 rm->char_offset[i].end = -1;
1036 continue;
1039 key.byte_pos = BEG(i);
1040 found = bsearch(&key, pairs, num_pos, sizeof(pair_t), pair_byte_cmp);
1041 rm->char_offset[i].beg = found->char_pos;
1043 key.byte_pos = END(i);
1044 found = bsearch(&key, pairs, num_pos, sizeof(pair_t), pair_byte_cmp);
1045 rm->char_offset[i].end = found->char_pos;
1049 static void
1050 match_check(VALUE match)
1052 if (!RMATCH(match)->regexp) {
1053 rb_raise(rb_eTypeError, "uninitialized MatchData");
1057 /* :nodoc: */
1058 static VALUE
1059 match_init_copy(VALUE obj, VALUE orig)
1061 struct rmatch *rm;
1063 if (!OBJ_INIT_COPY(obj, orig)) return obj;
1065 RMATCH(obj)->str = RMATCH(orig)->str;
1066 RMATCH(obj)->regexp = RMATCH(orig)->regexp;
1068 rm = RMATCH(obj)->rmatch;
1069 if (rb_reg_region_copy(&rm->regs, RMATCH_REGS(orig)))
1070 rb_memerror();
1072 if (RMATCH(orig)->rmatch->char_offset_num_allocated) {
1073 if (rm->char_offset_num_allocated < rm->regs.num_regs) {
1074 REALLOC_N(rm->char_offset, struct rmatch_offset, rm->regs.num_regs);
1075 rm->char_offset_num_allocated = rm->regs.num_regs;
1077 MEMCPY(rm->char_offset, RMATCH(orig)->rmatch->char_offset,
1078 struct rmatch_offset, rm->regs.num_regs);
1079 RB_GC_GUARD(orig);
1082 return obj;
1087 * call-seq:
1088 * mtch.regexp -> regexp
1090 * Returns the regexp.
1092 * m = /a.*b/.match("abc")
1093 * m.regexp #=> /a.*b/
1096 static VALUE
1097 match_regexp(VALUE match)
1099 VALUE regexp;
1100 match_check(match);
1101 regexp = RMATCH(match)->regexp;
1102 if (NIL_P(regexp)) {
1103 VALUE str = rb_reg_nth_match(0, match);
1104 regexp = rb_reg_regcomp(rb_reg_quote(str));
1105 RMATCH(match)->regexp = regexp;
1107 return regexp;
1111 * call-seq:
1112 * mtch.names -> [name1, name2, ...]
1114 * Returns a list of names of captures as an array of strings.
1115 * This is the same as mtch.regexp.names.
1117 * /(?<foo>.)(?<bar>.)(?<baz>.)/.match("hoge").names
1118 * #=> ["foo", "bar", "baz"]
1120 * m = /(?<x>.)(?<y>.)?/.match("a") #=> #<MatchData "a" x:"a" y:nil>
1121 * m.names #=> ["x", "y"]
1124 static VALUE
1125 match_names(VALUE match)
1127 match_check(match);
1128 if (NIL_P(RMATCH(match)->regexp))
1129 return rb_ary_new_capa(0);
1130 return rb_reg_names(RMATCH(match)->regexp);
1134 * call-seq:
1135 * mtch.length -> integer
1136 * mtch.size -> integer
1138 * Returns the number of elements in the match array.
1140 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1141 * m.length #=> 5
1142 * m.size #=> 5
1145 static VALUE
1146 match_size(VALUE match)
1148 match_check(match);
1149 return INT2FIX(RMATCH_REGS(match)->num_regs);
1152 static int name_to_backref_number(struct re_registers *, VALUE, const char*, const char*);
1153 NORETURN(static void name_to_backref_error(VALUE name));
1155 static void
1156 name_to_backref_error(VALUE name)
1158 rb_raise(rb_eIndexError, "undefined group name reference: % "PRIsVALUE,
1159 name);
1162 static void
1163 backref_number_check(struct re_registers *regs, int i)
1165 if (i < 0 || regs->num_regs <= i)
1166 rb_raise(rb_eIndexError, "index %d out of matches", i);
1169 static int
1170 match_backref_number(VALUE match, VALUE backref)
1172 const char *name;
1173 int num;
1175 struct re_registers *regs = RMATCH_REGS(match);
1176 VALUE regexp = RMATCH(match)->regexp;
1178 match_check(match);
1179 if (SYMBOL_P(backref)) {
1180 backref = rb_sym2str(backref);
1182 else if (!RB_TYPE_P(backref, T_STRING)) {
1183 return NUM2INT(backref);
1185 name = StringValueCStr(backref);
1187 num = name_to_backref_number(regs, regexp, name, name + RSTRING_LEN(backref));
1189 if (num < 1) {
1190 name_to_backref_error(backref);
1193 return num;
1197 rb_reg_backref_number(VALUE match, VALUE backref)
1199 return match_backref_number(match, backref);
1203 * call-seq:
1204 * mtch.offset(n) -> array
1206 * Returns a two-element array containing the beginning and ending offsets of
1207 * the <em>n</em>th match.
1208 * <em>n</em> can be a string or symbol to reference a named capture.
1210 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1211 * m.offset(0) #=> [1, 7]
1212 * m.offset(4) #=> [6, 7]
1214 * m = /(?<foo>.)(.)(?<bar>.)/.match("hoge")
1215 * p m.offset(:foo) #=> [0, 1]
1216 * p m.offset(:bar) #=> [2, 3]
1220 static VALUE
1221 match_offset(VALUE match, VALUE n)
1223 int i = match_backref_number(match, n);
1224 struct re_registers *regs = RMATCH_REGS(match);
1226 match_check(match);
1227 backref_number_check(regs, i);
1229 if (BEG(i) < 0)
1230 return rb_assoc_new(Qnil, Qnil);
1232 update_char_offset(match);
1233 return rb_assoc_new(INT2FIX(RMATCH(match)->rmatch->char_offset[i].beg),
1234 INT2FIX(RMATCH(match)->rmatch->char_offset[i].end));
1239 * call-seq:
1240 * mtch.begin(n) -> integer
1242 * Returns the offset of the start of the <em>n</em>th element of the match
1243 * array in the string.
1244 * <em>n</em> can be a string or symbol to reference a named capture.
1246 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1247 * m.begin(0) #=> 1
1248 * m.begin(2) #=> 2
1250 * m = /(?<foo>.)(.)(?<bar>.)/.match("hoge")
1251 * p m.begin(:foo) #=> 0
1252 * p m.begin(:bar) #=> 2
1255 static VALUE
1256 match_begin(VALUE match, VALUE n)
1258 int i = match_backref_number(match, n);
1259 struct re_registers *regs = RMATCH_REGS(match);
1261 match_check(match);
1262 backref_number_check(regs, i);
1264 if (BEG(i) < 0)
1265 return Qnil;
1267 update_char_offset(match);
1268 return INT2FIX(RMATCH(match)->rmatch->char_offset[i].beg);
1273 * call-seq:
1274 * mtch.end(n) -> integer
1276 * Returns the offset of the character immediately following the end of the
1277 * <em>n</em>th element of the match array in the string.
1278 * <em>n</em> can be a string or symbol to reference a named capture.
1280 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1281 * m.end(0) #=> 7
1282 * m.end(2) #=> 3
1284 * m = /(?<foo>.)(.)(?<bar>.)/.match("hoge")
1285 * p m.end(:foo) #=> 1
1286 * p m.end(:bar) #=> 3
1289 static VALUE
1290 match_end(VALUE match, VALUE n)
1292 int i = match_backref_number(match, n);
1293 struct re_registers *regs = RMATCH_REGS(match);
1295 match_check(match);
1296 backref_number_check(regs, i);
1298 if (BEG(i) < 0)
1299 return Qnil;
1301 update_char_offset(match);
1302 return INT2FIX(RMATCH(match)->rmatch->char_offset[i].end);
1306 * call-seq:
1307 * mtch.match(n) -> string or nil
1309 * Returns the captured substring corresponding to the argument.
1310 * <em>n</em> can be a string or symbol to reference a named capture.
1312 * m = /(.)(.)(\d+)(\d)(\w)?/.match("THX1138.")
1313 * m.match(0) #=> "HX1138"
1314 * m.match(4) #=> "8"
1315 * m.match(5) #=> nil
1317 * m = /(?<foo>.)(.)(?<bar>.+)/.match("hoge")
1318 * m.match(:foo) #=> "h"
1319 * m.match(:bar) #=> "ge"
1323 static VALUE
1324 match_nth(VALUE match, VALUE n)
1326 int i = match_backref_number(match, n);
1327 struct re_registers *regs = RMATCH_REGS(match);
1329 backref_number_check(regs, i);
1331 long start = BEG(i), end = END(i);
1332 if (start < 0)
1333 return Qnil;
1335 return rb_str_subseq(RMATCH(match)->str, start, end - start);
1339 * call-seq:
1340 * mtch.match_length(n) -> array
1342 * Returns the length of the captured substring corresponding to the argument.
1343 * <em>n</em> can be a string or symbol to reference a named capture.
1345 * m = /(.)(.)(\d+)(\d)(\w)?/.match("THX1138.")
1346 * m.match_length(0) #=> 6
1347 * m.match_length(4) #=> 1
1348 * m.match_length(5) #=> nil
1350 * m = /(?<foo>.)(.)(?<bar>.+)/.match("hoge")
1351 * m.match_length(:foo) #=> 1
1352 * m.match_length(:bar) #=> 2
1356 static VALUE
1357 match_nth_length(VALUE match, VALUE n)
1359 int i = match_backref_number(match, n);
1360 struct re_registers *regs = RMATCH_REGS(match);
1362 match_check(match);
1363 backref_number_check(regs, i);
1365 if (BEG(i) < 0)
1366 return Qnil;
1368 update_char_offset(match);
1369 const struct rmatch_offset *const ofs =
1370 &RMATCH(match)->rmatch->char_offset[i];
1371 return LONG2NUM(ofs->end - ofs->beg);
1374 #define MATCH_BUSY FL_USER2
1376 void
1377 rb_match_busy(VALUE match)
1379 FL_SET(match, MATCH_BUSY);
1382 void
1383 rb_match_unbusy(VALUE match)
1385 FL_UNSET(match, MATCH_BUSY);
1389 rb_match_count(VALUE match)
1391 struct re_registers *regs;
1392 if (NIL_P(match)) return -1;
1393 regs = RMATCH_REGS(match);
1394 if (!regs) return -1;
1395 return regs->num_regs;
1399 rb_match_nth_defined(int nth, VALUE match)
1401 struct re_registers *regs;
1402 if (NIL_P(match)) return FALSE;
1403 regs = RMATCH_REGS(match);
1404 if (!regs) return FALSE;
1405 if (nth >= regs->num_regs) {
1406 return FALSE;
1408 if (nth < 0) {
1409 nth += regs->num_regs;
1410 if (nth <= 0) return FALSE;
1412 return (BEG(nth) != -1);
1415 static void
1416 match_set_string(VALUE m, VALUE string, long pos, long len)
1418 struct RMatch *match = (struct RMatch *)m;
1419 struct rmatch *rmatch = match->rmatch;
1421 match->str = string;
1422 match->regexp = Qnil;
1423 int err = onig_region_resize(&rmatch->regs, 1);
1424 if (err) rb_memerror();
1425 rmatch->regs.beg[0] = pos;
1426 rmatch->regs.end[0] = pos + len;
1429 void
1430 rb_backref_set_string(VALUE string, long pos, long len)
1432 VALUE match = rb_backref_get();
1433 if (NIL_P(match) || FL_TEST(match, MATCH_BUSY)) {
1434 match = match_alloc(rb_cMatch);
1436 match_set_string(match, string, pos, len);
1437 rb_backref_set(match);
1441 * call-seq:
1442 * rxp.fixed_encoding? -> true or false
1444 * Returns false if rxp is applicable to
1445 * a string with any ASCII compatible encoding.
1446 * Returns true otherwise.
1448 * r = /a/
1449 * r.fixed_encoding? #=> false
1450 * r =~ "\u{6666} a" #=> 2
1451 * r =~ "\xa1\xa2 a".force_encoding("euc-jp") #=> 2
1452 * r =~ "abc".force_encoding("euc-jp") #=> 0
1454 * r = /a/u
1455 * r.fixed_encoding? #=> true
1456 * r.encoding #=> #<Encoding:UTF-8>
1457 * r =~ "\u{6666} a" #=> 2
1458 * r =~ "\xa1\xa2".force_encoding("euc-jp") #=> Encoding::CompatibilityError
1459 * r =~ "abc".force_encoding("euc-jp") #=> 0
1461 * r = /\u{6666}/
1462 * r.fixed_encoding? #=> true
1463 * r.encoding #=> #<Encoding:UTF-8>
1464 * r =~ "\u{6666} a" #=> 0
1465 * r =~ "\xa1\xa2".force_encoding("euc-jp") #=> Encoding::CompatibilityError
1466 * r =~ "abc".force_encoding("euc-jp") #=> nil
1469 static VALUE
1470 rb_reg_fixed_encoding_p(VALUE re)
1472 return RBOOL(FL_TEST(re, KCODE_FIXED));
1475 static VALUE
1476 rb_reg_preprocess(const char *p, const char *end, rb_encoding *enc,
1477 rb_encoding **fixed_enc, onig_errmsg_buffer err);
1479 NORETURN(static void reg_enc_error(VALUE re, VALUE str));
1481 static void
1482 reg_enc_error(VALUE re, VALUE str)
1484 rb_raise(rb_eEncCompatError,
1485 "incompatible encoding regexp match (%s regexp with %s string)",
1486 rb_enc_name(rb_enc_get(re)),
1487 rb_enc_name(rb_enc_get(str)));
1490 static inline int
1491 str_coderange(VALUE str)
1493 int cr = ENC_CODERANGE(str);
1494 if (cr == ENC_CODERANGE_UNKNOWN) {
1495 cr = rb_enc_str_coderange(str);
1497 return cr;
1500 static rb_encoding*
1501 rb_reg_prepare_enc(VALUE re, VALUE str, int warn)
1503 rb_encoding *enc = 0;
1504 int cr = str_coderange(str);
1506 if (cr == ENC_CODERANGE_BROKEN) {
1507 rb_raise(rb_eArgError,
1508 "invalid byte sequence in %s",
1509 rb_enc_name(rb_enc_get(str)));
1512 rb_reg_check(re);
1513 enc = rb_enc_get(str);
1514 if (RREGEXP_PTR(re)->enc == enc) {
1516 else if (cr == ENC_CODERANGE_7BIT &&
1517 RREGEXP_PTR(re)->enc == rb_usascii_encoding()) {
1518 enc = RREGEXP_PTR(re)->enc;
1520 else if (!rb_enc_asciicompat(enc)) {
1521 reg_enc_error(re, str);
1523 else if (rb_reg_fixed_encoding_p(re)) {
1524 if ((!rb_enc_asciicompat(RREGEXP_PTR(re)->enc) ||
1525 cr != ENC_CODERANGE_7BIT)) {
1526 reg_enc_error(re, str);
1528 enc = RREGEXP_PTR(re)->enc;
1530 else if (warn && (RBASIC(re)->flags & REG_ENCODING_NONE) &&
1531 enc != rb_ascii8bit_encoding() &&
1532 cr != ENC_CODERANGE_7BIT) {
1533 rb_warn("historical binary regexp match /.../n against %s string",
1534 rb_enc_name(enc));
1536 return enc;
1539 regex_t *
1540 rb_reg_prepare_re0(VALUE re, VALUE str, onig_errmsg_buffer err)
1542 regex_t *reg = RREGEXP_PTR(re);
1543 int r;
1544 OnigErrorInfo einfo;
1545 const char *pattern;
1546 VALUE unescaped;
1547 rb_encoding *fixed_enc = 0;
1548 rb_encoding *enc = rb_reg_prepare_enc(re, str, 1);
1550 if (reg->enc == enc) return reg;
1552 rb_reg_check(re);
1553 reg = RREGEXP_PTR(re);
1554 pattern = RREGEXP_SRC_PTR(re);
1556 unescaped = rb_reg_preprocess(
1557 pattern, pattern + RREGEXP_SRC_LEN(re), enc,
1558 &fixed_enc, err);
1560 if (NIL_P(unescaped)) {
1561 rb_raise(rb_eArgError, "regexp preprocess failed: %s", err);
1564 const char *ptr;
1565 long len;
1566 RSTRING_GETMEM(unescaped, ptr, len);
1567 r = onig_new(&reg, (UChar *)ptr, (UChar *)(ptr + len),
1568 reg->options, enc,
1569 OnigDefaultSyntax, &einfo);
1570 if (r) {
1571 onig_error_code_to_str((UChar*)err, r, &einfo);
1572 rb_reg_raise(pattern, RREGEXP_SRC_LEN(re), err, re);
1575 RB_GC_GUARD(unescaped);
1576 return reg;
1579 regex_t *
1580 rb_reg_prepare_re(VALUE re, VALUE str)
1582 onig_errmsg_buffer err = "";
1583 return rb_reg_prepare_re0(re, str, err);
1586 long
1587 rb_reg_adjust_startpos(VALUE re, VALUE str, long pos, int reverse)
1589 long range;
1590 rb_encoding *enc;
1591 UChar *p, *string;
1593 enc = rb_reg_prepare_enc(re, str, 0);
1595 if (reverse) {
1596 range = -pos;
1598 else {
1599 range = RSTRING_LEN(str) - pos;
1602 if (pos > 0 && ONIGENC_MBC_MAXLEN(enc) != 1 && pos < RSTRING_LEN(str)) {
1603 string = (UChar*)RSTRING_PTR(str);
1605 if (range > 0) {
1606 p = onigenc_get_right_adjust_char_head(enc, string, string + pos, string + RSTRING_LEN(str));
1608 else {
1609 p = ONIGENC_LEFT_ADJUST_CHAR_HEAD(enc, string, string + pos, string + RSTRING_LEN(str));
1611 return p - string;
1614 return pos;
1617 /* returns byte offset */
1618 static long
1619 rb_reg_search_set_match(VALUE re, VALUE str, long pos, int reverse, int set_backref_str, VALUE *set_match)
1621 long result;
1622 VALUE match;
1623 struct re_registers regi, *regs = &regi;
1624 char *start, *range;
1625 long len;
1626 regex_t *reg;
1627 int tmpreg;
1628 onig_errmsg_buffer err = "";
1630 RSTRING_GETMEM(str, start, len);
1631 range = start;
1632 if (pos > len || pos < 0) {
1633 rb_backref_set(Qnil);
1634 return -1;
1637 reg = rb_reg_prepare_re0(re, str, err);
1638 tmpreg = reg != RREGEXP_PTR(re);
1639 if (!tmpreg) RREGEXP(re)->usecnt++;
1641 MEMZERO(regs, struct re_registers, 1);
1642 if (!reverse) {
1643 range += len;
1645 result = onig_search(reg,
1646 (UChar*)start,
1647 ((UChar*)(start + len)),
1648 ((UChar*)(start + pos)),
1649 ((UChar*)range),
1650 regs, ONIG_OPTION_NONE);
1651 if (!tmpreg) RREGEXP(re)->usecnt--;
1652 if (tmpreg) {
1653 if (RREGEXP(re)->usecnt) {
1654 onig_free(reg);
1656 else {
1657 onig_free(RREGEXP_PTR(re));
1658 RREGEXP_PTR(re) = reg;
1661 if (result < 0) {
1662 if (regs == &regi)
1663 onig_region_free(regs, 0);
1664 if (result == ONIG_MISMATCH) {
1665 rb_backref_set(Qnil);
1666 return result;
1668 else {
1669 onig_error_code_to_str((UChar*)err, (int)result);
1670 rb_reg_raise(RREGEXP_SRC_PTR(re), RREGEXP_SRC_LEN(re), err, re);
1674 match = match_alloc(rb_cMatch);
1675 int copy_err = rb_reg_region_copy(RMATCH_REGS(match), regs);
1676 onig_region_free(regs, 0);
1677 if (copy_err) rb_memerror();
1679 if (set_backref_str) {
1680 RMATCH(match)->str = rb_str_new4(str);
1683 RMATCH(match)->regexp = re;
1684 rb_backref_set(match);
1685 if (set_match) *set_match = match;
1687 return result;
1690 long
1691 rb_reg_search0(VALUE re, VALUE str, long pos, int reverse, int set_backref_str)
1693 return rb_reg_search_set_match(re, str, pos, reverse, set_backref_str, NULL);
1696 long
1697 rb_reg_search(VALUE re, VALUE str, long pos, int reverse)
1699 return rb_reg_search0(re, str, pos, reverse, 1);
1702 bool
1703 rb_reg_start_with_p(VALUE re, VALUE str)
1705 long result;
1706 VALUE match;
1707 struct re_registers regi, *regs = &regi;
1708 regex_t *reg;
1709 int tmpreg;
1710 onig_errmsg_buffer err = "";
1712 reg = rb_reg_prepare_re0(re, str, err);
1713 tmpreg = reg != RREGEXP_PTR(re);
1714 if (!tmpreg) RREGEXP(re)->usecnt++;
1716 match = rb_backref_get();
1717 if (!NIL_P(match)) {
1718 if (FL_TEST(match, MATCH_BUSY)) {
1719 match = Qnil;
1721 else {
1722 regs = RMATCH_REGS(match);
1725 if (NIL_P(match)) {
1726 MEMZERO(regs, struct re_registers, 1);
1728 const char *ptr;
1729 long len;
1730 RSTRING_GETMEM(str, ptr, len);
1731 result = onig_match(reg,
1732 (UChar*)(ptr),
1733 ((UChar*)(ptr + len)),
1734 (UChar*)(ptr),
1735 regs, ONIG_OPTION_NONE);
1736 if (!tmpreg) RREGEXP(re)->usecnt--;
1737 if (tmpreg) {
1738 if (RREGEXP(re)->usecnt) {
1739 onig_free(reg);
1741 else {
1742 onig_free(RREGEXP_PTR(re));
1743 RREGEXP_PTR(re) = reg;
1746 if (result < 0) {
1747 if (regs == &regi)
1748 onig_region_free(regs, 0);
1749 if (result == ONIG_MISMATCH) {
1750 rb_backref_set(Qnil);
1751 return false;
1753 else {
1754 onig_error_code_to_str((UChar*)err, (int)result);
1755 rb_reg_raise(RREGEXP_SRC_PTR(re), RREGEXP_SRC_LEN(re), err, re);
1759 if (NIL_P(match)) {
1760 int err;
1761 match = match_alloc(rb_cMatch);
1762 err = rb_reg_region_copy(RMATCH_REGS(match), regs);
1763 onig_region_free(regs, 0);
1764 if (err) rb_memerror();
1767 RMATCH(match)->str = rb_str_new4(str);
1769 RMATCH(match)->regexp = re;
1770 rb_backref_set(match);
1772 return true;
1775 VALUE
1776 rb_reg_nth_defined(int nth, VALUE match)
1778 struct re_registers *regs;
1779 if (NIL_P(match)) return Qnil;
1780 match_check(match);
1781 regs = RMATCH_REGS(match);
1782 if (nth >= regs->num_regs) {
1783 return Qnil;
1785 if (nth < 0) {
1786 nth += regs->num_regs;
1787 if (nth <= 0) return Qnil;
1789 return RBOOL(BEG(nth) != -1);
1792 VALUE
1793 rb_reg_nth_match(int nth, VALUE match)
1795 VALUE str;
1796 long start, end, len;
1797 struct re_registers *regs;
1799 if (NIL_P(match)) return Qnil;
1800 match_check(match);
1801 regs = RMATCH_REGS(match);
1802 if (nth >= regs->num_regs) {
1803 return Qnil;
1805 if (nth < 0) {
1806 nth += regs->num_regs;
1807 if (nth <= 0) return Qnil;
1809 start = BEG(nth);
1810 if (start == -1) return Qnil;
1811 end = END(nth);
1812 len = end - start;
1813 str = rb_str_subseq(RMATCH(match)->str, start, len);
1814 return str;
1817 VALUE
1818 rb_reg_last_match(VALUE match)
1820 return rb_reg_nth_match(0, match);
1825 * call-seq:
1826 * mtch.pre_match -> str
1828 * Returns the portion of the original string before the current match.
1829 * Equivalent to the special variable <code>$`</code>.
1831 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1832 * m.pre_match #=> "T"
1835 VALUE
1836 rb_reg_match_pre(VALUE match)
1838 VALUE str;
1839 struct re_registers *regs;
1841 if (NIL_P(match)) return Qnil;
1842 match_check(match);
1843 regs = RMATCH_REGS(match);
1844 if (BEG(0) == -1) return Qnil;
1845 str = rb_str_subseq(RMATCH(match)->str, 0, BEG(0));
1846 return str;
1851 * call-seq:
1852 * mtch.post_match -> str
1854 * Returns the portion of the original string after the current match.
1855 * Equivalent to the special variable <code>$'</code>.
1857 * m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie")
1858 * m.post_match #=> ": The Movie"
1861 VALUE
1862 rb_reg_match_post(VALUE match)
1864 VALUE str;
1865 long pos;
1866 struct re_registers *regs;
1868 if (NIL_P(match)) return Qnil;
1869 match_check(match);
1870 regs = RMATCH_REGS(match);
1871 if (BEG(0) == -1) return Qnil;
1872 str = RMATCH(match)->str;
1873 pos = END(0);
1874 str = rb_str_subseq(str, pos, RSTRING_LEN(str) - pos);
1875 return str;
1878 VALUE
1879 rb_reg_match_last(VALUE match)
1881 int i;
1882 struct re_registers *regs;
1884 if (NIL_P(match)) return Qnil;
1885 match_check(match);
1886 regs = RMATCH_REGS(match);
1887 if (BEG(0) == -1) return Qnil;
1889 for (i=regs->num_regs-1; BEG(i) == -1 && i > 0; i--)
1891 if (i == 0) return Qnil;
1892 return rb_reg_nth_match(i, match);
1895 static VALUE
1896 last_match_getter(ID _x, VALUE *_y)
1898 return rb_reg_last_match(rb_backref_get());
1901 static VALUE
1902 prematch_getter(ID _x, VALUE *_y)
1904 return rb_reg_match_pre(rb_backref_get());
1907 static VALUE
1908 postmatch_getter(ID _x, VALUE *_y)
1910 return rb_reg_match_post(rb_backref_get());
1913 static VALUE
1914 last_paren_match_getter(ID _x, VALUE *_y)
1916 return rb_reg_match_last(rb_backref_get());
1919 static VALUE
1920 match_array(VALUE match, int start)
1922 struct re_registers *regs;
1923 VALUE ary;
1924 VALUE target;
1925 int i;
1927 match_check(match);
1928 regs = RMATCH_REGS(match);
1929 ary = rb_ary_new2(regs->num_regs);
1930 target = RMATCH(match)->str;
1932 for (i=start; i<regs->num_regs; i++) {
1933 if (regs->beg[i] == -1) {
1934 rb_ary_push(ary, Qnil);
1936 else {
1937 VALUE str = rb_str_subseq(target, regs->beg[i], regs->end[i]-regs->beg[i]);
1938 rb_ary_push(ary, str);
1941 return ary;
1946 * call-seq:
1947 * mtch.to_a -> anArray
1949 * Returns the array of matches.
1951 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1952 * m.to_a #=> ["HX1138", "H", "X", "113", "8"]
1954 * Because <code>to_a</code> is called when expanding
1955 * <code>*</code><em>variable</em>, there's a useful assignment
1956 * shortcut for extracting matched fields. This is slightly slower than
1957 * accessing the fields directly (as an intermediate array is
1958 * generated).
1960 * all,f1,f2,f3 = * /(.)(.)(\d+)(\d)/.match("THX1138.")
1961 * all #=> "HX1138"
1962 * f1 #=> "H"
1963 * f2 #=> "X"
1964 * f3 #=> "113"
1967 static VALUE
1968 match_to_a(VALUE match)
1970 return match_array(match, 0);
1975 * call-seq:
1976 * mtch.captures -> array
1978 * Returns the array of captures; equivalent to <code>mtch.to_a[1..-1]</code>.
1980 * f1,f2,f3,f4 = /(.)(.)(\d+)(\d)/.match("THX1138.").captures
1981 * f1 #=> "H"
1982 * f2 #=> "X"
1983 * f3 #=> "113"
1984 * f4 #=> "8"
1986 static VALUE
1987 match_captures(VALUE match)
1989 return match_array(match, 1);
1992 static int
1993 name_to_backref_number(struct re_registers *regs, VALUE regexp, const char* name, const char* name_end)
1995 if (NIL_P(regexp)) return -1;
1996 return onig_name_to_backref_number(RREGEXP_PTR(regexp),
1997 (const unsigned char *)name, (const unsigned char *)name_end, regs);
2000 #define NAME_TO_NUMBER(regs, re, name, name_ptr, name_end) \
2001 (NIL_P(re) ? 0 : \
2002 !rb_enc_compatible(RREGEXP_SRC(re), (name)) ? 0 : \
2003 name_to_backref_number((regs), (re), (name_ptr), (name_end)))
2005 static int
2006 namev_to_backref_number(struct re_registers *regs, VALUE re, VALUE name)
2008 int num;
2010 if (SYMBOL_P(name)) {
2011 name = rb_sym2str(name);
2013 else if (!RB_TYPE_P(name, T_STRING)) {
2014 return -1;
2016 num = NAME_TO_NUMBER(regs, re, name,
2017 RSTRING_PTR(name), RSTRING_END(name));
2018 if (num < 1) {
2019 name_to_backref_error(name);
2021 return num;
2024 static VALUE
2025 match_ary_subseq(VALUE match, long beg, long len, VALUE result)
2027 long olen = RMATCH_REGS(match)->num_regs;
2028 long j, end = olen < beg+len ? olen : beg+len;
2029 if (NIL_P(result)) result = rb_ary_new_capa(len);
2030 if (len == 0) return result;
2032 for (j = beg; j < end; j++) {
2033 rb_ary_push(result, rb_reg_nth_match((int)j, match));
2035 if (beg + len > j) {
2036 rb_ary_resize(result, RARRAY_LEN(result) + (beg + len) - j);
2038 return result;
2041 static VALUE
2042 match_ary_aref(VALUE match, VALUE idx, VALUE result)
2044 long beg, len;
2045 int num_regs = RMATCH_REGS(match)->num_regs;
2047 /* check if idx is Range */
2048 switch (rb_range_beg_len(idx, &beg, &len, (long)num_regs, !NIL_P(result))) {
2049 case Qfalse:
2050 if (NIL_P(result)) return rb_reg_nth_match(NUM2INT(idx), match);
2051 rb_ary_push(result, rb_reg_nth_match(NUM2INT(idx), match));
2052 return result;
2053 case Qnil:
2054 return Qnil;
2055 default:
2056 return match_ary_subseq(match, beg, len, result);
2061 * call-seq:
2062 * mtch[i] -> str or nil
2063 * mtch[start, length] -> array
2064 * mtch[range] -> array
2065 * mtch[name] -> str or nil
2067 * Match Reference -- MatchData acts as an array, and may be accessed
2068 * using the normal array indexing techniques. <code>mtch[0]</code>
2069 * is equivalent to the special variable <code>$&</code>, and returns
2070 * the entire matched string. <code>mtch[1]</code>,
2071 * <code>mtch[2]</code>, and so on return the values of the matched
2072 * backreferences (portions of the pattern between parentheses).
2074 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
2075 * m #=> #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8">
2076 * m[0] #=> "HX1138"
2077 * m[1, 2] #=> ["H", "X"]
2078 * m[1..3] #=> ["H", "X", "113"]
2079 * m[-3, 2] #=> ["X", "113"]
2081 * m = /(?<foo>a+)b/.match("ccaaab")
2082 * m #=> #<MatchData "aaab" foo:"aaa">
2083 * m["foo"] #=> "aaa"
2084 * m[:foo] #=> "aaa"
2087 static VALUE
2088 match_aref(int argc, VALUE *argv, VALUE match)
2090 VALUE idx, length;
2092 match_check(match);
2093 rb_scan_args(argc, argv, "11", &idx, &length);
2095 if (NIL_P(length)) {
2096 if (FIXNUM_P(idx)) {
2097 return rb_reg_nth_match(FIX2INT(idx), match);
2099 else {
2100 int num = namev_to_backref_number(RMATCH_REGS(match), RMATCH(match)->regexp, idx);
2101 if (num >= 0) {
2102 return rb_reg_nth_match(num, match);
2104 else {
2105 return match_ary_aref(match, idx, Qnil);
2109 else {
2110 long beg = NUM2LONG(idx);
2111 long len = NUM2LONG(length);
2112 long num_regs = RMATCH_REGS(match)->num_regs;
2113 if (len < 0) {
2114 return Qnil;
2116 if (beg < 0) {
2117 beg += num_regs;
2118 if (beg < 0) return Qnil;
2120 else if (beg > num_regs) {
2121 return Qnil;
2123 else if (beg+len > num_regs) {
2124 len = num_regs - beg;
2126 return match_ary_subseq(match, beg, len, Qnil);
2131 * call-seq:
2133 * mtch.values_at(index, ...) -> array
2135 * Uses each <i>index</i> to access the matching values, returning an array of
2136 * the corresponding matches.
2138 * m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie")
2139 * m.to_a #=> ["HX1138", "H", "X", "113", "8"]
2140 * m.values_at(0, 2, -2) #=> ["HX1138", "X", "113"]
2141 * m.values_at(1..2, -1) #=> ["H", "X", "8"]
2143 * m = /(?<a>\d+) *(?<op>[+\-*\/]) *(?<b>\d+)/.match("1 + 2")
2144 * m.to_a #=> ["1 + 2", "1", "+", "2"]
2145 * m.values_at(:a, :b, :op) #=> ["1", "2", "+"]
2148 static VALUE
2149 match_values_at(int argc, VALUE *argv, VALUE match)
2151 VALUE result;
2152 int i;
2154 match_check(match);
2155 result = rb_ary_new2(argc);
2157 for (i=0; i<argc; i++) {
2158 if (FIXNUM_P(argv[i])) {
2159 rb_ary_push(result, rb_reg_nth_match(FIX2INT(argv[i]), match));
2161 else {
2162 int num = namev_to_backref_number(RMATCH_REGS(match), RMATCH(match)->regexp, argv[i]);
2163 if (num >= 0) {
2164 rb_ary_push(result, rb_reg_nth_match(num, match));
2166 else {
2167 match_ary_aref(match, argv[i], result);
2171 return result;
2176 * call-seq:
2177 * mtch.to_s -> str
2179 * Returns the entire matched string.
2181 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
2182 * m.to_s #=> "HX1138"
2185 static VALUE
2186 match_to_s(VALUE match)
2188 VALUE str = rb_reg_last_match(match);
2190 match_check(match);
2191 if (NIL_P(str)) str = rb_str_new(0,0);
2192 return str;
2195 static int
2196 match_named_captures_iter(const OnigUChar *name, const OnigUChar *name_end,
2197 int back_num, int *back_refs, OnigRegex regex, void *arg) {
2198 struct MEMO *memo = MEMO_CAST(arg);
2199 VALUE hash = memo->v1;
2200 VALUE match = memo->v2;
2202 VALUE key = rb_enc_str_new((const char *)name, name_end-name, regex->enc);
2203 VALUE value;
2205 int i;
2206 int found = 0;
2208 for (i = 0; i < back_num; i++) {
2209 value = rb_reg_nth_match(back_refs[i], match);
2210 if (RTEST(value)) {
2211 rb_hash_aset(hash, key, value);
2212 found = 1;
2216 if (found == 0) {
2217 rb_hash_aset(hash, key, Qnil);
2220 return 0;
2224 * call-seq:
2225 * mtch.named_captures -> hash
2227 * Returns a Hash using named capture.
2229 * A key of the hash is a name of the named captures.
2230 * A value of the hash is a string of last successful capture of corresponding
2231 * group.
2233 * m = /(?<a>.)(?<b>.)/.match("01")
2234 * m.named_captures #=> {"a" => "0", "b" => "1"}
2236 * m = /(?<a>.)(?<b>.)?/.match("0")
2237 * m.named_captures #=> {"a" => "0", "b" => nil}
2239 * m = /(?<a>.)(?<a>.)/.match("01")
2240 * m.named_captures #=> {"a" => "1"}
2242 * m = /(?<a>x)|(?<a>y)/.match("x")
2243 * m.named_captures #=> {"a" => "x"}
2246 static VALUE
2247 match_named_captures(VALUE match)
2249 VALUE hash;
2250 struct MEMO *memo;
2252 match_check(match);
2253 if (NIL_P(RMATCH(match)->regexp))
2254 return rb_hash_new();
2256 hash = rb_hash_new();
2257 memo = MEMO_NEW(hash, match, 0);
2259 onig_foreach_name(RREGEXP(RMATCH(match)->regexp)->ptr, match_named_captures_iter, (void*)memo);
2261 return hash;
2265 * call-seq:
2266 * mtch.string -> str
2268 * Returns a frozen copy of the string passed in to <code>match</code>.
2270 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
2271 * m.string #=> "THX1138."
2274 static VALUE
2275 match_string(VALUE match)
2277 match_check(match);
2278 return RMATCH(match)->str; /* str is frozen */
2281 struct backref_name_tag {
2282 const UChar *name;
2283 long len;
2286 static int
2287 match_inspect_name_iter(const OnigUChar *name, const OnigUChar *name_end,
2288 int back_num, int *back_refs, OnigRegex regex, void *arg0)
2290 struct backref_name_tag *arg = (struct backref_name_tag *)arg0;
2291 int i;
2293 for (i = 0; i < back_num; i++) {
2294 arg[back_refs[i]].name = name;
2295 arg[back_refs[i]].len = name_end - name;
2297 return 0;
2301 * call-seq:
2302 * mtch.inspect -> str
2304 * Returns a printable version of <i>mtch</i>.
2306 * puts /.$/.match("foo").inspect
2307 * #=> #<MatchData "o">
2309 * puts /(.)(.)(.)/.match("foo").inspect
2310 * #=> #<MatchData "foo" 1:"f" 2:"o" 3:"o">
2312 * puts /(.)(.)?(.)/.match("fo").inspect
2313 * #=> #<MatchData "fo" 1:"f" 2:nil 3:"o">
2315 * puts /(?<foo>.)(?<bar>.)(?<baz>.)/.match("hoge").inspect
2316 * #=> #<MatchData "hog" foo:"h" bar:"o" baz:"g">
2320 static VALUE
2321 match_inspect(VALUE match)
2323 VALUE cname = rb_class_path(rb_obj_class(match));
2324 VALUE str;
2325 int i;
2326 struct re_registers *regs = RMATCH_REGS(match);
2327 int num_regs = regs->num_regs;
2328 struct backref_name_tag *names;
2329 VALUE regexp = RMATCH(match)->regexp;
2331 if (regexp == 0) {
2332 return rb_sprintf("#<%"PRIsVALUE":%p>", cname, (void*)match);
2334 else if (NIL_P(regexp)) {
2335 return rb_sprintf("#<%"PRIsVALUE": %"PRIsVALUE">",
2336 cname, rb_reg_nth_match(0, match));
2339 names = ALLOCA_N(struct backref_name_tag, num_regs);
2340 MEMZERO(names, struct backref_name_tag, num_regs);
2342 onig_foreach_name(RREGEXP_PTR(regexp),
2343 match_inspect_name_iter, names);
2345 str = rb_str_buf_new2("#<");
2346 rb_str_append(str, cname);
2348 for (i = 0; i < num_regs; i++) {
2349 VALUE v;
2350 rb_str_buf_cat2(str, " ");
2351 if (0 < i) {
2352 if (names[i].name)
2353 rb_str_buf_cat(str, (const char *)names[i].name, names[i].len);
2354 else {
2355 rb_str_catf(str, "%d", i);
2357 rb_str_buf_cat2(str, ":");
2359 v = rb_reg_nth_match(i, match);
2360 if (NIL_P(v))
2361 rb_str_buf_cat2(str, "nil");
2362 else
2363 rb_str_buf_append(str, rb_str_inspect(v));
2365 rb_str_buf_cat2(str, ">");
2367 return str;
2370 VALUE rb_cRegexp;
2372 static int
2373 read_escaped_byte(const char **pp, const char *end, onig_errmsg_buffer err)
2375 const char *p = *pp;
2376 int code;
2377 int meta_prefix = 0, ctrl_prefix = 0;
2378 size_t len;
2380 if (p == end || *p++ != '\\') {
2381 errcpy(err, "too short escaped multibyte character");
2382 return -1;
2385 again:
2386 if (p == end) {
2387 errcpy(err, "too short escape sequence");
2388 return -1;
2390 switch (*p++) {
2391 case '\\': code = '\\'; break;
2392 case 'n': code = '\n'; break;
2393 case 't': code = '\t'; break;
2394 case 'r': code = '\r'; break;
2395 case 'f': code = '\f'; break;
2396 case 'v': code = '\013'; break;
2397 case 'a': code = '\007'; break;
2398 case 'e': code = '\033'; break;
2400 /* \OOO */
2401 case '0': case '1': case '2': case '3':
2402 case '4': case '5': case '6': case '7':
2403 p--;
2404 code = scan_oct(p, end < p+3 ? end-p : 3, &len);
2405 p += len;
2406 break;
2408 case 'x': /* \xHH */
2409 code = scan_hex(p, end < p+2 ? end-p : 2, &len);
2410 if (len < 1) {
2411 errcpy(err, "invalid hex escape");
2412 return -1;
2414 p += len;
2415 break;
2417 case 'M': /* \M-X, \M-\C-X, \M-\cX */
2418 if (meta_prefix) {
2419 errcpy(err, "duplicate meta escape");
2420 return -1;
2422 meta_prefix = 1;
2423 if (p+1 < end && *p++ == '-' && (*p & 0x80) == 0) {
2424 if (*p == '\\') {
2425 p++;
2426 goto again;
2428 else {
2429 code = *p++;
2430 break;
2433 errcpy(err, "too short meta escape");
2434 return -1;
2436 case 'C': /* \C-X, \C-\M-X */
2437 if (p == end || *p++ != '-') {
2438 errcpy(err, "too short control escape");
2439 return -1;
2441 case 'c': /* \cX, \c\M-X */
2442 if (ctrl_prefix) {
2443 errcpy(err, "duplicate control escape");
2444 return -1;
2446 ctrl_prefix = 1;
2447 if (p < end && (*p & 0x80) == 0) {
2448 if (*p == '\\') {
2449 p++;
2450 goto again;
2452 else {
2453 code = *p++;
2454 break;
2457 errcpy(err, "too short control escape");
2458 return -1;
2460 default:
2461 errcpy(err, "unexpected escape sequence");
2462 return -1;
2464 if (code < 0 || 0xff < code) {
2465 errcpy(err, "invalid escape code");
2466 return -1;
2469 if (ctrl_prefix)
2470 code &= 0x1f;
2471 if (meta_prefix)
2472 code |= 0x80;
2474 *pp = p;
2475 return code;
2478 static int
2479 unescape_escaped_nonascii(const char **pp, const char *end, rb_encoding *enc,
2480 VALUE buf, rb_encoding **encp, onig_errmsg_buffer err)
2482 const char *p = *pp;
2483 int chmaxlen = rb_enc_mbmaxlen(enc);
2484 unsigned char *area = ALLOCA_N(unsigned char, chmaxlen);
2485 char *chbuf = (char *)area;
2486 int chlen = 0;
2487 int byte;
2488 int l;
2490 memset(chbuf, 0, chmaxlen);
2492 byte = read_escaped_byte(&p, end, err);
2493 if (byte == -1) {
2494 return -1;
2497 area[chlen++] = byte;
2498 while (chlen < chmaxlen &&
2499 MBCLEN_NEEDMORE_P(rb_enc_precise_mbclen(chbuf, chbuf+chlen, enc))) {
2500 byte = read_escaped_byte(&p, end, err);
2501 if (byte == -1) {
2502 return -1;
2504 area[chlen++] = byte;
2507 l = rb_enc_precise_mbclen(chbuf, chbuf+chlen, enc);
2508 if (MBCLEN_INVALID_P(l)) {
2509 errcpy(err, "invalid multibyte escape");
2510 return -1;
2512 if (1 < chlen || (area[0] & 0x80)) {
2513 rb_str_buf_cat(buf, chbuf, chlen);
2515 if (*encp == 0)
2516 *encp = enc;
2517 else if (*encp != enc) {
2518 errcpy(err, "escaped non ASCII character in UTF-8 regexp");
2519 return -1;
2522 else {
2523 char escbuf[5];
2524 snprintf(escbuf, sizeof(escbuf), "\\x%02X", area[0]&0xff);
2525 rb_str_buf_cat(buf, escbuf, 4);
2527 *pp = p;
2528 return 0;
2531 static int
2532 check_unicode_range(unsigned long code, onig_errmsg_buffer err)
2534 if ((0xd800 <= code && code <= 0xdfff) || /* Surrogates */
2535 0x10ffff < code) {
2536 errcpy(err, "invalid Unicode range");
2537 return -1;
2539 return 0;
2542 static int
2543 append_utf8(unsigned long uv,
2544 VALUE buf, rb_encoding **encp, onig_errmsg_buffer err)
2546 if (check_unicode_range(uv, err) != 0)
2547 return -1;
2548 if (uv < 0x80) {
2549 char escbuf[5];
2550 snprintf(escbuf, sizeof(escbuf), "\\x%02X", (int)uv);
2551 rb_str_buf_cat(buf, escbuf, 4);
2553 else {
2554 int len;
2555 char utf8buf[6];
2556 len = rb_uv_to_utf8(utf8buf, uv);
2557 rb_str_buf_cat(buf, utf8buf, len);
2559 if (*encp == 0)
2560 *encp = rb_utf8_encoding();
2561 else if (*encp != rb_utf8_encoding()) {
2562 errcpy(err, "UTF-8 character in non UTF-8 regexp");
2563 return -1;
2566 return 0;
2569 static int
2570 unescape_unicode_list(const char **pp, const char *end,
2571 VALUE buf, rb_encoding **encp, onig_errmsg_buffer err)
2573 const char *p = *pp;
2574 int has_unicode = 0;
2575 unsigned long code;
2576 size_t len;
2578 while (p < end && ISSPACE(*p)) p++;
2580 while (1) {
2581 code = ruby_scan_hex(p, end-p, &len);
2582 if (len == 0)
2583 break;
2584 if (6 < len) { /* max 10FFFF */
2585 errcpy(err, "invalid Unicode range");
2586 return -1;
2588 p += len;
2589 if (append_utf8(code, buf, encp, err) != 0)
2590 return -1;
2591 has_unicode = 1;
2593 while (p < end && ISSPACE(*p)) p++;
2596 if (has_unicode == 0) {
2597 errcpy(err, "invalid Unicode list");
2598 return -1;
2601 *pp = p;
2603 return 0;
2606 static int
2607 unescape_unicode_bmp(const char **pp, const char *end,
2608 VALUE buf, rb_encoding **encp, onig_errmsg_buffer err)
2610 const char *p = *pp;
2611 size_t len;
2612 unsigned long code;
2614 if (end < p+4) {
2615 errcpy(err, "invalid Unicode escape");
2616 return -1;
2618 code = ruby_scan_hex(p, 4, &len);
2619 if (len != 4) {
2620 errcpy(err, "invalid Unicode escape");
2621 return -1;
2623 if (append_utf8(code, buf, encp, err) != 0)
2624 return -1;
2625 *pp = p + 4;
2626 return 0;
2629 static int
2630 unescape_nonascii(const char *p, const char *end, rb_encoding *enc,
2631 VALUE buf, rb_encoding **encp, int *has_property,
2632 onig_errmsg_buffer err)
2634 unsigned char c;
2635 char smallbuf[2];
2637 while (p < end) {
2638 int chlen = rb_enc_precise_mbclen(p, end, enc);
2639 if (!MBCLEN_CHARFOUND_P(chlen)) {
2640 invalid_multibyte:
2641 errcpy(err, "invalid multibyte character");
2642 return -1;
2644 chlen = MBCLEN_CHARFOUND_LEN(chlen);
2645 if (1 < chlen || (*p & 0x80)) {
2646 multibyte:
2647 rb_str_buf_cat(buf, p, chlen);
2648 p += chlen;
2649 if (*encp == 0)
2650 *encp = enc;
2651 else if (*encp != enc) {
2652 errcpy(err, "non ASCII character in UTF-8 regexp");
2653 return -1;
2655 continue;
2658 switch (c = *p++) {
2659 case '\\':
2660 if (p == end) {
2661 errcpy(err, "too short escape sequence");
2662 return -1;
2664 chlen = rb_enc_precise_mbclen(p, end, enc);
2665 if (!MBCLEN_CHARFOUND_P(chlen)) {
2666 goto invalid_multibyte;
2668 if ((chlen = MBCLEN_CHARFOUND_LEN(chlen)) > 1) {
2669 /* include the previous backslash */
2670 --p;
2671 ++chlen;
2672 goto multibyte;
2674 switch (c = *p++) {
2675 case '1': case '2': case '3':
2676 case '4': case '5': case '6': case '7': /* \O, \OO, \OOO or backref */
2678 size_t len = end-(p-1), octlen;
2679 if (ruby_scan_oct(p-1, len < 3 ? len : 3, &octlen) <= 0177) {
2680 /* backref or 7bit octal.
2681 no need to unescape anyway.
2682 re-escaping may break backref */
2683 goto escape_asis;
2686 /* xxx: How about more than 199 subexpressions? */
2688 case '0': /* \0, \0O, \0OO */
2690 case 'x': /* \xHH */
2691 case 'c': /* \cX, \c\M-X */
2692 case 'C': /* \C-X, \C-\M-X */
2693 case 'M': /* \M-X, \M-\C-X, \M-\cX */
2694 p = p-2;
2695 if (enc == rb_usascii_encoding()) {
2696 const char *pbeg = p;
2697 int byte = read_escaped_byte(&p, end, err);
2698 if (byte == -1) return -1;
2699 c = byte;
2700 rb_str_buf_cat(buf, pbeg, p-pbeg);
2702 else {
2703 if (unescape_escaped_nonascii(&p, end, enc, buf, encp, err) != 0)
2704 return -1;
2706 break;
2708 case 'u':
2709 if (p == end) {
2710 errcpy(err, "too short escape sequence");
2711 return -1;
2713 if (*p == '{') {
2714 /* \u{H HH HHH HHHH HHHHH HHHHHH ...} */
2715 p++;
2716 if (unescape_unicode_list(&p, end, buf, encp, err) != 0)
2717 return -1;
2718 if (p == end || *p++ != '}') {
2719 errcpy(err, "invalid Unicode list");
2720 return -1;
2722 break;
2724 else {
2725 /* \uHHHH */
2726 if (unescape_unicode_bmp(&p, end, buf, encp, err) != 0)
2727 return -1;
2728 break;
2731 case 'p': /* \p{Hiragana} */
2732 case 'P':
2733 if (!*encp) {
2734 *has_property = 1;
2736 goto escape_asis;
2738 default: /* \n, \\, \d, \9, etc. */
2739 escape_asis:
2740 smallbuf[0] = '\\';
2741 smallbuf[1] = c;
2742 rb_str_buf_cat(buf, smallbuf, 2);
2743 break;
2745 break;
2747 default:
2748 rb_str_buf_cat(buf, (char *)&c, 1);
2749 break;
2753 return 0;
2756 static VALUE
2757 rb_reg_preprocess(const char *p, const char *end, rb_encoding *enc,
2758 rb_encoding **fixed_enc, onig_errmsg_buffer err)
2760 VALUE buf;
2761 int has_property = 0;
2763 buf = rb_str_buf_new(0);
2765 if (rb_enc_asciicompat(enc))
2766 *fixed_enc = 0;
2767 else {
2768 *fixed_enc = enc;
2769 rb_enc_associate(buf, enc);
2772 if (unescape_nonascii(p, end, enc, buf, fixed_enc, &has_property, err) != 0)
2773 return Qnil;
2775 if (has_property && !*fixed_enc) {
2776 *fixed_enc = enc;
2779 if (*fixed_enc) {
2780 rb_enc_associate(buf, *fixed_enc);
2783 return buf;
2786 VALUE
2787 rb_reg_check_preprocess(VALUE str)
2789 rb_encoding *fixed_enc = 0;
2790 onig_errmsg_buffer err = "";
2791 VALUE buf;
2792 char *p, *end;
2793 rb_encoding *enc;
2795 StringValue(str);
2796 p = RSTRING_PTR(str);
2797 end = p + RSTRING_LEN(str);
2798 enc = rb_enc_get(str);
2800 buf = rb_reg_preprocess(p, end, enc, &fixed_enc, err);
2801 RB_GC_GUARD(str);
2803 if (NIL_P(buf)) {
2804 return rb_reg_error_desc(str, 0, err);
2806 return Qnil;
2809 static VALUE
2810 rb_reg_preprocess_dregexp(VALUE ary, int options)
2812 rb_encoding *fixed_enc = 0;
2813 rb_encoding *regexp_enc = 0;
2814 onig_errmsg_buffer err = "";
2815 int i;
2816 VALUE result = 0;
2817 rb_encoding *ascii8bit = rb_ascii8bit_encoding();
2819 if (RARRAY_LEN(ary) == 0) {
2820 rb_raise(rb_eArgError, "no arguments given");
2823 for (i = 0; i < RARRAY_LEN(ary); i++) {
2824 VALUE str = RARRAY_AREF(ary, i);
2825 VALUE buf;
2826 char *p, *end;
2827 rb_encoding *src_enc;
2829 src_enc = rb_enc_get(str);
2830 if (options & ARG_ENCODING_NONE &&
2831 src_enc != ascii8bit) {
2832 if (str_coderange(str) != ENC_CODERANGE_7BIT)
2833 rb_raise(rb_eRegexpError, "/.../n has a non escaped non ASCII character in non ASCII-8BIT script");
2834 else
2835 src_enc = ascii8bit;
2838 StringValue(str);
2839 p = RSTRING_PTR(str);
2840 end = p + RSTRING_LEN(str);
2842 buf = rb_reg_preprocess(p, end, src_enc, &fixed_enc, err);
2844 if (NIL_P(buf))
2845 rb_raise(rb_eArgError, "%s", err);
2847 if (fixed_enc != 0) {
2848 if (regexp_enc != 0 && regexp_enc != fixed_enc) {
2849 rb_raise(rb_eRegexpError, "encoding mismatch in dynamic regexp : %s and %s",
2850 rb_enc_name(regexp_enc), rb_enc_name(fixed_enc));
2852 regexp_enc = fixed_enc;
2855 if (!result)
2856 result = rb_str_new3(str);
2857 else
2858 rb_str_buf_append(result, str);
2860 if (regexp_enc) {
2861 rb_enc_associate(result, regexp_enc);
2864 return result;
2867 static int
2868 rb_reg_initialize(VALUE obj, const char *s, long len, rb_encoding *enc,
2869 int options, onig_errmsg_buffer err,
2870 const char *sourcefile, int sourceline)
2872 struct RRegexp *re = RREGEXP(obj);
2873 VALUE unescaped;
2874 rb_encoding *fixed_enc = 0;
2875 rb_encoding *a_enc = rb_ascii8bit_encoding();
2877 rb_check_frozen(obj);
2878 if (FL_TEST(obj, REG_LITERAL))
2879 rb_raise(rb_eSecurityError, "can't modify literal regexp");
2880 if (re->ptr)
2881 rb_raise(rb_eTypeError, "already initialized regexp");
2882 re->ptr = 0;
2884 if (rb_enc_dummy_p(enc)) {
2885 errcpy(err, "can't make regexp with dummy encoding");
2886 return -1;
2889 unescaped = rb_reg_preprocess(s, s+len, enc, &fixed_enc, err);
2890 if (NIL_P(unescaped))
2891 return -1;
2893 if (fixed_enc) {
2894 if ((fixed_enc != enc && (options & ARG_ENCODING_FIXED)) ||
2895 (fixed_enc != a_enc && (options & ARG_ENCODING_NONE))) {
2896 errcpy(err, "incompatible character encoding");
2897 return -1;
2899 if (fixed_enc != a_enc) {
2900 options |= ARG_ENCODING_FIXED;
2901 enc = fixed_enc;
2904 else if (!(options & ARG_ENCODING_FIXED)) {
2905 enc = rb_usascii_encoding();
2908 rb_enc_associate((VALUE)re, enc);
2909 if ((options & ARG_ENCODING_FIXED) || fixed_enc) {
2910 re->basic.flags |= KCODE_FIXED;
2912 if (options & ARG_ENCODING_NONE) {
2913 re->basic.flags |= REG_ENCODING_NONE;
2916 re->ptr = make_regexp(RSTRING_PTR(unescaped), RSTRING_LEN(unescaped), enc,
2917 options & ARG_REG_OPTION_MASK, err,
2918 sourcefile, sourceline);
2919 if (!re->ptr) return -1;
2920 RB_GC_GUARD(unescaped);
2921 return 0;
2924 static void
2925 reg_set_source(VALUE reg, VALUE str, rb_encoding *enc)
2927 rb_encoding *regenc = rb_enc_get(reg);
2928 if (regenc != enc) {
2929 str = rb_enc_associate(rb_str_dup(str), enc = regenc);
2931 RB_OBJ_WRITE(reg, &RREGEXP(reg)->src, rb_fstring(str));
2934 static int
2935 rb_reg_initialize_str(VALUE obj, VALUE str, int options, onig_errmsg_buffer err,
2936 const char *sourcefile, int sourceline)
2938 int ret;
2939 rb_encoding *str_enc = rb_enc_get(str), *enc = str_enc;
2940 if (options & ARG_ENCODING_NONE) {
2941 rb_encoding *ascii8bit = rb_ascii8bit_encoding();
2942 if (enc != ascii8bit) {
2943 if (str_coderange(str) != ENC_CODERANGE_7BIT) {
2944 errcpy(err, "/.../n has a non escaped non ASCII character in non ASCII-8BIT script");
2945 return -1;
2947 enc = ascii8bit;
2950 ret = rb_reg_initialize(obj, RSTRING_PTR(str), RSTRING_LEN(str), enc,
2951 options, err, sourcefile, sourceline);
2952 if (ret == 0) reg_set_source(obj, str, str_enc);
2953 return ret;
2956 static VALUE
2957 rb_reg_s_alloc(VALUE klass)
2959 NEWOBJ_OF(re, struct RRegexp, klass, T_REGEXP | (RGENGC_WB_PROTECTED_REGEXP ? FL_WB_PROTECTED : 0));
2961 re->ptr = 0;
2962 RB_OBJ_WRITE(re, &re->src, 0);
2963 re->usecnt = 0;
2965 return (VALUE)re;
2968 VALUE
2969 rb_reg_alloc(void)
2971 return rb_reg_s_alloc(rb_cRegexp);
2974 VALUE
2975 rb_reg_new_str(VALUE s, int options)
2977 return rb_reg_init_str(rb_reg_alloc(), s, options);
2980 VALUE
2981 rb_reg_init_str(VALUE re, VALUE s, int options)
2983 onig_errmsg_buffer err = "";
2985 if (rb_reg_initialize_str(re, s, options, err, NULL, 0) != 0) {
2986 rb_reg_raise_str(s, options, err);
2989 return re;
2992 static VALUE
2993 rb_reg_init_str_enc(VALUE re, VALUE s, rb_encoding *enc, int options)
2995 onig_errmsg_buffer err = "";
2997 if (rb_reg_initialize(re, RSTRING_PTR(s), RSTRING_LEN(s),
2998 enc, options, err, NULL, 0) != 0) {
2999 rb_reg_raise_str(s, options, err);
3001 reg_set_source(re, s, enc);
3003 return re;
3006 MJIT_FUNC_EXPORTED VALUE
3007 rb_reg_new_ary(VALUE ary, int opt)
3009 VALUE re = rb_reg_new_str(rb_reg_preprocess_dregexp(ary, opt), opt);
3010 rb_obj_freeze(re);
3011 return re;
3014 VALUE
3015 rb_enc_reg_new(const char *s, long len, rb_encoding *enc, int options)
3017 VALUE re = rb_reg_alloc();
3018 onig_errmsg_buffer err = "";
3020 if (rb_reg_initialize(re, s, len, enc, options, err, NULL, 0) != 0) {
3021 rb_enc_reg_raise(s, len, enc, options, err);
3023 RB_OBJ_WRITE(re, &RREGEXP(re)->src, rb_fstring(rb_enc_str_new(s, len, enc)));
3025 return re;
3028 VALUE
3029 rb_reg_new(const char *s, long len, int options)
3031 return rb_enc_reg_new(s, len, rb_ascii8bit_encoding(), options);
3034 VALUE
3035 rb_reg_compile(VALUE str, int options, const char *sourcefile, int sourceline)
3037 VALUE re = rb_reg_alloc();
3038 onig_errmsg_buffer err = "";
3040 if (!str) str = rb_str_new(0,0);
3041 if (rb_reg_initialize_str(re, str, options, err, sourcefile, sourceline) != 0) {
3042 rb_set_errinfo(rb_reg_error_desc(str, options, err));
3043 return Qnil;
3045 FL_SET(re, REG_LITERAL);
3046 rb_obj_freeze(re);
3047 return re;
3050 static VALUE reg_cache;
3052 VALUE
3053 rb_reg_regcomp(VALUE str)
3055 if (reg_cache && RREGEXP_SRC_LEN(reg_cache) == RSTRING_LEN(str)
3056 && ENCODING_GET(reg_cache) == ENCODING_GET(str)
3057 && memcmp(RREGEXP_SRC_PTR(reg_cache), RSTRING_PTR(str), RSTRING_LEN(str)) == 0)
3058 return reg_cache;
3060 return reg_cache = rb_reg_new_str(str, 0);
3063 static st_index_t reg_hash(VALUE re);
3065 * call-seq:
3066 * rxp.hash -> integer
3068 * Produce a hash based on the text and options of this regular expression.
3070 * See also Object#hash.
3073 VALUE
3074 rb_reg_hash(VALUE re)
3076 st_index_t hashval = reg_hash(re);
3077 return ST2FIX(hashval);
3080 static st_index_t
3081 reg_hash(VALUE re)
3083 st_index_t hashval;
3085 rb_reg_check(re);
3086 hashval = RREGEXP_PTR(re)->options;
3087 hashval = rb_hash_uint(hashval, rb_memhash(RREGEXP_SRC_PTR(re), RREGEXP_SRC_LEN(re)));
3088 return rb_hash_end(hashval);
3093 * call-seq:
3094 * rxp == other_rxp -> true or false
3095 * rxp.eql?(other_rxp) -> true or false
3097 * Equality---Two regexps are equal if their patterns are identical, they have
3098 * the same character set code, and their <code>casefold?</code> values are the
3099 * same.
3101 * /abc/ == /abc/x #=> false
3102 * /abc/ == /abc/i #=> false
3103 * /abc/ == /abc/u #=> false
3104 * /abc/u == /abc/n #=> false
3107 VALUE
3108 rb_reg_equal(VALUE re1, VALUE re2)
3110 if (re1 == re2) return Qtrue;
3111 if (!RB_TYPE_P(re2, T_REGEXP)) return Qfalse;
3112 rb_reg_check(re1); rb_reg_check(re2);
3113 if (FL_TEST(re1, KCODE_FIXED) != FL_TEST(re2, KCODE_FIXED)) return Qfalse;
3114 if (RREGEXP_PTR(re1)->options != RREGEXP_PTR(re2)->options) return Qfalse;
3115 if (RREGEXP_SRC_LEN(re1) != RREGEXP_SRC_LEN(re2)) return Qfalse;
3116 if (ENCODING_GET(re1) != ENCODING_GET(re2)) return Qfalse;
3117 return RBOOL(memcmp(RREGEXP_SRC_PTR(re1), RREGEXP_SRC_PTR(re2), RREGEXP_SRC_LEN(re1)) == 0);
3121 * call-seq:
3122 * mtch.hash -> integer
3124 * Produce a hash based on the target string, regexp and matched
3125 * positions of this matchdata.
3127 * See also Object#hash.
3130 static VALUE
3131 match_hash(VALUE match)
3133 const struct re_registers *regs;
3134 st_index_t hashval;
3136 match_check(match);
3137 hashval = rb_hash_start(rb_str_hash(RMATCH(match)->str));
3138 hashval = rb_hash_uint(hashval, reg_hash(match_regexp(match)));
3139 regs = RMATCH_REGS(match);
3140 hashval = rb_hash_uint(hashval, regs->num_regs);
3141 hashval = rb_hash_uint(hashval, rb_memhash(regs->beg, regs->num_regs * sizeof(*regs->beg)));
3142 hashval = rb_hash_uint(hashval, rb_memhash(regs->end, regs->num_regs * sizeof(*regs->end)));
3143 hashval = rb_hash_end(hashval);
3144 return ST2FIX(hashval);
3148 * call-seq:
3149 * mtch == mtch2 -> true or false
3150 * mtch.eql?(mtch2) -> true or false
3152 * Equality---Two matchdata are equal if their target strings,
3153 * patterns, and matched positions are identical.
3156 static VALUE
3157 match_equal(VALUE match1, VALUE match2)
3159 const struct re_registers *regs1, *regs2;
3161 if (match1 == match2) return Qtrue;
3162 if (!RB_TYPE_P(match2, T_MATCH)) return Qfalse;
3163 if (!RMATCH(match1)->regexp || !RMATCH(match2)->regexp) return Qfalse;
3164 if (!rb_str_equal(RMATCH(match1)->str, RMATCH(match2)->str)) return Qfalse;
3165 if (!rb_reg_equal(match_regexp(match1), match_regexp(match2))) return Qfalse;
3166 regs1 = RMATCH_REGS(match1);
3167 regs2 = RMATCH_REGS(match2);
3168 if (regs1->num_regs != regs2->num_regs) return Qfalse;
3169 if (memcmp(regs1->beg, regs2->beg, regs1->num_regs * sizeof(*regs1->beg))) return Qfalse;
3170 if (memcmp(regs1->end, regs2->end, regs1->num_regs * sizeof(*regs1->end))) return Qfalse;
3171 return Qtrue;
3174 static VALUE
3175 reg_operand(VALUE s, int check)
3177 if (SYMBOL_P(s)) {
3178 return rb_sym2str(s);
3180 else if (RB_TYPE_P(s, T_STRING)) {
3181 return s;
3183 else {
3184 return check ? rb_str_to_str(s) : rb_check_string_type(s);
3188 static long
3189 reg_match_pos(VALUE re, VALUE *strp, long pos, VALUE* set_match)
3191 VALUE str = *strp;
3193 if (NIL_P(str)) {
3194 rb_backref_set(Qnil);
3195 return -1;
3197 *strp = str = reg_operand(str, TRUE);
3198 if (pos != 0) {
3199 if (pos < 0) {
3200 VALUE l = rb_str_length(str);
3201 pos += NUM2INT(l);
3202 if (pos < 0) {
3203 return pos;
3206 pos = rb_str_offset(str, pos);
3208 return rb_reg_search_set_match(re, str, pos, 0, 1, set_match);
3212 * call-seq:
3213 * rxp =~ str -> integer or nil
3215 * Match---Matches <i>rxp</i> against <i>str</i>.
3217 * /at/ =~ "input data" #=> 7
3218 * /ax/ =~ "input data" #=> nil
3220 * If <code>=~</code> is used with a regexp literal with named captures,
3221 * captured strings (or nil) is assigned to local variables named by
3222 * the capture names.
3224 * /(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/ =~ " x = y "
3225 * p lhs #=> "x"
3226 * p rhs #=> "y"
3228 * If it is not matched, nil is assigned for the variables.
3230 * /(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/ =~ " x = "
3231 * p lhs #=> nil
3232 * p rhs #=> nil
3234 * This assignment is implemented in the Ruby parser.
3235 * The parser detects 'regexp-literal =~ expression' for the assignment.
3236 * The regexp must be a literal without interpolation and placed at left hand side.
3238 * The assignment does not occur if the regexp is not a literal.
3240 * re = /(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/
3241 * re =~ " x = y "
3242 * p lhs # undefined local variable
3243 * p rhs # undefined local variable
3245 * A regexp interpolation, <code>#{}</code>, also disables
3246 * the assignment.
3248 * rhs_pat = /(?<rhs>\w+)/
3249 * /(?<lhs>\w+)\s*=\s*#{rhs_pat}/ =~ "x = y"
3250 * p lhs # undefined local variable
3252 * The assignment does not occur if the regexp is placed at the right hand side.
3254 * " x = y " =~ /(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/
3255 * p lhs, rhs # undefined local variable
3259 VALUE
3260 rb_reg_match(VALUE re, VALUE str)
3262 long pos = reg_match_pos(re, &str, 0, NULL);
3263 if (pos < 0) return Qnil;
3264 pos = rb_str_sublen(str, pos);
3265 return LONG2FIX(pos);
3269 * call-seq:
3270 * rxp === str -> true or false
3272 * Case Equality---Used in case statements.
3274 * a = "HELLO"
3275 * case a
3276 * when /\A[a-z]*\z/; print "Lower case\n"
3277 * when /\A[A-Z]*\z/; print "Upper case\n"
3278 * else; print "Mixed case\n"
3279 * end
3280 * #=> "Upper case"
3282 * Following a regular expression literal with the #=== operator allows you to
3283 * compare against a String.
3285 * /^[a-z]*$/ === "HELLO" #=> false
3286 * /^[A-Z]*$/ === "HELLO" #=> true
3289 static VALUE
3290 rb_reg_eqq(VALUE re, VALUE str)
3292 long start;
3294 str = reg_operand(str, FALSE);
3295 if (NIL_P(str)) {
3296 rb_backref_set(Qnil);
3297 return Qfalse;
3299 start = rb_reg_search(re, str, 0, 0);
3300 if (start < 0) {
3301 return Qfalse;
3303 return Qtrue;
3308 * call-seq:
3309 * ~ rxp -> integer or nil
3311 * Match---Matches <i>rxp</i> against the contents of <code>$_</code>.
3312 * Equivalent to <code><i>rxp</i> =~ $_</code>.
3314 * $_ = "input data"
3315 * ~ /at/ #=> 7
3318 VALUE
3319 rb_reg_match2(VALUE re)
3321 long start;
3322 VALUE line = rb_lastline_get();
3324 if (!RB_TYPE_P(line, T_STRING)) {
3325 rb_backref_set(Qnil);
3326 return Qnil;
3329 start = rb_reg_search(re, line, 0, 0);
3330 if (start < 0) {
3331 return Qnil;
3333 start = rb_str_sublen(line, start);
3334 return LONG2FIX(start);
3339 * call-seq:
3340 * rxp.match(str, pos=0) -> matchdata or nil
3341 * rxp.match(str, pos=0) {|match| block } -> obj
3343 * Returns a MatchData object describing the match, or
3344 * <code>nil</code> if there was no match. This is equivalent to
3345 * retrieving the value of the special variable <code>$~</code>
3346 * following a normal match. If the second parameter is present, it
3347 * specifies the position in the string to begin the search.
3349 * /(.)(.)(.)/.match("abc")[2] #=> "b"
3350 * /(.)(.)/.match("abc", 1)[2] #=> "c"
3352 * If a block is given, invoke the block with MatchData if match succeed, so
3353 * that you can write
3355 * /M(.*)/.match("Matz") do |m|
3356 * puts m[0]
3357 * puts m[1]
3358 * end
3360 * instead of
3362 * if m = /M(.*)/.match("Matz")
3363 * puts m[0]
3364 * puts m[1]
3365 * end
3367 * The return value is a value from block execution in this case.
3370 static VALUE
3371 rb_reg_match_m(int argc, VALUE *argv, VALUE re)
3373 VALUE result = Qnil, str, initpos;
3374 long pos;
3376 if (rb_scan_args(argc, argv, "11", &str, &initpos) == 2) {
3377 pos = NUM2LONG(initpos);
3379 else {
3380 pos = 0;
3383 pos = reg_match_pos(re, &str, pos, &result);
3384 if (pos < 0) {
3385 rb_backref_set(Qnil);
3386 return Qnil;
3388 rb_match_busy(result);
3389 if (!NIL_P(result) && rb_block_given_p()) {
3390 return rb_yield(result);
3392 return result;
3396 * call-seq:
3397 * rxp.match?(str) -> true or false
3398 * rxp.match?(str, pos=0) -> true or false
3400 * Returns <code>true</code> or <code>false</code> to indicate whether the
3401 * regexp is matched or not without updating $~ and other related variables.
3402 * If the second parameter is present, it specifies the position in the string
3403 * to begin the search.
3405 * /R.../.match?("Ruby") #=> true
3406 * /R.../.match?("Ruby", 1) #=> false
3407 * /P.../.match?("Ruby") #=> false
3408 * $& #=> nil
3411 static VALUE
3412 rb_reg_match_m_p(int argc, VALUE *argv, VALUE re)
3414 long pos = rb_check_arity(argc, 1, 2) > 1 ? NUM2LONG(argv[1]) : 0;
3415 return rb_reg_match_p(re, argv[0], pos);
3418 VALUE
3419 rb_reg_match_p(VALUE re, VALUE str, long pos)
3421 regex_t *reg;
3422 onig_errmsg_buffer err = "";
3423 OnigPosition result;
3424 const UChar *start, *end;
3425 int tmpreg;
3427 if (NIL_P(str)) return Qfalse;
3428 str = SYMBOL_P(str) ? rb_sym2str(str) : StringValue(str);
3429 if (pos) {
3430 if (pos < 0) {
3431 pos += NUM2LONG(rb_str_length(str));
3432 if (pos < 0) return Qfalse;
3434 if (pos > 0) {
3435 long len = 1;
3436 const char *beg = rb_str_subpos(str, pos, &len);
3437 if (!beg) return Qfalse;
3438 pos = beg - RSTRING_PTR(str);
3441 reg = rb_reg_prepare_re0(re, str, err);
3442 tmpreg = reg != RREGEXP_PTR(re);
3443 if (!tmpreg) RREGEXP(re)->usecnt++;
3444 start = ((UChar*)RSTRING_PTR(str));
3445 end = start + RSTRING_LEN(str);
3446 result = onig_search(reg, start, end, start + pos, end,
3447 NULL, ONIG_OPTION_NONE);
3448 if (!tmpreg) RREGEXP(re)->usecnt--;
3449 if (tmpreg) {
3450 if (RREGEXP(re)->usecnt) {
3451 onig_free(reg);
3453 else {
3454 onig_free(RREGEXP_PTR(re));
3455 RREGEXP_PTR(re) = reg;
3458 if (result < 0) {
3459 if (result == ONIG_MISMATCH) {
3460 return Qfalse;
3462 else {
3463 onig_error_code_to_str((UChar*)err, (int)result);
3464 rb_reg_raise(RREGEXP_SRC_PTR(re), RREGEXP_SRC_LEN(re), err, re);
3467 return Qtrue;
3471 * Document-method: compile
3473 * Alias for Regexp.new
3477 * call-seq:
3478 * Regexp.new(string, [options]) -> regexp
3479 * Regexp.new(regexp) -> regexp
3480 * Regexp.compile(string, [options]) -> regexp
3481 * Regexp.compile(regexp) -> regexp
3483 * Constructs a new regular expression from +pattern+, which can be either a
3484 * String or a Regexp (in which case that regexp's options are propagated),
3485 * and new options may not be specified (a change as of Ruby 1.8).
3487 * If +options+ is an Integer, it should be one or more of the constants
3488 * Regexp::EXTENDED, Regexp::IGNORECASE, and Regexp::MULTILINE,
3489 * <em>or</em>-ed together. Otherwise, if +options+ is not
3490 * +nil+ or +false+, the regexp will be case insensitive.
3492 * r1 = Regexp.new('^a-z+:\\s+\w+') #=> /^a-z+:\s+\w+/
3493 * r2 = Regexp.new('cat', true) #=> /cat/i
3494 * r3 = Regexp.new(r2) #=> /cat/i
3495 * r4 = Regexp.new('dog', Regexp::EXTENDED | Regexp::IGNORECASE) #=> /dog/ix
3498 static VALUE
3499 rb_reg_initialize_m(int argc, VALUE *argv, VALUE self)
3501 int flags = 0;
3502 VALUE str;
3503 rb_encoding *enc = 0;
3505 rb_check_arity(argc, 1, 3);
3506 if (RB_TYPE_P(argv[0], T_REGEXP)) {
3507 VALUE re = argv[0];
3509 if (argc > 1) {
3510 rb_warn("flags ignored");
3512 rb_reg_check(re);
3513 flags = rb_reg_options(re);
3514 str = RREGEXP_SRC(re);
3516 else {
3517 if (argc >= 2) {
3518 if (FIXNUM_P(argv[1])) flags = FIX2INT(argv[1]);
3519 else if (RTEST(argv[1])) flags = ONIG_OPTION_IGNORECASE;
3521 if (argc == 3 && !NIL_P(argv[2])) {
3522 char *kcode = StringValuePtr(argv[2]);
3523 if (kcode[0] == 'n' || kcode[0] == 'N') {
3524 enc = rb_ascii8bit_encoding();
3525 flags |= ARG_ENCODING_NONE;
3527 else {
3528 rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "encoding option is ignored - %s", kcode);
3531 str = StringValue(argv[0]);
3533 if (enc && rb_enc_get(str) != enc)
3534 rb_reg_init_str_enc(self, str, enc, flags);
3535 else
3536 rb_reg_init_str(self, str, flags);
3537 return self;
3540 VALUE
3541 rb_reg_quote(VALUE str)
3543 rb_encoding *enc = rb_enc_get(str);
3544 char *s, *send, *t;
3545 VALUE tmp;
3546 int c, clen;
3547 int ascii_only = rb_enc_str_asciionly_p(str);
3549 s = RSTRING_PTR(str);
3550 send = s + RSTRING_LEN(str);
3551 while (s < send) {
3552 c = rb_enc_ascget(s, send, &clen, enc);
3553 if (c == -1) {
3554 s += mbclen(s, send, enc);
3555 continue;
3557 switch (c) {
3558 case '[': case ']': case '{': case '}':
3559 case '(': case ')': case '|': case '-':
3560 case '*': case '.': case '\\':
3561 case '?': case '+': case '^': case '$':
3562 case ' ': case '#':
3563 case '\t': case '\f': case '\v': case '\n': case '\r':
3564 goto meta_found;
3566 s += clen;
3568 tmp = rb_str_new3(str);
3569 if (ascii_only) {
3570 rb_enc_associate(tmp, rb_usascii_encoding());
3572 return tmp;
3574 meta_found:
3575 tmp = rb_str_new(0, RSTRING_LEN(str)*2);
3576 if (ascii_only) {
3577 rb_enc_associate(tmp, rb_usascii_encoding());
3579 else {
3580 rb_enc_copy(tmp, str);
3582 t = RSTRING_PTR(tmp);
3583 /* copy upto metacharacter */
3584 const char *p = RSTRING_PTR(str);
3585 memcpy(t, p, s - p);
3586 t += s - p;
3588 while (s < send) {
3589 c = rb_enc_ascget(s, send, &clen, enc);
3590 if (c == -1) {
3591 int n = mbclen(s, send, enc);
3593 while (n--)
3594 *t++ = *s++;
3595 continue;
3597 s += clen;
3598 switch (c) {
3599 case '[': case ']': case '{': case '}':
3600 case '(': case ')': case '|': case '-':
3601 case '*': case '.': case '\\':
3602 case '?': case '+': case '^': case '$':
3603 case '#':
3604 t += rb_enc_mbcput('\\', t, enc);
3605 break;
3606 case ' ':
3607 t += rb_enc_mbcput('\\', t, enc);
3608 t += rb_enc_mbcput(' ', t, enc);
3609 continue;
3610 case '\t':
3611 t += rb_enc_mbcput('\\', t, enc);
3612 t += rb_enc_mbcput('t', t, enc);
3613 continue;
3614 case '\n':
3615 t += rb_enc_mbcput('\\', t, enc);
3616 t += rb_enc_mbcput('n', t, enc);
3617 continue;
3618 case '\r':
3619 t += rb_enc_mbcput('\\', t, enc);
3620 t += rb_enc_mbcput('r', t, enc);
3621 continue;
3622 case '\f':
3623 t += rb_enc_mbcput('\\', t, enc);
3624 t += rb_enc_mbcput('f', t, enc);
3625 continue;
3626 case '\v':
3627 t += rb_enc_mbcput('\\', t, enc);
3628 t += rb_enc_mbcput('v', t, enc);
3629 continue;
3631 t += rb_enc_mbcput(c, t, enc);
3633 rb_str_resize(tmp, t - RSTRING_PTR(tmp));
3634 return tmp;
3639 * call-seq:
3640 * Regexp.escape(str) -> string
3641 * Regexp.quote(str) -> string
3643 * Escapes any characters that would have special meaning in a regular
3644 * expression. Returns a new escaped string with the same or compatible
3645 * encoding. For any string,
3646 * <code>Regexp.new(Regexp.escape(<i>str</i>))=~<i>str</i></code> will be true.
3648 * Regexp.escape('\*?{}.') #=> \\\*\?\{\}\.
3652 static VALUE
3653 rb_reg_s_quote(VALUE c, VALUE str)
3655 return rb_reg_quote(reg_operand(str, TRUE));
3659 rb_reg_options(VALUE re)
3661 int options;
3663 rb_reg_check(re);
3664 options = RREGEXP_PTR(re)->options & ARG_REG_OPTION_MASK;
3665 if (RBASIC(re)->flags & KCODE_FIXED) options |= ARG_ENCODING_FIXED;
3666 if (RBASIC(re)->flags & REG_ENCODING_NONE) options |= ARG_ENCODING_NONE;
3667 return options;
3670 static VALUE
3671 rb_check_regexp_type(VALUE re)
3673 return rb_check_convert_type(re, T_REGEXP, "Regexp", "to_regexp");
3677 * call-seq:
3678 * Regexp.try_convert(obj) -> re or nil
3680 * Try to convert <i>obj</i> into a Regexp, using to_regexp method.
3681 * Returns converted regexp or nil if <i>obj</i> cannot be converted
3682 * for any reason.
3684 * Regexp.try_convert(/re/) #=> /re/
3685 * Regexp.try_convert("re") #=> nil
3687 * o = Object.new
3688 * Regexp.try_convert(o) #=> nil
3689 * def o.to_regexp() /foo/ end
3690 * Regexp.try_convert(o) #=> /foo/
3693 static VALUE
3694 rb_reg_s_try_convert(VALUE dummy, VALUE re)
3696 return rb_check_regexp_type(re);
3699 static VALUE
3700 rb_reg_s_union(VALUE self, VALUE args0)
3702 long argc = RARRAY_LEN(args0);
3704 if (argc == 0) {
3705 VALUE args[1];
3706 args[0] = rb_str_new2("(?!)");
3707 return rb_class_new_instance(1, args, rb_cRegexp);
3709 else if (argc == 1) {
3710 VALUE arg = rb_ary_entry(args0, 0);
3711 VALUE re = rb_check_regexp_type(arg);
3712 if (!NIL_P(re))
3713 return re;
3714 else {
3715 VALUE quoted;
3716 quoted = rb_reg_s_quote(Qnil, arg);
3717 return rb_reg_new_str(quoted, 0);
3720 else {
3721 int i;
3722 VALUE source = rb_str_buf_new(0);
3723 rb_encoding *result_enc;
3725 int has_asciionly = 0;
3726 rb_encoding *has_ascii_compat_fixed = 0;
3727 rb_encoding *has_ascii_incompat = 0;
3729 for (i = 0; i < argc; i++) {
3730 volatile VALUE v;
3731 VALUE e = rb_ary_entry(args0, i);
3733 if (0 < i)
3734 rb_str_buf_cat_ascii(source, "|");
3736 v = rb_check_regexp_type(e);
3737 if (!NIL_P(v)) {
3738 rb_encoding *enc = rb_enc_get(v);
3739 if (!rb_enc_asciicompat(enc)) {
3740 if (!has_ascii_incompat)
3741 has_ascii_incompat = enc;
3742 else if (has_ascii_incompat != enc)
3743 rb_raise(rb_eArgError, "incompatible encodings: %s and %s",
3744 rb_enc_name(has_ascii_incompat), rb_enc_name(enc));
3746 else if (rb_reg_fixed_encoding_p(v)) {
3747 if (!has_ascii_compat_fixed)
3748 has_ascii_compat_fixed = enc;
3749 else if (has_ascii_compat_fixed != enc)
3750 rb_raise(rb_eArgError, "incompatible encodings: %s and %s",
3751 rb_enc_name(has_ascii_compat_fixed), rb_enc_name(enc));
3753 else {
3754 has_asciionly = 1;
3756 v = rb_reg_str_with_term(v, -1);
3758 else {
3759 rb_encoding *enc;
3760 StringValue(e);
3761 enc = rb_enc_get(e);
3762 if (!rb_enc_asciicompat(enc)) {
3763 if (!has_ascii_incompat)
3764 has_ascii_incompat = enc;
3765 else if (has_ascii_incompat != enc)
3766 rb_raise(rb_eArgError, "incompatible encodings: %s and %s",
3767 rb_enc_name(has_ascii_incompat), rb_enc_name(enc));
3769 else if (rb_enc_str_asciionly_p(e)) {
3770 has_asciionly = 1;
3772 else {
3773 if (!has_ascii_compat_fixed)
3774 has_ascii_compat_fixed = enc;
3775 else if (has_ascii_compat_fixed != enc)
3776 rb_raise(rb_eArgError, "incompatible encodings: %s and %s",
3777 rb_enc_name(has_ascii_compat_fixed), rb_enc_name(enc));
3779 v = rb_reg_s_quote(Qnil, e);
3781 if (has_ascii_incompat) {
3782 if (has_asciionly) {
3783 rb_raise(rb_eArgError, "ASCII incompatible encoding: %s",
3784 rb_enc_name(has_ascii_incompat));
3786 if (has_ascii_compat_fixed) {
3787 rb_raise(rb_eArgError, "incompatible encodings: %s and %s",
3788 rb_enc_name(has_ascii_incompat), rb_enc_name(has_ascii_compat_fixed));
3792 if (i == 0) {
3793 rb_enc_copy(source, v);
3795 rb_str_append(source, v);
3798 if (has_ascii_incompat) {
3799 result_enc = has_ascii_incompat;
3801 else if (has_ascii_compat_fixed) {
3802 result_enc = has_ascii_compat_fixed;
3804 else {
3805 result_enc = rb_ascii8bit_encoding();
3808 rb_enc_associate(source, result_enc);
3809 return rb_class_new_instance(1, &source, rb_cRegexp);
3814 * call-seq:
3815 * Regexp.union(pat1, pat2, ...) -> new_regexp
3816 * Regexp.union(pats_ary) -> new_regexp
3818 * Return a Regexp object that is the union of the given
3819 * <em>pattern</em>s, i.e., will match any of its parts. The
3820 * <em>pattern</em>s can be Regexp objects, in which case their
3821 * options will be preserved, or Strings. If no patterns are given,
3822 * returns <code>/(?!)/</code>. The behavior is unspecified if any
3823 * given <em>pattern</em> contains capture.
3825 * Regexp.union #=> /(?!)/
3826 * Regexp.union("penzance") #=> /penzance/
3827 * Regexp.union("a+b*c") #=> /a\+b\*c/
3828 * Regexp.union("skiing", "sledding") #=> /skiing|sledding/
3829 * Regexp.union(["skiing", "sledding"]) #=> /skiing|sledding/
3830 * Regexp.union(/dogs/, /cats/i) #=> /(?-mix:dogs)|(?i-mx:cats)/
3832 * Note: the arguments for ::union will try to be converted into a regular
3833 * expression literal via #to_regexp.
3835 static VALUE
3836 rb_reg_s_union_m(VALUE self, VALUE args)
3838 VALUE v;
3839 if (RARRAY_LEN(args) == 1 &&
3840 !NIL_P(v = rb_check_array_type(rb_ary_entry(args, 0)))) {
3841 return rb_reg_s_union(self, v);
3843 return rb_reg_s_union(self, args);
3846 /* :nodoc: */
3847 static VALUE
3848 rb_reg_init_copy(VALUE copy, VALUE re)
3850 if (!OBJ_INIT_COPY(copy, re)) return copy;
3851 rb_reg_check(re);
3852 return rb_reg_init_str(copy, RREGEXP_SRC(re), rb_reg_options(re));
3855 VALUE
3856 rb_reg_regsub(VALUE str, VALUE src, struct re_registers *regs, VALUE regexp)
3858 VALUE val = 0;
3859 char *p, *s, *e;
3860 int no, clen;
3861 rb_encoding *str_enc = rb_enc_get(str);
3862 rb_encoding *src_enc = rb_enc_get(src);
3863 int acompat = rb_enc_asciicompat(str_enc);
3864 long n;
3865 #define ASCGET(s,e,cl) (acompat ? (*(cl)=1,ISASCII((s)[0])?(s)[0]:-1) : rb_enc_ascget((s), (e), (cl), str_enc))
3867 RSTRING_GETMEM(str, s, n);
3868 p = s;
3869 e = s + n;
3871 while (s < e) {
3872 int c = ASCGET(s, e, &clen);
3873 char *ss;
3875 if (c == -1) {
3876 s += mbclen(s, e, str_enc);
3877 continue;
3879 ss = s;
3880 s += clen;
3882 if (c != '\\' || s == e) continue;
3884 if (!val) {
3885 val = rb_str_buf_new(ss-p);
3887 rb_enc_str_buf_cat(val, p, ss-p, str_enc);
3889 c = ASCGET(s, e, &clen);
3890 if (c == -1) {
3891 s += mbclen(s, e, str_enc);
3892 rb_enc_str_buf_cat(val, ss, s-ss, str_enc);
3893 p = s;
3894 continue;
3896 s += clen;
3898 p = s;
3899 switch (c) {
3900 case '1': case '2': case '3': case '4':
3901 case '5': case '6': case '7': case '8': case '9':
3902 if (!NIL_P(regexp) && onig_noname_group_capture_is_active(RREGEXP_PTR(regexp))) {
3903 no = c - '0';
3905 else {
3906 continue;
3908 break;
3910 case 'k':
3911 if (s < e && ASCGET(s, e, &clen) == '<') {
3912 char *name, *name_end;
3914 name_end = name = s + clen;
3915 while (name_end < e) {
3916 c = ASCGET(name_end, e, &clen);
3917 if (c == '>') break;
3918 name_end += c == -1 ? mbclen(name_end, e, str_enc) : clen;
3920 if (name_end < e) {
3921 VALUE n = rb_str_subseq(str, (long)(name - RSTRING_PTR(str)),
3922 (long)(name_end - name));
3923 if ((no = NAME_TO_NUMBER(regs, regexp, n, name, name_end)) < 1) {
3924 name_to_backref_error(n);
3926 p = s = name_end + clen;
3927 break;
3929 else {
3930 rb_raise(rb_eRuntimeError, "invalid group name reference format");
3934 rb_enc_str_buf_cat(val, ss, s-ss, str_enc);
3935 continue;
3937 case '0':
3938 case '&':
3939 no = 0;
3940 break;
3942 case '`':
3943 rb_enc_str_buf_cat(val, RSTRING_PTR(src), BEG(0), src_enc);
3944 continue;
3946 case '\'':
3947 rb_enc_str_buf_cat(val, RSTRING_PTR(src)+END(0), RSTRING_LEN(src)-END(0), src_enc);
3948 continue;
3950 case '+':
3951 no = regs->num_regs-1;
3952 while (BEG(no) == -1 && no > 0) no--;
3953 if (no == 0) continue;
3954 break;
3956 case '\\':
3957 rb_enc_str_buf_cat(val, s-clen, clen, str_enc);
3958 continue;
3960 default:
3961 rb_enc_str_buf_cat(val, ss, s-ss, str_enc);
3962 continue;
3965 if (no >= 0) {
3966 if (no >= regs->num_regs) continue;
3967 if (BEG(no) == -1) continue;
3968 rb_enc_str_buf_cat(val, RSTRING_PTR(src)+BEG(no), END(no)-BEG(no), src_enc);
3972 if (!val) return str;
3973 if (p < e) {
3974 rb_enc_str_buf_cat(val, p, e-p, str_enc);
3977 return val;
3980 static VALUE
3981 ignorecase_getter(ID _x, VALUE *_y)
3983 rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "variable $= is no longer effective");
3984 return Qfalse;
3987 static void
3988 ignorecase_setter(VALUE val, ID id, VALUE *_)
3990 rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "variable $= is no longer effective; ignored");
3993 static VALUE
3994 match_getter(void)
3996 VALUE match = rb_backref_get();
3998 if (NIL_P(match)) return Qnil;
3999 rb_match_busy(match);
4000 return match;
4003 static VALUE
4004 get_LAST_MATCH_INFO(ID _x, VALUE *_y)
4006 return match_getter();
4009 static void
4010 match_setter(VALUE val, ID _x, VALUE *_y)
4012 if (!NIL_P(val)) {
4013 Check_Type(val, T_MATCH);
4015 rb_backref_set(val);
4019 * call-seq:
4020 * Regexp.last_match -> matchdata
4021 * Regexp.last_match(n) -> str
4023 * The first form returns the MatchData object generated by the
4024 * last successful pattern match. Equivalent to reading the special global
4025 * variable <code>$~</code> (see Special global variables in Regexp for
4026 * details).
4028 * The second form returns the <i>n</i>th field in this MatchData object.
4029 * _n_ can be a string or symbol to reference a named capture.
4031 * Note that the last_match is local to the thread and method scope of the
4032 * method that did the pattern match.
4034 * /c(.)t/ =~ 'cat' #=> 0
4035 * Regexp.last_match #=> #<MatchData "cat" 1:"a">
4036 * Regexp.last_match(0) #=> "cat"
4037 * Regexp.last_match(1) #=> "a"
4038 * Regexp.last_match(2) #=> nil
4040 * /(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/ =~ "var = val"
4041 * Regexp.last_match #=> #<MatchData "var = val" lhs:"var" rhs:"val">
4042 * Regexp.last_match(:lhs) #=> "var"
4043 * Regexp.last_match(:rhs) #=> "val"
4046 static VALUE
4047 rb_reg_s_last_match(int argc, VALUE *argv, VALUE _)
4049 if (rb_check_arity(argc, 0, 1) == 1) {
4050 VALUE match = rb_backref_get();
4051 int n;
4052 if (NIL_P(match)) return Qnil;
4053 n = match_backref_number(match, argv[0]);
4054 return rb_reg_nth_match(n, match);
4056 return match_getter();
4059 static void
4060 re_warn(const char *s)
4062 rb_warn("%s", s);
4066 * Document-class: RegexpError
4068 * Raised when given an invalid regexp expression.
4070 * Regexp.new("?")
4072 * <em>raises the exception:</em>
4074 * RegexpError: target of repeat operator is not specified: /?/
4078 * Document-class: Regexp
4080 * A Regexp holds a regular expression, used to match a pattern
4081 * against strings. Regexps are created using the <code>/.../</code>
4082 * and <code>%r{...}</code> literals, and by the Regexp::new
4083 * constructor.
4085 * You can create a \Regexp object explicitly with:
4087 * - A {regexp literal}[doc/syntax/literals_rdoc.html#label-Regexp+Literals].
4089 * :include: doc/regexp.rdoc
4092 void
4093 Init_Regexp(void)
4095 rb_eRegexpError = rb_define_class("RegexpError", rb_eStandardError);
4097 onigenc_set_default_encoding(ONIG_ENCODING_ASCII);
4098 onig_set_warn_func(re_warn);
4099 onig_set_verb_warn_func(re_warn);
4101 rb_define_virtual_variable("$~", get_LAST_MATCH_INFO, match_setter);
4102 rb_define_virtual_variable("$&", last_match_getter, 0);
4103 rb_define_virtual_variable("$`", prematch_getter, 0);
4104 rb_define_virtual_variable("$'", postmatch_getter, 0);
4105 rb_define_virtual_variable("$+", last_paren_match_getter, 0);
4107 rb_gvar_ractor_local("$~");
4108 rb_gvar_ractor_local("$&");
4109 rb_gvar_ractor_local("$`");
4110 rb_gvar_ractor_local("$'");
4111 rb_gvar_ractor_local("$+");
4113 rb_define_virtual_variable("$=", ignorecase_getter, ignorecase_setter);
4115 rb_cRegexp = rb_define_class("Regexp", rb_cObject);
4116 rb_define_alloc_func(rb_cRegexp, rb_reg_s_alloc);
4117 rb_define_singleton_method(rb_cRegexp, "compile", rb_class_new_instance, -1);
4118 rb_define_singleton_method(rb_cRegexp, "quote", rb_reg_s_quote, 1);
4119 rb_define_singleton_method(rb_cRegexp, "escape", rb_reg_s_quote, 1);
4120 rb_define_singleton_method(rb_cRegexp, "union", rb_reg_s_union_m, -2);
4121 rb_define_singleton_method(rb_cRegexp, "last_match", rb_reg_s_last_match, -1);
4122 rb_define_singleton_method(rb_cRegexp, "try_convert", rb_reg_s_try_convert, 1);
4124 rb_define_method(rb_cRegexp, "initialize", rb_reg_initialize_m, -1);
4125 rb_define_method(rb_cRegexp, "initialize_copy", rb_reg_init_copy, 1);
4126 rb_define_method(rb_cRegexp, "hash", rb_reg_hash, 0);
4127 rb_define_method(rb_cRegexp, "eql?", rb_reg_equal, 1);
4128 rb_define_method(rb_cRegexp, "==", rb_reg_equal, 1);
4129 rb_define_method(rb_cRegexp, "=~", rb_reg_match, 1);
4130 rb_define_method(rb_cRegexp, "===", rb_reg_eqq, 1);
4131 rb_define_method(rb_cRegexp, "~", rb_reg_match2, 0);
4132 rb_define_method(rb_cRegexp, "match", rb_reg_match_m, -1);
4133 rb_define_method(rb_cRegexp, "match?", rb_reg_match_m_p, -1);
4134 rb_define_method(rb_cRegexp, "to_s", rb_reg_to_s, 0);
4135 rb_define_method(rb_cRegexp, "inspect", rb_reg_inspect, 0);
4136 rb_define_method(rb_cRegexp, "source", rb_reg_source, 0);
4137 rb_define_method(rb_cRegexp, "casefold?", rb_reg_casefold_p, 0);
4138 rb_define_method(rb_cRegexp, "options", rb_reg_options_m, 0);
4139 rb_define_method(rb_cRegexp, "encoding", rb_obj_encoding, 0); /* in encoding.c */
4140 rb_define_method(rb_cRegexp, "fixed_encoding?", rb_reg_fixed_encoding_p, 0);
4141 rb_define_method(rb_cRegexp, "names", rb_reg_names, 0);
4142 rb_define_method(rb_cRegexp, "named_captures", rb_reg_named_captures, 0);
4144 /* see Regexp.options and Regexp.new */
4145 rb_define_const(rb_cRegexp, "IGNORECASE", INT2FIX(ONIG_OPTION_IGNORECASE));
4146 /* see Regexp.options and Regexp.new */
4147 rb_define_const(rb_cRegexp, "EXTENDED", INT2FIX(ONIG_OPTION_EXTEND));
4148 /* see Regexp.options and Regexp.new */
4149 rb_define_const(rb_cRegexp, "MULTILINE", INT2FIX(ONIG_OPTION_MULTILINE));
4150 /* see Regexp.options and Regexp.new */
4151 rb_define_const(rb_cRegexp, "FIXEDENCODING", INT2FIX(ARG_ENCODING_FIXED));
4152 /* see Regexp.options and Regexp.new */
4153 rb_define_const(rb_cRegexp, "NOENCODING", INT2FIX(ARG_ENCODING_NONE));
4155 rb_global_variable(&reg_cache);
4157 rb_cMatch = rb_define_class("MatchData", rb_cObject);
4158 rb_define_alloc_func(rb_cMatch, match_alloc);
4159 rb_undef_method(CLASS_OF(rb_cMatch), "new");
4160 rb_undef_method(CLASS_OF(rb_cMatch), "allocate");
4162 rb_define_method(rb_cMatch, "initialize_copy", match_init_copy, 1);
4163 rb_define_method(rb_cMatch, "regexp", match_regexp, 0);
4164 rb_define_method(rb_cMatch, "names", match_names, 0);
4165 rb_define_method(rb_cMatch, "size", match_size, 0);
4166 rb_define_method(rb_cMatch, "length", match_size, 0);
4167 rb_define_method(rb_cMatch, "offset", match_offset, 1);
4168 rb_define_method(rb_cMatch, "begin", match_begin, 1);
4169 rb_define_method(rb_cMatch, "end", match_end, 1);
4170 rb_define_method(rb_cMatch, "match", match_nth, 1);
4171 rb_define_method(rb_cMatch, "match_length", match_nth_length, 1);
4172 rb_define_method(rb_cMatch, "to_a", match_to_a, 0);
4173 rb_define_method(rb_cMatch, "[]", match_aref, -1);
4174 rb_define_method(rb_cMatch, "captures", match_captures, 0);
4175 rb_define_method(rb_cMatch, "named_captures", match_named_captures, 0);
4176 rb_define_method(rb_cMatch, "values_at", match_values_at, -1);
4177 rb_define_method(rb_cMatch, "pre_match", rb_reg_match_pre, 0);
4178 rb_define_method(rb_cMatch, "post_match", rb_reg_match_post, 0);
4179 rb_define_method(rb_cMatch, "to_s", match_to_s, 0);
4180 rb_define_method(rb_cMatch, "inspect", match_inspect, 0);
4181 rb_define_method(rb_cMatch, "string", match_string, 0);
4182 rb_define_method(rb_cMatch, "hash", match_hash, 0);
4183 rb_define_method(rb_cMatch, "eql?", match_equal, 1);
4184 rb_define_method(rb_cMatch, "==", match_equal, 1);