refine configure message of broken erfc of glibc-2.3 on IA64.
[ruby-svn.git] / re.c
blob7734132600a04e2c928a0703971539e2c41a388f
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/ruby.h"
13 #include "ruby/re.h"
14 #include "ruby/encoding.h"
15 #include "ruby/util.h"
16 #include "regint.h"
17 #include <ctype.h>
19 VALUE rb_eRegexpError;
21 typedef char onig_errmsg_buffer[ONIG_MAX_ERROR_MESSAGE_LEN];
23 #define BEG(no) regs->beg[no]
24 #define END(no) regs->end[no]
26 #if 'a' == 97 /* it's ascii */
27 static const char casetable[] = {
28 '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
29 '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
30 '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
31 '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
32 /* ' ' '!' '"' '#' '$' '%' '&' ''' */
33 '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
34 /* '(' ')' '*' '+' ',' '-' '.' '/' */
35 '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
36 /* '0' '1' '2' '3' '4' '5' '6' '7' */
37 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
38 /* '8' '9' ':' ';' '<' '=' '>' '?' */
39 '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
40 /* '@' 'A' 'B' 'C' 'D' 'E' 'F' 'G' */
41 '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
42 /* 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O' */
43 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
44 /* 'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' */
45 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
46 /* 'X' 'Y' 'Z' '[' '\' ']' '^' '_' */
47 '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
48 /* '`' 'a' 'b' 'c' 'd' 'e' 'f' 'g' */
49 '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
50 /* 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' */
51 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
52 /* 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' */
53 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
54 /* 'x' 'y' 'z' '{' '|' '}' '~' */
55 '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
56 '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
57 '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
58 '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
59 '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
60 '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
61 '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
62 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
63 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
64 '\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
65 '\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317',
66 '\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327',
67 '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
68 '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
69 '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
70 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
71 '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
73 #else
74 # error >>> "You lose. You will need a translation table for your character set." <<<
75 #endif
77 int
78 rb_memcicmp(const void *x, const void *y, long len)
80 const unsigned char *p1 = x, *p2 = y;
81 int tmp;
83 while (len--) {
84 if ((tmp = casetable[(unsigned)*p1++] - casetable[(unsigned)*p2++]))
85 return tmp;
87 return 0;
90 #undef rb_memcmp
92 int
93 rb_memcmp(const void *p1, const void *p2, long len)
95 return memcmp(p1, p2, len);
98 static inline long
99 rb_memsearch_ss(const unsigned char *xs, long m, const unsigned char *ys, long n)
101 const unsigned char *x = xs, *xe = xs + m;
102 const unsigned char *y = ys, *ye = ys + n;
103 #ifndef VALUE_MAX
104 # if SIZEOF_VALUE == 8
105 # define VALUE_MAX 0xFFFFFFFFFFFFFFFFULL
106 # elif SIZEOF_VALUE == 4
107 # define VALUE_MAX 0xFFFFFFFFUL
108 # endif
109 #endif
110 VALUE hx, hy, mask = VALUE_MAX >> ((SIZEOF_VALUE - m) * CHAR_BIT);
112 if (m > SIZEOF_VALUE)
113 rb_bug("!!too long pattern string!!");
115 /* Prepare hash value */
116 for (hx = *x++, hy = *y++; x < xe; ++x, ++y) {
117 hx <<= CHAR_BIT;
118 hy <<= CHAR_BIT;
119 hx |= *x;
120 hy |= *y;
122 /* Searching */
123 while (hx != hy) {
124 if (y == ye)
125 return -1;
126 hy <<= CHAR_BIT;
127 hy |= *y;
128 hy &= mask;
129 y++;
131 return y - ys - m;
134 static inline long
135 rb_memsearch_qs(const unsigned char *xs, long m, const unsigned char *ys, long n)
137 const unsigned char *x = xs, *xe = xs + m;
138 const unsigned char *y = ys;
139 VALUE i, qstable[256];
141 /* Preprocessing */
142 for (i = 0; i < 256; ++i)
143 qstable[i] = m + 1;
144 for (; x < xe; ++x)
145 qstable[*x] = xe - x;
146 /* Searching */
147 for (; y + m <= ys + n; y += *(qstable + y[m])) {
148 if (*xs == *y && memcmp(xs, y, m) == 0)
149 return y - ys;
151 return -1;
154 static inline unsigned int
155 rb_memsearch_qs_utf8_hash(const unsigned char *x)
157 register const unsigned int mix = 8353;
158 register unsigned int h = *x;
159 if (h < 0xC0) {
160 return h + 256;
162 else if (h < 0xE0) {
163 h *= mix;
164 h += x[1];
166 else if (h < 0xF0) {
167 h *= mix;
168 h += x[1];
169 h *= mix;
170 h += x[2];
172 else if (h < 0xF5) {
173 h *= mix;
174 h += x[1];
175 h *= mix;
176 h += x[2];
177 h *= mix;
178 h += x[3];
180 else {
181 return h + 256;
183 return (unsigned char)h;
186 static inline long
187 rb_memsearch_qs_utf8(const unsigned char *xs, long m, const unsigned char *ys, long n)
189 const unsigned char *x = xs, *xe = xs + m;
190 const unsigned char *y = ys;
191 VALUE i, qstable[512];
193 /* Preprocessing */
194 for (i = 0; i < 512; ++i) {
195 qstable[i] = m + 1;
197 for (; x < xe; ++x) {
198 qstable[rb_memsearch_qs_utf8_hash(x)] = xe - x;
200 /* Searching */
201 for (; y + m <= ys + n; y += qstable[rb_memsearch_qs_utf8_hash(y+m)]) {
202 if (*xs == *y && memcmp(xs, y, m) == 0)
203 return y - ys;
205 return -1;
208 long
209 rb_memsearch(const void *x0, long m, const void *y0, long n, rb_encoding *enc)
211 const unsigned char *x = x0, *y = y0;
213 if (m > n) return -1;
214 else if (m == n) {
215 return memcmp(x0, y0, m) == 0 ? 0 : -1;
217 else if (m < 1) {
218 return 0;
220 else if (m == 1) {
221 const unsigned char *ys = y, *ye = ys + n;
222 for (; y < ye; ++y) {
223 if (*x == *y)
224 return y - ys;
226 return -1;
228 else if (m <= SIZEOF_VALUE) {
229 return rb_memsearch_ss(x0, m, y0, n);
231 else if (enc == rb_utf8_encoding()){
232 return rb_memsearch_qs_utf8(x0, m, y0, n);
234 else {
235 return rb_memsearch_qs(x0, m, y0, n);
239 #define REG_LITERAL FL_USER5
240 #define REG_ENCODING_NONE FL_USER6
242 #define KCODE_FIXED FL_USER4
244 #define ARG_REG_OPTION_MASK \
245 (ONIG_OPTION_IGNORECASE|ONIG_OPTION_MULTILINE|ONIG_OPTION_EXTEND)
246 #define ARG_ENCODING_FIXED 16
247 #define ARG_ENCODING_NONE 32
249 static int
250 char_to_option(int c)
252 int val;
254 switch (c) {
255 case 'i':
256 val = ONIG_OPTION_IGNORECASE;
257 break;
258 case 'x':
259 val = ONIG_OPTION_EXTEND;
260 break;
261 case 'm':
262 val = ONIG_OPTION_MULTILINE;
263 break;
264 default:
265 val = 0;
266 break;
268 return val;
271 static char *
272 option_to_str(char str[4], int options)
274 char *p = str;
275 if (options & ONIG_OPTION_MULTILINE) *p++ = 'm';
276 if (options & ONIG_OPTION_IGNORECASE) *p++ = 'i';
277 if (options & ONIG_OPTION_EXTEND) *p++ = 'x';
278 *p = 0;
279 return str;
282 extern int
283 rb_char_to_option_kcode(int c, int *option, int *kcode)
285 *option = 0;
287 switch (c) {
288 case 'n':
289 *kcode = -1;
290 return (*option = ARG_ENCODING_NONE);
291 case 'e':
292 *kcode = rb_enc_find_index("EUC-JP");
293 break;
294 case 's':
295 *kcode = rb_enc_find_index("Windows-31J");
296 break;
297 case 'u':
298 *kcode = rb_enc_find_index("UTF-8");
299 break;
300 default:
301 *kcode = -1;
302 return (*option = char_to_option(c));
304 *option = ARG_ENCODING_FIXED;
305 return 1;
308 static void
309 rb_reg_check(VALUE re)
311 if (!RREGEXP(re)->ptr || !RREGEXP_SRC(re) || !RREGEXP_SRC_PTR(re)) {
312 rb_raise(rb_eTypeError, "uninitialized Regexp");
316 static void
317 rb_reg_expr_str(VALUE str, const char *s, long len)
319 rb_encoding *enc = rb_enc_get(str);
320 const char *p, *pend;
321 int need_escape = 0;
322 int c, clen;
324 p = s; pend = p + len;
325 while (p<pend) {
326 c = rb_enc_ascget(p, pend, &clen, enc);
327 if (c == -1) {
328 p += mbclen(p, pend, enc);
330 else if (c != '/' && rb_enc_isprint(c, enc)) {
331 p += clen;
333 else {
334 need_escape = 1;
335 break;
338 if (!need_escape) {
339 rb_str_buf_cat(str, s, len);
341 else {
342 p = s;
343 while (p<pend) {
344 c = rb_enc_ascget(p, pend, &clen, enc);
345 if (c == '\\' && p+clen < pend) {
346 int n = clen + mbclen(p+clen, pend, enc);
347 rb_str_buf_cat(str, p, n);
348 p += n;
349 continue;
351 else if (c == '/') {
352 char c = '\\';
353 rb_str_buf_cat(str, &c, 1);
354 rb_str_buf_cat(str, p, clen);
356 else if (c == -1) {
357 int l = mbclen(p, pend, enc);
358 rb_str_buf_cat(str, p, l);
359 p += l;
360 continue;
362 else if (rb_enc_isprint(c, enc)) {
363 rb_str_buf_cat(str, p, clen);
365 else if (!rb_enc_isspace(c, enc)) {
366 char b[8];
368 sprintf(b, "\\x%02X", c);
369 rb_str_buf_cat(str, b, 4);
371 else {
372 rb_str_buf_cat(str, p, clen);
374 p += clen;
379 static VALUE
380 rb_reg_desc(const char *s, long len, VALUE re)
382 VALUE str = rb_str_buf_new2("/");
384 rb_enc_copy(str, re);
385 rb_reg_expr_str(str, s, len);
386 rb_str_buf_cat2(str, "/");
387 if (re) {
388 char opts[4];
389 rb_reg_check(re);
390 if (*option_to_str(opts, RREGEXP(re)->ptr->options))
391 rb_str_buf_cat2(str, opts);
393 OBJ_INFECT(str, re);
394 return str;
399 * call-seq:
400 * rxp.source => str
402 * Returns the original string of the pattern.
404 * /ab+c/ix.source #=> "ab+c"
406 * Note that escape sequences are retained as is.
408 * /\x20\+/.source #=> "\\x20\\+"
412 static VALUE
413 rb_reg_source(VALUE re)
415 VALUE str;
417 rb_reg_check(re);
418 str = rb_enc_str_new(RREGEXP_SRC_PTR(re),RREGEXP_SRC_LEN(re), rb_enc_get(re));
419 if (OBJ_TAINTED(re)) OBJ_TAINT(str);
420 return str;
424 * call-seq:
425 * rxp.inspect => string
427 * Produce a nicely formatted string-version of _rxp_. Perhaps surprisingly,
428 * <code>#inspect</code> actually produces the more natural version of
429 * the string than <code>#to_s</code>.
431 * /ab+c/ix.inspect #=> "/ab+c/ix"
435 static VALUE
436 rb_reg_inspect(VALUE re)
438 rb_reg_check(re);
439 return rb_reg_desc(RREGEXP_SRC_PTR(re), RREGEXP_SRC_LEN(re), re);
444 * call-seq:
445 * rxp.to_s => str
447 * Returns a string containing the regular expression and its options (using the
448 * <code>(?opts:source)</code> notation. This string can be fed back in to
449 * <code>Regexp::new</code> to a regular expression with the same semantics as
450 * the original. (However, <code>Regexp#==</code> may not return true when
451 * comparing the two, as the source of the regular expression itself may
452 * differ, as the example shows). <code>Regexp#inspect</code> produces a
453 * generally more readable version of <i>rxp</i>.
455 * r1 = /ab+c/ix #=> /ab+c/ix
456 * s1 = r1.to_s #=> "(?ix-m:ab+c)"
457 * r2 = Regexp.new(s1) #=> /(?ix-m:ab+c)/
458 * r1 == r2 #=> false
459 * r1.source #=> "ab+c"
460 * r2.source #=> "(?ix-m:ab+c)"
463 static VALUE
464 rb_reg_to_s(VALUE re)
466 int options, opt;
467 const int embeddable = ONIG_OPTION_MULTILINE|ONIG_OPTION_IGNORECASE|ONIG_OPTION_EXTEND;
468 long len;
469 const UChar* ptr;
470 VALUE str = rb_str_buf_new2("(?");
471 char optbuf[5];
473 rb_reg_check(re);
475 rb_enc_copy(str, re);
476 options = RREGEXP(re)->ptr->options;
477 ptr = (UChar*)RREGEXP_SRC_PTR(re);
478 len = RREGEXP_SRC_LEN(re);
479 again:
480 if (len >= 4 && ptr[0] == '(' && ptr[1] == '?') {
481 int err = 1;
482 ptr += 2;
483 if ((len -= 2) > 0) {
484 do {
485 opt = char_to_option((int )*ptr);
486 if (opt != 0) {
487 options |= opt;
489 else {
490 break;
492 ++ptr;
493 } while (--len > 0);
495 if (len > 1 && *ptr == '-') {
496 ++ptr;
497 --len;
498 do {
499 opt = char_to_option((int )*ptr);
500 if (opt != 0) {
501 options &= ~opt;
503 else {
504 break;
506 ++ptr;
507 } while (--len > 0);
509 if (*ptr == ')') {
510 --len;
511 ++ptr;
512 goto again;
514 if (*ptr == ':' && ptr[len-1] == ')') {
515 int r;
516 Regexp *rp;
517 r = onig_alloc_init(&rp, ONIG_OPTION_DEFAULT,
518 ONIGENC_CASE_FOLD_DEFAULT,
519 rb_enc_get(re),
520 OnigDefaultSyntax);
521 if (r == 0) {
522 ++ptr;
523 len -= 2;
524 err = (onig_compile(rp, ptr, ptr + len, NULL) != 0);
526 onig_free(rp);
528 if (err) {
529 options = RREGEXP(re)->ptr->options;
530 ptr = (UChar*)RREGEXP_SRC_PTR(re);
531 len = RREGEXP_SRC_LEN(re);
535 if (*option_to_str(optbuf, options)) rb_str_buf_cat2(str, optbuf);
537 if ((options & embeddable) != embeddable) {
538 optbuf[0] = '-';
539 option_to_str(optbuf + 1, ~options);
540 rb_str_buf_cat2(str, optbuf);
543 rb_str_buf_cat2(str, ":");
544 rb_reg_expr_str(str, (char*)ptr, len);
545 rb_str_buf_cat2(str, ")");
546 rb_enc_copy(str, re);
548 OBJ_INFECT(str, re);
549 return str;
552 static void
553 rb_reg_raise(const char *s, long len, const char *err, VALUE re)
555 VALUE desc = rb_reg_desc(s, len, re);
557 rb_raise(rb_eRegexpError, "%s: %s", err, RSTRING_PTR(desc));
560 static VALUE
561 rb_enc_reg_error_desc(const char *s, long len, rb_encoding *enc, int options, const char *err)
563 char opts[6];
564 VALUE desc = rb_str_buf_new2(err);
566 rb_enc_associate(desc, enc);
567 rb_str_buf_cat2(desc, ": /");
568 rb_reg_expr_str(desc, s, len);
569 opts[0] = '/';
570 option_to_str(opts + 1, options);
571 rb_str_buf_cat2(desc, opts);
572 return rb_exc_new3(rb_eRegexpError, desc);
575 static void
576 rb_enc_reg_raise(const char *s, long len, rb_encoding *enc, int options, const char *err)
578 rb_exc_raise(rb_enc_reg_error_desc(s, len, enc, options, err));
581 static VALUE
582 rb_reg_error_desc(VALUE str, int options, const char *err)
584 return rb_enc_reg_error_desc(RSTRING_PTR(str), RSTRING_LEN(str),
585 rb_enc_get(str), options, err);
588 static void
589 rb_reg_raise_str(VALUE str, int options, const char *err)
591 rb_exc_raise(rb_reg_error_desc(str, options, err));
596 * call-seq:
597 * rxp.casefold? => true or false
599 * Returns the value of the case-insensitive flag.
601 * /a/.casefold? #=> false
602 * /a/i.casefold? #=> true
603 * /(?i:a)/.casefold? #=> false
606 static VALUE
607 rb_reg_casefold_p(VALUE re)
609 rb_reg_check(re);
610 if (RREGEXP(re)->ptr->options & ONIG_OPTION_IGNORECASE) return Qtrue;
611 return Qfalse;
616 * call-seq:
617 * rxp.options => fixnum
619 * Returns the set of bits corresponding to the options used when creating this
620 * Regexp (see <code>Regexp::new</code> for details. Note that additional bits
621 * may be set in the returned options: these are used internally by the regular
622 * expression code. These extra bits are ignored if the options are passed to
623 * <code>Regexp::new</code>.
625 * Regexp::IGNORECASE #=> 1
626 * Regexp::EXTENDED #=> 2
627 * Regexp::MULTILINE #=> 4
629 * /cat/.options #=> 0
630 * /cat/ix.options #=> 3
631 * Regexp.new('cat', true).options #=> 1
632 * /\xa1\xa2/e.options #=> 16
634 * r = /cat/ix
635 * Regexp.new(r.source, r.options) #=> /cat/ix
638 static VALUE
639 rb_reg_options_m(VALUE re)
641 int options = rb_reg_options(re);
642 return INT2NUM(options);
645 static int
646 reg_names_iter(const OnigUChar *name, const OnigUChar *name_end,
647 int back_num, int *back_refs, OnigRegex regex, void *arg)
649 VALUE ary = (VALUE)arg;
650 rb_ary_push(ary, rb_str_new((const char *)name, name_end-name));
651 return 0;
655 * call-seq:
656 * rxp.names => [name1, name2, ...]
658 * Returns a list of names of captures as an array of strings.
660 * /(?<foo>.)(?<bar>.)(?<baz>.)/.names
661 * #=> ["foo", "bar", "baz"]
663 * /(?<foo>.)(?<foo>.)/.names
664 * #=> ["foo"]
666 * /(.)(.)/.names
667 * #=> []
670 static VALUE
671 rb_reg_names(VALUE re)
673 VALUE ary = rb_ary_new();
674 rb_reg_check(re);
675 onig_foreach_name(RREGEXP(re)->ptr, reg_names_iter, (void*)ary);
676 return ary;
679 static int
680 reg_named_captures_iter(const OnigUChar *name, const OnigUChar *name_end,
681 int back_num, int *back_refs, OnigRegex regex, void *arg)
683 VALUE hash = (VALUE)arg;
684 VALUE ary = rb_ary_new2(back_num);
685 int i;
687 for(i = 0; i < back_num; i++)
688 rb_ary_store(ary, i, INT2NUM(back_refs[i]));
690 rb_hash_aset(hash, rb_str_new((const char*)name, name_end-name),ary);
692 return 0;
696 * call-seq:
697 * rxp.named_captures => hash
699 * Returns a hash representing information about named captures of <i>rxp</i>.
701 * A key of the hash is a name of the named captures.
702 * A value of the hash is an array which is list of indexes of corresponding
703 * named captures.
705 * /(?<foo>.)(?<bar>.)/.named_captures
706 * #=> {"foo"=>[1], "bar"=>[2]}
708 * /(?<foo>.)(?<foo>.)/.named_captures
709 * #=> {"foo"=>[1, 2]}
711 * If there are no named captures, an empty hash is returned.
713 * /(.)(.)/.named_captures
714 * #=> {}
717 static VALUE
718 rb_reg_named_captures(VALUE re)
720 VALUE hash = rb_hash_new();
721 rb_reg_check(re);
722 onig_foreach_name(RREGEXP(re)->ptr, reg_named_captures_iter, (void*)hash);
723 return hash;
726 static Regexp*
727 make_regexp(const char *s, long len, rb_encoding *enc, int flags, onig_errmsg_buffer err)
729 Regexp *rp;
730 int r;
731 OnigErrorInfo einfo;
733 /* Handle escaped characters first. */
735 /* Build a copy of the string (in dest) with the
736 escaped characters translated, and generate the regex
737 from that.
740 r = onig_alloc_init(&rp, flags, ONIGENC_CASE_FOLD_DEFAULT,
741 enc, OnigDefaultSyntax);
742 if (r) {
743 onig_error_code_to_str((UChar*)err, r);
744 return 0;
747 r = onig_compile(rp, (UChar*)s, (UChar*)(s + len), &einfo);
749 if (r != 0) {
750 onig_free(rp);
751 (void )onig_error_code_to_str((UChar*)err, r, &einfo);
752 return 0;
754 return rp;
759 * Document-class: MatchData
761 * <code>MatchData</code> is the type of the special variable <code>$~</code>,
762 * and is the type of the object returned by <code>Regexp#match</code> and
763 * <code>Regexp.last_match</code>. It encapsulates all the results of a pattern
764 * match, results normally accessed through the special variables
765 * <code>$&</code>, <code>$'</code>, <code>$`</code>, <code>$1</code>,
766 * <code>$2</code>, and so on.
770 VALUE rb_cMatch;
772 static VALUE
773 match_alloc(VALUE klass)
775 NEWOBJ(match, struct RMatch);
776 OBJSETUP(match, klass, T_MATCH);
778 match->str = 0;
779 match->rmatch = 0;
780 match->regexp = 0;
781 match->rmatch = ALLOC(struct rmatch);
782 MEMZERO(match->rmatch, struct rmatch, 1);
784 return (VALUE)match;
787 typedef struct {
788 int byte_pos;
789 int char_pos;
790 } pair_t;
792 static int
793 pair_byte_cmp(const void *pair1, const void *pair2)
795 return ((pair_t*)pair1)->byte_pos - ((pair_t*)pair2)->byte_pos;
798 static void
799 update_char_offset(VALUE match)
801 struct rmatch *rm = RMATCH(match)->rmatch;
802 struct re_registers *regs;
803 int num_regs;
804 int i, num_pos, c;
805 char *s, *p, *q, *e;
806 rb_encoding *enc;
807 pair_t *pairs;
809 if (rm->char_offset_updated)
810 return;
812 regs = &rm->regs;
813 num_regs = rm->regs.num_regs;
815 if (rm->char_offset_num_allocated < num_regs) {
816 REALLOC_N(rm->char_offset, struct rmatch_offset, num_regs);
817 rm->char_offset_num_allocated = num_regs;
820 enc = rb_enc_get(RMATCH(match)->str);
821 if (rb_enc_mbmaxlen(enc) == 1) {
822 for (i = 0; i < num_regs; i++) {
823 rm->char_offset[i].beg = BEG(i);
824 rm->char_offset[i].end = END(i);
826 rm->char_offset_updated = 1;
827 return;
830 pairs = ALLOCA_N(pair_t, num_regs*2);
831 num_pos = 0;
832 for (i = 0; i < num_regs; i++) {
833 if (BEG(i) < 0)
834 continue;
835 pairs[num_pos++].byte_pos = BEG(i);
836 pairs[num_pos++].byte_pos = END(i);
838 qsort(pairs, num_pos, sizeof(pair_t), pair_byte_cmp);
840 s = p = RSTRING_PTR(RMATCH(match)->str);
841 e = s + RSTRING_LEN(RMATCH(match)->str);
842 c = 0;
843 for (i = 0; i < num_pos; i++) {
844 q = s + pairs[i].byte_pos;
845 c += rb_enc_strlen(p, q, enc);
846 pairs[i].char_pos = c;
847 p = q;
850 for (i = 0; i < num_regs; i++) {
851 pair_t key, *found;
852 if (BEG(i) < 0) {
853 rm->char_offset[i].beg = -1;
854 rm->char_offset[i].end = -1;
855 continue;
858 key.byte_pos = BEG(i);
859 found = bsearch(&key, pairs, num_pos, sizeof(pair_t), pair_byte_cmp);
860 rm->char_offset[i].beg = found->char_pos;
862 key.byte_pos = END(i);
863 found = bsearch(&key, pairs, num_pos, sizeof(pair_t), pair_byte_cmp);
864 rm->char_offset[i].end = found->char_pos;
867 rm->char_offset_updated = 1;
870 static void
871 match_check(VALUE match)
873 if (!RMATCH(match)->regexp) {
874 rb_raise(rb_eTypeError, "uninitialized Match");
878 /* :nodoc: */
879 static VALUE
880 match_init_copy(VALUE obj, VALUE orig)
882 struct rmatch *rm;
884 if (obj == orig) return obj;
886 if (!rb_obj_is_instance_of(orig, rb_obj_class(obj))) {
887 rb_raise(rb_eTypeError, "wrong argument class");
889 RMATCH(obj)->str = RMATCH(orig)->str;
890 RMATCH(obj)->regexp = RMATCH(orig)->regexp;
892 rm = RMATCH(obj)->rmatch;
893 onig_region_copy(&rm->regs, RMATCH_REGS(orig));
895 if (!RMATCH(orig)->rmatch->char_offset_updated) {
896 rm->char_offset_updated = 0;
898 else {
899 if (rm->char_offset_num_allocated < rm->regs.num_regs) {
900 REALLOC_N(rm->char_offset, struct rmatch_offset, rm->regs.num_regs);
901 rm->char_offset_num_allocated = rm->regs.num_regs;
903 MEMCPY(rm->char_offset, RMATCH(orig)->rmatch->char_offset,
904 struct rmatch_offset, rm->regs.num_regs);
905 rm->char_offset_updated = 1;
908 return obj;
913 * call-seq:
914 * mtch.regexp => regexp
916 * Returns the regexp.
918 * m = /a.*b/.match("abc")
919 * m.regexp #=> /a.*b/
922 static VALUE
923 match_regexp(VALUE match)
925 match_check(match);
926 return RMATCH(match)->regexp;
930 * call-seq:
931 * mtch.names => [name1, name2, ...]
933 * Returns a list of names of captures as an array of strings.
934 * It is same as mtch.regexp.names.
936 * /(?<foo>.)(?<bar>.)(?<baz>.)/.match("hoge").names
937 * #=> ["foo", "bar", "baz"]
939 * m = /(?<x>.)(?<y>.)?/.match("a") #=> #<MatchData "a" x:"a" y:nil>
940 * m.names #=> ["x", "y"]
943 static VALUE
944 match_names(VALUE match)
946 match_check(match);
947 return rb_reg_names(RMATCH(match)->regexp);
951 * call-seq:
952 * mtch.length => integer
953 * mtch.size => integer
955 * Returns the number of elements in the match array.
957 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
958 * m.length #=> 5
959 * m.size #=> 5
962 static VALUE
963 match_size(VALUE match)
965 match_check(match);
966 return INT2FIX(RMATCH_REGS(match)->num_regs);
969 static int
970 match_backref_number(VALUE match, VALUE backref)
972 const char *name;
973 int num;
975 struct re_registers *regs = RMATCH_REGS(match);
976 VALUE regexp = RMATCH(match)->regexp;
978 match_check(match);
979 switch(TYPE(backref)) {
980 default:
981 return NUM2INT(backref);
983 case T_SYMBOL:
984 name = rb_id2name(SYM2ID(backref));
985 break;
987 case T_STRING:
988 name = StringValueCStr(backref);
989 break;
992 num = onig_name_to_backref_number(RREGEXP(regexp)->ptr,
993 (const unsigned char*)name,
994 (const unsigned char*)name + strlen(name),
995 regs);
997 if (num < 1) {
998 rb_raise(rb_eIndexError, "undefined group name reference: %s", name);
1001 return num;
1006 * call-seq:
1007 * mtch.offset(n) => array
1009 * Returns a two-element array containing the beginning and ending offsets of
1010 * the <em>n</em>th match.
1011 * <em>n</em> can be a string or symbol to reference a named capture.
1013 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1014 * m.offset(0) #=> [1, 7]
1015 * m.offset(4) #=> [6, 7]
1017 * m = /(?<foo>.)(.)(?<bar>.)/.match("hoge")
1018 * p m.offset(:foo) #=> [0, 1]
1019 * p m.offset(:bar) #=> [2, 3]
1023 static VALUE
1024 match_offset(VALUE match, VALUE n)
1026 int i = match_backref_number(match, n);
1027 struct re_registers *regs = RMATCH_REGS(match);
1029 match_check(match);
1030 if (i < 0 || regs->num_regs <= i)
1031 rb_raise(rb_eIndexError, "index %d out of matches", i);
1033 if (BEG(i) < 0)
1034 return rb_assoc_new(Qnil, Qnil);
1036 update_char_offset(match);
1037 return rb_assoc_new(INT2FIX(RMATCH(match)->rmatch->char_offset[i].beg),
1038 INT2FIX(RMATCH(match)->rmatch->char_offset[i].end));
1043 * call-seq:
1044 * mtch.begin(n) => integer
1046 * Returns the offset of the start of the <em>n</em>th element of the match
1047 * array in the string.
1048 * <em>n</em> can be a string or symbol to reference a named capture.
1050 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1051 * m.begin(0) #=> 1
1052 * m.begin(2) #=> 2
1054 * m = /(?<foo>.)(.)(?<bar>.)/.match("hoge")
1055 * p m.begin(:foo) #=> 0
1056 * p m.begin(:bar) #=> 2
1059 static VALUE
1060 match_begin(VALUE match, VALUE n)
1062 int i = match_backref_number(match, n);
1063 struct re_registers *regs = RMATCH_REGS(match);
1065 match_check(match);
1066 if (i < 0 || regs->num_regs <= i)
1067 rb_raise(rb_eIndexError, "index %d out of matches", i);
1069 if (BEG(i) < 0)
1070 return Qnil;
1072 update_char_offset(match);
1073 return INT2FIX(RMATCH(match)->rmatch->char_offset[i].beg);
1078 * call-seq:
1079 * mtch.end(n) => integer
1081 * Returns the offset of the character immediately following the end of the
1082 * <em>n</em>th element of the match array in the string.
1083 * <em>n</em> can be a string or symbol to reference a named capture.
1085 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1086 * m.end(0) #=> 7
1087 * m.end(2) #=> 3
1089 * m = /(?<foo>.)(.)(?<bar>.)/.match("hoge")
1090 * p m.end(:foo) #=> 1
1091 * p m.end(:bar) #=> 3
1094 static VALUE
1095 match_end(VALUE match, VALUE n)
1097 int i = match_backref_number(match, n);
1098 struct re_registers *regs = RMATCH_REGS(match);
1100 match_check(match);
1101 if (i < 0 || regs->num_regs <= i)
1102 rb_raise(rb_eIndexError, "index %d out of matches", i);
1104 if (BEG(i) < 0)
1105 return Qnil;
1107 update_char_offset(match);
1108 return INT2FIX(RMATCH(match)->rmatch->char_offset[i].end);
1111 #define MATCH_BUSY FL_USER2
1113 void
1114 rb_match_busy(VALUE match)
1116 FL_SET(match, MATCH_BUSY);
1120 * call-seq:
1121 * rxp.fixed_encoding? => true or false
1123 * Returns false if rxp is applicable to
1124 * a string with any ASCII compatible encoding.
1125 * Returns true otherwise.
1127 * r = /a/
1128 * r.fixed_encoding? #=> false
1129 * r =~ "\u{6666} a" #=> 2
1130 * r =~ "\xa1\xa2 a".force_encoding("euc-jp") #=> 2
1131 * r =~ "abc".force_encoding("euc-jp") #=> 0
1133 * r = /a/u
1134 * r.fixed_encoding? #=> true
1135 * r.encoding #=> #<Encoding:UTF-8>
1136 * r =~ "\u{6666} a" #=> 2
1137 * r =~ "\xa1\xa2".force_encoding("euc-jp") #=> ArgumentError
1138 * r =~ "abc".force_encoding("euc-jp") #=> 0
1140 * r = /\u{6666}/
1141 * r.fixed_encoding? #=> true
1142 * r.encoding #=> #<Encoding:UTF-8>
1143 * r =~ "\u{6666} a" #=> 0
1144 * r =~ "\xa1\xa2".force_encoding("euc-jp") #=> ArgumentError
1145 * r =~ "abc".force_encoding("euc-jp") #=> nil
1148 static VALUE
1149 rb_reg_fixed_encoding_p(VALUE re)
1151 if (FL_TEST(re, KCODE_FIXED))
1152 return Qtrue;
1153 else
1154 return Qfalse;
1157 static VALUE
1158 rb_reg_preprocess(const char *p, const char *end, rb_encoding *enc,
1159 rb_encoding **fixed_enc, onig_errmsg_buffer err);
1162 static void
1163 reg_enc_error(VALUE re, VALUE str)
1165 rb_raise(rb_eArgError,
1166 "incompatible encoding regexp match (%s regexp with %s string)",
1167 rb_enc_name(RREGEXP(re)->ptr->enc),
1168 rb_enc_name(rb_enc_get(str)));
1171 static rb_encoding*
1172 rb_reg_prepare_enc(VALUE re, VALUE str, int warn)
1174 rb_encoding *enc = 0;
1176 if (rb_enc_str_coderange(str) == ENC_CODERANGE_BROKEN) {
1177 rb_raise(rb_eArgError,
1178 "broken %s string",
1179 rb_enc_name(rb_enc_get(str)));
1182 rb_reg_check(re);
1183 enc = rb_enc_get(str);
1184 if (!rb_enc_str_asciicompat_p(str)) {
1185 if (RREGEXP(re)->ptr->enc != enc) {
1186 reg_enc_error(re, str);
1189 else if (rb_reg_fixed_encoding_p(re)) {
1190 if (RREGEXP(re)->ptr->enc != enc &&
1191 (!rb_enc_asciicompat(RREGEXP(re)->ptr->enc) ||
1192 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT)) {
1193 reg_enc_error(re, str);
1195 enc = RREGEXP(re)->ptr->enc;
1197 if (warn && (RBASIC(re)->flags & REG_ENCODING_NONE) &&
1198 enc != rb_ascii8bit_encoding() &&
1199 rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
1200 rb_warn("regexp match /.../n against to %s string",
1201 rb_enc_name(enc));
1203 return enc;
1206 regex_t *
1207 rb_reg_prepare_re(VALUE re, VALUE str)
1209 regex_t *reg = RREGEXP(re)->ptr;
1210 onig_errmsg_buffer err = "";
1211 int r;
1212 OnigErrorInfo einfo;
1213 const char *pattern;
1214 VALUE unescaped;
1215 rb_encoding *fixed_enc = 0;
1216 rb_encoding *enc = rb_reg_prepare_enc(re, str, 1);
1218 if (reg->enc == enc) return reg;
1220 rb_reg_check(re);
1221 reg = RREGEXP(re)->ptr;
1222 pattern = RREGEXP_SRC_PTR(re);
1224 unescaped = rb_reg_preprocess(
1225 pattern, pattern + RREGEXP_SRC_LEN(re), enc,
1226 &fixed_enc, err);
1228 if (unescaped == Qnil) {
1229 rb_raise(rb_eArgError, "regexp preprocess failed: %s", err);
1232 r = onig_new(&reg, (UChar* )RSTRING_PTR(unescaped),
1233 (UChar* )(RSTRING_PTR(unescaped) + RSTRING_LEN(unescaped)),
1234 reg->options, enc,
1235 OnigDefaultSyntax, &einfo);
1236 if (r) {
1237 onig_error_code_to_str((UChar*)err, r, &einfo);
1238 rb_reg_raise(pattern, RREGEXP_SRC_LEN(re), err, re);
1241 RB_GC_GUARD(unescaped);
1242 return reg;
1246 rb_reg_adjust_startpos(VALUE re, VALUE str, int pos, int reverse)
1248 int range;
1249 rb_encoding *enc;
1250 UChar *p, *string;
1252 enc = rb_reg_prepare_enc(re, str, 0);
1254 if (reverse) {
1255 range = -pos;
1257 else {
1258 range = RSTRING_LEN(str) - pos;
1261 if (pos > 0 && ONIGENC_MBC_MAXLEN(enc) != 1 && pos < RSTRING_LEN(str)) {
1262 string = (UChar*)RSTRING_PTR(str);
1264 if (range > 0) {
1265 p = onigenc_get_right_adjust_char_head(enc, string, string + pos);
1267 else {
1268 p = ONIGENC_LEFT_ADJUST_CHAR_HEAD(enc, string, string + pos);
1270 return p - string;
1273 return pos;
1277 rb_reg_search(VALUE re, VALUE str, int pos, int reverse)
1279 int result;
1280 VALUE match;
1281 struct re_registers regi, *regs = &regi;
1282 char *range = RSTRING_PTR(str);
1283 regex_t *reg;
1284 int tmpreg;
1286 if (pos > RSTRING_LEN(str) || pos < 0) {
1287 rb_backref_set(Qnil);
1288 return -1;
1291 reg = rb_reg_prepare_re(re, str);
1292 tmpreg = reg != RREGEXP(re)->ptr;
1293 if (!tmpreg) RREGEXP(re)->usecnt++;
1295 match = rb_backref_get();
1296 if (!NIL_P(match)) {
1297 if (FL_TEST(match, MATCH_BUSY)) {
1298 match = Qnil;
1300 else {
1301 regs = RMATCH_REGS(match);
1304 if (NIL_P(match)) {
1305 MEMZERO(regs, struct re_registers, 1);
1307 if (!reverse) {
1308 range += RSTRING_LEN(str);
1310 result = onig_search(reg,
1311 (UChar*)(RSTRING_PTR(str)),
1312 ((UChar*)(RSTRING_PTR(str)) + RSTRING_LEN(str)),
1313 ((UChar*)(RSTRING_PTR(str)) + pos),
1314 ((UChar*)range),
1315 regs, ONIG_OPTION_NONE);
1316 if (!tmpreg) RREGEXP(re)->usecnt--;
1317 if (tmpreg) {
1318 if (RREGEXP(re)->usecnt) {
1319 onig_free(reg);
1321 else {
1322 onig_free(RREGEXP(re)->ptr);
1323 RREGEXP(re)->ptr = reg;
1326 if (result < 0) {
1327 if (regs == &regi)
1328 onig_region_free(regs, 0);
1329 if (result == ONIG_MISMATCH) {
1330 rb_backref_set(Qnil);
1331 return result;
1333 else {
1334 onig_errmsg_buffer err = "";
1335 onig_error_code_to_str((UChar*)err, result);
1336 rb_reg_raise(RREGEXP_SRC_PTR(re), RREGEXP_SRC_LEN(re), err, 0);
1340 if (NIL_P(match)) {
1341 match = match_alloc(rb_cMatch);
1342 onig_region_copy(RMATCH_REGS(match), regs);
1343 onig_region_free(regs, 0);
1345 else {
1346 if (rb_safe_level() >= 3)
1347 OBJ_TAINT(match);
1348 else
1349 FL_UNSET(match, FL_TAINT);
1352 RMATCH(match)->str = rb_str_new4(str);
1353 RMATCH(match)->regexp = re;
1354 RMATCH(match)->rmatch->char_offset_updated = 0;
1355 rb_backref_set(match);
1357 OBJ_INFECT(match, re);
1358 OBJ_INFECT(match, str);
1360 return result;
1363 VALUE
1364 rb_reg_nth_defined(int nth, VALUE match)
1366 struct re_registers *regs;
1367 if (NIL_P(match)) return Qnil;
1368 match_check(match);
1369 regs = RMATCH_REGS(match);
1370 if (nth >= regs->num_regs) {
1371 return Qnil;
1373 if (nth < 0) {
1374 nth += regs->num_regs;
1375 if (nth <= 0) return Qnil;
1377 if (BEG(nth) == -1) return Qfalse;
1378 return Qtrue;
1381 VALUE
1382 rb_reg_nth_match(int nth, VALUE match)
1384 VALUE str;
1385 long start, end, len;
1386 struct re_registers *regs;
1388 if (NIL_P(match)) return Qnil;
1389 match_check(match);
1390 regs = RMATCH_REGS(match);
1391 if (nth >= regs->num_regs) {
1392 return Qnil;
1394 if (nth < 0) {
1395 nth += regs->num_regs;
1396 if (nth <= 0) return Qnil;
1398 start = BEG(nth);
1399 if (start == -1) return Qnil;
1400 end = END(nth);
1401 len = end - start;
1402 str = rb_str_subseq(RMATCH(match)->str, start, len);
1403 OBJ_INFECT(str, match);
1404 return str;
1407 VALUE
1408 rb_reg_last_match(VALUE match)
1410 return rb_reg_nth_match(0, match);
1415 * call-seq:
1416 * mtch.pre_match => str
1418 * Returns the portion of the original string before the current match.
1419 * Equivalent to the special variable <code>$`</code>.
1421 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1422 * m.pre_match #=> "T"
1425 VALUE
1426 rb_reg_match_pre(VALUE match)
1428 VALUE str;
1429 struct re_registers *regs;
1431 if (NIL_P(match)) return Qnil;
1432 match_check(match);
1433 regs = RMATCH_REGS(match);
1434 if (BEG(0) == -1) return Qnil;
1435 str = rb_str_subseq(RMATCH(match)->str, 0, BEG(0));
1436 if (OBJ_TAINTED(match)) OBJ_TAINT(str);
1437 return str;
1442 * call-seq:
1443 * mtch.post_match => str
1445 * Returns the portion of the original string after the current match.
1446 * Equivalent to the special variable <code>$'</code>.
1448 * m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie")
1449 * m.post_match #=> ": The Movie"
1452 VALUE
1453 rb_reg_match_post(VALUE match)
1455 VALUE str;
1456 long pos;
1457 struct re_registers *regs;
1459 if (NIL_P(match)) return Qnil;
1460 match_check(match);
1461 regs = RMATCH_REGS(match);
1462 if (BEG(0) == -1) return Qnil;
1463 str = RMATCH(match)->str;
1464 pos = END(0);
1465 str = rb_str_subseq(str, pos, RSTRING_LEN(str) - pos);
1466 if (OBJ_TAINTED(match)) OBJ_TAINT(str);
1467 return str;
1470 VALUE
1471 rb_reg_match_last(VALUE match)
1473 int i;
1474 struct re_registers *regs;
1476 if (NIL_P(match)) return Qnil;
1477 match_check(match);
1478 regs = RMATCH_REGS(match);
1479 if (BEG(0) == -1) return Qnil;
1481 for (i=regs->num_regs-1; BEG(i) == -1 && i > 0; i--)
1483 if (i == 0) return Qnil;
1484 return rb_reg_nth_match(i, match);
1487 static VALUE
1488 last_match_getter(void)
1490 return rb_reg_last_match(rb_backref_get());
1493 static VALUE
1494 prematch_getter(void)
1496 return rb_reg_match_pre(rb_backref_get());
1499 static VALUE
1500 postmatch_getter(void)
1502 return rb_reg_match_post(rb_backref_get());
1505 static VALUE
1506 last_paren_match_getter(void)
1508 return rb_reg_match_last(rb_backref_get());
1511 static VALUE
1512 match_array(VALUE match, int start)
1514 struct re_registers *regs = RMATCH_REGS(match);
1515 VALUE ary = rb_ary_new2(regs->num_regs);
1516 VALUE target = RMATCH(match)->str;
1517 int i;
1518 int taint = OBJ_TAINTED(match);
1520 match_check(match);
1521 for (i=start; i<regs->num_regs; i++) {
1522 if (regs->beg[i] == -1) {
1523 rb_ary_push(ary, Qnil);
1525 else {
1526 VALUE str = rb_str_subseq(target, regs->beg[i], regs->end[i]-regs->beg[i]);
1527 if (taint) OBJ_TAINT(str);
1528 rb_ary_push(ary, str);
1531 return ary;
1535 /* [MG]:FIXME: I put parens around the /.../.match() in the first line of the
1536 second example to prevent the '*' followed by a '/' from ending the
1537 comment. */
1540 * call-seq:
1541 * mtch.to_a => anArray
1543 * Returns the array of matches.
1545 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1546 * m.to_a #=> ["HX1138", "H", "X", "113", "8"]
1548 * Because <code>to_a</code> is called when expanding
1549 * <code>*</code><em>variable</em>, there's a useful assignment
1550 * shortcut for extracting matched fields. This is slightly slower than
1551 * accessing the fields directly (as an intermediate array is
1552 * generated).
1554 * all,f1,f2,f3 = *(/(.)(.)(\d+)(\d)/.match("THX1138."))
1555 * all #=> "HX1138"
1556 * f1 #=> "H"
1557 * f2 #=> "X"
1558 * f3 #=> "113"
1561 static VALUE
1562 match_to_a(VALUE match)
1564 return match_array(match, 0);
1569 * call-seq:
1570 * mtch.captures => array
1572 * Returns the array of captures; equivalent to <code>mtch.to_a[1..-1]</code>.
1574 * f1,f2,f3,f4 = /(.)(.)(\d+)(\d)/.match("THX1138.").captures
1575 * f1 #=> "H"
1576 * f2 #=> "X"
1577 * f3 #=> "113"
1578 * f4 #=> "8"
1580 static VALUE
1581 match_captures(VALUE match)
1583 return match_array(match, 1);
1586 static int
1587 name_to_backref_number(struct re_registers *regs, VALUE regexp, const char* name, const char* name_end)
1589 int num;
1591 num = onig_name_to_backref_number(RREGEXP(regexp)->ptr,
1592 (const unsigned char* )name, (const unsigned char* )name_end, regs);
1593 if (num >= 1) {
1594 return num;
1596 else {
1597 VALUE s = rb_str_new(name, (long )(name_end - name));
1598 rb_raise(rb_eIndexError, "undefined group name reference: %s",
1599 StringValuePtr(s));
1604 * call-seq:
1605 * mtch[i] => str or nil
1606 * mtch[start, length] => array
1607 * mtch[range] => array
1608 * mtch[name] => str or nil
1610 * Match Reference---<code>MatchData</code> acts as an array, and may be
1611 * accessed using the normal array indexing techniques. <i>mtch</i>[0] is
1612 * equivalent to the special variable <code>$&</code>, and returns the entire
1613 * matched string. <i>mtch</i>[1], <i>mtch</i>[2], and so on return the values
1614 * of the matched backreferences (portions of the pattern between parentheses).
1616 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1617 * m #=> #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8">
1618 * m[0] #=> "HX1138"
1619 * m[1, 2] #=> ["H", "X"]
1620 * m[1..3] #=> ["H", "X", "113"]
1621 * m[-3, 2] #=> ["X", "113"]
1623 * m = /(?<foo>a+)b/.match("ccaaab")
1624 * m #=> #<MatchData "aaab" foo:"aaa">
1625 * m["foo"] #=> "aaa"
1626 * m[:foo] #=> "aaa"
1629 static VALUE
1630 match_aref(int argc, VALUE *argv, VALUE match)
1632 VALUE idx, rest;
1634 match_check(match);
1635 rb_scan_args(argc, argv, "11", &idx, &rest);
1637 if (NIL_P(rest)) {
1638 if (FIXNUM_P(idx)) {
1639 if (FIX2INT(idx) >= 0) {
1640 return rb_reg_nth_match(FIX2INT(idx), match);
1643 else {
1644 const char *p;
1645 int num;
1647 switch (TYPE(idx)) {
1648 case T_SYMBOL:
1649 p = rb_id2name(SYM2ID(idx));
1650 goto name_to_backref;
1651 break;
1652 case T_STRING:
1653 p = StringValuePtr(idx);
1655 name_to_backref:
1656 num = name_to_backref_number(RMATCH_REGS(match),
1657 RMATCH(match)->regexp, p, p + strlen(p));
1658 return rb_reg_nth_match(num, match);
1659 break;
1661 default:
1662 break;
1667 return rb_ary_aref(argc, argv, match_to_a(match));
1670 static VALUE
1671 match_entry(VALUE match, long n)
1673 return rb_reg_nth_match(n, match);
1678 * call-seq:
1680 * mtch.values_at([index]*) => array
1682 * Uses each <i>index</i> to access the matching values, returning an array of
1683 * the corresponding matches.
1685 * m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie")
1686 * m.to_a #=> ["HX1138", "H", "X", "113", "8"]
1687 * m.values_at(0, 2, -2) #=> ["HX1138", "X", "113"]
1690 static VALUE
1691 match_values_at(int argc, VALUE *argv, VALUE match)
1693 struct re_registers *regs = RMATCH_REGS(match);
1694 match_check(match);
1695 return rb_get_values_at(match, regs->num_regs, argc, argv, match_entry);
1700 * call-seq:
1701 * mtch.to_s => str
1703 * Returns the entire matched string.
1705 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1706 * m.to_s #=> "HX1138"
1709 static VALUE
1710 match_to_s(VALUE match)
1712 VALUE str = rb_reg_last_match(match);
1714 match_check(match);
1715 if (NIL_P(str)) str = rb_str_new(0,0);
1716 if (OBJ_TAINTED(match)) OBJ_TAINT(str);
1717 if (OBJ_TAINTED(RMATCH(match)->str)) OBJ_TAINT(str);
1718 return str;
1723 * call-seq:
1724 * mtch.string => str
1726 * Returns a frozen copy of the string passed in to <code>match</code>.
1728 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1729 * m.string #=> "THX1138."
1732 static VALUE
1733 match_string(VALUE match)
1735 match_check(match);
1736 return RMATCH(match)->str; /* str is frozen */
1739 struct backref_name_tag {
1740 const UChar *name;
1741 long len;
1744 static int
1745 match_inspect_name_iter(const OnigUChar *name, const OnigUChar *name_end,
1746 int back_num, int *back_refs, OnigRegex regex, void *arg0)
1748 struct backref_name_tag *arg = (struct backref_name_tag *)arg0;
1749 int i;
1751 for (i = 0; i < back_num; i++) {
1752 arg[back_refs[i]].name = name;
1753 arg[back_refs[i]].len = name_end - name;
1755 return 0;
1759 * call-seq:
1760 * mtch.inspect => str
1762 * Returns a printable version of <i>mtch</i>.
1764 * puts /.$/.match("foo").inspect
1765 * #=> #<MatchData "o">
1767 * puts /(.)(.)(.)/.match("foo").inspect
1768 * #=> #<MatchData "foo" 1:"f" 2:"o" 3:"o">
1770 * puts /(.)(.)?(.)/.match("fo").inspect
1771 * #=> #<MatchData "fo" 1:"f" 2:nil 3:"o">
1773 * puts /(?<foo>.)(?<bar>.)(?<baz>.)/.match("hoge").inspect
1774 * #=> #<MatchData "hog" foo:"h" bar:"o" baz:"g">
1778 static VALUE
1779 match_inspect(VALUE match)
1781 const char *cname = rb_obj_classname(match);
1782 VALUE str;
1783 int i;
1784 struct re_registers *regs = RMATCH_REGS(match);
1785 int num_regs = regs->num_regs;
1786 struct backref_name_tag *names;
1787 VALUE regexp = RMATCH(match)->regexp;
1789 if (regexp == 0) {
1790 return rb_sprintf("#<%s:%p>", cname, (void*)match);
1793 names = ALLOCA_N(struct backref_name_tag, num_regs);
1794 MEMZERO(names, struct backref_name_tag, num_regs);
1796 onig_foreach_name(RREGEXP(regexp)->ptr,
1797 match_inspect_name_iter, names);
1799 str = rb_str_buf_new2("#<");
1800 rb_str_buf_cat2(str, cname);
1802 for (i = 0; i < num_regs; i++) {
1803 VALUE v;
1804 rb_str_buf_cat2(str, " ");
1805 if (0 < i) {
1806 if (names[i].name)
1807 rb_str_buf_cat(str, (const char *)names[i].name, names[i].len);
1808 else {
1809 char buf[sizeof(i)*3+1];
1810 snprintf(buf, sizeof(buf), "%d", i);
1811 rb_str_buf_cat2(str, buf);
1813 rb_str_buf_cat2(str, ":");
1815 v = rb_reg_nth_match(i, match);
1816 if (v == Qnil)
1817 rb_str_buf_cat2(str, "nil");
1818 else
1819 rb_str_buf_append(str, rb_str_inspect(v));
1821 rb_str_buf_cat2(str, ">");
1823 return str;
1826 VALUE rb_cRegexp;
1828 static int
1829 read_escaped_byte(const char **pp, const char *end, onig_errmsg_buffer err)
1831 const char *p = *pp;
1832 int code;
1833 int meta_prefix = 0, ctrl_prefix = 0;
1834 int len;
1835 int retbyte;
1837 retbyte = -1;
1838 if (p == end || *p++ != '\\') {
1839 strcpy(err, "too short escaped multibyte character");
1840 return -1;
1843 again:
1844 if (p == end) {
1845 strcpy(err, "too short escape sequence");
1846 return -1;
1848 switch (*p++) {
1849 case '\\': code = '\\'; break;
1850 case 'n': code = '\n'; break;
1851 case 't': code = '\t'; break;
1852 case 'r': code = '\r'; break;
1853 case 'f': code = '\f'; break;
1854 case 'v': code = '\013'; break;
1855 case 'a': code = '\007'; break;
1856 case 'e': code = '\033'; break;
1858 /* \OOO */
1859 case '0': case '1': case '2': case '3':
1860 case '4': case '5': case '6': case '7':
1861 p--;
1862 code = ruby_scan_oct(p, end < p+3 ? end-p : 3, &len);
1863 p += len;
1864 break;
1866 case 'x': /* \xHH */
1867 code = ruby_scan_hex(p, end < p+2 ? end-p : 2, &len);
1868 if (len < 1) {
1869 strcpy(err, "invalid hex escape");
1870 return -1;
1872 p += len;
1873 break;
1875 case 'M': /* \M-X, \M-\C-X, \M-\cX */
1876 if (meta_prefix) {
1877 strcpy(err, "duplicate meta escape");
1878 return -1;
1880 meta_prefix = 1;
1881 if (p+1 < end && *p++ == '-' && (*p & 0x80) == 0) {
1882 if (*p == '\\') {
1883 p++;
1884 goto again;
1886 else {
1887 code = *p++;
1888 break;
1891 strcpy(err, "too short meta escape");
1892 return -1;
1894 case 'C': /* \C-X, \C-\M-X */
1895 if (p == end || *p++ != '-') {
1896 strcpy(err, "too short control escape");
1897 return -1;
1899 case 'c': /* \cX, \c\M-X */
1900 if (ctrl_prefix) {
1901 strcpy(err, "duplicate control escape");
1902 return -1;
1904 ctrl_prefix = 1;
1905 if (p < end && (*p & 0x80) == 0) {
1906 if (*p == '\\') {
1907 p++;
1908 goto again;
1910 else {
1911 code = *p++;
1912 break;
1915 strcpy(err, "too short control escape");
1916 return -1;
1918 default:
1919 strcpy(err, "unexpected escape sequence");
1920 return -1;
1922 if (code < 0 || 0xff < code) {
1923 strcpy(err, "invalid escape code");
1924 return -1;
1927 if (ctrl_prefix)
1928 code &= 0x1f;
1929 if (meta_prefix)
1930 code |= 0x80;
1932 *pp = p;
1933 return code;
1936 static int
1937 unescape_escaped_nonascii(const char **pp, const char *end, rb_encoding *enc,
1938 VALUE buf, rb_encoding **encp, onig_errmsg_buffer err)
1940 const char *p = *pp;
1941 int chmaxlen = rb_enc_mbmaxlen(enc);
1942 char *chbuf = ALLOCA_N(char, chmaxlen);
1943 int chlen = 0;
1944 int byte;
1945 int l;
1947 memset(chbuf, 0, chmaxlen);
1949 byte = read_escaped_byte(&p, end, err);
1950 if (byte == -1) {
1951 return -1;
1954 chbuf[chlen++] = byte;
1955 while (chlen < chmaxlen &&
1956 MBCLEN_NEEDMORE_P(rb_enc_precise_mbclen(chbuf, chbuf+chlen, enc))) {
1957 byte = read_escaped_byte(&p, end, err);
1958 if (byte == -1) {
1959 return -1;
1961 chbuf[chlen++] = byte;
1964 l = rb_enc_precise_mbclen(chbuf, chbuf+chlen, enc);
1965 if (MBCLEN_INVALID_P(l)) {
1966 strcpy(err, "invalid multibyte escape");
1967 return -1;
1969 if (1 < chlen || (chbuf[0] & 0x80)) {
1970 rb_str_buf_cat(buf, chbuf, chlen);
1972 if (*encp == 0)
1973 *encp = enc;
1974 else if (*encp != enc) {
1975 strcpy(err, "escaped non ASCII character in UTF-8 regexp");
1976 return -1;
1979 else {
1980 char escbuf[5];
1981 snprintf(escbuf, sizeof(escbuf), "\\x%02X", chbuf[0]&0xff);
1982 rb_str_buf_cat(buf, escbuf, 4);
1984 *pp = p;
1985 return 0;
1988 static int
1989 check_unicode_range(unsigned long code, onig_errmsg_buffer err)
1991 if ((0xd800 <= code && code <= 0xdfff) || /* Surrogates */
1992 0x10ffff < code) {
1993 strcpy(err, "invalid Unicode range");
1994 return -1;
1996 return 0;
1999 static int
2000 append_utf8(unsigned long uv,
2001 VALUE buf, rb_encoding **encp, onig_errmsg_buffer err)
2003 if (check_unicode_range(uv, err) != 0)
2004 return -1;
2005 if (uv < 0x80) {
2006 char escbuf[5];
2007 snprintf(escbuf, sizeof(escbuf), "\\x%02X", (int)uv);
2008 rb_str_buf_cat(buf, escbuf, 4);
2010 else {
2011 int len;
2012 char utf8buf[6];
2013 len = rb_uv_to_utf8(utf8buf, uv);
2014 rb_str_buf_cat(buf, utf8buf, len);
2016 if (*encp == 0)
2017 *encp = rb_utf8_encoding();
2018 else if (*encp != rb_utf8_encoding()) {
2019 strcpy(err, "UTF-8 character in non UTF-8 regexp");
2020 return -1;
2023 return 0;
2026 static int
2027 unescape_unicode_list(const char **pp, const char *end,
2028 VALUE buf, rb_encoding **encp, onig_errmsg_buffer err)
2030 const char *p = *pp;
2031 int has_unicode = 0;
2032 unsigned long code;
2033 int len;
2035 while (p < end && ISSPACE(*p)) p++;
2037 while (1) {
2038 code = ruby_scan_hex(p, end-p, &len);
2039 if (len == 0)
2040 break;
2041 if (6 < len) { /* max 10FFFF */
2042 strcpy(err, "invalid Unicode range");
2043 return -1;
2045 p += len;
2046 if (append_utf8(code, buf, encp, err) != 0)
2047 return -1;
2048 has_unicode = 1;
2050 while (p < end && ISSPACE(*p)) p++;
2053 if (has_unicode == 0) {
2054 strcpy(err, "invalid Unicode list");
2055 return -1;
2058 *pp = p;
2060 return 0;
2063 static int
2064 unescape_unicode_bmp(const char **pp, const char *end,
2065 VALUE buf, rb_encoding **encp, onig_errmsg_buffer err)
2067 const char *p = *pp;
2068 int len;
2069 unsigned long code;
2071 if (end < p+4) {
2072 strcpy(err, "invalid Unicode escape");
2073 return -1;
2075 code = ruby_scan_hex(p, 4, &len);
2076 if (len != 4) {
2077 strcpy(err, "invalid Unicode escape");
2078 return -1;
2080 if (append_utf8(code, buf, encp, err) != 0)
2081 return -1;
2082 *pp = p + 4;
2083 return 0;
2086 static int
2087 unescape_nonascii(const char *p, const char *end, rb_encoding *enc,
2088 VALUE buf, rb_encoding **encp, onig_errmsg_buffer err)
2090 char c;
2091 char smallbuf[2];
2093 while (p < end) {
2094 int chlen = rb_enc_precise_mbclen(p, end, enc);
2095 if (!MBCLEN_CHARFOUND_P(chlen)) {
2096 strcpy(err, "invalid multibyte character");
2097 return -1;
2099 chlen = MBCLEN_CHARFOUND_LEN(chlen);
2100 if (1 < chlen || (*p & 0x80)) {
2101 rb_str_buf_cat(buf, p, chlen);
2102 p += chlen;
2103 if (*encp == 0)
2104 *encp = enc;
2105 else if (*encp != enc) {
2106 strcpy(err, "non ASCII character in UTF-8 regexp");
2107 return -1;
2109 continue;
2112 switch (c = *p++) {
2113 case '\\':
2114 if (p == end) {
2115 strcpy(err, "too short escape sequence");
2116 return -1;
2118 switch (c = *p++) {
2119 case '1': case '2': case '3':
2120 case '4': case '5': case '6': case '7': /* \O, \OO, \OOO or backref */
2122 int octlen;
2123 if (ruby_scan_oct(p-1, end-(p-1), &octlen) <= 0177) {
2124 /* backref or 7bit octal.
2125 no need to unescape anyway.
2126 re-escaping may break backref */
2127 goto escape_asis;
2130 /* xxx: How about more than 199 subexpressions? */
2132 case '0': /* \0, \0O, \0OO */
2134 case 'x': /* \xHH */
2135 case 'c': /* \cX, \c\M-X */
2136 case 'C': /* \C-X, \C-\M-X */
2137 case 'M': /* \M-X, \M-\C-X, \M-\cX */
2138 p = p-2;
2139 if (unescape_escaped_nonascii(&p, end, enc, buf, encp, err) != 0)
2140 return -1;
2141 break;
2143 case 'u':
2144 if (p == end) {
2145 strcpy(err, "too short escape sequence");
2146 return -1;
2148 if (*p == '{') {
2149 /* \u{H HH HHH HHHH HHHHH HHHHHH ...} */
2150 p++;
2151 if (unescape_unicode_list(&p, end, buf, encp, err) != 0)
2152 return -1;
2153 if (p == end || *p++ != '}') {
2154 strcpy(err, "invalid Unicode list");
2155 return -1;
2157 break;
2159 else {
2160 /* \uHHHH */
2161 if (unescape_unicode_bmp(&p, end, buf, encp, err) != 0)
2162 return -1;
2163 break;
2166 default: /* \n, \\, \d, \9, etc. */
2167 escape_asis:
2168 smallbuf[0] = '\\';
2169 smallbuf[1] = c;
2170 rb_str_buf_cat(buf, smallbuf, 2);
2171 break;
2173 break;
2175 default:
2176 rb_str_buf_cat(buf, &c, 1);
2177 break;
2181 return 0;
2184 static VALUE
2185 rb_reg_preprocess(const char *p, const char *end, rb_encoding *enc,
2186 rb_encoding **fixed_enc, onig_errmsg_buffer err)
2188 VALUE buf;
2190 buf = rb_str_buf_new(0);
2192 if (rb_enc_asciicompat(enc))
2193 *fixed_enc = 0;
2194 else {
2195 *fixed_enc = enc;
2196 rb_enc_associate(buf, enc);
2199 if (unescape_nonascii(p, end, enc, buf, fixed_enc, err) != 0)
2200 return Qnil;
2202 if (*fixed_enc) {
2203 rb_enc_associate(buf, *fixed_enc);
2206 return buf;
2209 VALUE
2210 rb_reg_check_preprocess(VALUE str)
2212 rb_encoding *fixed_enc = 0;
2213 onig_errmsg_buffer err = "";
2214 VALUE buf;
2215 char *p, *end;
2216 rb_encoding *enc;
2218 StringValue(str);
2219 p = RSTRING_PTR(str);
2220 end = p + RSTRING_LEN(str);
2221 enc = rb_enc_get(str);
2223 buf = rb_reg_preprocess(p, end, enc, &fixed_enc, err);
2224 RB_GC_GUARD(str);
2226 if (buf == Qnil) {
2227 return rb_reg_error_desc(str, 0, err);
2229 return Qnil;
2232 static VALUE
2233 rb_reg_preprocess_dregexp(VALUE ary)
2235 rb_encoding *fixed_enc = 0;
2236 rb_encoding *regexp_enc = 0;
2237 onig_errmsg_buffer err = "";
2238 int i;
2239 VALUE result = 0;
2240 int argc = RARRAY_LEN(ary);
2241 VALUE *argv = RARRAY_PTR(ary);
2243 if (argc == 0) {
2244 rb_raise(rb_eArgError, "no arguments given");
2247 for (i = 0; i < argc; i++) {
2248 VALUE str = argv[i];
2249 VALUE buf;
2250 char *p, *end;
2251 rb_encoding *src_enc;
2253 StringValue(str);
2254 p = RSTRING_PTR(str);
2255 end = p + RSTRING_LEN(str);
2256 src_enc = rb_enc_get(str);
2258 buf = rb_reg_preprocess(p, end, src_enc, &fixed_enc, err);
2260 if (buf == Qnil)
2261 rb_raise(rb_eArgError, "%s", err);
2263 if (fixed_enc != 0) {
2264 if (regexp_enc != 0 && regexp_enc != fixed_enc) {
2265 rb_raise(rb_eArgError, "encoding mismatch in dynamic regexp : %s and %s",
2266 rb_enc_name(regexp_enc), rb_enc_name(fixed_enc));
2268 regexp_enc = fixed_enc;
2271 if (!result)
2272 result = rb_str_new3(str);
2273 else
2274 rb_str_buf_append(result, str);
2276 if (regexp_enc) {
2277 rb_enc_associate(result, regexp_enc);
2280 return result;
2283 static int
2284 rb_reg_initialize(VALUE obj, const char *s, int len, rb_encoding *enc,
2285 int options, onig_errmsg_buffer err)
2287 struct RRegexp *re = RREGEXP(obj);
2288 VALUE unescaped;
2289 rb_encoding *fixed_enc = 0;
2290 rb_encoding *a_enc = rb_ascii8bit_encoding();
2292 if (!OBJ_TAINTED(obj) && rb_safe_level() >= 4)
2293 rb_raise(rb_eSecurityError, "Insecure: can't modify regexp");
2294 rb_check_frozen(obj);
2295 if (FL_TEST(obj, REG_LITERAL))
2296 rb_raise(rb_eSecurityError, "can't modify literal regexp");
2297 if (re->ptr)
2298 rb_raise(rb_eTypeError, "already initialized regexp");
2299 re->ptr = 0;
2301 unescaped = rb_reg_preprocess(s, s+len, enc, &fixed_enc, err);
2302 if (unescaped == Qnil)
2303 return -1;
2305 if (fixed_enc) {
2306 if ((fixed_enc != enc && (options & ARG_ENCODING_FIXED)) ||
2307 (fixed_enc != a_enc && (options & ARG_ENCODING_NONE))) {
2308 strcpy(err, "incompatible character encoding");
2309 return -1;
2311 if (fixed_enc != a_enc) {
2312 options |= ARG_ENCODING_FIXED;
2313 enc = fixed_enc;
2316 else if (!(options & ARG_ENCODING_FIXED)) {
2317 enc = rb_usascii_encoding();
2320 rb_enc_associate((VALUE)re, enc);
2321 if ((options & ARG_ENCODING_FIXED) || fixed_enc) {
2322 re->basic.flags |= KCODE_FIXED;
2324 if (options & ARG_ENCODING_NONE) {
2325 re->basic.flags |= REG_ENCODING_NONE;
2328 re->ptr = make_regexp(RSTRING_PTR(unescaped), RSTRING_LEN(unescaped), enc,
2329 options & ARG_REG_OPTION_MASK, err);
2330 if (!re->ptr) return -1;
2331 re->src = rb_enc_str_new(s, len, enc);
2332 OBJ_FREEZE(re->src);
2333 RB_GC_GUARD(unescaped);
2334 return 0;
2337 static int
2338 rb_reg_initialize_str(VALUE obj, VALUE str, int options, onig_errmsg_buffer err)
2340 int ret;
2341 rb_encoding *enc = rb_enc_get(str);
2342 if (options & ARG_ENCODING_NONE) {
2343 rb_encoding *ascii8bit = rb_ascii8bit_encoding();
2344 if (enc != ascii8bit) {
2345 if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
2346 strcpy(err, "/.../n has a non escaped non ASCII character in non ASCII-8BIT script");
2347 return -1;
2349 enc = ascii8bit;
2352 ret = rb_reg_initialize(obj, RSTRING_PTR(str), RSTRING_LEN(str), enc,
2353 options, err);
2354 RB_GC_GUARD(str);
2355 return ret;
2358 static VALUE
2359 rb_reg_s_alloc(VALUE klass)
2361 NEWOBJ(re, struct RRegexp);
2362 OBJSETUP(re, klass, T_REGEXP);
2364 re->ptr = 0;
2365 re->src = 0;
2366 re->usecnt = 0;
2368 return (VALUE)re;
2371 VALUE
2372 rb_reg_new_str(VALUE s, int options)
2374 VALUE re = rb_reg_s_alloc(rb_cRegexp);
2375 onig_errmsg_buffer err = "";
2377 if (rb_reg_initialize_str(re, s, options, err) != 0) {
2378 rb_reg_raise_str(s, options, err);
2381 return re;
2384 VALUE
2385 rb_reg_new_ary(VALUE ary, int opt)
2387 return rb_reg_new_str(rb_reg_preprocess_dregexp(ary), opt);
2390 VALUE
2391 rb_enc_reg_new(const char *s, long len, rb_encoding *enc, int options)
2393 VALUE re = rb_reg_s_alloc(rb_cRegexp);
2394 onig_errmsg_buffer err = "";
2396 if (rb_reg_initialize(re, s, len, enc, options, err) != 0) {
2397 rb_enc_reg_raise(s, len, enc, options, err);
2400 return re;
2403 VALUE
2404 rb_reg_new(const char *s, long len, int options)
2406 return rb_enc_reg_new(s, len, rb_ascii8bit_encoding(), options);
2409 VALUE
2410 rb_reg_compile(VALUE str, int options)
2412 VALUE re = rb_reg_s_alloc(rb_cRegexp);
2413 onig_errmsg_buffer err = "";
2415 if (!str) str = rb_str_new(0,0);
2416 if (rb_reg_initialize_str(re, str, options, err) != 0) {
2417 rb_set_errinfo(rb_reg_error_desc(str, options, err));
2418 return Qnil;
2420 FL_SET(re, REG_LITERAL);
2421 return re;
2424 static VALUE reg_cache;
2426 VALUE
2427 rb_reg_regcomp(VALUE str)
2429 volatile VALUE save_str = str;
2430 if (reg_cache && RREGEXP_SRC_LEN(reg_cache) == RSTRING_LEN(str)
2431 && ENCODING_GET(reg_cache) == ENCODING_GET(str)
2432 && memcmp(RREGEXP_SRC_PTR(reg_cache), RSTRING_PTR(str), RSTRING_LEN(str)) == 0)
2433 return reg_cache;
2435 return reg_cache = rb_reg_new_str(save_str, 0);
2439 * call-seq:
2440 * rxp.hash => fixnum
2442 * Produce a hash based on the text and options of this regular expression.
2445 static VALUE
2446 rb_reg_hash(VALUE re)
2448 int hashval, len;
2449 char *p;
2451 rb_reg_check(re);
2452 hashval = RREGEXP(re)->ptr->options;
2453 len = RREGEXP_SRC_LEN(re);
2454 p = RREGEXP_SRC_PTR(re);
2455 while (len--) {
2456 hashval = hashval * 33 + *p++;
2458 hashval = hashval + (hashval>>5);
2460 return INT2FIX(hashval);
2465 * call-seq:
2466 * rxp == other_rxp => true or false
2467 * rxp.eql?(other_rxp) => true or false
2469 * Equality---Two regexps are equal if their patterns are identical, they have
2470 * the same character set code, and their <code>casefold?</code> values are the
2471 * same.
2473 * /abc/ == /abc/x #=> false
2474 * /abc/ == /abc/i #=> false
2475 * /abc/ == /abc/n #=> false
2476 * /abc/u == /abc/n #=> false
2479 static VALUE
2480 rb_reg_equal(VALUE re1, VALUE re2)
2482 if (re1 == re2) return Qtrue;
2483 if (TYPE(re2) != T_REGEXP) return Qfalse;
2484 rb_reg_check(re1); rb_reg_check(re2);
2485 if (FL_TEST(re1, KCODE_FIXED) != FL_TEST(re2, KCODE_FIXED)) return Qfalse;
2486 if (RREGEXP(re1)->ptr->options != RREGEXP(re2)->ptr->options) return Qfalse;
2487 if (RREGEXP_SRC_LEN(re1) != RREGEXP_SRC_LEN(re2)) return Qfalse;
2488 if (ENCODING_GET(re1) != ENCODING_GET(re2)) return Qfalse;
2489 if (memcmp(RREGEXP_SRC_PTR(re1), RREGEXP_SRC_PTR(re2), RREGEXP_SRC_LEN(re1)) == 0) {
2490 return Qtrue;
2492 return Qfalse;
2495 static VALUE
2496 reg_operand(VALUE s, int check)
2498 if (SYMBOL_P(s)) {
2499 return rb_sym_to_s(s);
2501 else {
2502 VALUE tmp = rb_check_string_type(s);
2503 if (check && NIL_P(tmp)) {
2504 rb_raise(rb_eTypeError, "can't convert %s to String",
2505 rb_obj_classname(s));
2507 return tmp;
2511 static long
2512 reg_match_pos(VALUE re, VALUE *strp, long pos)
2514 VALUE str = *strp;
2516 if (NIL_P(str)) {
2517 rb_backref_set(Qnil);
2518 return -1;
2520 *strp = str = reg_operand(str, Qtrue);
2521 if (pos != 0) {
2522 if (pos < 0) {
2523 VALUE l = rb_str_length(str);
2524 pos += NUM2INT(l);
2525 if (pos < 0) {
2526 return pos;
2529 pos = rb_reg_adjust_startpos(re, str, pos, 0);
2531 return rb_reg_search(re, str, pos, 0);
2535 * call-seq:
2536 * rxp =~ str => integer or nil
2538 * Match---Matches <i>rxp</i> against <i>str</i>.
2540 * /at/ =~ "input data" #=> 7
2541 * /ax/ =~ "input data" #=> nil
2543 * If <code>=~</code> is used with a regexp literal with named captures,
2544 * captured strings (or nil) is assigned to local variables named by
2545 * the capture names.
2547 * /(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/ =~ " x = y "
2548 * p lhs #=> "x"
2549 * p rhs #=> "y"
2551 * If it is not matched, nil is assigned for the variables.
2553 * /(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/ =~ " x = "
2554 * p lhs #=> nil
2555 * p rhs #=> nil
2557 * This assignment is implemented in the Ruby parser.
2558 * So a regexp literal is required for the assignment.
2559 * The assignment is not occur if the regexp is not a literal.
2561 * re = /(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/
2562 * re =~ " x = "
2563 * p lhs # undefined local variable
2564 * p rhs # undefined local variable
2566 * A regexp interpolation, <code>#{}</code>, also disables
2567 * the assignment.
2569 * rhs_pat = /(?<rhs>\w+)/
2570 * /(?<lhs>\w+)\s*=\s*#{rhs_pat}/ =~ "x = y"
2571 * p lhs # undefined local variable
2575 VALUE
2576 rb_reg_match(VALUE re, VALUE str)
2578 long pos = reg_match_pos(re, &str, 0);
2579 if (pos < 0) return Qnil;
2580 pos = rb_str_sublen(str, pos);
2581 return LONG2FIX(pos);
2585 * call-seq:
2586 * rxp === str => true or false
2588 * Case Equality---Synonym for <code>Regexp#=~</code> used in case statements.
2590 * a = "HELLO"
2591 * case a
2592 * when /^[a-z]*$/; print "Lower case\n"
2593 * when /^[A-Z]*$/; print "Upper case\n"
2594 * else; print "Mixed case\n"
2595 * end
2597 * <em>produces:</em>
2599 * Upper case
2602 VALUE
2603 rb_reg_eqq(VALUE re, VALUE str)
2605 long start;
2607 str = reg_operand(str, Qfalse);
2608 if (NIL_P(str)) {
2609 rb_backref_set(Qnil);
2610 return Qfalse;
2612 start = rb_reg_search(re, str, 0, 0);
2613 if (start < 0) {
2614 return Qfalse;
2616 return Qtrue;
2621 * call-seq:
2622 * ~ rxp => integer or nil
2624 * Match---Matches <i>rxp</i> against the contents of <code>$_</code>.
2625 * Equivalent to <code><i>rxp</i> =~ $_</code>.
2627 * $_ = "input data"
2628 * ~ /at/ #=> 7
2631 VALUE
2632 rb_reg_match2(VALUE re)
2634 long start;
2635 VALUE line = rb_lastline_get();
2637 if (TYPE(line) != T_STRING) {
2638 rb_backref_set(Qnil);
2639 return Qnil;
2642 start = rb_reg_search(re, line, 0, 0);
2643 if (start < 0) {
2644 return Qnil;
2646 start = rb_str_sublen(line, start);
2647 return LONG2FIX(start);
2652 * call-seq:
2653 * rxp.match(str) => matchdata or nil
2654 * rxp.match(str,pos) => matchdata or nil
2656 * Returns a <code>MatchData</code> object describing the match, or
2657 * <code>nil</code> if there was no match. This is equivalent to retrieving the
2658 * value of the special variable <code>$~</code> following a normal match.
2659 * If the second parameter is present, it specifies the position in the string
2660 * to begin the search.
2662 * /(.)(.)(.)/.match("abc")[2] #=> "b"
2663 * /(.)(.)/.match("abc", 1)[2] #=> "c"
2665 * If a block is given, invoke the block with MatchData if match succeed, so
2666 * that you can write
2668 * pat.match(str) {|m| ...}
2670 * instead of
2672 * if m = pat.match(str)
2673 * ...
2674 * end
2676 * The return value is a value from block execution in this case.
2679 static VALUE
2680 rb_reg_match_m(int argc, VALUE *argv, VALUE re)
2682 VALUE result, str, initpos;
2683 long pos;
2685 if (rb_scan_args(argc, argv, "11", &str, &initpos) == 2) {
2686 pos = NUM2LONG(initpos);
2688 else {
2689 pos = 0;
2692 pos = reg_match_pos(re, &str, pos);
2693 if (pos < 0) {
2694 rb_backref_set(Qnil);
2695 return Qnil;
2697 result = rb_backref_get();
2698 rb_match_busy(result);
2699 if (!NIL_P(result) && rb_block_given_p()) {
2700 return rb_yield(result);
2702 return result;
2706 * Document-method: compile
2708 * Synonym for <code>Regexp.new</code>
2713 * call-seq:
2714 * Regexp.new(string [, options]) => regexp
2715 * Regexp.new(regexp) => regexp
2716 * Regexp.compile(string [, options]) => regexp
2717 * Regexp.compile(regexp) => regexp
2719 * Constructs a new regular expression from <i>pattern</i>, which can be either
2720 * a <code>String</code> or a <code>Regexp</code> (in which case that regexp's
2721 * options are propagated, and new options may not be specified (a change as of
2722 * Ruby 1.8). If <i>options</i> is a <code>Fixnum</code>, it should be one or
2723 * more of the constants <code>Regexp::EXTENDED</code>,
2724 * <code>Regexp::IGNORECASE</code>, and <code>Regexp::MULTILINE</code>,
2725 * <em>or</em>-ed together. Otherwise, if <i>options</i> is not
2726 * <code>nil</code>, the regexp will be case insensitive.
2728 * r1 = Regexp.new('^a-z+:\\s+\w+') #=> /^a-z+:\s+\w+/
2729 * r2 = Regexp.new('cat', true) #=> /cat/i
2730 * r3 = Regexp.new('dog', Regexp::EXTENDED) #=> /dog/x
2731 * r4 = Regexp.new(r2) #=> /cat/i
2734 static VALUE
2735 rb_reg_initialize_m(int argc, VALUE *argv, VALUE self)
2737 onig_errmsg_buffer err = "";
2738 int flags = 0;
2739 VALUE str;
2740 rb_encoding *enc;
2741 const char *ptr;
2742 long len;
2744 if (argc == 0 || argc > 3) {
2745 rb_raise(rb_eArgError, "wrong number of arguments");
2747 if (TYPE(argv[0]) == T_REGEXP) {
2748 VALUE re = argv[0];
2750 if (argc > 1) {
2751 rb_warn("flags ignored");
2753 rb_reg_check(re);
2754 flags = rb_reg_options(re);
2755 ptr = RREGEXP_SRC_PTR(re);
2756 len = RREGEXP_SRC_LEN(re);
2757 enc = rb_enc_get(re);
2758 if (rb_reg_initialize(self, ptr, len, enc, flags, err)) {
2759 str = rb_enc_str_new(ptr, len, enc);
2760 rb_reg_raise_str(str, flags, err);
2763 else {
2764 if (argc >= 2) {
2765 if (FIXNUM_P(argv[1])) flags = FIX2INT(argv[1]);
2766 else if (RTEST(argv[1])) flags = ONIG_OPTION_IGNORECASE;
2768 enc = 0;
2769 if (argc == 3 && !NIL_P(argv[2])) {
2770 char *kcode = StringValuePtr(argv[2]);
2771 if (kcode[0] == 'n' || kcode[1] == 'N') {
2772 enc = rb_ascii8bit_encoding();
2773 flags |= ARG_ENCODING_FIXED;
2775 else {
2776 rb_warning("encoding option is obsolete - %s", kcode);
2779 str = argv[0];
2780 ptr = StringValuePtr(str);
2781 if (enc
2782 ? rb_reg_initialize(self, ptr, RSTRING_LEN(str), enc, flags, err)
2783 : rb_reg_initialize_str(self, str, flags, err)) {
2784 rb_reg_raise_str(str, flags, err);
2787 return self;
2790 VALUE
2791 rb_reg_quote(VALUE str)
2793 rb_encoding *enc = rb_enc_get(str);
2794 char *s, *send, *t;
2795 VALUE tmp;
2796 int c, clen;
2797 int ascii_only = rb_enc_str_asciionly_p(str);
2799 s = RSTRING_PTR(str);
2800 send = s + RSTRING_LEN(str);
2801 while (s < send) {
2802 c = rb_enc_ascget(s, send, &clen, enc);
2803 if (c == -1) {
2804 s += mbclen(s, send, enc);
2805 continue;
2807 switch (c) {
2808 case '[': case ']': case '{': case '}':
2809 case '(': case ')': case '|': case '-':
2810 case '*': case '.': case '\\':
2811 case '?': case '+': case '^': case '$':
2812 case ' ': case '#':
2813 case '\t': case '\f': case '\v': case '\n': case '\r':
2814 goto meta_found;
2816 s += clen;
2818 tmp = rb_str_new3(str);
2819 if (ascii_only) {
2820 rb_enc_associate(tmp, rb_usascii_encoding());
2822 return tmp;
2824 meta_found:
2825 tmp = rb_str_new(0, RSTRING_LEN(str)*2);
2826 if (ascii_only) {
2827 rb_enc_associate(tmp, rb_usascii_encoding());
2829 else {
2830 rb_enc_copy(tmp, str);
2832 t = RSTRING_PTR(tmp);
2833 /* copy upto metacharacter */
2834 memcpy(t, RSTRING_PTR(str), s - RSTRING_PTR(str));
2835 t += s - RSTRING_PTR(str);
2837 while (s < send) {
2838 c = rb_enc_ascget(s, send, &clen, enc);
2839 if (c == -1) {
2840 int n = mbclen(s, send, enc);
2842 while (n--)
2843 *t++ = *s++;
2844 continue;
2846 s += clen;
2847 switch (c) {
2848 case '[': case ']': case '{': case '}':
2849 case '(': case ')': case '|': case '-':
2850 case '*': case '.': case '\\':
2851 case '?': case '+': case '^': case '$':
2852 case '#':
2853 *t++ = '\\';
2854 break;
2855 case ' ':
2856 *t++ = '\\';
2857 *t++ = ' ';
2858 continue;
2859 case '\t':
2860 *t++ = '\\';
2861 *t++ = 't';
2862 continue;
2863 case '\n':
2864 *t++ = '\\';
2865 *t++ = 'n';
2866 continue;
2867 case '\r':
2868 *t++ = '\\';
2869 *t++ = 'r';
2870 continue;
2871 case '\f':
2872 *t++ = '\\';
2873 *t++ = 'f';
2874 continue;
2875 case '\v':
2876 *t++ = '\\';
2877 *t++ = 'v';
2878 continue;
2880 *t++ = c;
2882 rb_str_resize(tmp, t - RSTRING_PTR(tmp));
2883 OBJ_INFECT(tmp, str);
2884 return tmp;
2889 * call-seq:
2890 * Regexp.escape(str) => string
2891 * Regexp.quote(str) => string
2893 * Escapes any characters that would have special meaning in a regular
2894 * expression. Returns a new escaped string, or self if no characters are
2895 * escaped. For any string,
2896 * <code>Regexp.new(Regexp.escape(<i>str</i>))=~<i>str</i></code> will be true.
2898 * Regexp.escape('\*?{}.') #=> \\\*\?\{\}\.
2902 static VALUE
2903 rb_reg_s_quote(VALUE c, VALUE str)
2905 return rb_reg_quote(reg_operand(str, Qtrue));
2909 rb_reg_options(VALUE re)
2911 int options;
2913 rb_reg_check(re);
2914 options = RREGEXP(re)->ptr->options & ARG_REG_OPTION_MASK;
2915 if (RBASIC(re)->flags & KCODE_FIXED) options |= ARG_ENCODING_FIXED;
2916 if (RBASIC(re)->flags & REG_ENCODING_NONE) options |= ARG_ENCODING_NONE;
2917 return options;
2920 VALUE
2921 rb_check_regexp_type(VALUE re)
2923 return rb_check_convert_type(re, T_REGEXP, "Regexp", "to_regexp");
2927 * call-seq:
2928 * Regexp.try_convert(obj) -> re or nil
2930 * Try to convert <i>obj</i> into a Regexp, using to_regexp method.
2931 * Returns converted regexp or nil if <i>obj</i> cannot be converted
2932 * for any reason.
2934 * Regexp.try_convert(/re/) #=> /re/
2935 * Regexp.try_convert("re") #=> nil
2937 * o = Object.new
2938 * Regexp.try_convert(o) #=> nil
2939 * def o.to_regexp() /foo/ end
2940 * Regexp.try_convert(o) #=> /foo/
2943 static VALUE
2944 rb_reg_s_try_convert(VALUE dummy, VALUE re)
2946 return rb_check_regexp_type(re);
2949 static VALUE
2950 rb_reg_s_union(VALUE self, VALUE args0)
2952 long argc = RARRAY_LEN(args0);
2954 if (argc == 0) {
2955 VALUE args[1];
2956 args[0] = rb_str_new2("(?!)");
2957 return rb_class_new_instance(1, args, rb_cRegexp);
2959 else if (argc == 1) {
2960 VALUE arg = rb_ary_entry(args0, 0);
2961 VALUE re = rb_check_regexp_type(arg);
2962 if (!NIL_P(re))
2963 return re;
2964 else {
2965 VALUE quoted;
2966 quoted = rb_reg_s_quote(Qnil, arg);
2967 return rb_reg_new_str(quoted, 0);
2970 else {
2971 int i;
2972 VALUE source = rb_str_buf_new(0);
2973 rb_encoding *result_enc;
2975 int has_asciionly = 0;
2976 rb_encoding *has_ascii_compat_fixed = 0;
2977 rb_encoding *has_ascii_incompat = 0;
2979 for (i = 0; i < argc; i++) {
2980 volatile VALUE v;
2981 VALUE e = rb_ary_entry(args0, i);
2983 if (0 < i)
2984 rb_str_buf_cat_ascii(source, "|");
2986 v = rb_check_regexp_type(e);
2987 if (!NIL_P(v)) {
2988 rb_encoding *enc = rb_enc_get(v);
2989 if (!rb_enc_asciicompat(enc)) {
2990 if (!has_ascii_incompat)
2991 has_ascii_incompat = enc;
2992 else if (has_ascii_incompat != enc)
2993 rb_raise(rb_eArgError, "incompatible encodings: %s and %s",
2994 rb_enc_name(has_ascii_incompat), rb_enc_name(enc));
2996 else if (rb_reg_fixed_encoding_p(v)) {
2997 if (!has_ascii_compat_fixed)
2998 has_ascii_compat_fixed = enc;
2999 else if (has_ascii_compat_fixed != enc)
3000 rb_raise(rb_eArgError, "incompatible encodings: %s and %s",
3001 rb_enc_name(has_ascii_compat_fixed), rb_enc_name(enc));
3003 else {
3004 has_asciionly = 1;
3006 v = rb_reg_to_s(v);
3008 else {
3009 rb_encoding *enc = rb_enc_get(e);
3010 StringValue(e);
3011 enc = rb_enc_get(e);
3012 if (!rb_enc_str_asciicompat_p(e)) {
3013 if (!has_ascii_incompat)
3014 has_ascii_incompat = enc;
3015 else if (has_ascii_incompat != enc)
3016 rb_raise(rb_eArgError, "incompatible encodings: %s and %s",
3017 rb_enc_name(has_ascii_incompat), rb_enc_name(enc));
3019 else if (rb_enc_str_asciionly_p(e)) {
3020 has_asciionly = 1;
3022 else {
3023 if (!has_ascii_compat_fixed)
3024 has_ascii_compat_fixed = enc;
3025 else if (has_ascii_compat_fixed != enc)
3026 rb_raise(rb_eArgError, "incompatible encodings: %s and %s",
3027 rb_enc_name(has_ascii_compat_fixed), rb_enc_name(enc));
3029 v = rb_reg_s_quote(Qnil, e);
3031 if (has_ascii_incompat) {
3032 if (has_asciionly) {
3033 rb_raise(rb_eArgError, "ASCII incompatible encoding: %s",
3034 rb_enc_name(has_ascii_incompat));
3036 if (has_ascii_compat_fixed) {
3037 rb_raise(rb_eArgError, "incompatible encodings: %s and %s",
3038 rb_enc_name(has_ascii_incompat), rb_enc_name(has_ascii_compat_fixed));
3042 if (i == 0) {
3043 rb_enc_copy(source, v);
3045 rb_str_append(source, v);
3048 if (has_ascii_incompat) {
3049 result_enc = has_ascii_incompat;
3051 else if (has_ascii_compat_fixed) {
3052 result_enc = has_ascii_compat_fixed;
3054 else {
3055 result_enc = rb_ascii8bit_encoding();
3058 rb_enc_associate(source, result_enc);
3059 return rb_class_new_instance(1, &source, rb_cRegexp);
3064 * call-seq:
3065 * Regexp.union(pat1, pat2, ...) => new_regexp
3066 * Regexp.union(pats_ary) => new_regexp
3068 * Return a <code>Regexp</code> object that is the union of the given
3069 * <em>pattern</em>s, i.e., will match any of its parts. The <em>pattern</em>s
3070 * can be Regexp objects, in which case their options will be preserved, or
3071 * Strings. If no patterns are given, returns <code>/(?!)/</code>.
3073 * Regexp.union #=> /(?!)/
3074 * Regexp.union("penzance") #=> /penzance/
3075 * Regexp.union("a+b*c") #=> /a\+b\*c/
3076 * Regexp.union("skiing", "sledding") #=> /skiing|sledding/
3077 * Regexp.union(["skiing", "sledding"]) #=> /skiing|sledding/
3078 * Regexp.union(/dogs/, /cats/i) #=> /(?-mix:dogs)|(?i-mx:cats)/
3080 static VALUE
3081 rb_reg_s_union_m(VALUE self, VALUE args)
3083 VALUE v;
3084 if (RARRAY_LEN(args) == 1 &&
3085 !NIL_P(v = rb_check_array_type(rb_ary_entry(args, 0)))) {
3086 return rb_reg_s_union(self, v);
3088 return rb_reg_s_union(self, args);
3091 /* :nodoc: */
3092 static VALUE
3093 rb_reg_init_copy(VALUE copy, VALUE re)
3095 onig_errmsg_buffer err = "";
3096 const char *s;
3097 long len;
3099 if (copy == re) return copy;
3100 rb_check_frozen(copy);
3101 /* need better argument type check */
3102 if (!rb_obj_is_instance_of(re, rb_obj_class(copy))) {
3103 rb_raise(rb_eTypeError, "wrong argument type");
3105 rb_reg_check(re);
3106 s = RREGEXP_SRC_PTR(re);
3107 len = RREGEXP_SRC_LEN(re);
3108 if (rb_reg_initialize(copy, s, len, rb_enc_get(re), rb_reg_options(re), err) != 0) {
3109 rb_reg_raise(s, len, err, re);
3111 return copy;
3114 VALUE
3115 rb_reg_regsub(VALUE str, VALUE src, struct re_registers *regs, VALUE regexp)
3117 VALUE val = 0;
3118 char *p, *s, *e;
3119 int no, clen;
3120 rb_encoding *str_enc = rb_enc_get(str);
3121 rb_encoding *src_enc = rb_enc_get(src);
3122 int acompat = rb_enc_asciicompat(str_enc);
3123 #define ASCGET(s,e,cl) (acompat ? (*cl=1,s[0]) : rb_enc_ascget(s, e, cl, str_enc))
3125 p = s = RSTRING_PTR(str);
3126 e = s + RSTRING_LEN(str);
3128 while (s < e) {
3129 int c = ASCGET(s, e, &clen);
3130 char *ss;
3132 if (c == -1) {
3133 s += mbclen(s, e, str_enc);
3134 continue;
3136 ss = s;
3137 s += clen;
3139 if (c != '\\' || s == e) continue;
3141 if (!val) {
3142 val = rb_str_buf_new(ss-p);
3144 rb_enc_str_buf_cat(val, p, ss-p, str_enc);
3146 c = ASCGET(s, e, &clen);
3147 if (c == -1) {
3148 s += mbclen(s, e, str_enc);
3149 rb_enc_str_buf_cat(val, ss, s-ss, str_enc);
3150 p = s;
3151 continue;
3153 s += clen;
3155 p = s;
3156 switch (c) {
3157 case '1': case '2': case '3': case '4':
3158 case '5': case '6': case '7': case '8': case '9':
3159 if (onig_noname_group_capture_is_active(RREGEXP(regexp)->ptr)) {
3160 no = c - '0';
3162 else {
3163 continue;
3165 break;
3167 case 'k':
3168 if (s < e && ASCGET(s, e, &clen) == '<') {
3169 char *name, *name_end;
3171 name_end = name = s + clen;
3172 while (name_end < e) {
3173 c = ASCGET(name_end, e, &clen);
3174 if (c == '>') break;
3175 name_end += c == -1 ? mbclen(name_end, e, str_enc) : clen;
3177 if (name_end < e) {
3178 no = name_to_backref_number(regs, regexp, name, name_end);
3179 p = s = name_end + clen;
3180 break;
3182 else {
3183 rb_raise(rb_eRuntimeError, "invalid group name reference format");
3187 rb_enc_str_buf_cat(val, ss, s-ss, str_enc);
3188 continue;
3190 case '0':
3191 case '&':
3192 no = 0;
3193 break;
3195 case '`':
3196 rb_enc_str_buf_cat(val, RSTRING_PTR(src), BEG(0), src_enc);
3197 continue;
3199 case '\'':
3200 rb_enc_str_buf_cat(val, RSTRING_PTR(src)+END(0), RSTRING_LEN(src)-END(0), src_enc);
3201 continue;
3203 case '+':
3204 no = regs->num_regs-1;
3205 while (BEG(no) == -1 && no > 0) no--;
3206 if (no == 0) continue;
3207 break;
3209 case '\\':
3210 rb_enc_str_buf_cat(val, s-clen, clen, str_enc);
3211 continue;
3213 default:
3214 rb_enc_str_buf_cat(val, ss, s-ss, str_enc);
3215 continue;
3218 if (no >= 0) {
3219 if (no >= regs->num_regs) continue;
3220 if (BEG(no) == -1) continue;
3221 rb_enc_str_buf_cat(val, RSTRING_PTR(src)+BEG(no), END(no)-BEG(no), src_enc);
3225 if (!val) return str;
3226 if (p < e) {
3227 rb_enc_str_buf_cat(val, p, e-p, str_enc);
3230 return val;
3233 static VALUE
3234 kcode_getter(void)
3236 rb_warn("variable $KCODE is no longer effective");
3237 return Qnil;
3240 static void
3241 kcode_setter(VALUE val, ID id)
3243 rb_warn("variable $KCODE is no longer effective; ignored");
3246 static VALUE
3247 ignorecase_getter(void)
3249 rb_warn("variable $= is no longer effective");
3250 return Qfalse;
3253 static void
3254 ignorecase_setter(VALUE val, ID id)
3256 rb_warn("variable $= is no longer effective; ignored");
3259 static VALUE
3260 match_getter(void)
3262 VALUE match = rb_backref_get();
3264 if (NIL_P(match)) return Qnil;
3265 rb_match_busy(match);
3266 return match;
3269 static void
3270 match_setter(VALUE val)
3272 if (!NIL_P(val)) {
3273 Check_Type(val, T_MATCH);
3275 rb_backref_set(val);
3279 * call-seq:
3280 * Regexp.last_match => matchdata
3281 * Regexp.last_match(n) => str
3283 * The first form returns the <code>MatchData</code> object generated by the
3284 * last successful pattern match. Equivalent to reading the global variable
3285 * <code>$~</code>. The second form returns the <i>n</i>th field in this
3286 * <code>MatchData</code> object.
3287 * <em>n</em> can be a string or symbol to reference a named capture.
3289 * /c(.)t/ =~ 'cat' #=> 0
3290 * Regexp.last_match #=> #<MatchData "cat" 1:"a">
3291 * Regexp.last_match(0) #=> "cat"
3292 * Regexp.last_match(1) #=> "a"
3293 * Regexp.last_match(2) #=> nil
3295 * /(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/ =~ "var = val"
3296 * Regexp.last_match #=> #<MatchData "var = val" lhs:"var" rhs:"val">
3297 * Regexp.last_match(:lhs) #=> "var"
3298 * Regexp.last_match(:rhs) #=> "val"
3301 static VALUE
3302 rb_reg_s_last_match(int argc, VALUE *argv)
3304 VALUE nth;
3306 if (argc > 0 && rb_scan_args(argc, argv, "01", &nth) == 1) {
3307 VALUE match = rb_backref_get();
3308 int n;
3309 if (NIL_P(match)) return Qnil;
3310 n = match_backref_number(match, nth);
3311 return rb_reg_nth_match(n, match);
3313 return match_getter();
3316 static void
3317 re_warn(const char *s)
3319 rb_warn("%s", s);
3323 * Document-class: Regexp
3325 * A <code>Regexp</code> holds a regular expression, used to match a pattern
3326 * against strings. Regexps are created using the <code>/.../</code> and
3327 * <code>%r{...}</code> literals, and by the <code>Regexp::new</code>
3328 * constructor.
3332 void
3333 Init_Regexp(void)
3335 rb_eRegexpError = rb_define_class("RegexpError", rb_eStandardError);
3337 onigenc_set_default_caseconv_table((UChar*)casetable);
3338 onigenc_set_default_encoding(ONIG_ENCODING_ASCII);
3339 onig_set_warn_func(re_warn);
3340 onig_set_verb_warn_func(re_warn);
3342 rb_define_virtual_variable("$~", match_getter, match_setter);
3343 rb_define_virtual_variable("$&", last_match_getter, 0);
3344 rb_define_virtual_variable("$`", prematch_getter, 0);
3345 rb_define_virtual_variable("$'", postmatch_getter, 0);
3346 rb_define_virtual_variable("$+", last_paren_match_getter, 0);
3348 rb_define_virtual_variable("$=", ignorecase_getter, ignorecase_setter);
3349 rb_define_virtual_variable("$KCODE", kcode_getter, kcode_setter);
3350 rb_define_virtual_variable("$-K", kcode_getter, kcode_setter);
3352 rb_cRegexp = rb_define_class("Regexp", rb_cObject);
3353 rb_define_alloc_func(rb_cRegexp, rb_reg_s_alloc);
3354 rb_define_singleton_method(rb_cRegexp, "compile", rb_class_new_instance, -1);
3355 rb_define_singleton_method(rb_cRegexp, "quote", rb_reg_s_quote, 1);
3356 rb_define_singleton_method(rb_cRegexp, "escape", rb_reg_s_quote, 1);
3357 rb_define_singleton_method(rb_cRegexp, "union", rb_reg_s_union_m, -2);
3358 rb_define_singleton_method(rb_cRegexp, "last_match", rb_reg_s_last_match, -1);
3359 rb_define_singleton_method(rb_cRegexp, "try_convert", rb_reg_s_try_convert, 1);
3361 rb_define_method(rb_cRegexp, "initialize", rb_reg_initialize_m, -1);
3362 rb_define_method(rb_cRegexp, "initialize_copy", rb_reg_init_copy, 1);
3363 rb_define_method(rb_cRegexp, "hash", rb_reg_hash, 0);
3364 rb_define_method(rb_cRegexp, "eql?", rb_reg_equal, 1);
3365 rb_define_method(rb_cRegexp, "==", rb_reg_equal, 1);
3366 rb_define_method(rb_cRegexp, "=~", rb_reg_match, 1);
3367 rb_define_method(rb_cRegexp, "===", rb_reg_eqq, 1);
3368 rb_define_method(rb_cRegexp, "~", rb_reg_match2, 0);
3369 rb_define_method(rb_cRegexp, "match", rb_reg_match_m, -1);
3370 rb_define_method(rb_cRegexp, "to_s", rb_reg_to_s, 0);
3371 rb_define_method(rb_cRegexp, "inspect", rb_reg_inspect, 0);
3372 rb_define_method(rb_cRegexp, "source", rb_reg_source, 0);
3373 rb_define_method(rb_cRegexp, "casefold?", rb_reg_casefold_p, 0);
3374 rb_define_method(rb_cRegexp, "options", rb_reg_options_m, 0);
3375 rb_define_method(rb_cRegexp, "encoding", rb_obj_encoding, 0); /* in encoding.c */
3376 rb_define_method(rb_cRegexp, "fixed_encoding?", rb_reg_fixed_encoding_p, 0);
3377 rb_define_method(rb_cRegexp, "names", rb_reg_names, 0);
3378 rb_define_method(rb_cRegexp, "named_captures", rb_reg_named_captures, 0);
3380 rb_define_const(rb_cRegexp, "IGNORECASE", INT2FIX(ONIG_OPTION_IGNORECASE));
3381 rb_define_const(rb_cRegexp, "EXTENDED", INT2FIX(ONIG_OPTION_EXTEND));
3382 rb_define_const(rb_cRegexp, "MULTILINE", INT2FIX(ONIG_OPTION_MULTILINE));
3384 rb_global_variable(&reg_cache);
3386 rb_cMatch = rb_define_class("MatchData", rb_cObject);
3387 rb_define_alloc_func(rb_cMatch, match_alloc);
3388 rb_undef_method(CLASS_OF(rb_cMatch), "new");
3390 rb_define_method(rb_cMatch, "initialize_copy", match_init_copy, 1);
3391 rb_define_method(rb_cMatch, "regexp", match_regexp, 0);
3392 rb_define_method(rb_cMatch, "names", match_names, 0);
3393 rb_define_method(rb_cMatch, "size", match_size, 0);
3394 rb_define_method(rb_cMatch, "length", match_size, 0);
3395 rb_define_method(rb_cMatch, "offset", match_offset, 1);
3396 rb_define_method(rb_cMatch, "begin", match_begin, 1);
3397 rb_define_method(rb_cMatch, "end", match_end, 1);
3398 rb_define_method(rb_cMatch, "to_a", match_to_a, 0);
3399 rb_define_method(rb_cMatch, "[]", match_aref, -1);
3400 rb_define_method(rb_cMatch, "captures", match_captures, 0);
3401 rb_define_method(rb_cMatch, "values_at", match_values_at, -1);
3402 rb_define_method(rb_cMatch, "pre_match", rb_reg_match_pre, 0);
3403 rb_define_method(rb_cMatch, "post_match", rb_reg_match_post, 0);
3404 rb_define_method(rb_cMatch, "to_s", match_to_s, 0);
3405 rb_define_method(rb_cMatch, "inspect", match_inspect, 0);
3406 rb_define_method(rb_cMatch, "string", match_string, 0);