Ruby 4.1.0dev (2026-04-23 revision 7d4941d3e1c5721a551eeb1d7410e3ead4a6b432)
re.c (7d4941d3e1c5721a551eeb1d7410e3ead4a6b432)
1/**********************************************************************
2
3 re.c -
4
5 $Author$
6 created at: Mon Aug 9 18:24:49 JST 1993
7
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
9
10**********************************************************************/
11
12#include "ruby/internal/config.h"
13
14#include <ctype.h>
15
16#include "encindex.h"
17#include "hrtime.h"
18#include "internal.h"
19#include "internal/bignum.h"
20#include "internal/encoding.h"
21#include "internal/error.h"
22#include "internal/hash.h"
23#include "internal/imemo.h"
24#include "internal/re.h"
25#include "internal/string.h"
26#include "internal/object.h"
27#include "internal/ractor.h"
28#include "internal/variable.h"
29#include "regint.h"
30#include "ruby/encoding.h"
31#include "ruby/re.h"
32#include "ruby/util.h"
33#include "ractor_core.h"
34
35VALUE rb_eRegexpError, rb_eRegexpTimeoutError;
36
37typedef char onig_errmsg_buffer[ONIG_MAX_ERROR_MESSAGE_LEN];
38#define errcpy(err, msg) strlcpy((err), (msg), ONIG_MAX_ERROR_MESSAGE_LEN)
39
40#define BEG(no) (regs->beg[(no)])
41#define END(no) (regs->end[(no)])
42
43#if 'a' == 97 /* it's ascii */
44static const char casetable[] = {
45 '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
46 '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
47 '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
48 '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
49 /* ' ' '!' '"' '#' '$' '%' '&' ''' */
50 '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
51 /* '(' ')' '*' '+' ',' '-' '.' '/' */
52 '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
53 /* '0' '1' '2' '3' '4' '5' '6' '7' */
54 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
55 /* '8' '9' ':' ';' '<' '=' '>' '?' */
56 '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
57 /* '@' 'A' 'B' 'C' 'D' 'E' 'F' 'G' */
58 '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
59 /* 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O' */
60 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
61 /* 'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' */
62 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
63 /* 'X' 'Y' 'Z' '[' '\' ']' '^' '_' */
64 '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
65 /* '`' 'a' 'b' 'c' 'd' 'e' 'f' 'g' */
66 '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
67 /* 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' */
68 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
69 /* 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' */
70 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
71 /* 'x' 'y' 'z' '{' '|' '}' '~' */
72 '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
73 '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
74 '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
75 '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
76 '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
77 '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
78 '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
79 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
80 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
81 '\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
82 '\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317',
83 '\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327',
84 '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
85 '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
86 '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
87 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
88 '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
89};
90#else
91# error >>> "You lose. You will need a translation table for your character set." <<<
92#endif
93
94// The process-global timeout for regexp matching
95rb_hrtime_t rb_reg_match_time_limit = 0;
96
97int
98rb_memcicmp(const void *x, const void *y, long len)
99{
100 const unsigned char *p1 = x, *p2 = y;
101 int tmp;
102
103 while (len--) {
104 if ((tmp = casetable[(unsigned)*p1++] - casetable[(unsigned)*p2++]))
105 return tmp;
106 }
107 return 0;
108}
109
110#if defined(HAVE_MEMMEM) && !defined(__APPLE__)
111static inline long
112rb_memsearch_ss(const unsigned char *xs, long m, const unsigned char *ys, long n)
113{
114 const unsigned char *y;
115
116 if ((y = memmem(ys, n, xs, m)) != NULL)
117 return y - ys;
118 else
119 return -1;
120}
121#else
122static inline long
123rb_memsearch_ss(const unsigned char *xs, long m, const unsigned char *ys, long n)
124{
125 const unsigned char *x = xs, *xe = xs + m;
126 const unsigned char *y = ys, *ye = ys + n;
127#define VALUE_MAX ((VALUE)~(VALUE)0)
128 VALUE hx, hy, mask = VALUE_MAX >> ((SIZEOF_VALUE - m) * CHAR_BIT);
129
130 if (m > SIZEOF_VALUE)
131 rb_bug("!!too long pattern string!!");
132
133 if (!(y = memchr(y, *x, n - m + 1)))
134 return -1;
135
136 /* Prepare hash value */
137 for (hx = *x++, hy = *y++; x < xe; ++x, ++y) {
138 hx <<= CHAR_BIT;
139 hy <<= CHAR_BIT;
140 hx |= *x;
141 hy |= *y;
142 }
143 /* Searching */
144 while (hx != hy) {
145 if (y == ye)
146 return -1;
147 hy <<= CHAR_BIT;
148 hy |= *y;
149 hy &= mask;
150 y++;
151 }
152 return y - ys - m;
153}
154#endif
155
156static inline long
157rb_memsearch_qs(const unsigned char *xs, long m, const unsigned char *ys, long n)
158{
159 const unsigned char *x = xs, *xe = xs + m;
160 const unsigned char *y = ys;
161 VALUE i, qstable[256];
162
163 /* Preprocessing */
164 for (i = 0; i < 256; ++i)
165 qstable[i] = m + 1;
166 for (; x < xe; ++x)
167 qstable[*x] = xe - x;
168 /* Searching */
169 for (; y + m <= ys + n; y += *(qstable + y[m])) {
170 if (*xs == *y && memcmp(xs, y, m) == 0)
171 return y - ys;
172 }
173 return -1;
174}
175
176static inline unsigned int
177rb_memsearch_qs_utf8_hash(const unsigned char *x)
178{
179 register const unsigned int mix = 8353;
180 register unsigned int h = *x;
181 if (h < 0xC0) {
182 return h + 256;
183 }
184 else if (h < 0xE0) {
185 h *= mix;
186 h += x[1];
187 }
188 else if (h < 0xF0) {
189 h *= mix;
190 h += x[1];
191 h *= mix;
192 h += x[2];
193 }
194 else if (h < 0xF5) {
195 h *= mix;
196 h += x[1];
197 h *= mix;
198 h += x[2];
199 h *= mix;
200 h += x[3];
201 }
202 else {
203 return h + 256;
204 }
205 return (unsigned char)h;
206}
207
208static inline long
209rb_memsearch_qs_utf8(const unsigned char *xs, long m, const unsigned char *ys, long n)
210{
211 const unsigned char *x = xs, *xe = xs + m;
212 const unsigned char *y = ys;
213 VALUE i, qstable[512];
214
215 /* Preprocessing */
216 for (i = 0; i < 512; ++i) {
217 qstable[i] = m + 1;
218 }
219 for (; x < xe; ++x) {
220 qstable[rb_memsearch_qs_utf8_hash(x)] = xe - x;
221 }
222 /* Searching */
223 for (; y + m <= ys + n; y += qstable[rb_memsearch_qs_utf8_hash(y+m)]) {
224 if (*xs == *y && memcmp(xs, y, m) == 0)
225 return y - ys;
226 }
227 return -1;
228}
229
230static inline long
231rb_memsearch_with_char_size(const unsigned char *xs, long m, const unsigned char *ys, long n, int char_size)
232{
233 const unsigned char *x = xs, x0 = *xs, *y = ys;
234
235 for (n -= m; n >= 0; n -= char_size, y += char_size) {
236 if (x0 == *y && memcmp(x+1, y+1, m-1) == 0)
237 return y - ys;
238 }
239 return -1;
240}
241
242static inline long
243rb_memsearch_wchar(const unsigned char *xs, long m, const unsigned char *ys, long n)
244{
245 return rb_memsearch_with_char_size(xs, m, ys, n, 2);
246}
247
248static inline long
249rb_memsearch_qchar(const unsigned char *xs, long m, const unsigned char *ys, long n)
250{
251 return rb_memsearch_with_char_size(xs, m, ys, n, 4);
252}
253
254long
255rb_memsearch(const void *x0, long m, const void *y0, long n, rb_encoding *enc)
256{
257 const unsigned char *x = x0, *y = y0;
258
259 if (m > n) return -1;
260 else if (m == n) {
261 return memcmp(x0, y0, m) == 0 ? 0 : -1;
262 }
263 else if (m < 1) {
264 return 0;
265 }
266 else if (m == 1) {
267 const unsigned char *ys = memchr(y, *x, n);
268
269 if (ys)
270 return ys - y;
271 else
272 return -1;
273 }
274 else if (LIKELY(rb_enc_mbminlen(enc) == 1)) {
275 if (m <= SIZEOF_VALUE) {
276 return rb_memsearch_ss(x0, m, y0, n);
277 }
278 else if (enc == rb_utf8_encoding()){
279 return rb_memsearch_qs_utf8(x0, m, y0, n);
280 }
281 }
282 else if (LIKELY(rb_enc_mbminlen(enc) == 2)) {
283 return rb_memsearch_wchar(x0, m, y0, n);
284 }
285 else if (LIKELY(rb_enc_mbminlen(enc) == 4)) {
286 return rb_memsearch_qchar(x0, m, y0, n);
287 }
288 return rb_memsearch_qs(x0, m, y0, n);
289}
290
291#define REG_ENCODING_NONE FL_USER6
292
293#define KCODE_FIXED FL_USER4
294
295static int
296char_to_option(int c)
297{
298 int val;
299
300 switch (c) {
301 case 'i':
302 val = ONIG_OPTION_IGNORECASE;
303 break;
304 case 'x':
305 val = ONIG_OPTION_EXTEND;
306 break;
307 case 'm':
308 val = ONIG_OPTION_MULTILINE;
309 break;
310 default:
311 val = 0;
312 break;
313 }
314 return val;
315}
316
317enum { OPTBUF_SIZE = 4 };
318
319static char *
320option_to_str(char str[OPTBUF_SIZE], int options)
321{
322 char *p = str;
323 if (options & ONIG_OPTION_MULTILINE) *p++ = 'm';
324 if (options & ONIG_OPTION_IGNORECASE) *p++ = 'i';
325 if (options & ONIG_OPTION_EXTEND) *p++ = 'x';
326 *p = 0;
327 return str;
328}
329
330extern int
331rb_char_to_option_kcode(int c, int *option, int *kcode)
332{
333 *option = 0;
334
335 switch (c) {
336 case 'n':
337 *kcode = rb_ascii8bit_encindex();
338 return (*option = ARG_ENCODING_NONE);
339 case 'e':
340 *kcode = ENCINDEX_EUC_JP;
341 break;
342 case 's':
343 *kcode = ENCINDEX_Windows_31J;
344 break;
345 case 'u':
346 *kcode = rb_utf8_encindex();
347 break;
348 default:
349 *kcode = -1;
350 return (*option = char_to_option(c));
351 }
352 *option = ARG_ENCODING_FIXED;
353 return 1;
354}
355
356static void
357rb_reg_check(VALUE re)
358{
359 if (!RREGEXP_PTR(re) || !RREGEXP_SRC(re) || !RREGEXP_SRC_PTR(re)) {
360 rb_raise(rb_eTypeError, "uninitialized Regexp");
361 }
362}
363
364static void
365rb_reg_expr_str(VALUE str, const char *s, long len,
366 rb_encoding *enc, rb_encoding *resenc, int term)
367{
368 const char *p, *pend;
369 int cr = ENC_CODERANGE_UNKNOWN;
370 int need_escape = 0;
371 int c, clen;
372
373 p = s; pend = p + len;
374 rb_str_coderange_scan_restartable(p, pend, enc, &cr);
375 if (rb_enc_asciicompat(enc) && ENC_CODERANGE_CLEAN_P(cr)) {
376 while (p < pend) {
377 c = rb_enc_ascget(p, pend, &clen, enc);
378 if (c == -1) {
379 if (enc == resenc) {
380 p += mbclen(p, pend, enc);
381 }
382 else {
383 need_escape = 1;
384 break;
385 }
386 }
387 else if (c != term && rb_enc_isprint(c, enc)) {
388 p += clen;
389 }
390 else {
391 need_escape = 1;
392 break;
393 }
394 }
395 }
396 else {
397 need_escape = 1;
398 }
399
400 if (!need_escape) {
401 rb_str_buf_cat(str, s, len);
402 }
403 else {
404 int unicode_p = rb_enc_unicode_p(enc);
405 p = s;
406 while (p<pend) {
407 c = rb_enc_ascget(p, pend, &clen, enc);
408 if (c == '\\' && p+clen < pend) {
409 int n = clen + mbclen(p+clen, pend, enc);
410 rb_str_buf_cat(str, p, n);
411 p += n;
412 continue;
413 }
414 else if (c == -1) {
415 clen = rb_enc_precise_mbclen(p, pend, enc);
416 if (!MBCLEN_CHARFOUND_P(clen)) {
417 c = (unsigned char)*p;
418 clen = 1;
419 goto hex;
420 }
421 if (resenc) {
422 unsigned int c = rb_enc_mbc_to_codepoint(p, pend, enc);
423 rb_str_buf_cat_escaped_char(str, c, unicode_p);
424 }
425 else {
426 clen = MBCLEN_CHARFOUND_LEN(clen);
427 rb_str_buf_cat(str, p, clen);
428 }
429 }
430 else if (c == term) {
431 char c = '\\';
432 rb_str_buf_cat(str, &c, 1);
433 rb_str_buf_cat(str, p, clen);
434 }
435 else if (rb_enc_isprint(c, enc)) {
436 rb_str_buf_cat(str, p, clen);
437 }
438 else if (!rb_enc_isspace(c, enc)) {
439 char b[8];
440
441 hex:
442 snprintf(b, sizeof(b), "\\x%02X", c);
443 rb_str_buf_cat(str, b, 4);
444 }
445 else {
446 rb_str_buf_cat(str, p, clen);
447 }
448 p += clen;
449 }
450 }
451}
452
453static VALUE
454rb_reg_desc(VALUE re)
455{
456 rb_encoding *enc = rb_enc_get(re);
457 VALUE str = rb_str_buf_new2("/");
458 rb_encoding *resenc = rb_default_internal_encoding();
459 if (resenc == NULL) resenc = rb_default_external_encoding();
460
461 if (re && rb_enc_asciicompat(enc)) {
462 rb_enc_copy(str, re);
463 }
464 else {
465 rb_enc_associate(str, rb_usascii_encoding());
466 }
467
468 VALUE src_str = RREGEXP_SRC(re);
469 rb_reg_expr_str(str, RSTRING_PTR(src_str), RSTRING_LEN(src_str), enc, resenc, '/');
470 RB_GC_GUARD(src_str);
471
472 rb_str_buf_cat2(str, "/");
473 if (re) {
474 char opts[OPTBUF_SIZE];
475 rb_reg_check(re);
476 if (*option_to_str(opts, RREGEXP_PTR(re)->options))
477 rb_str_buf_cat2(str, opts);
478 if (RBASIC(re)->flags & REG_ENCODING_NONE)
479 rb_str_buf_cat2(str, "n");
480 }
481 return str;
482}
483
484
485/*
486 * call-seq:
487 * source -> string
488 *
489 * Returns the original string of +self+:
490 *
491 * /ab+c/ix.source # => "ab+c"
492 *
493 * Regexp escape sequences are retained:
494 *
495 * /\x20\+/.source # => "\\x20\\+"
496 *
497 * Lexer escape characters are not retained:
498 *
499 * /\//.source # => "/"
500 *
501 */
502
503static VALUE
504rb_reg_source(VALUE re)
505{
506 VALUE str;
507
508 rb_reg_check(re);
509 str = rb_str_dup(RREGEXP_SRC(re));
510 return str;
511}
512
513/*
514 * call-seq:
515 * inspect -> string
516 *
517 * Returns a nicely-formatted string representation of +self+:
518 *
519 * /ab+c/ix.inspect # => "/ab+c/ix"
520 *
521 * Related: Regexp#to_s.
522 */
523
524static VALUE
525rb_reg_inspect(VALUE re)
526{
527 if (!RREGEXP_PTR(re) || !RREGEXP_SRC(re) || !RREGEXP_SRC_PTR(re)) {
528 return rb_any_to_s(re);
529 }
530 return rb_reg_desc(re);
531}
532
533static VALUE rb_reg_str_with_term(VALUE re, int term);
534
535/*
536 * call-seq:
537 * to_s -> string
538 *
539 * Returns a string showing the options and string of +self+:
540 *
541 * r0 = /ab+c/ix
542 * s0 = r0.to_s # => "(?ix-m:ab+c)"
543 *
544 * The returned string may be used as an argument to Regexp.new,
545 * or as interpolated text for a
546 * {Regexp interpolation}[rdoc-ref:Regexp@Interpolation+Mode]:
547 *
548 * r1 = Regexp.new(s0) # => /(?ix-m:ab+c)/
549 * r2 = /#{s0}/ # => /(?ix-m:ab+c)/
550 *
551 * Note that +r1+ and +r2+ are not equal to +r0+
552 * because their original strings are different:
553 *
554 * r0 == r1 # => false
555 * r0.source # => "ab+c"
556 * r1.source # => "(?ix-m:ab+c)"
557 *
558 * Related: Regexp#inspect.
559 *
560 */
561
562static VALUE
563rb_reg_to_s(VALUE re)
564{
565 return rb_reg_str_with_term(re, '/');
566}
567
568static VALUE
569rb_reg_str_with_term(VALUE re, int term)
570{
571 int options, opt;
572 const int embeddable = ONIG_OPTION_MULTILINE|ONIG_OPTION_IGNORECASE|ONIG_OPTION_EXTEND;
573 VALUE str = rb_str_buf_new2("(?");
574 char optbuf[OPTBUF_SIZE + 1]; /* for '-' */
575 rb_encoding *enc = rb_enc_get(re);
576
577 rb_reg_check(re);
578
579 rb_enc_copy(str, re);
580 options = RREGEXP_PTR(re)->options;
581 VALUE src_str = RREGEXP_SRC(re);
582 const UChar *ptr = (UChar *)RSTRING_PTR(src_str);
583 long len = RSTRING_LEN(src_str);
584 again:
585 if (len >= 4 && ptr[0] == '(' && ptr[1] == '?') {
586 int err = 1;
587 ptr += 2;
588 if ((len -= 2) > 0) {
589 do {
590 opt = char_to_option((int )*ptr);
591 if (opt != 0) {
592 options |= opt;
593 }
594 else {
595 break;
596 }
597 ++ptr;
598 } while (--len > 0);
599 }
600 if (len > 1 && *ptr == '-') {
601 ++ptr;
602 --len;
603 do {
604 opt = char_to_option((int )*ptr);
605 if (opt != 0) {
606 options &= ~opt;
607 }
608 else {
609 break;
610 }
611 ++ptr;
612 } while (--len > 0);
613 }
614 if (*ptr == ')') {
615 --len;
616 ++ptr;
617 goto again;
618 }
619 if (*ptr == ':' && ptr[len-1] == ')') {
620 Regexp *rp;
621 VALUE verbose = ruby_verbose;
623
624 ++ptr;
625 len -= 2;
626 err = onig_new(&rp, ptr, ptr + len, options,
627 enc, OnigDefaultSyntax, NULL);
628 onig_free(rp);
629 ruby_verbose = verbose;
630 }
631 if (err) {
632 options = RREGEXP_PTR(re)->options;
633 ptr = (UChar*)RREGEXP_SRC_PTR(re);
634 len = RREGEXP_SRC_LEN(re);
635 }
636 }
637
638 if (*option_to_str(optbuf, options)) rb_str_buf_cat2(str, optbuf);
639
640 if ((options & embeddable) != embeddable) {
641 optbuf[0] = '-';
642 option_to_str(optbuf + 1, ~options);
643 rb_str_buf_cat2(str, optbuf);
644 }
645
646 rb_str_buf_cat2(str, ":");
647 if (rb_enc_asciicompat(enc)) {
648 rb_reg_expr_str(str, (char*)ptr, len, enc, NULL, term);
649 rb_str_buf_cat2(str, ")");
650 }
651 else {
652 const char *s, *e;
653 char *paren;
654 ptrdiff_t n;
655 rb_str_buf_cat2(str, ")");
656 rb_enc_associate(str, rb_usascii_encoding());
657 str = rb_str_encode(str, rb_enc_from_encoding(enc), 0, Qnil);
658
659 /* backup encoded ")" to paren */
660 s = RSTRING_PTR(str);
661 e = RSTRING_END(str);
662 s = rb_enc_left_char_head(s, e-1, e, enc);
663 n = e - s;
664 paren = ALLOCA_N(char, n);
665 memcpy(paren, s, n);
666 rb_str_resize(str, RSTRING_LEN(str) - n);
667
668 rb_reg_expr_str(str, (char*)ptr, len, enc, NULL, term);
669 rb_str_buf_cat(str, paren, n);
670 }
671 rb_enc_copy(str, re);
672
673 RB_GC_GUARD(src_str);
674
675 return str;
676}
677
678NORETURN(static void rb_reg_raise(const char *err, VALUE re));
679
680static void
681rb_reg_raise(const char *err, VALUE re)
682{
683 VALUE desc = rb_reg_desc(re);
684
685 rb_raise(rb_eRegexpError, "%s: %"PRIsVALUE, err, desc);
686}
687
688static VALUE
689rb_enc_reg_error_desc(const char *s, long len, rb_encoding *enc, int options, const char *err)
690{
691 char opts[OPTBUF_SIZE + 1]; /* for '/' */
692 VALUE desc = rb_str_buf_new2(err);
693 rb_encoding *resenc = rb_default_internal_encoding();
694 if (resenc == NULL) resenc = rb_default_external_encoding();
695
696 rb_enc_associate(desc, enc);
697 rb_str_buf_cat2(desc, ": /");
698 rb_reg_expr_str(desc, s, len, enc, resenc, '/');
699 opts[0] = '/';
700 option_to_str(opts + 1, options);
701 rb_str_buf_cat2(desc, opts);
702 return rb_exc_new3(rb_eRegexpError, desc);
703}
704
705NORETURN(static void rb_enc_reg_raise(const char *s, long len, rb_encoding *enc, int options, const char *err));
706
707static void
708rb_enc_reg_raise(const char *s, long len, rb_encoding *enc, int options, const char *err)
709{
710 rb_exc_raise(rb_enc_reg_error_desc(s, len, enc, options, err));
711}
712
713static VALUE
714rb_reg_error_desc(VALUE str, int options, const char *err)
715{
716 return rb_enc_reg_error_desc(RSTRING_PTR(str), RSTRING_LEN(str),
717 rb_enc_get(str), options, err);
718}
719
720NORETURN(static void rb_reg_raise_str(VALUE str, int options, const char *err));
721
722static void
723rb_reg_raise_str(VALUE str, int options, const char *err)
724{
725 rb_exc_raise(rb_reg_error_desc(str, options, err));
726}
727
728
729/*
730 * call-seq:
731 * casefold?-> true or false
732 *
733 * Returns +true+ if the case-insensitivity flag in +self+ is set,
734 * +false+ otherwise:
735 *
736 * /a/.casefold? # => false
737 * /a/i.casefold? # => true
738 * /(?i:a)/.casefold? # => false
739 *
740 */
741
742static VALUE
743rb_reg_casefold_p(VALUE re)
744{
745 rb_reg_check(re);
746 return RBOOL(RREGEXP_PTR(re)->options & ONIG_OPTION_IGNORECASE);
747}
748
749
750/*
751 * call-seq:
752 * options -> integer
753 *
754 * Returns an integer whose bits show the options set in +self+.
755 *
756 * The option bits are:
757 *
758 * Regexp::IGNORECASE # => 1
759 * Regexp::EXTENDED # => 2
760 * Regexp::MULTILINE # => 4
761 *
762 * Examples:
763 *
764 * /foo/.options # => 0
765 * /foo/i.options # => 1
766 * /foo/x.options # => 2
767 * /foo/m.options # => 4
768 * /foo/mix.options # => 7
769 *
770 * Note that additional bits may be set in the returned integer;
771 * these are maintained internally in +self+, are ignored if passed
772 * to Regexp.new, and may be ignored by the caller:
773 *
774 * Returns the set of bits corresponding to the options used when
775 * creating this regexp (see Regexp::new for details). Note that
776 * additional bits may be set in the returned options: these are used
777 * internally by the regular expression code. These extra bits are
778 * ignored if the options are passed to Regexp::new:
779 *
780 * r = /\xa1\xa2/e # => /\xa1\xa2/
781 * r.source # => "\\xa1\\xa2"
782 * r.options # => 16
783 * Regexp.new(r.source, r.options) # => /\xa1\xa2/
784 *
785 */
786
787static VALUE
788rb_reg_options_m(VALUE re)
789{
790 int options = rb_reg_options(re);
791 return INT2NUM(options);
792}
793
794static int
795reg_names_iter(const OnigUChar *name, const OnigUChar *name_end,
796 int back_num, int *back_refs, OnigRegex regex, void *arg)
797{
798 VALUE ary = (VALUE)arg;
799 rb_ary_push(ary, rb_enc_str_new((const char *)name, name_end-name, regex->enc));
800 return 0;
801}
802
803/*
804 * call-seq:
805 * names -> array_of_names
806 *
807 * Returns an array of names of captures
808 * (see {Named Captures}[rdoc-ref:Regexp@Named+Captures]):
809 *
810 * /(?<foo>.)(?<bar>.)(?<baz>.)/.names # => ["foo", "bar", "baz"]
811 * /(?<foo>.)(?<foo>.)/.names # => ["foo"]
812 * /(.)(.)/.names # => []
813 *
814 */
815
816static VALUE
817rb_reg_names(VALUE re)
818{
819 VALUE ary;
820 rb_reg_check(re);
821 ary = rb_ary_new_capa(onig_number_of_names(RREGEXP_PTR(re)));
822 onig_foreach_name(RREGEXP_PTR(re), reg_names_iter, (void*)ary);
823 return ary;
824}
825
826static int
827reg_named_captures_iter(const OnigUChar *name, const OnigUChar *name_end,
828 int back_num, int *back_refs, OnigRegex regex, void *arg)
829{
830 VALUE hash = (VALUE)arg;
831 VALUE ary = rb_ary_new2(back_num);
832 int i;
833
834 for (i = 0; i < back_num; i++)
835 rb_ary_store(ary, i, INT2NUM(back_refs[i]));
836
837 rb_hash_aset(hash, rb_str_new((const char*)name, name_end-name),ary);
838
839 return 0;
840}
841
842/*
843 * call-seq:
844 * named_captures -> hash
845 *
846 * Returns a hash representing named captures of +self+
847 * (see {Named Captures}[rdoc-ref:Regexp@Named+Captures]):
848 *
849 * - Each key is the name of a named capture.
850 * - Each value is an array of integer indexes for that named capture.
851 *
852 * Examples:
853 *
854 * /(?<foo>.)(?<bar>.)/.named_captures # => {"foo"=>[1], "bar"=>[2]}
855 * /(?<foo>.)(?<foo>.)/.named_captures # => {"foo"=>[1, 2]}
856 * /(.)(.)/.named_captures # => {}
857 *
858 */
859
860static VALUE
861rb_reg_named_captures(VALUE re)
862{
863 regex_t *reg = (rb_reg_check(re), RREGEXP_PTR(re));
864 VALUE hash = rb_hash_new_with_size(onig_number_of_names(reg));
865 onig_foreach_name(reg, reg_named_captures_iter, (void*)hash);
866 return hash;
867}
868
869static int
870onig_new_with_source(regex_t** reg, const UChar* pattern, const UChar* pattern_end,
871 OnigOptionType option, OnigEncoding enc, const OnigSyntaxType* syntax,
872 OnigErrorInfo* einfo, const char *sourcefile, int sourceline)
873{
874 int r;
875
876 *reg = (regex_t* )malloc(sizeof(regex_t));
877 if (IS_NULL(*reg)) return ONIGERR_MEMORY;
878
879 r = onig_reg_init(*reg, option, ONIGENC_CASE_FOLD_DEFAULT, enc, syntax);
880 if (r) goto err;
881
882 r = onig_compile_ruby(*reg, pattern, pattern_end, einfo, sourcefile, sourceline);
883 if (r) {
884 err:
885 onig_free(*reg);
886 *reg = NULL;
887 }
888 return r;
889}
890
891static Regexp*
892make_regexp(const char *s, long len, rb_encoding *enc, int flags, onig_errmsg_buffer err,
893 const char *sourcefile, int sourceline)
894{
895 Regexp *rp;
896 int r;
897 OnigErrorInfo einfo;
898
899 /* Handle escaped characters first. */
900
901 /* Build a copy of the string (in dest) with the
902 escaped characters translated, and generate the regex
903 from that.
904 */
905
906 r = onig_new_with_source(&rp, (UChar*)s, (UChar*)(s + len), flags,
907 enc, OnigDefaultSyntax, &einfo, sourcefile, sourceline);
908 if (r) {
909 onig_error_code_to_str((UChar*)err, r, &einfo);
910 return 0;
911 }
912 return rp;
913}
914
915
916/*
917 * Document-class: MatchData
918 *
919 * MatchData encapsulates the result of matching a Regexp against
920 * string. It is returned by Regexp#match and String#match, and also
921 * stored in a global variable returned by Regexp.last_match.
922 *
923 * Usage:
924 *
925 * url = 'https://docs.ruby-lang.org/en/2.5.0/MatchData.html'
926 * m = url.match(/(\d\.?)+/) # => #<MatchData "2.5.0" 1:"0">
927 * m.string # => "https://docs.ruby-lang.org/en/2.5.0/MatchData.html"
928 * m.regexp # => /(\d\.?)+/
929 * # entire matched substring:
930 * m[0] # => "2.5.0"
931 *
932 * # Working with unnamed captures
933 * m = url.match(%r{([^/]+)/([^/]+)\.html$})
934 * m.captures # => ["2.5.0", "MatchData"]
935 * m[1] # => "2.5.0"
936 * m.values_at(1, 2) # => ["2.5.0", "MatchData"]
937 *
938 * # Working with named captures
939 * m = url.match(%r{(?<version>[^/]+)/(?<module>[^/]+)\.html$})
940 * m.captures # => ["2.5.0", "MatchData"]
941 * m.named_captures # => {"version"=>"2.5.0", "module"=>"MatchData"}
942 * m[:version] # => "2.5.0"
943 * m.values_at(:version, :module)
944 * # => ["2.5.0", "MatchData"]
945 * # Numerical indexes are working, too
946 * m[1] # => "2.5.0"
947 * m.values_at(1, 2) # => ["2.5.0", "MatchData"]
948 *
949 * == Global variables equivalence
950 *
951 * Parts of last MatchData (returned by Regexp.last_match) are also
952 * aliased as global variables:
953 *
954 * * <code>$~</code> is Regexp.last_match;
955 * * <code>$&</code> is Regexp.last_match<code>[ 0 ]</code>;
956 * * <code>$1</code>, <code>$2</code>, and so on are
957 * Regexp.last_match<code>[ i ]</code> (captures by number);
958 * * <code>$`</code> is Regexp.last_match<code>.pre_match</code>;
959 * * <code>$'</code> is Regexp.last_match<code>.post_match</code>;
960 * * <code>$+</code> is Regexp.last_match<code>[ -1 ]</code> (the last capture).
961 *
962 * See also Regexp@Global+Variables.
963 */
964
966
967static VALUE
968match_alloc(VALUE klass)
969{
970 size_t alloc_size = sizeof(struct RMatch) + sizeof(rb_matchext_t);
972 NEWOBJ_OF(match, struct RMatch, klass, flags, alloc_size, 0);
973
974 match->str = Qfalse;
975 match->regexp = Qfalse;
976 memset(RMATCH_EXT(match), 0, sizeof(rb_matchext_t));
977
978 return (VALUE)match;
979}
980
981int
982rb_reg_region_copy(struct re_registers *to, const struct re_registers *from)
983{
984 onig_region_copy(to, (OnigRegion *)from);
985 if (to->allocated) return 0;
986 rb_gc();
987 onig_region_copy(to, (OnigRegion *)from);
988 if (to->allocated) return 0;
989 return ONIGERR_MEMORY;
990}
991
992typedef struct {
993 long byte_pos;
994 long char_pos;
995} pair_t;
996
997static int
998pair_byte_cmp(const void *pair1, const void *pair2)
999{
1000 long diff = ((pair_t*)pair1)->byte_pos - ((pair_t*)pair2)->byte_pos;
1001#if SIZEOF_LONG > SIZEOF_INT
1002 return diff ? diff > 0 ? 1 : -1 : 0;
1003#else
1004 return (int)diff;
1005#endif
1006}
1007
1008static void
1009update_char_offset(VALUE match)
1010{
1011 rb_matchext_t *rm = RMATCH_EXT(match);
1012 struct re_registers *regs;
1013 int i, num_regs, num_pos;
1014 long c;
1015 char *s, *p, *q;
1016 rb_encoding *enc;
1017 pair_t *pairs;
1018 VALUE pairs_obj = Qnil;
1019
1021 return;
1022
1023 regs = &rm->regs;
1024 num_regs = rm->regs.num_regs;
1025
1026 if (rm->char_offset_num_allocated < num_regs) {
1027 SIZED_REALLOC_N(rm->char_offset, struct rmatch_offset, num_regs, rm->char_offset_num_allocated);
1028 rm->char_offset_num_allocated = num_regs;
1029 }
1030
1031 enc = rb_enc_get(RMATCH(match)->str);
1032 if (rb_enc_mbmaxlen(enc) == 1) {
1033 for (i = 0; i < num_regs; i++) {
1034 rm->char_offset[i].beg = BEG(i);
1035 rm->char_offset[i].end = END(i);
1036 }
1037 return;
1038 }
1039
1040 pairs = RB_ALLOCV_N(pair_t, pairs_obj, num_regs * 2);
1041 num_pos = 0;
1042 for (i = 0; i < num_regs; i++) {
1043 if (BEG(i) < 0)
1044 continue;
1045 pairs[num_pos++].byte_pos = BEG(i);
1046 pairs[num_pos++].byte_pos = END(i);
1047 }
1048 qsort(pairs, num_pos, sizeof(pair_t), pair_byte_cmp);
1049
1050 s = p = RSTRING_PTR(RMATCH(match)->str);
1051 c = 0;
1052 for (i = 0; i < num_pos; i++) {
1053 q = s + pairs[i].byte_pos;
1054 c += rb_enc_strlen(p, q, enc);
1055 pairs[i].char_pos = c;
1056 p = q;
1057 }
1058
1059 for (i = 0; i < num_regs; i++) {
1060 pair_t key, *found;
1061 if (BEG(i) < 0) {
1062 rm->char_offset[i].beg = -1;
1063 rm->char_offset[i].end = -1;
1064 continue;
1065 }
1066
1067 key.byte_pos = BEG(i);
1068 found = bsearch(&key, pairs, num_pos, sizeof(pair_t), pair_byte_cmp);
1069 rm->char_offset[i].beg = found->char_pos;
1070
1071 key.byte_pos = END(i);
1072 found = bsearch(&key, pairs, num_pos, sizeof(pair_t), pair_byte_cmp);
1073 rm->char_offset[i].end = found->char_pos;
1074 }
1075
1076 RB_ALLOCV_END(pairs_obj);
1077}
1078
1079static VALUE
1080match_check(VALUE match)
1081{
1082 if (!RMATCH(match)->regexp) {
1083 rb_raise(rb_eTypeError, "uninitialized MatchData");
1084 }
1085 return match;
1086}
1087
1088/* :nodoc: */
1089static VALUE
1090match_init_copy(VALUE obj, VALUE orig)
1091{
1092 rb_matchext_t *rm;
1093
1094 if (!OBJ_INIT_COPY(obj, orig)) return obj;
1095
1096 RB_OBJ_WRITE(obj, &RMATCH(obj)->str, RMATCH(orig)->str);
1097 RB_OBJ_WRITE(obj, &RMATCH(obj)->regexp, RMATCH(orig)->regexp);
1098
1099 rm = RMATCH_EXT(obj);
1100 if (rb_reg_region_copy(&rm->regs, RMATCH_REGS(orig)))
1101 rb_memerror();
1102
1103 if (RMATCH_EXT(orig)->char_offset_num_allocated) {
1104 if (rm->char_offset_num_allocated < rm->regs.num_regs) {
1105 SIZED_REALLOC_N(rm->char_offset, struct rmatch_offset, rm->regs.num_regs, rm->char_offset_num_allocated);
1106 rm->char_offset_num_allocated = rm->regs.num_regs;
1107 }
1108 MEMCPY(rm->char_offset, RMATCH_EXT(orig)->char_offset,
1109 struct rmatch_offset, rm->regs.num_regs);
1110 RB_GC_GUARD(orig);
1111 }
1112
1113 return obj;
1114}
1115
1116
1117/*
1118 * call-seq:
1119 * regexp -> regexp
1120 *
1121 * Returns the regexp that produced the match:
1122 *
1123 * m = /a.*b/.match("abc") # => #<MatchData "ab">
1124 * m.regexp # => /a.*b/
1125 *
1126 */
1127
1128static VALUE
1129match_regexp(VALUE match)
1130{
1131 VALUE regexp;
1132 match_check(match);
1133 regexp = RMATCH(match)->regexp;
1134 if (NIL_P(regexp)) {
1135 VALUE str = rb_reg_nth_match(0, match);
1136 regexp = rb_reg_regcomp(rb_reg_quote(str));
1137 RB_OBJ_WRITE(match, &RMATCH(match)->regexp, regexp);
1138 }
1139 return regexp;
1140}
1141
1142/*
1143 * call-seq:
1144 * names -> array_of_names
1145 *
1146 * Returns an array of the capture names
1147 * (see {Named Captures}[rdoc-ref:Regexp@Named+Captures]):
1148 *
1149 * m = /(?<foo>.)(?<bar>.)(?<baz>.)/.match("hoge")
1150 * # => #<MatchData "hog" foo:"h" bar:"o" baz:"g">
1151 * m.names # => ["foo", "bar", "baz"]
1152 *
1153 * m = /foo/.match('foo') # => #<MatchData "foo">
1154 * m.names # => [] # No named captures.
1155 *
1156 * Equivalent to:
1157 *
1158 * m = /(?<foo>.)(?<bar>.)(?<baz>.)/.match("hoge")
1159 * m.regexp.names # => ["foo", "bar", "baz"]
1160 *
1161 */
1162
1163static VALUE
1164match_names(VALUE match)
1165{
1166 match_check(match);
1167 if (NIL_P(RMATCH(match)->regexp))
1168 return rb_ary_new_capa(0);
1169 return rb_reg_names(RMATCH(match)->regexp);
1170}
1171
1172/*
1173 * call-seq:
1174 * size -> integer
1175 *
1176 * Returns size of the match array:
1177 *
1178 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1179 * # => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8">
1180 * m.size # => 5
1181 *
1182 */
1183
1184static VALUE
1185match_size(VALUE match)
1186{
1187 match_check(match);
1188 return INT2FIX(RMATCH_REGS(match)->num_regs);
1189}
1190
1191static int name_to_backref_number(const struct re_registers *, VALUE, const char*, const char*);
1192NORETURN(static void name_to_backref_error(VALUE name));
1193
1194static void
1195name_to_backref_error(VALUE name)
1196{
1197 rb_raise(rb_eIndexError, "undefined group name reference: % "PRIsVALUE,
1198 name);
1199}
1200
1201static void
1202backref_number_check(struct re_registers *regs, int i)
1203{
1204 if (i < 0 || regs->num_regs <= i)
1205 rb_raise(rb_eIndexError, "index %d out of matches", i);
1206}
1207
1208static int
1209match_backref_number(VALUE match, VALUE backref)
1210{
1211 const char *name;
1212 int num;
1213
1214 struct re_registers *regs = RMATCH_REGS(match);
1215 VALUE regexp = RMATCH(match)->regexp;
1216
1217 match_check(match);
1218 if (SYMBOL_P(backref)) {
1219 backref = rb_sym2str(backref);
1220 }
1221 else if (!RB_TYPE_P(backref, T_STRING)) {
1222 return NUM2INT(backref);
1223 }
1224 name = StringValueCStr(backref);
1225
1226 num = name_to_backref_number(regs, regexp, name, name + RSTRING_LEN(backref));
1227
1228 if (num < 1) {
1229 name_to_backref_error(backref);
1230 }
1231
1232 return num;
1233}
1234
1235int
1237{
1238 return match_backref_number(match, backref);
1239}
1240
1241/*
1242 * call-seq:
1243 * offset(n) -> [start_offset, end_offset]
1244 * offset(name) -> [start_offset, end_offset]
1245 *
1246 * :include: doc/matchdata/offset.rdoc
1247 *
1248 */
1249
1250static VALUE
1251match_offset(VALUE match, VALUE n)
1252{
1253 int i = match_backref_number(match, n);
1254 struct re_registers *regs = RMATCH_REGS(match);
1255
1256 match_check(match);
1257 backref_number_check(regs, i);
1258
1259 if (BEG(i) < 0)
1260 return rb_assoc_new(Qnil, Qnil);
1261
1262 update_char_offset(match);
1263 return rb_assoc_new(LONG2NUM(RMATCH_EXT(match)->char_offset[i].beg),
1264 LONG2NUM(RMATCH_EXT(match)->char_offset[i].end));
1265}
1266
1267/*
1268 * call-seq:
1269 * mtch.byteoffset(n) -> array
1270 *
1271 * Returns a two-element array containing the beginning and ending byte-based offsets of
1272 * the <em>n</em>th match.
1273 * <em>n</em> can be a string or symbol to reference a named capture.
1274 *
1275 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1276 * m.byteoffset(0) #=> [1, 7]
1277 * m.byteoffset(4) #=> [6, 7]
1278 *
1279 * m = /(?<foo>.)(.)(?<bar>.)/.match("hoge")
1280 * p m.byteoffset(:foo) #=> [0, 1]
1281 * p m.byteoffset(:bar) #=> [2, 3]
1282 *
1283 */
1284
1285static VALUE
1286match_byteoffset(VALUE match, VALUE n)
1287{
1288 int i = match_backref_number(match, n);
1289 struct re_registers *regs = RMATCH_REGS(match);
1290
1291 match_check(match);
1292 backref_number_check(regs, i);
1293
1294 if (BEG(i) < 0)
1295 return rb_assoc_new(Qnil, Qnil);
1296 return rb_assoc_new(LONG2NUM(BEG(i)), LONG2NUM(END(i)));
1297}
1298
1299
1300/*
1301 * call-seq:
1302 * bytebegin(n) -> integer
1303 * bytebegin(name) -> integer
1304 *
1305 * :include: doc/matchdata/bytebegin.rdoc
1306 *
1307 */
1308
1309static VALUE
1310match_bytebegin(VALUE match, VALUE n)
1311{
1312 int i = match_backref_number(match, n);
1313 struct re_registers *regs = RMATCH_REGS(match);
1314
1315 match_check(match);
1316 backref_number_check(regs, i);
1317
1318 if (BEG(i) < 0)
1319 return Qnil;
1320 return LONG2NUM(BEG(i));
1321}
1322
1323
1324/*
1325 * call-seq:
1326 * byteend(n) -> integer
1327 * byteend(name) -> integer
1328 *
1329 * :include: doc/matchdata/byteend.rdoc
1330 *
1331 */
1332
1333static VALUE
1334match_byteend(VALUE match, VALUE n)
1335{
1336 int i = match_backref_number(match, n);
1337 struct re_registers *regs = RMATCH_REGS(match);
1338
1339 match_check(match);
1340 backref_number_check(regs, i);
1341
1342 if (BEG(i) < 0)
1343 return Qnil;
1344 return LONG2NUM(END(i));
1345}
1346
1347
1348/*
1349 * call-seq:
1350 * begin(n) -> integer
1351 * begin(name) -> integer
1352 *
1353 * :include: doc/matchdata/begin.rdoc
1354 *
1355 */
1356
1357static VALUE
1358match_begin(VALUE match, VALUE n)
1359{
1360 int i = match_backref_number(match, n);
1361 struct re_registers *regs = RMATCH_REGS(match);
1362
1363 match_check(match);
1364 backref_number_check(regs, i);
1365
1366 if (BEG(i) < 0)
1367 return Qnil;
1368
1369 update_char_offset(match);
1370 return LONG2NUM(RMATCH_EXT(match)->char_offset[i].beg);
1371}
1372
1373
1374/*
1375 * call-seq:
1376 * end(n) -> integer
1377 * end(name) -> integer
1378 *
1379 * :include: doc/matchdata/end.rdoc
1380 *
1381 */
1382
1383static VALUE
1384match_end(VALUE match, VALUE n)
1385{
1386 int i = match_backref_number(match, n);
1387 struct re_registers *regs = RMATCH_REGS(match);
1388
1389 match_check(match);
1390 backref_number_check(regs, i);
1391
1392 if (BEG(i) < 0)
1393 return Qnil;
1394
1395 update_char_offset(match);
1396 return LONG2NUM(RMATCH_EXT(match)->char_offset[i].end);
1397}
1398
1399/*
1400 * call-seq:
1401 * match(n) -> string or nil
1402 * match(name) -> string or nil
1403 *
1404 * Returns the matched substring corresponding to the given argument.
1405 *
1406 * When non-negative argument +n+ is given,
1407 * returns the matched substring for the <tt>n</tt>th match:
1408 *
1409 * m = /(.)(.)(\d+)(\d)(\w)?/.match("THX1138.")
1410 * # => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8" 5:nil>
1411 * m.match(0) # => "HX1138"
1412 * m.match(4) # => "8"
1413 * m.match(5) # => nil
1414 *
1415 * When string or symbol argument +name+ is given,
1416 * returns the matched substring for the given name:
1417 *
1418 * m = /(?<foo>.)(.)(?<bar>.+)/.match("hoge")
1419 * # => #<MatchData "hoge" foo:"h" bar:"ge">
1420 * m.match('foo') # => "h"
1421 * m.match(:bar) # => "ge"
1422 *
1423 */
1424
1425static VALUE
1426match_nth(VALUE match, VALUE n)
1427{
1428 int i = match_backref_number(match, n);
1429 struct re_registers *regs = RMATCH_REGS(match);
1430
1431 backref_number_check(regs, i);
1432
1433 long start = BEG(i), end = END(i);
1434 if (start < 0)
1435 return Qnil;
1436
1437 return rb_str_subseq(RMATCH(match)->str, start, end - start);
1438}
1439
1440/*
1441 * call-seq:
1442 * match_length(n) -> integer or nil
1443 * match_length(name) -> integer or nil
1444 *
1445 * Returns the length (in characters) of the matched substring
1446 * corresponding to the given argument.
1447 *
1448 * When non-negative argument +n+ is given,
1449 * returns the length of the matched substring
1450 * for the <tt>n</tt>th match:
1451 *
1452 * m = /(.)(.)(\d+)(\d)(\w)?/.match("THX1138.")
1453 * # => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8" 5:nil>
1454 * m.match_length(0) # => 6
1455 * m.match_length(4) # => 1
1456 * m.match_length(5) # => nil
1457 *
1458 * When string or symbol argument +name+ is given,
1459 * returns the length of the matched substring
1460 * for the named match:
1461 *
1462 * m = /(?<foo>.)(.)(?<bar>.+)/.match("hoge")
1463 * # => #<MatchData "hoge" foo:"h" bar:"ge">
1464 * m.match_length('foo') # => 1
1465 * m.match_length(:bar) # => 2
1466 *
1467 */
1468
1469static VALUE
1470match_nth_length(VALUE match, VALUE n)
1471{
1472 int i = match_backref_number(match, n);
1473 struct re_registers *regs = RMATCH_REGS(match);
1474
1475 match_check(match);
1476 backref_number_check(regs, i);
1477
1478 if (BEG(i) < 0)
1479 return Qnil;
1480
1481 update_char_offset(match);
1482 const struct rmatch_offset *const ofs =
1483 &RMATCH_EXT(match)->char_offset[i];
1484 return LONG2NUM(ofs->end - ofs->beg);
1485}
1486
1487#define MATCH_BUSY FL_USER2
1488
1489void
1491{
1492 FL_SET(match, MATCH_BUSY);
1493}
1494
1495void
1496rb_match_unbusy(VALUE match)
1497{
1498 FL_UNSET(match, MATCH_BUSY);
1499}
1500
1501int
1502rb_match_count(VALUE match)
1503{
1504 struct re_registers *regs;
1505 if (NIL_P(match)) return -1;
1506 regs = RMATCH_REGS(match);
1507 if (!regs) return -1;
1508 return regs->num_regs;
1509}
1510
1511static void
1512match_set_string(VALUE m, VALUE string, long pos, long len)
1513{
1514 struct RMatch *match = (struct RMatch *)m;
1515 rb_matchext_t *rmatch = RMATCH_EXT(match);
1516
1517 RB_OBJ_WRITE(match, &RMATCH(match)->str, string);
1518 RB_OBJ_WRITE(match, &RMATCH(match)->regexp, Qnil);
1519 int err = onig_region_resize(&rmatch->regs, 1);
1520 if (err) rb_memerror();
1521 rmatch->regs.beg[0] = pos;
1522 rmatch->regs.end[0] = pos + len;
1523}
1524
1525VALUE
1526rb_backref_set_string(VALUE string, long pos, long len)
1527{
1528 VALUE match = rb_backref_get();
1529 if (NIL_P(match) || FL_TEST(match, MATCH_BUSY)) {
1530 match = match_alloc(rb_cMatch);
1531 }
1532 match_set_string(match, string, pos, len);
1533 rb_backref_set(match);
1534 return match;
1535}
1536
1537/*
1538 * call-seq:
1539 * fixed_encoding? -> true or false
1540 *
1541 * Returns +false+ if +self+ is applicable to
1542 * a string with any ASCII-compatible encoding;
1543 * otherwise returns +true+:
1544 *
1545 * r = /a/ # => /a/
1546 * r.fixed_encoding? # => false
1547 * r.match?("\u{6666} a") # => true
1548 * r.match?("\xa1\xa2 a".force_encoding("euc-jp")) # => true
1549 * r.match?("abc".force_encoding("euc-jp")) # => true
1550 *
1551 * r = /a/u # => /a/
1552 * r.fixed_encoding? # => true
1553 * r.match?("\u{6666} a") # => true
1554 * r.match?("\xa1\xa2".force_encoding("euc-jp")) # Raises exception.
1555 * r.match?("abc".force_encoding("euc-jp")) # => true
1556 *
1557 * r = /\u{6666}/ # => /\u{6666}/
1558 * r.fixed_encoding? # => true
1559 * r.encoding # => #<Encoding:UTF-8>
1560 * r.match?("\u{6666} a") # => true
1561 * r.match?("\xa1\xa2".force_encoding("euc-jp")) # Raises exception.
1562 * r.match?("abc".force_encoding("euc-jp")) # => false
1563 *
1564 */
1565
1566static VALUE
1567rb_reg_fixed_encoding_p(VALUE re)
1568{
1569 return RBOOL(FL_TEST(re, KCODE_FIXED));
1570}
1571
1572static VALUE
1573rb_reg_preprocess(const char *p, const char *end, rb_encoding *enc,
1574 rb_encoding **fixed_enc, onig_errmsg_buffer err, int options);
1575
1576NORETURN(static void reg_enc_error(VALUE re, VALUE str));
1577
1578static void
1579reg_enc_error(VALUE re, VALUE str)
1580{
1581 rb_raise(rb_eEncCompatError,
1582 "incompatible encoding regexp match (%s regexp with %s string)",
1583 rb_enc_inspect_name(rb_enc_get(re)),
1584 rb_enc_inspect_name(rb_enc_get(str)));
1585}
1586
1587static rb_encoding*
1588rb_reg_prepare_enc(VALUE re, VALUE str, int warn)
1589{
1590 rb_encoding *enc = 0;
1591 int cr = rb_enc_str_coderange(str);
1592
1593 if (cr == ENC_CODERANGE_BROKEN) {
1594 rb_raise(rb_eArgError,
1595 "invalid byte sequence in %s",
1596 rb_enc_name(rb_enc_get(str)));
1597 }
1598
1599 rb_reg_check(re);
1600 enc = rb_enc_get(str);
1601 if (RREGEXP_PTR(re)->enc == enc) {
1602 }
1603 else if (cr == ENC_CODERANGE_7BIT &&
1604 RREGEXP_PTR(re)->enc == rb_usascii_encoding()) {
1605 enc = RREGEXP_PTR(re)->enc;
1606 }
1607 else if (!rb_enc_asciicompat(enc)) {
1608 reg_enc_error(re, str);
1609 }
1610 else if (rb_reg_fixed_encoding_p(re)) {
1611 if ((!rb_enc_asciicompat(RREGEXP_PTR(re)->enc) ||
1612 cr != ENC_CODERANGE_7BIT)) {
1613 reg_enc_error(re, str);
1614 }
1615 enc = RREGEXP_PTR(re)->enc;
1616 }
1617 else if (warn && (RBASIC(re)->flags & REG_ENCODING_NONE) &&
1618 enc != rb_ascii8bit_encoding() &&
1619 cr != ENC_CODERANGE_7BIT) {
1620 rb_warn("historical binary regexp match /.../n against %s string",
1621 rb_enc_name(enc));
1622 }
1623 return enc;
1624}
1625
1626regex_t *
1628{
1629 int r;
1630 OnigErrorInfo einfo;
1631 VALUE unescaped;
1632 rb_encoding *fixed_enc = 0;
1633 rb_encoding *enc = rb_reg_prepare_enc(re, str, 1);
1634
1635 regex_t *reg = RREGEXP_PTR(re);
1636 if (reg->enc == enc) return reg;
1637
1638 rb_reg_check(re);
1639
1640 VALUE src_str = RREGEXP_SRC(re);
1641 const char *pattern = RSTRING_PTR(src_str);
1642
1643 onig_errmsg_buffer err = "";
1644 unescaped = rb_reg_preprocess(
1645 pattern, pattern + RSTRING_LEN(src_str), enc,
1646 &fixed_enc, err, 0);
1647
1648 if (NIL_P(unescaped)) {
1649 rb_raise(rb_eArgError, "regexp preprocess failed: %s", err);
1650 }
1651
1652 // inherit the timeout settings
1653 rb_hrtime_t timelimit = reg->timelimit;
1654
1655 const char *ptr;
1656 long len;
1657 RSTRING_GETMEM(unescaped, ptr, len);
1658
1659 /* If there are no other users of this regex, then we can directly overwrite it. */
1660 if (ruby_single_main_ractor && RREGEXP(re)->usecnt == 0) {
1661 regex_t tmp_reg;
1662 r = onig_new_without_alloc(&tmp_reg, (UChar *)ptr, (UChar *)(ptr + len),
1663 reg->options, enc,
1664 OnigDefaultSyntax, &einfo);
1665
1666 if (r) {
1667 /* There was an error so perform cleanups. */
1668 onig_free_body(&tmp_reg);
1669 }
1670 else {
1671 onig_free_body(reg);
1672 /* There are no errors so set reg to tmp_reg. */
1673 *reg = tmp_reg;
1674 }
1675 }
1676 else {
1677 r = onig_new(&reg, (UChar *)ptr, (UChar *)(ptr + len),
1678 reg->options, enc,
1679 OnigDefaultSyntax, &einfo);
1680 }
1681
1682 if (r) {
1683 onig_error_code_to_str((UChar*)err, r, &einfo);
1684 rb_reg_raise(err, re);
1685 }
1686
1687 reg->timelimit = timelimit;
1688
1689 RB_GC_GUARD(unescaped);
1690 RB_GC_GUARD(src_str);
1691 return reg;
1692}
1693
1694OnigPosition
1696 OnigPosition (*match)(regex_t *reg, VALUE str, struct re_registers *regs, void *args),
1697 void *args, struct re_registers *regs)
1698{
1699 regex_t *reg = rb_reg_prepare_re(re, str);
1700
1701 bool tmpreg = reg != RREGEXP_PTR(re);
1702 if (!tmpreg) RREGEXP(re)->usecnt++;
1703
1704 OnigPosition result = match(reg, str, regs, args);
1705
1706 if (!tmpreg) RREGEXP(re)->usecnt--;
1707 if (tmpreg) {
1708 onig_free(reg);
1709 }
1710
1711 if (result < 0) {
1712 onig_region_free(regs, 0);
1713
1714 switch (result) {
1715 case ONIG_MISMATCH:
1716 break;
1717 case ONIGERR_TIMEOUT:
1718 rb_raise(rb_eRegexpTimeoutError, "regexp match timeout");
1719 default: {
1720 onig_errmsg_buffer err = "";
1721 onig_error_code_to_str((UChar*)err, (int)result);
1722 rb_reg_raise(err, re);
1723 }
1724 }
1725 }
1726
1727 return result;
1728}
1729
1730long
1731rb_reg_adjust_startpos(VALUE re, VALUE str, long pos, int reverse)
1732{
1733 long range;
1734 rb_encoding *enc;
1735 UChar *p, *string;
1736
1737 enc = rb_reg_prepare_enc(re, str, 0);
1738
1739 if (reverse) {
1740 range = -pos;
1741 }
1742 else {
1743 range = RSTRING_LEN(str) - pos;
1744 }
1745
1746 if (pos > 0 && ONIGENC_MBC_MAXLEN(enc) != 1 && pos < RSTRING_LEN(str)) {
1747 string = (UChar*)RSTRING_PTR(str);
1748
1749 if (range > 0) {
1750 p = onigenc_get_right_adjust_char_head(enc, string, string + pos, string + RSTRING_LEN(str));
1751 }
1752 else {
1753 p = ONIGENC_LEFT_ADJUST_CHAR_HEAD(enc, string, string + pos, string + RSTRING_LEN(str));
1754 }
1755 return p - string;
1756 }
1757
1758 return pos;
1759}
1760
1762 long pos;
1763 long range;
1764};
1765
1766static OnigPosition
1767reg_onig_search(regex_t *reg, VALUE str, struct re_registers *regs, void *args_ptr)
1768{
1769 struct reg_onig_search_args *args = (struct reg_onig_search_args *)args_ptr;
1770 const char *ptr;
1771 long len;
1772 RSTRING_GETMEM(str, ptr, len);
1773
1774 return onig_search(
1775 reg,
1776 (UChar *)ptr,
1777 (UChar *)(ptr + len),
1778 (UChar *)(ptr + args->pos),
1779 (UChar *)(ptr + args->range),
1780 regs,
1781 ONIG_OPTION_NONE);
1782}
1783
1784/* returns byte offset */
1785static long
1786rb_reg_search_set_match(VALUE re, VALUE str, long pos, int reverse, int set_backref_str, VALUE *set_match)
1787{
1788 long len = RSTRING_LEN(str);
1789 if (pos > len || pos < 0) {
1791 return -1;
1792 }
1793
1794 struct reg_onig_search_args args = {
1795 .pos = pos,
1796 .range = reverse ? 0 : len,
1797 };
1798 struct re_registers regs = {0};
1799
1800 OnigPosition result = rb_reg_onig_match(re, str, reg_onig_search, &args, &regs);
1801
1802 if (result == ONIG_MISMATCH) {
1804 return ONIG_MISMATCH;
1805 }
1806
1807 VALUE match = Qnil;
1808 if (set_match) {
1809 match = *set_match;
1810 }
1811
1812 if (NIL_P(match)) {
1813 match = rb_backref_get();
1814 }
1815
1816 if (!NIL_P(match) && FL_TEST(match, MATCH_BUSY)) {
1817 match = Qnil;
1818 }
1819
1820 if (NIL_P(match)) {
1821 match = match_alloc(rb_cMatch);
1822 }
1823 else {
1824 onig_region_free(&RMATCH_EXT(match)->regs, false);
1825 }
1826
1827 rb_matchext_t *rm = RMATCH_EXT(match);
1828 rm->regs = regs;
1829
1830 if (set_backref_str) {
1831 RB_OBJ_WRITE(match, &RMATCH(match)->str, rb_str_new4(str));
1832 rb_obj_reveal(match, rb_cMatch);
1833 }
1834 else {
1835 /* Note that a MatchData object with RMATCH(match)->str == 0 is incomplete!
1836 * We need to hide the object from ObjectSpace.each_object.
1837 * https://bugs.ruby-lang.org/issues/19159
1838 */
1839 rb_obj_hide(match);
1840 }
1841
1842 RB_OBJ_WRITE(match, &RMATCH(match)->regexp, re);
1843 rb_backref_set(match);
1844 if (set_match) *set_match = match;
1845
1846 return result;
1847}
1848
1849long
1850rb_reg_search0(VALUE re, VALUE str, long pos, int reverse, int set_backref_str, VALUE *match)
1851{
1852 return rb_reg_search_set_match(re, str, pos, reverse, set_backref_str, match);
1853}
1854
1855long
1856rb_reg_search(VALUE re, VALUE str, long pos, int reverse)
1857{
1858 return rb_reg_search_set_match(re, str, pos, reverse, 1, NULL);
1859}
1860
1861static OnigPosition
1862reg_onig_match(regex_t *reg, VALUE str, struct re_registers *regs, void *_)
1863{
1864 const char *ptr;
1865 long len;
1866 RSTRING_GETMEM(str, ptr, len);
1867
1868 return onig_match(
1869 reg,
1870 (UChar *)ptr,
1871 (UChar *)(ptr + len),
1872 (UChar *)ptr,
1873 regs,
1874 ONIG_OPTION_NONE);
1875}
1876
1877bool
1878rb_reg_start_with_p(VALUE re, VALUE str)
1879{
1880 VALUE match = rb_backref_get();
1881 if (NIL_P(match) || FL_TEST(match, MATCH_BUSY)) {
1882 match = match_alloc(rb_cMatch);
1883 }
1884
1885 struct re_registers *regs = RMATCH_REGS(match);
1886
1887 if (rb_reg_onig_match(re, str, reg_onig_match, NULL, regs) == ONIG_MISMATCH) {
1889 return false;
1890 }
1891
1892 RB_OBJ_WRITE(match, &RMATCH(match)->str, rb_str_new4(str));
1893 RB_OBJ_WRITE(match, &RMATCH(match)->regexp, re);
1894 rb_backref_set(match);
1895
1896 return true;
1897}
1898
1899VALUE
1901{
1902 struct re_registers *regs;
1903 if (NIL_P(match)) return Qnil;
1904 match_check(match);
1905 regs = RMATCH_REGS(match);
1906 if (nth >= regs->num_regs) {
1907 return Qnil;
1908 }
1909 if (nth < 0) {
1910 nth += regs->num_regs;
1911 if (nth <= 0) return Qnil;
1912 }
1913 return RBOOL(BEG(nth) != -1);
1914}
1915
1916VALUE
1918{
1919 VALUE str;
1920 long start, end, len;
1921 struct re_registers *regs;
1922
1923 if (NIL_P(match)) return Qnil;
1924 match_check(match);
1925 regs = RMATCH_REGS(match);
1926 if (nth >= regs->num_regs) {
1927 return Qnil;
1928 }
1929 if (nth < 0) {
1930 nth += regs->num_regs;
1931 if (nth <= 0) return Qnil;
1932 }
1933 start = BEG(nth);
1934 if (start == -1) return Qnil;
1935 end = END(nth);
1936 len = end - start;
1937 str = rb_str_subseq(RMATCH(match)->str, start, len);
1938 return str;
1939}
1940
1941VALUE
1943{
1944 return rb_reg_nth_match(0, match);
1945}
1946
1947
1948/*
1949 * call-seq:
1950 * pre_match -> string
1951 *
1952 * Returns the substring of the target string from its beginning
1953 * up to the first match in +self+ (that is, <tt>self[0]</tt>);
1954 * equivalent to regexp global variable <tt>$`</tt>:
1955 *
1956 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1957 * # => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8">
1958 * m[0] # => "HX1138"
1959 * m.pre_match # => "T"
1960 *
1961 * Related: MatchData#post_match.
1962 *
1963 */
1964
1965VALUE
1967{
1968 VALUE str;
1969 struct re_registers *regs;
1970
1971 if (NIL_P(match)) return Qnil;
1972 match_check(match);
1973 regs = RMATCH_REGS(match);
1974 if (BEG(0) == -1) return Qnil;
1975 str = rb_str_subseq(RMATCH(match)->str, 0, BEG(0));
1976 return str;
1977}
1978
1979
1980/*
1981 * call-seq:
1982 * post_match -> str
1983 *
1984 * Returns the substring of the target string from
1985 * the end of the first match in +self+ (that is, <tt>self[0]</tt>)
1986 * to the end of the string;
1987 * equivalent to regexp global variable <tt>$'</tt>:
1988 *
1989 * m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie")
1990 * # => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8">
1991 * m[0] # => "HX1138"
1992 * m.post_match # => ": The Movie"\
1993 *
1994 * Related: MatchData.pre_match.
1995 *
1996 */
1997
1998VALUE
2000{
2001 VALUE str;
2002 long pos;
2003 struct re_registers *regs;
2004
2005 if (NIL_P(match)) return Qnil;
2006 match_check(match);
2007 regs = RMATCH_REGS(match);
2008 if (BEG(0) == -1) return Qnil;
2009 str = RMATCH(match)->str;
2010 pos = END(0);
2011 str = rb_str_subseq(str, pos, RSTRING_LEN(str) - pos);
2012 return str;
2013}
2014
2015static int
2016match_last_index(VALUE match)
2017{
2018 int i;
2019 struct re_registers *regs;
2020
2021 if (NIL_P(match)) return -1;
2022 match_check(match);
2023 regs = RMATCH_REGS(match);
2024 if (BEG(0) == -1) return -1;
2025
2026 for (i=regs->num_regs-1; BEG(i) == -1 && i > 0; i--)
2027 ;
2028 return i;
2029}
2030
2031VALUE
2033{
2034 int i = match_last_index(match);
2035 if (i <= 0) return Qnil;
2036 struct re_registers *regs = RMATCH_REGS(match);
2037 return rb_str_subseq(RMATCH(match)->str, BEG(i), END(i) - BEG(i));
2038}
2039
2040VALUE
2041rb_reg_last_defined(VALUE match)
2042{
2043 int i = match_last_index(match);
2044 if (i < 0) return Qnil;
2045 return RBOOL(i);
2046}
2047
2048static VALUE
2049last_match_getter(ID _x, VALUE *_y)
2050{
2052}
2053
2054static VALUE
2055prematch_getter(ID _x, VALUE *_y)
2056{
2058}
2059
2060static VALUE
2061postmatch_getter(ID _x, VALUE *_y)
2062{
2064}
2065
2066static VALUE
2067last_paren_match_getter(ID _x, VALUE *_y)
2068{
2070}
2071
2072static VALUE
2073match_array(VALUE match, int start)
2074{
2075 struct re_registers *regs;
2076 VALUE ary;
2077 VALUE target;
2078 int i;
2079
2080 match_check(match);
2081 regs = RMATCH_REGS(match);
2082 ary = rb_ary_new2(regs->num_regs);
2083 target = RMATCH(match)->str;
2084
2085 for (i=start; i<regs->num_regs; i++) {
2086 if (regs->beg[i] == -1) {
2087 rb_ary_push(ary, Qnil);
2088 }
2089 else {
2090 VALUE str = rb_str_subseq(target, regs->beg[i], regs->end[i]-regs->beg[i]);
2091 rb_ary_push(ary, str);
2092 }
2093 }
2094 return ary;
2095}
2096
2097
2098/*
2099 * call-seq:
2100 * to_a -> array
2101 *
2102 * Returns the array of matches:
2103 *
2104 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
2105 * # => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8">
2106 * m.to_a # => ["HX1138", "H", "X", "113", "8"]
2107 *
2108 * Related: MatchData#captures.
2109 *
2110 */
2111
2112static VALUE
2113match_to_a(VALUE match)
2114{
2115 return match_array(match, 0);
2116}
2117
2118
2119/*
2120 * call-seq:
2121 * captures -> array
2122 *
2123 * Returns the array of captures,
2124 * which are all matches except <tt>m[0]</tt>:
2125 *
2126 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
2127 * # => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8">
2128 * m[0] # => "HX1138"
2129 * m.captures # => ["H", "X", "113", "8"]
2130 *
2131 * Related: MatchData.to_a.
2132 *
2133 */
2134static VALUE
2135match_captures(VALUE match)
2136{
2137 return match_array(match, 1);
2138}
2139
2140static int
2141name_to_backref_number(const struct re_registers *regs, VALUE regexp, const char* name, const char* name_end)
2142{
2143 if (NIL_P(regexp)) return -1;
2144 return onig_name_to_backref_number(RREGEXP_PTR(regexp),
2145 (const unsigned char *)name, (const unsigned char *)name_end, regs);
2146}
2147
2148#define NAME_TO_NUMBER(regs, re, name, name_ptr, name_end) \
2149 (NIL_P(re) ? 0 : \
2150 !rb_enc_compatible(RREGEXP_SRC(re), (name)) ? 0 : \
2151 name_to_backref_number((regs), (re), (name_ptr), (name_end)))
2152
2153static int
2154namev_to_backref_number(const struct re_registers *regs, VALUE re, VALUE name)
2155{
2156 int num;
2157
2158 if (SYMBOL_P(name)) {
2159 name = rb_sym2str(name);
2160 }
2161 else if (!RB_TYPE_P(name, T_STRING)) {
2162 return -1;
2163 }
2164 num = NAME_TO_NUMBER(regs, re, name,
2165 RSTRING_PTR(name), RSTRING_END(name));
2166 if (num < 1) {
2167 name_to_backref_error(name);
2168 }
2169 return num;
2170}
2171
2172static VALUE
2173match_ary_subseq(VALUE match, long beg, long len, VALUE result)
2174{
2175 long olen = RMATCH_REGS(match)->num_regs;
2176 long j, end = olen < beg+len ? olen : beg+len;
2177 if (NIL_P(result)) result = rb_ary_new_capa(len);
2178 if (len == 0) return result;
2179
2180 for (j = beg; j < end; j++) {
2181 rb_ary_push(result, rb_reg_nth_match((int)j, match));
2182 }
2183 if (beg + len > j) {
2184 rb_ary_resize(result, RARRAY_LEN(result) + (beg + len) - j);
2185 }
2186 return result;
2187}
2188
2189static VALUE
2190match_ary_aref(VALUE match, VALUE idx, VALUE result)
2191{
2192 long beg, len;
2193 int num_regs = RMATCH_REGS(match)->num_regs;
2194
2195 /* check if idx is Range */
2196 switch (rb_range_beg_len(idx, &beg, &len, (long)num_regs, !NIL_P(result))) {
2197 case Qfalse:
2198 if (NIL_P(result)) return rb_reg_nth_match(NUM2INT(idx), match);
2199 rb_ary_push(result, rb_reg_nth_match(NUM2INT(idx), match));
2200 return result;
2201 case Qnil:
2202 return Qnil;
2203 default:
2204 return match_ary_subseq(match, beg, len, result);
2205 }
2206}
2207
2208/*
2209 * call-seq:
2210 * self[offset] -> string or nil
2211 * self[offset, size] -> array
2212 * self[range] -> array
2213 * self[name] -> string or nil
2214 *
2215 * When arguments +offset+, +offset+ and +size+, or +range+ are given,
2216 * returns match and captures in the style of Array#[]:
2217 *
2218 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
2219 * # => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8">
2220 * m[0] # => "HX1138"
2221 * m[1, 2] # => ["H", "X"]
2222 * m[1..3] # => ["H", "X", "113"]
2223 * m[-3, 2] # => ["X", "113"]
2224 *
2225 * When string or symbol argument +name+ is given,
2226 * returns the matched substring for the given name:
2227 *
2228 * m = /(?<foo>.)(.)(?<bar>.+)/.match("hoge")
2229 * # => #<MatchData "hoge" foo:"h" bar:"ge">
2230 * m['foo'] # => "h"
2231 * m[:bar] # => "ge"
2232 *
2233 * If multiple captures have the same name, returns the last matched
2234 * substring.
2235 *
2236 * m = /(?<foo>.)(?<foo>.+)/.match("hoge")
2237 * # => #<MatchData "hoge" foo:"h" foo:"oge">
2238 * m[:foo] #=> "oge"
2239 *
2240 * m = /\W(?<foo>.+)|\w(?<foo>.+)|(?<foo>.+)/.match("hoge")
2241 * #<MatchData "hoge" foo:nil foo:"oge" foo:nil>
2242 * m[:foo] #=> "oge"
2243 *
2244 */
2245
2246static VALUE
2247match_aref(int argc, VALUE *argv, VALUE match)
2248{
2249 VALUE idx, length;
2250
2251 match_check(match);
2252 rb_scan_args(argc, argv, "11", &idx, &length);
2253
2254 if (NIL_P(length)) {
2255 if (FIXNUM_P(idx)) {
2256 return rb_reg_nth_match(FIX2INT(idx), match);
2257 }
2258 else {
2259 int num = namev_to_backref_number(RMATCH_REGS(match), RMATCH(match)->regexp, idx);
2260 if (num >= 0) {
2261 return rb_reg_nth_match(num, match);
2262 }
2263 else {
2264 return match_ary_aref(match, idx, Qnil);
2265 }
2266 }
2267 }
2268 else {
2269 long beg = NUM2LONG(idx);
2270 long len = NUM2LONG(length);
2271 long num_regs = RMATCH_REGS(match)->num_regs;
2272 if (len < 0) {
2273 return Qnil;
2274 }
2275 if (beg < 0) {
2276 beg += num_regs;
2277 if (beg < 0) return Qnil;
2278 }
2279 else if (beg > num_regs) {
2280 return Qnil;
2281 }
2282 if (beg+len > num_regs) {
2283 len = num_regs - beg;
2284 }
2285 return match_ary_subseq(match, beg, len, Qnil);
2286 }
2287}
2288
2289/*
2290 * call-seq:
2291 * values_at(*indexes) -> array
2292 *
2293 * Returns match and captures at the given +indexes+,
2294 * which may include any mixture of:
2295 *
2296 * - Integers.
2297 * - Ranges.
2298 * - Names (strings and symbols).
2299 *
2300 *
2301 * Examples:
2302 *
2303 * m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie")
2304 * # => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8">
2305 * m.values_at(0, 2, -2) # => ["HX1138", "X", "113"]
2306 * m.values_at(1..2, -1) # => ["H", "X", "8"]
2307 *
2308 * m = /(?<a>\d+) *(?<op>[+\-*\/]) *(?<b>\d+)/.match("1 + 2")
2309 * # => #<MatchData "1 + 2" a:"1" op:"+" b:"2">
2310 * m.values_at(0, 1..2, :a, :b, :op)
2311 * # => ["1 + 2", "1", "+", "1", "2", "+"]
2312 *
2313 */
2314
2315static VALUE
2316match_values_at(int argc, VALUE *argv, VALUE match)
2317{
2318 VALUE result;
2319 int i;
2320
2321 match_check(match);
2322 result = rb_ary_new2(argc);
2323
2324 for (i=0; i<argc; i++) {
2325 if (FIXNUM_P(argv[i])) {
2326 rb_ary_push(result, rb_reg_nth_match(FIX2INT(argv[i]), match));
2327 }
2328 else {
2329 int num = namev_to_backref_number(RMATCH_REGS(match), RMATCH(match)->regexp, argv[i]);
2330 if (num >= 0) {
2331 rb_ary_push(result, rb_reg_nth_match(num, match));
2332 }
2333 else {
2334 match_ary_aref(match, argv[i], result);
2335 }
2336 }
2337 }
2338 return result;
2339}
2340
2341
2342/*
2343 * call-seq:
2344 * to_s -> string
2345 *
2346 * Returns the matched string:
2347 *
2348 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
2349 * # => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8">
2350 * m.to_s # => "HX1138"
2351 *
2352 * m = /(?<foo>.)(.)(?<bar>.+)/.match("hoge")
2353 * # => #<MatchData "hoge" foo:"h" bar:"ge">
2354 * m.to_s # => "hoge"
2355 *
2356 * Related: MatchData.inspect.
2357 *
2358 */
2359
2360static VALUE
2361match_to_s(VALUE match)
2362{
2363 VALUE str = rb_reg_last_match(match_check(match));
2364
2365 if (NIL_P(str)) str = rb_str_new(0,0);
2366 return str;
2367}
2368
2369static int
2370match_named_captures_iter(const OnigUChar *name, const OnigUChar *name_end,
2371 int back_num, int *back_refs, OnigRegex regex, void *arg)
2372{
2373 struct MEMO *memo = MEMO_CAST(arg);
2374 VALUE hash = memo->v1;
2375 VALUE match = memo->v2;
2376 long symbolize = memo->u3.state;
2377
2378 VALUE key = rb_enc_str_new((const char *)name, name_end-name, regex->enc);
2379
2380 if (symbolize > 0) {
2381 key = rb_str_intern(key);
2382 }
2383
2384 VALUE value;
2385
2386 int i;
2387 int found = 0;
2388
2389 for (i = 0; i < back_num; i++) {
2390 value = rb_reg_nth_match(back_refs[i], match);
2391 if (RTEST(value)) {
2392 rb_hash_aset(hash, key, value);
2393 found = 1;
2394 }
2395 }
2396
2397 if (found == 0) {
2398 rb_hash_aset(hash, key, Qnil);
2399 }
2400
2401 return 0;
2402}
2403
2404/*
2405 * call-seq:
2406 * named_captures(symbolize_names: false) -> hash
2407 *
2408 * Returns a hash of the named captures;
2409 * each key is a capture name; each value is its captured string or +nil+:
2410 *
2411 * m = /(?<foo>.)(.)(?<bar>.+)/.match("hoge")
2412 * # => #<MatchData "hoge" foo:"h" bar:"ge">
2413 * m.named_captures # => {"foo"=>"h", "bar"=>"ge"}
2414 *
2415 * m = /(?<a>.)(?<b>.)/.match("01")
2416 * # => #<MatchData "01" a:"0" b:"1">
2417 * m.named_captures #=> {"a" => "0", "b" => "1"}
2418 *
2419 * m = /(?<a>.)(?<b>.)?/.match("0")
2420 * # => #<MatchData "0" a:"0" b:nil>
2421 * m.named_captures #=> {"a" => "0", "b" => nil}
2422 *
2423 * m = /(?<a>.)(?<a>.)/.match("01")
2424 * # => #<MatchData "01" a:"0" a:"1">
2425 * m.named_captures #=> {"a" => "1"}
2426 *
2427 * If keyword argument +symbolize_names+ is given
2428 * a true value, the keys in the resulting hash are Symbols:
2429 *
2430 * m = /(?<a>.)(?<a>.)/.match("01")
2431 * # => #<MatchData "01" a:"0" a:"1">
2432 * m.named_captures(symbolize_names: true) #=> {:a => "1"}
2433 *
2434 */
2435
2436static VALUE
2437match_named_captures(int argc, VALUE *argv, VALUE match)
2438{
2439 VALUE hash;
2440 struct MEMO *memo;
2441
2442 match_check(match);
2443 if (NIL_P(RMATCH(match)->regexp))
2444 return rb_hash_new();
2445
2446 VALUE opt;
2447 VALUE symbolize_names = 0;
2448
2449 rb_scan_args(argc, argv, "0:", &opt);
2450
2451 if (!NIL_P(opt)) {
2452 static ID keyword_ids[1];
2453
2454 VALUE symbolize_names_val;
2455
2456 if (!keyword_ids[0]) {
2457 keyword_ids[0] = rb_intern_const("symbolize_names");
2458 }
2459 rb_get_kwargs(opt, keyword_ids, 0, 1, &symbolize_names_val);
2460 if (!UNDEF_P(symbolize_names_val) && RTEST(symbolize_names_val)) {
2461 symbolize_names = 1;
2462 }
2463 }
2464
2465 hash = rb_hash_new();
2466 memo = rb_imemo_memo_new(hash, match, symbolize_names);
2467
2468 onig_foreach_name(RREGEXP(RMATCH(match)->regexp)->ptr, match_named_captures_iter, (void*)memo);
2469
2470 return hash;
2471}
2472
2473/*
2474 * call-seq:
2475 * deconstruct_keys(array_of_names) -> hash
2476 *
2477 * Returns a hash of the named captures for the given names.
2478 *
2479 * m = /(?<hours>\d{2}):(?<minutes>\d{2}):(?<seconds>\d{2})/.match("18:37:22")
2480 * m.deconstruct_keys([:hours, :minutes]) # => {:hours => "18", :minutes => "37"}
2481 * m.deconstruct_keys(nil) # => {:hours => "18", :minutes => "37", :seconds => "22"}
2482 *
2483 * Returns an empty hash if no named captures were defined:
2484 *
2485 * m = /(\d{2}):(\d{2}):(\d{2})/.match("18:37:22")
2486 * m.deconstruct_keys(nil) # => {}
2487 *
2488 */
2489static VALUE
2490match_deconstruct_keys(VALUE match, VALUE keys)
2491{
2492 VALUE h;
2493 long i;
2494
2495 match_check(match);
2496
2497 if (NIL_P(RMATCH(match)->regexp)) {
2498 return rb_hash_new_with_size(0);
2499 }
2500
2501 if (NIL_P(keys)) {
2502 h = rb_hash_new_with_size(onig_number_of_names(RREGEXP_PTR(RMATCH(match)->regexp)));
2503
2504 struct MEMO *memo;
2505 memo = rb_imemo_memo_new(h, match, 1);
2506
2507 onig_foreach_name(RREGEXP_PTR(RMATCH(match)->regexp), match_named_captures_iter, (void*)memo);
2508
2509 return h;
2510 }
2511
2512 Check_Type(keys, T_ARRAY);
2513
2514 if (onig_number_of_names(RREGEXP_PTR(RMATCH(match)->regexp)) < RARRAY_LEN(keys)) {
2515 return rb_hash_new_with_size(0);
2516 }
2517
2518 h = rb_hash_new_with_size(RARRAY_LEN(keys));
2519
2520 for (i=0; i<RARRAY_LEN(keys); i++) {
2521 VALUE key = RARRAY_AREF(keys, i);
2522 VALUE name;
2523
2524 Check_Type(key, T_SYMBOL);
2525
2526 name = rb_sym2str(key);
2527
2528 int num = NAME_TO_NUMBER(RMATCH_REGS(match), RMATCH(match)->regexp, RMATCH(match)->regexp,
2529 RSTRING_PTR(name), RSTRING_END(name));
2530
2531 if (num >= 0) {
2532 rb_hash_aset(h, key, rb_reg_nth_match(num, match));
2533 }
2534 else {
2535 return h;
2536 }
2537 }
2538
2539 return h;
2540}
2541
2542/*
2543 * call-seq:
2544 * string -> string
2545 *
2546 * Returns the target string if it was frozen;
2547 * otherwise, returns a frozen copy of the target string:
2548 *
2549 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
2550 * # => #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8">
2551 * m.string # => "THX1138."
2552 *
2553 */
2554
2555static VALUE
2556match_string(VALUE match)
2557{
2558 match_check(match);
2559 return RMATCH(match)->str; /* str is frozen */
2560}
2561
2563 const UChar *name;
2564 long len;
2565};
2566
2567static int
2568match_inspect_name_iter(const OnigUChar *name, const OnigUChar *name_end,
2569 int back_num, int *back_refs, OnigRegex regex, void *arg0)
2570{
2571 struct backref_name_tag *arg = (struct backref_name_tag *)arg0;
2572 int i;
2573
2574 for (i = 0; i < back_num; i++) {
2575 arg[back_refs[i]].name = name;
2576 arg[back_refs[i]].len = name_end - name;
2577 }
2578 return 0;
2579}
2580
2581/*
2582 * call-seq:
2583 * inspect -> string
2584 *
2585 * Returns a string representation of +self+:
2586 *
2587 * m = /.$/.match("foo")
2588 * # => #<MatchData "o">
2589 * m.inspect # => "#<MatchData \"o\">"
2590 *
2591 * m = /(.)(.)(.)/.match("foo")
2592 * # => #<MatchData "foo" 1:"f" 2:"o" 3:"o">
2593 * m.inspect # => "#<MatchData \"foo\" 1:\"f\" 2:\"o\
2594 *
2595 * m = /(.)(.)?(.)/.match("fo")
2596 * # => #<MatchData "fo" 1:"f" 2:nil 3:"o">
2597 * m.inspect # => "#<MatchData \"fo\" 1:\"f\" 2:nil 3:\"o\">"
2598 *
2599 * Related: MatchData#to_s.
2600 */
2601
2602static VALUE
2603match_inspect(VALUE match)
2604{
2605 VALUE cname = rb_class_path(rb_obj_class(match));
2606 VALUE str;
2607 int i;
2608 struct re_registers *regs = RMATCH_REGS(match);
2609 int num_regs = regs->num_regs;
2610 struct backref_name_tag *names;
2611 VALUE names_obj = Qnil;
2612 VALUE regexp = RMATCH(match)->regexp;
2613
2614 if (regexp == 0) {
2615 return rb_sprintf("#<%"PRIsVALUE":%p>", cname, (void*)match);
2616 }
2617 else if (NIL_P(regexp)) {
2618 return rb_sprintf("#<%"PRIsVALUE": %"PRIsVALUE">",
2619 cname, rb_reg_nth_match(0, match));
2620 }
2621
2622 names = RB_ALLOCV_N(struct backref_name_tag, names_obj, num_regs);
2623 MEMZERO(names, struct backref_name_tag, num_regs);
2624
2625 onig_foreach_name(RREGEXP_PTR(regexp),
2626 match_inspect_name_iter, names);
2627
2628 str = rb_str_buf_new2("#<");
2629 rb_str_append(str, cname);
2630
2631 for (i = 0; i < num_regs; i++) {
2632 VALUE v;
2633 rb_str_buf_cat2(str, " ");
2634 if (0 < i) {
2635 if (names[i].name)
2636 rb_str_buf_cat(str, (const char *)names[i].name, names[i].len);
2637 else {
2638 rb_str_catf(str, "%d", i);
2639 }
2640 rb_str_buf_cat2(str, ":");
2641 }
2642 v = rb_reg_nth_match(i, match);
2643 if (NIL_P(v))
2644 rb_str_buf_cat2(str, "nil");
2645 else
2647 }
2648 rb_str_buf_cat2(str, ">");
2649
2650 RB_ALLOCV_END(names_obj);
2651 return str;
2652}
2653
2655
2656static int
2657read_escaped_byte(const char **pp, const char *end, onig_errmsg_buffer err)
2658{
2659 const char *p = *pp;
2660 int code;
2661 int meta_prefix = 0, ctrl_prefix = 0;
2662 size_t len;
2663
2664 if (p == end || *p++ != '\\') {
2665 errcpy(err, "too short escaped multibyte character");
2666 return -1;
2667 }
2668
2669again:
2670 if (p == end) {
2671 errcpy(err, "too short escape sequence");
2672 return -1;
2673 }
2674 switch (*p++) {
2675 case '\\': code = '\\'; break;
2676 case 'n': code = '\n'; break;
2677 case 't': code = '\t'; break;
2678 case 'r': code = '\r'; break;
2679 case 'f': code = '\f'; break;
2680 case 'v': code = '\013'; break;
2681 case 'a': code = '\007'; break;
2682 case 'e': code = '\033'; break;
2683
2684 /* \OOO */
2685 case '0': case '1': case '2': case '3':
2686 case '4': case '5': case '6': case '7':
2687 p--;
2688 code = scan_oct(p, end < p+3 ? end-p : 3, &len);
2689 p += len;
2690 break;
2691
2692 case 'x': /* \xHH */
2693 code = scan_hex(p, end < p+2 ? end-p : 2, &len);
2694 if (len < 1) {
2695 errcpy(err, "invalid hex escape");
2696 return -1;
2697 }
2698 p += len;
2699 break;
2700
2701 case 'M': /* \M-X, \M-\C-X, \M-\cX */
2702 if (meta_prefix) {
2703 errcpy(err, "duplicate meta escape");
2704 return -1;
2705 }
2706 meta_prefix = 1;
2707 if (p+1 < end && *p++ == '-' && (*p & 0x80) == 0) {
2708 if (*p == '\\') {
2709 p++;
2710 goto again;
2711 }
2712 else {
2713 code = *p++;
2714 break;
2715 }
2716 }
2717 errcpy(err, "too short meta escape");
2718 return -1;
2719
2720 case 'C': /* \C-X, \C-\M-X */
2721 if (p == end || *p++ != '-') {
2722 errcpy(err, "too short control escape");
2723 return -1;
2724 }
2725 case 'c': /* \cX, \c\M-X */
2726 if (ctrl_prefix) {
2727 errcpy(err, "duplicate control escape");
2728 return -1;
2729 }
2730 ctrl_prefix = 1;
2731 if (p < end && (*p & 0x80) == 0) {
2732 if (*p == '\\') {
2733 p++;
2734 goto again;
2735 }
2736 else {
2737 code = *p++;
2738 break;
2739 }
2740 }
2741 errcpy(err, "too short control escape");
2742 return -1;
2743
2744 default:
2745 errcpy(err, "unexpected escape sequence");
2746 return -1;
2747 }
2748 if (code < 0 || 0xff < code) {
2749 errcpy(err, "invalid escape code");
2750 return -1;
2751 }
2752
2753 if (ctrl_prefix)
2754 code &= 0x1f;
2755 if (meta_prefix)
2756 code |= 0x80;
2757
2758 *pp = p;
2759 return code;
2760}
2761
2762static int
2763unescape_escaped_nonascii(const char **pp, const char *end, rb_encoding *enc,
2764 VALUE buf, rb_encoding **encp, onig_errmsg_buffer err)
2765{
2766 const char *p = *pp;
2767 int chmaxlen = rb_enc_mbmaxlen(enc);
2768 unsigned char *area = ALLOCA_N(unsigned char, chmaxlen);
2769 char *chbuf = (char *)area;
2770 int chlen = 0;
2771 int byte;
2772 int l;
2773
2774 memset(chbuf, 0, chmaxlen);
2775
2776 byte = read_escaped_byte(&p, end, err);
2777 if (byte == -1) {
2778 return -1;
2779 }
2780
2781 area[chlen++] = byte;
2782 while (chlen < chmaxlen &&
2783 MBCLEN_NEEDMORE_P(rb_enc_precise_mbclen(chbuf, chbuf+chlen, enc))) {
2784 byte = read_escaped_byte(&p, end, err);
2785 if (byte == -1) {
2786 return -1;
2787 }
2788 area[chlen++] = byte;
2789 }
2790
2791 l = rb_enc_precise_mbclen(chbuf, chbuf+chlen, enc);
2792 if (MBCLEN_INVALID_P(l)) {
2793 errcpy(err, "invalid multibyte escape");
2794 return -1;
2795 }
2796 if (1 < chlen || (area[0] & 0x80)) {
2797 rb_str_buf_cat(buf, chbuf, chlen);
2798
2799 if (*encp == 0)
2800 *encp = enc;
2801 else if (*encp != enc) {
2802 errcpy(err, "escaped non ASCII character in UTF-8 regexp");
2803 return -1;
2804 }
2805 }
2806 else {
2807 char escbuf[5];
2808 snprintf(escbuf, sizeof(escbuf), "\\x%02X", area[0]&0xff);
2809 rb_str_buf_cat(buf, escbuf, 4);
2810 }
2811 *pp = p;
2812 return 0;
2813}
2814
2815static int
2816check_unicode_range(unsigned long code, onig_errmsg_buffer err)
2817{
2818 if ((0xd800 <= code && code <= 0xdfff) || /* Surrogates */
2819 0x10ffff < code) {
2820 errcpy(err, "invalid Unicode range");
2821 return -1;
2822 }
2823 return 0;
2824}
2825
2826static int
2827append_utf8(unsigned long uv,
2828 VALUE buf, rb_encoding **encp, onig_errmsg_buffer err)
2829{
2830 if (check_unicode_range(uv, err) != 0)
2831 return -1;
2832 if (uv < 0x80) {
2833 char escbuf[5];
2834 snprintf(escbuf, sizeof(escbuf), "\\x%02X", (int)uv);
2835 rb_str_buf_cat(buf, escbuf, 4);
2836 }
2837 else {
2838 int len;
2839 char utf8buf[6];
2840 len = rb_uv_to_utf8(utf8buf, uv);
2841 rb_str_buf_cat(buf, utf8buf, len);
2842
2843 if (*encp == 0)
2844 *encp = rb_utf8_encoding();
2845 else if (*encp != rb_utf8_encoding()) {
2846 errcpy(err, "UTF-8 character in non UTF-8 regexp");
2847 return -1;
2848 }
2849 }
2850 return 0;
2851}
2852
2853static int
2854unescape_unicode_list(const char **pp, const char *end,
2855 VALUE buf, rb_encoding **encp, onig_errmsg_buffer err)
2856{
2857 const char *p = *pp;
2858 int has_unicode = 0;
2859 unsigned long code;
2860 size_t len;
2861
2862 while (p < end && ISSPACE(*p)) p++;
2863
2864 while (1) {
2865 code = ruby_scan_hex(p, end-p, &len);
2866 if (len == 0)
2867 break;
2868 if (6 < len) { /* max 10FFFF */
2869 errcpy(err, "invalid Unicode range");
2870 return -1;
2871 }
2872 p += len;
2873 if (append_utf8(code, buf, encp, err) != 0)
2874 return -1;
2875 has_unicode = 1;
2876
2877 while (p < end && ISSPACE(*p)) p++;
2878 }
2879
2880 if (has_unicode == 0) {
2881 errcpy(err, "invalid Unicode list");
2882 return -1;
2883 }
2884
2885 *pp = p;
2886
2887 return 0;
2888}
2889
2890static int
2891unescape_unicode_bmp(const char **pp, const char *end,
2892 VALUE buf, rb_encoding **encp, onig_errmsg_buffer err)
2893{
2894 const char *p = *pp;
2895 size_t len;
2896 unsigned long code;
2897
2898 if (end < p+4) {
2899 errcpy(err, "invalid Unicode escape");
2900 return -1;
2901 }
2902 code = ruby_scan_hex(p, 4, &len);
2903 if (len != 4) {
2904 errcpy(err, "invalid Unicode escape");
2905 return -1;
2906 }
2907 if (append_utf8(code, buf, encp, err) != 0)
2908 return -1;
2909 *pp = p + 4;
2910 return 0;
2911}
2912
2913static int
2914unescape_nonascii0(const char **pp, const char *end, rb_encoding *enc,
2915 VALUE buf, rb_encoding **encp, int *has_property,
2916 onig_errmsg_buffer err, int options, int recurse)
2917{
2918 const char *p = *pp;
2919 unsigned char c;
2920 char smallbuf[2];
2921 int in_char_class = 0;
2922 int parens = 1; /* ignored unless recurse is true */
2923 int extended_mode = options & ONIG_OPTION_EXTEND;
2924
2925begin_scan:
2926 while (p < end) {
2927 int chlen = rb_enc_precise_mbclen(p, end, enc);
2928 if (!MBCLEN_CHARFOUND_P(chlen)) {
2929 invalid_multibyte:
2930 errcpy(err, "invalid multibyte character");
2931 return -1;
2932 }
2933 chlen = MBCLEN_CHARFOUND_LEN(chlen);
2934 if (1 < chlen || (*p & 0x80)) {
2935 multibyte:
2936 rb_str_buf_cat(buf, p, chlen);
2937 p += chlen;
2938 if (*encp == 0)
2939 *encp = enc;
2940 else if (*encp != enc) {
2941 errcpy(err, "non ASCII character in UTF-8 regexp");
2942 return -1;
2943 }
2944 continue;
2945 }
2946
2947 switch (c = *p++) {
2948 case '\\':
2949 if (p == end) {
2950 errcpy(err, "too short escape sequence");
2951 return -1;
2952 }
2953 chlen = rb_enc_precise_mbclen(p, end, enc);
2954 if (!MBCLEN_CHARFOUND_P(chlen)) {
2955 goto invalid_multibyte;
2956 }
2957 if ((chlen = MBCLEN_CHARFOUND_LEN(chlen)) > 1) {
2958 /* include the previous backslash */
2959 --p;
2960 ++chlen;
2961 goto multibyte;
2962 }
2963 switch (c = *p++) {
2964 case '1': case '2': case '3':
2965 case '4': case '5': case '6': case '7': /* \O, \OO, \OOO or backref */
2966 {
2967 size_t len = end-(p-1), octlen;
2968 if (ruby_scan_oct(p-1, len < 3 ? len : 3, &octlen) <= 0177) {
2969 /* backref or 7bit octal.
2970 no need to unescape anyway.
2971 re-escaping may break backref */
2972 goto escape_asis;
2973 }
2974 }
2975 /* xxx: How about more than 199 subexpressions? */
2976
2977 case '0': /* \0, \0O, \0OO */
2978
2979 case 'x': /* \xHH */
2980 case 'c': /* \cX, \c\M-X */
2981 case 'C': /* \C-X, \C-\M-X */
2982 case 'M': /* \M-X, \M-\C-X, \M-\cX */
2983 p = p-2;
2984 if (rb_is_usascii_enc(enc)) {
2985 const char *pbeg = p;
2986 int byte = read_escaped_byte(&p, end, err);
2987 if (byte == -1) return -1;
2988 c = byte;
2989 rb_str_buf_cat(buf, pbeg, p-pbeg);
2990 }
2991 else {
2992 if (unescape_escaped_nonascii(&p, end, enc, buf, encp, err) != 0)
2993 return -1;
2994 }
2995 break;
2996
2997 case 'u':
2998 if (p == end) {
2999 errcpy(err, "too short escape sequence");
3000 return -1;
3001 }
3002 if (*p == '{') {
3003 /* \u{H HH HHH HHHH HHHHH HHHHHH ...} */
3004 p++;
3005 if (unescape_unicode_list(&p, end, buf, encp, err) != 0)
3006 return -1;
3007 if (p == end || *p++ != '}') {
3008 errcpy(err, "invalid Unicode list");
3009 return -1;
3010 }
3011 break;
3012 }
3013 else {
3014 /* \uHHHH */
3015 if (unescape_unicode_bmp(&p, end, buf, encp, err) != 0)
3016 return -1;
3017 break;
3018 }
3019
3020 case 'p': /* \p{Hiragana} */
3021 case 'P':
3022 if (!*encp) {
3023 *has_property = 1;
3024 }
3025 goto escape_asis;
3026
3027 default: /* \n, \\, \d, \9, etc. */
3028escape_asis:
3029 smallbuf[0] = '\\';
3030 smallbuf[1] = c;
3031 rb_str_buf_cat(buf, smallbuf, 2);
3032 break;
3033 }
3034 break;
3035
3036 case '#':
3037 if (extended_mode && !in_char_class) {
3038 /* consume and ignore comment in extended regexp */
3039 while ((p < end) && ((c = *p++) != '\n')) {
3040 if ((c & 0x80) && !*encp && enc == rb_utf8_encoding()) {
3041 *encp = enc;
3042 }
3043 }
3044 break;
3045 }
3046 rb_str_buf_cat(buf, (char *)&c, 1);
3047 break;
3048 case '[':
3049 in_char_class++;
3050 rb_str_buf_cat(buf, (char *)&c, 1);
3051 break;
3052 case ']':
3053 if (in_char_class) {
3054 in_char_class--;
3055 }
3056 rb_str_buf_cat(buf, (char *)&c, 1);
3057 break;
3058 case ')':
3059 rb_str_buf_cat(buf, (char *)&c, 1);
3060 if (!in_char_class && recurse) {
3061 if (--parens == 0) {
3062 *pp = p;
3063 return 0;
3064 }
3065 }
3066 break;
3067 case '(':
3068 if (!in_char_class && p + 1 < end && *p == '?') {
3069 if (*(p+1) == '#') {
3070 /* (?# is comment inside any regexp, and content inside should be ignored */
3071 const char *orig_p = p;
3072 int cont = 1;
3073
3074 while (cont && (p < end)) {
3075 switch (c = *p++) {
3076 default:
3077 if (!(c & 0x80)) break;
3078 if (!*encp && enc == rb_utf8_encoding()) {
3079 *encp = enc;
3080 }
3081 --p;
3082 /* fallthrough */
3083 case '\\':
3084 chlen = rb_enc_precise_mbclen(p, end, enc);
3085 if (!MBCLEN_CHARFOUND_P(chlen)) {
3086 goto invalid_multibyte;
3087 }
3088 p += MBCLEN_CHARFOUND_LEN(chlen);
3089 break;
3090 case ')':
3091 cont = 0;
3092 break;
3093 }
3094 }
3095
3096 if (cont) {
3097 /* unterminated (?#, rewind so it is syntax error */
3098 p = orig_p;
3099 c = '(';
3100 rb_str_buf_cat(buf, (char *)&c, 1);
3101 }
3102 break;
3103 }
3104 else {
3105 /* potential change of extended option */
3106 int invert = 0;
3107 int local_extend = 0;
3108 const char *s;
3109
3110 if (recurse) {
3111 parens++;
3112 }
3113
3114 for (s = p+1; s < end; s++) {
3115 switch(*s) {
3116 case 'x':
3117 local_extend = invert ? -1 : 1;
3118 break;
3119 case '-':
3120 invert = 1;
3121 break;
3122 case ':':
3123 case ')':
3124 if (local_extend == 0 ||
3125 (local_extend == -1 && !extended_mode) ||
3126 (local_extend == 1 && extended_mode)) {
3127 /* no changes to extended flag */
3128 goto fallthrough;
3129 }
3130
3131 if (*s == ':') {
3132 /* change extended flag until ')' */
3133 int local_options = options;
3134 if (local_extend == 1) {
3135 local_options |= ONIG_OPTION_EXTEND;
3136 }
3137 else {
3138 local_options &= ~ONIG_OPTION_EXTEND;
3139 }
3140
3141 rb_str_buf_cat(buf, (char *)&c, 1);
3142 int ret = unescape_nonascii0(&p, end, enc, buf, encp,
3143 has_property, err,
3144 local_options, 1);
3145 if (ret < 0) return ret;
3146 goto begin_scan;
3147 }
3148 else {
3149 /* change extended flag for rest of expression */
3150 extended_mode = local_extend == 1;
3151 goto fallthrough;
3152 }
3153 case 'i':
3154 case 'm':
3155 case 'a':
3156 case 'd':
3157 case 'u':
3158 /* other option flags, ignored during scanning */
3159 break;
3160 default:
3161 /* other character, no extended flag change*/
3162 goto fallthrough;
3163 }
3164 }
3165 }
3166 }
3167 else if (!in_char_class && recurse) {
3168 parens++;
3169 }
3170 /* FALLTHROUGH */
3171 default:
3172fallthrough:
3173 rb_str_buf_cat(buf, (char *)&c, 1);
3174 break;
3175 }
3176 }
3177
3178 if (recurse) {
3179 *pp = p;
3180 }
3181 return 0;
3182}
3183
3184static int
3185unescape_nonascii(const char *p, const char *end, rb_encoding *enc,
3186 VALUE buf, rb_encoding **encp, int *has_property,
3187 onig_errmsg_buffer err, int options)
3188{
3189 return unescape_nonascii0(&p, end, enc, buf, encp, has_property,
3190 err, options, 0);
3191}
3192
3193static VALUE
3194rb_reg_preprocess(const char *p, const char *end, rb_encoding *enc,
3195 rb_encoding **fixed_enc, onig_errmsg_buffer err, int options)
3196{
3197 VALUE buf;
3198 int has_property = 0;
3199
3200 buf = rb_str_buf_new(0);
3201
3202 if (rb_enc_asciicompat(enc))
3203 *fixed_enc = 0;
3204 else {
3205 *fixed_enc = enc;
3206 rb_enc_associate(buf, enc);
3207 }
3208
3209 if (unescape_nonascii(p, end, enc, buf, fixed_enc, &has_property, err, options) != 0)
3210 return Qnil;
3211
3212 if (has_property && !*fixed_enc) {
3213 *fixed_enc = enc;
3214 }
3215
3216 if (*fixed_enc) {
3217 rb_enc_associate(buf, *fixed_enc);
3218 }
3219
3220 return buf;
3221}
3222
3223VALUE
3224rb_reg_check_preprocess(VALUE str)
3225{
3226 rb_encoding *fixed_enc = 0;
3227 onig_errmsg_buffer err = "";
3228 VALUE buf;
3229 char *p, *end;
3230 rb_encoding *enc;
3231
3232 StringValue(str);
3233 p = RSTRING_PTR(str);
3234 end = p + RSTRING_LEN(str);
3235 enc = rb_enc_get(str);
3236
3237 buf = rb_reg_preprocess(p, end, enc, &fixed_enc, err, 0);
3238 RB_GC_GUARD(str);
3239
3240 if (NIL_P(buf)) {
3241 return rb_reg_error_desc(str, 0, err);
3242 }
3243 return Qnil;
3244}
3245
3246static VALUE
3247rb_reg_preprocess_dregexp(VALUE ary, int options)
3248{
3249 rb_encoding *fixed_enc = 0;
3250 rb_encoding *regexp_enc = 0;
3251 onig_errmsg_buffer err = "";
3252 int i;
3253 VALUE result = 0;
3254 rb_encoding *ascii8bit = rb_ascii8bit_encoding();
3255
3256 if (RARRAY_LEN(ary) == 0) {
3257 rb_raise(rb_eArgError, "no arguments given");
3258 }
3259
3260 for (i = 0; i < RARRAY_LEN(ary); i++) {
3261 VALUE str = RARRAY_AREF(ary, i);
3262 VALUE buf;
3263 char *p, *end;
3264 rb_encoding *src_enc;
3265
3266 src_enc = rb_enc_get(str);
3267 if (options & ARG_ENCODING_NONE &&
3268 src_enc != ascii8bit) {
3269 if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT)
3270 rb_raise(rb_eRegexpError, "/.../n has a non escaped non ASCII character in non ASCII-8BIT script");
3271 else
3272 src_enc = ascii8bit;
3273 }
3274
3275 StringValue(str);
3276 p = RSTRING_PTR(str);
3277 end = p + RSTRING_LEN(str);
3278
3279 buf = rb_reg_preprocess(p, end, src_enc, &fixed_enc, err, options);
3280
3281 if (NIL_P(buf))
3282 rb_raise(rb_eArgError, "%s", err);
3283
3284 if (fixed_enc != 0) {
3285 if (regexp_enc != 0 && regexp_enc != fixed_enc) {
3286 rb_raise(rb_eRegexpError, "encoding mismatch in dynamic regexp : %s and %s",
3287 rb_enc_name(regexp_enc), rb_enc_name(fixed_enc));
3288 }
3289 regexp_enc = fixed_enc;
3290 }
3291
3292 if (!result)
3293 result = rb_str_new3(str);
3294 else
3295 rb_str_buf_append(result, str);
3296 }
3297 if (regexp_enc) {
3298 rb_enc_associate(result, regexp_enc);
3299 }
3300
3301 return result;
3302}
3303
3304static void
3305rb_reg_initialize_check(VALUE obj)
3306{
3307 rb_check_frozen(obj);
3308 if (RREGEXP_PTR(obj)) {
3309 rb_raise(rb_eTypeError, "already initialized regexp");
3310 }
3311}
3312
3313static int
3314rb_reg_initialize(VALUE obj, const char *s, long len, rb_encoding *enc,
3315 int options, onig_errmsg_buffer err,
3316 const char *sourcefile, int sourceline)
3317{
3318 struct RRegexp *re = RREGEXP(obj);
3319 VALUE unescaped;
3320 rb_encoding *fixed_enc = 0;
3321 rb_encoding *a_enc = rb_ascii8bit_encoding();
3322
3323 rb_reg_initialize_check(obj);
3324
3325 if (rb_enc_dummy_p(enc)) {
3326 errcpy(err, "can't make regexp with dummy encoding");
3327 return -1;
3328 }
3329
3330 unescaped = rb_reg_preprocess(s, s+len, enc, &fixed_enc, err, options);
3331 if (NIL_P(unescaped))
3332 return -1;
3333
3334 if (fixed_enc) {
3335 if ((fixed_enc != enc && (options & ARG_ENCODING_FIXED)) ||
3336 (fixed_enc != a_enc && (options & ARG_ENCODING_NONE))) {
3337 errcpy(err, "incompatible character encoding");
3338 return -1;
3339 }
3340 if (fixed_enc != a_enc) {
3341 options |= ARG_ENCODING_FIXED;
3342 enc = fixed_enc;
3343 }
3344 }
3345 else if (!(options & ARG_ENCODING_FIXED)) {
3346 enc = rb_usascii_encoding();
3347 }
3348
3349 rb_enc_associate((VALUE)re, enc);
3350 if ((options & ARG_ENCODING_FIXED) || fixed_enc) {
3351 re->basic.flags |= KCODE_FIXED;
3352 }
3353 if (options & ARG_ENCODING_NONE) {
3354 re->basic.flags |= REG_ENCODING_NONE;
3355 }
3356
3357 re->ptr = make_regexp(RSTRING_PTR(unescaped), RSTRING_LEN(unescaped), enc,
3358 options & ARG_REG_OPTION_MASK, err,
3359 sourcefile, sourceline);
3360 if (!re->ptr) return -1;
3361 if (RBASIC_CLASS(obj) == rb_cRegexp) {
3362 OBJ_FREEZE(obj);
3363 }
3364 RB_GC_GUARD(unescaped);
3365 return 0;
3366}
3367
3368static void
3369reg_set_source(VALUE reg, VALUE str, rb_encoding *enc)
3370{
3371 rb_encoding *regenc = rb_enc_get(reg);
3372
3373 if (regenc != enc) {
3374 VALUE dup = rb_str_dup(str);
3375 str = rb_enc_associate(dup, enc = regenc);
3376 }
3377 str = rb_fstring(str);
3378 RB_OBJ_WRITE(reg, &RREGEXP(reg)->src, str);
3379}
3380
3381static int
3382rb_reg_initialize_str(VALUE obj, VALUE str, int options, onig_errmsg_buffer err,
3383 const char *sourcefile, int sourceline)
3384{
3385 int ret;
3386 rb_encoding *str_enc = rb_enc_get(str), *enc = str_enc;
3387 if (options & ARG_ENCODING_NONE) {
3388 rb_encoding *ascii8bit = rb_ascii8bit_encoding();
3389 if (enc != ascii8bit) {
3390 if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) {
3391 errcpy(err, "/.../n has a non escaped non ASCII character in non ASCII-8BIT script");
3392 return -1;
3393 }
3394 enc = ascii8bit;
3395 }
3396 }
3397 ret = rb_reg_initialize(obj, RSTRING_PTR(str), RSTRING_LEN(str), enc,
3398 options, err, sourcefile, sourceline);
3399 if (ret == 0) reg_set_source(obj, str, str_enc);
3400 return ret;
3401}
3402
3403VALUE
3404rb_reg_s_alloc(VALUE klass)
3405{
3406 NEWOBJ_OF(re, struct RRegexp, klass, T_REGEXP | (RGENGC_WB_PROTECTED_REGEXP ? FL_WB_PROTECTED : 0), sizeof(struct RRegexp), 0);
3407
3408 re->ptr = 0;
3409 RB_OBJ_WRITE(re, &re->src, 0);
3410 re->usecnt = 0;
3411
3412 return (VALUE)re;
3413}
3414
3415VALUE
3416rb_reg_alloc(void)
3417{
3418 return rb_reg_s_alloc(rb_cRegexp);
3419}
3420
3421VALUE
3422rb_reg_new_str(VALUE s, int options)
3423{
3424 return rb_reg_init_str(rb_reg_alloc(), s, options);
3425}
3426
3427VALUE
3428rb_reg_init_str(VALUE re, VALUE s, int options)
3429{
3430 onig_errmsg_buffer err = "";
3431
3432 if (rb_reg_initialize_str(re, s, options, err, NULL, 0) != 0) {
3433 rb_reg_raise_str(s, options, err);
3434 }
3435
3436 return re;
3437}
3438
3439static VALUE
3440rb_reg_init_str_enc(VALUE re, VALUE s, rb_encoding *enc, int options)
3441{
3442 onig_errmsg_buffer err = "";
3443
3444 if (rb_reg_initialize(re, RSTRING_PTR(s), RSTRING_LEN(s),
3445 enc, options, err, NULL, 0) != 0) {
3446 rb_reg_raise_str(s, options, err);
3447 }
3448 reg_set_source(re, s, enc);
3449
3450 return re;
3451}
3452
3453VALUE
3454rb_reg_new_ary(VALUE ary, int opt)
3455{
3456 return rb_reg_new_str(rb_reg_preprocess_dregexp(ary, opt), opt);
3457}
3458
3459VALUE
3460rb_enc_reg_new(const char *s, long len, rb_encoding *enc, int options)
3461{
3462 VALUE re = rb_reg_alloc();
3463 onig_errmsg_buffer err = "";
3464
3465 if (rb_reg_initialize(re, s, len, enc, options, err, NULL, 0) != 0) {
3466 rb_enc_reg_raise(s, len, enc, options, err);
3467 }
3468 RB_OBJ_WRITE(re, &RREGEXP(re)->src, rb_fstring(rb_enc_str_new(s, len, enc)));
3469
3470 return re;
3471}
3472
3473VALUE
3474rb_reg_new(const char *s, long len, int options)
3475{
3476 return rb_enc_reg_new(s, len, rb_ascii8bit_encoding(), options);
3477}
3478
3479VALUE
3480rb_reg_compile(VALUE str, int options, const char *sourcefile, int sourceline)
3481{
3482 VALUE re = rb_reg_alloc();
3483 onig_errmsg_buffer err = "";
3484
3485 if (!str) str = rb_str_new(0,0);
3486 if (rb_reg_initialize_str(re, str, options, err, sourcefile, sourceline) != 0) {
3487 rb_set_errinfo(rb_reg_error_desc(str, options, err));
3488 return Qnil;
3489 }
3490 return re;
3491}
3492
3493static VALUE reg_cache;
3494
3495VALUE
3497{
3498 if (rb_ractor_main_p()) {
3499 if (reg_cache && RREGEXP_SRC_LEN(reg_cache) == RSTRING_LEN(str)
3500 && ENCODING_GET(reg_cache) == ENCODING_GET(str)
3501 && memcmp(RREGEXP_SRC_PTR(reg_cache), RSTRING_PTR(str), RSTRING_LEN(str)) == 0)
3502 return reg_cache;
3503
3504 return reg_cache = rb_reg_new_str(str, 0);
3505 }
3506 else {
3507 return rb_reg_new_str(str, 0);
3508 }
3509}
3510
3511static st_index_t reg_hash(VALUE re);
3512/*
3513 * call-seq:
3514 * hash -> integer
3515 *
3516 * Returns the integer hash value for +self+.
3517 *
3518 * Related: Object#hash.
3519 *
3520 */
3521
3522VALUE
3523rb_reg_hash(VALUE re)
3524{
3525 st_index_t hashval = reg_hash(re);
3526 return ST2FIX(hashval);
3527}
3528
3529static st_index_t
3530reg_hash(VALUE re)
3531{
3532 st_index_t hashval;
3533
3534 rb_reg_check(re);
3535 hashval = RREGEXP_PTR(re)->options;
3536 hashval = rb_hash_uint(hashval, rb_memhash(RREGEXP_SRC_PTR(re), RREGEXP_SRC_LEN(re)));
3537 return rb_hash_end(hashval);
3538}
3539
3540
3541/*
3542 * call-seq:
3543 * self == other -> true or false
3544 *
3545 * Returns whether +other+ is another \Regexp whose pattern,
3546 * flags, and encoding are the same as +self+:
3547 *
3548 * /foo/ == Regexp.new('foo') # => true
3549 * /foo/ == /foo/i # => false
3550 * /foo/ == Regexp.new('food') # => false
3551 * /foo/ == Regexp.new("abc".force_encoding("euc-jp")) # => false
3552 *
3553 */
3554
3555VALUE
3556rb_reg_equal(VALUE re1, VALUE re2)
3557{
3558 if (re1 == re2) return Qtrue;
3559 if (!RB_TYPE_P(re2, T_REGEXP)) return Qfalse;
3560 rb_reg_check(re1); rb_reg_check(re2);
3561 if (FL_TEST(re1, KCODE_FIXED) != FL_TEST(re2, KCODE_FIXED)) return Qfalse;
3562 if (RREGEXP_PTR(re1)->options != RREGEXP_PTR(re2)->options) return Qfalse;
3563 if (RREGEXP_SRC_LEN(re1) != RREGEXP_SRC_LEN(re2)) return Qfalse;
3564 if (ENCODING_GET(re1) != ENCODING_GET(re2)) return Qfalse;
3565 return RBOOL(memcmp(RREGEXP_SRC_PTR(re1), RREGEXP_SRC_PTR(re2), RREGEXP_SRC_LEN(re1)) == 0);
3566}
3567
3568/*
3569 * call-seq:
3570 * hash -> integer
3571 *
3572 * Returns the integer hash value for +self+,
3573 * based on the target string, regexp, match, and captures.
3574 *
3575 * See also Object#hash.
3576 *
3577 */
3578
3579static VALUE
3580match_hash(VALUE match)
3581{
3582 const struct re_registers *regs;
3583 st_index_t hashval;
3584
3585 match_check(match);
3586 hashval = rb_hash_start(rb_str_hash(RMATCH(match)->str));
3587 hashval = rb_hash_uint(hashval, reg_hash(match_regexp(match)));
3588 regs = RMATCH_REGS(match);
3589 hashval = rb_hash_uint(hashval, regs->num_regs);
3590 hashval = rb_hash_uint(hashval, rb_memhash(regs->beg, regs->num_regs * sizeof(*regs->beg)));
3591 hashval = rb_hash_uint(hashval, rb_memhash(regs->end, regs->num_regs * sizeof(*regs->end)));
3592 hashval = rb_hash_end(hashval);
3593 return ST2FIX(hashval);
3594}
3595
3596/*
3597 * call-seq:
3598 * self == other -> true or false
3599 *
3600 * Returns whether +other+ is another \MatchData object
3601 * whose target string, regexp, match, and captures
3602 * are the same as +self+.
3603 */
3604
3605static VALUE
3606match_equal(VALUE match1, VALUE match2)
3607{
3608 const struct re_registers *regs1, *regs2;
3609
3610 if (match1 == match2) return Qtrue;
3611 if (!RB_TYPE_P(match2, T_MATCH)) return Qfalse;
3612 if (!RMATCH(match1)->regexp || !RMATCH(match2)->regexp) return Qfalse;
3613 if (!rb_str_equal(RMATCH(match1)->str, RMATCH(match2)->str)) return Qfalse;
3614 if (!rb_reg_equal(match_regexp(match1), match_regexp(match2))) return Qfalse;
3615 regs1 = RMATCH_REGS(match1);
3616 regs2 = RMATCH_REGS(match2);
3617 if (regs1->num_regs != regs2->num_regs) return Qfalse;
3618 if (memcmp(regs1->beg, regs2->beg, regs1->num_regs * sizeof(*regs1->beg))) return Qfalse;
3619 if (memcmp(regs1->end, regs2->end, regs1->num_regs * sizeof(*regs1->end))) return Qfalse;
3620 return Qtrue;
3621}
3622
3623/*
3624 * call-seq:
3625 * integer_at(index, base = 10) -> integer or nil
3626 * integer_at(name, base = 10) -> integer or nil
3627 *
3628 * Converts the matched substring to integer and return the result.
3629 * +$~.integer_at(N)+ is equivalent to +$N&.to_i+.
3630 *
3631 * m = /(\d+{4})(\d+{2})(\d+{2})/.match("20260308")
3632 * # => #<MatchData "20260308" 1:"2026" 2:"03" 3:"08">
3633 * m.integer_at(0) # => 20260308
3634 * m.integer_at(1) # => 2026
3635 * m.integer_at(2) # => 3
3636 * m.integer_at(3) # => 8
3637 *
3638 * m = /(?<y>\d+{4})(?<m>\d+{2})(?<d>\d+{2})/.match("20260308")
3639 * m.integer_at("y") # => 2026
3640 * m.integer_at("m") # => 3
3641 * m.integer_at("d") # => 8
3642 *
3643 * If the substring does not match, returns +nil+.
3644 *
3645 * re = /(\d+)?/
3646 * re.match("123").integer_at(1) #=> 123
3647 * re.match("abc").integer_at(1) #=> nil
3648 *
3649 * The string is converted in decimal by default.
3650 *
3651 * /\d+/.match("011").integer_at(0) #=> 10
3652 * /\d+/.match("011").integer_at(0, 12) #=> 13
3653 * /\d+/.match("011").integer_at(0, 0) #=> 9
3654 *
3655 * See also MatchData#[], String#to_i.
3656 */
3657static VALUE
3658match_integer_at(int argc, VALUE *argv, VALUE match)
3659{
3660 const struct re_registers *regs = RMATCH_REGS(match_check(match));
3661
3662 int base = 10;
3663 VALUE idx;
3664 long nth;
3665
3666 argc = rb_check_arity(argc, 1, 2);
3667 if (FIXNUM_P(idx = argv[0])) {
3668 nth = NUM2INT(idx);
3669 }
3670 else if ((nth = namev_to_backref_number(regs, RMATCH(match)->regexp, idx)) < 0) {
3671 name_to_backref_error(idx);
3672 }
3673
3674 if (argc > 1 && (base = NUM2INT(argv[1])) < 0) {
3675 rb_raise(rb_eArgError, "invalid radix %d", base);
3676 }
3677
3678 if (nth >= regs->num_regs) return Qnil;
3679 if (nth < 0 && (nth += regs->num_regs) <= 0) return Qnil;
3680
3681 long start = BEG(nth), end = END(nth);
3682 if (start < 0) return Qnil;
3683 RUBY_ASSERT(start <= end, "%ld > %ld", start, end);
3684
3685 VALUE str = RMATCH(match)->str;
3686 RUBY_ASSERT(end <= RSTRING_LEN(str), "%ld > %ld", end, RSTRING_LEN(str));
3687
3688 char *endp;
3689 return rb_int_parse_cstr(RSTRING_PTR(str) + start, end - start, &endp, NULL,
3690 base, RB_INT_PARSE_DEFAULT);
3691}
3692
3693static VALUE
3694reg_operand(VALUE s, int check)
3695{
3696 if (SYMBOL_P(s)) {
3697 return rb_sym2str(s);
3698 }
3699 else if (RB_TYPE_P(s, T_STRING)) {
3700 return s;
3701 }
3702 else {
3703 return check ? rb_str_to_str(s) : rb_check_string_type(s);
3704 }
3705}
3706
3707static long
3708reg_match_pos(VALUE re, VALUE *strp, long pos, VALUE* set_match)
3709{
3710 VALUE str = *strp;
3711
3712 if (NIL_P(str)) {
3713 rb_backref_set(Qnil);
3714 return -1;
3715 }
3716 *strp = str = reg_operand(str, TRUE);
3717 if (pos != 0) {
3718 if (pos < 0) {
3719 VALUE l = rb_str_length(str);
3720 pos += NUM2INT(l);
3721 if (pos < 0) {
3722 return pos;
3723 }
3724 }
3725 pos = rb_str_offset(str, pos);
3726 }
3727 return rb_reg_search_set_match(re, str, pos, 0, 1, set_match);
3728}
3729
3730/*
3731 * call-seq:
3732 * self =~ other -> integer or nil
3733 *
3734 * Returns the integer index (in characters) of the first match
3735 * for +self+ and +other+, or +nil+ if none;
3736 * updates {Regexp-related global variables}[rdoc-ref:Regexp@Global+Variables].
3737 *
3738 * /at/ =~ 'input data' # => 7
3739 * $~ # => #<MatchData "at">
3740 * /ax/ =~ 'input data' # => nil
3741 * $~ # => nil
3742 *
3743 * Assigns named captures to local variables of the same names
3744 * if and only if +self+:
3745 *
3746 * - Is a regexp literal;
3747 * see {Regexp Literals}[rdoc-ref:syntax/literals.rdoc@Regexp+Literals].
3748 * - Does not contain interpolations;
3749 * see {Regexp interpolation}[rdoc-ref:Regexp@Interpolation+Mode].
3750 * - Is at the left of the expression.
3751 *
3752 * Example:
3753 *
3754 * /(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/ =~ ' x = y '
3755 * p lhs # => "x"
3756 * p rhs # => "y"
3757 *
3758 * Assigns +nil+ if not matched:
3759 *
3760 * /(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/ =~ ' x = '
3761 * p lhs # => nil
3762 * p rhs # => nil
3763 *
3764 * Does not make local variable assignments if +self+ is not a regexp literal:
3765 *
3766 * r = /(?<foo>\w+)\s*=\s*(?<foo>\w+)/
3767 * r =~ ' x = y '
3768 * p foo # Undefined local variable
3769 * p bar # Undefined local variable
3770 *
3771 * The assignment does not occur if the regexp is not at the left:
3772 *
3773 * ' x = y ' =~ /(?<foo>\w+)\s*=\s*(?<foo>\w+)/
3774 * p foo, foo # Undefined local variables
3775 *
3776 * A regexp interpolation, <tt>#{}</tt>, also disables
3777 * the assignment:
3778 *
3779 * r = /(?<foo>\w+)/
3780 * /(?<foo>\w+)\s*=\s*#{r}/ =~ 'x = y'
3781 * p foo # Undefined local variable
3782 *
3783 */
3784
3785VALUE
3786rb_reg_match(VALUE re, VALUE str)
3787{
3788 long pos = reg_match_pos(re, &str, 0, NULL);
3789 if (pos < 0) return Qnil;
3790 pos = rb_str_sublen(str, pos);
3791 return LONG2FIX(pos);
3792}
3793
3794/*
3795 * call-seq:
3796 * self === other -> true or false
3797 *
3798 * Returns whether +self+ finds a match in +other+:
3799 *
3800 * /^[a-z]*$/ === 'HELLO' # => false
3801 * /^[A-Z]*$/ === 'HELLO' # => true
3802 *
3803 * This method is called in case statements:
3804 *
3805 * s = 'HELLO'
3806 * case s
3807 * when /\A[a-z]*\z/; print "Lower case\n"
3808 * when /\A[A-Z]*\z/; print "Upper case\n"
3809 * else print "Mixed case\n"
3810 * end # => "Upper case"
3811 *
3812 */
3813
3814static VALUE
3815rb_reg_eqq(VALUE re, VALUE str)
3816{
3817 long start;
3818
3819 str = reg_operand(str, FALSE);
3820 if (NIL_P(str)) {
3821 rb_backref_set(Qnil);
3822 return Qfalse;
3823 }
3824 start = rb_reg_search(re, str, 0, 0);
3825 return RBOOL(start >= 0);
3826}
3827
3828
3829/*
3830 * call-seq:
3831 * ~ rxp -> integer or nil
3832 *
3833 * Equivalent to <tt><i>rxp</i> =~ $_</tt>:
3834 *
3835 * $_ = "input data"
3836 * ~ /at/ # => 7
3837 *
3838 */
3839
3840VALUE
3841rb_reg_match2(VALUE re)
3842{
3843 long start;
3844 VALUE line = rb_lastline_get();
3845
3846 if (!RB_TYPE_P(line, T_STRING)) {
3847 rb_backref_set(Qnil);
3848 return Qnil;
3849 }
3850
3851 start = rb_reg_search(re, line, 0, 0);
3852 if (start < 0) {
3853 return Qnil;
3854 }
3855 start = rb_str_sublen(line, start);
3856 return LONG2FIX(start);
3857}
3858
3859
3860/*
3861 * call-seq:
3862 * match(string, offset = 0) -> matchdata or nil
3863 * match(string, offset = 0) {|matchdata| ... } -> object
3864 *
3865 * With no block given, returns the MatchData object
3866 * that describes the match, if any, or +nil+ if none;
3867 * the search begins at the given character +offset+ in +string+:
3868 *
3869 * /abra/.match('abracadabra') # => #<MatchData "abra">
3870 * /abra/.match('abracadabra', 4) # => #<MatchData "abra">
3871 * /abra/.match('abracadabra', 8) # => nil
3872 * /abra/.match('abracadabra', 800) # => nil
3873 *
3874 * string = "\u{5d0 5d1 5e8 5d0}cadabra"
3875 * /abra/.match(string, 7) #=> #<MatchData "abra">
3876 * /abra/.match(string, 8) #=> nil
3877 * /abra/.match(string.b, 8) #=> #<MatchData "abra">
3878 *
3879 * With a block given, calls the block if and only if a match is found;
3880 * returns the block's value:
3881 *
3882 * /abra/.match('abracadabra') {|matchdata| p matchdata }
3883 * # => #<MatchData "abra">
3884 * /abra/.match('abracadabra', 4) {|matchdata| p matchdata }
3885 * # => #<MatchData "abra">
3886 * /abra/.match('abracadabra', 8) {|matchdata| p matchdata }
3887 * # => nil
3888 * /abra/.match('abracadabra', 8) {|marchdata| fail 'Cannot happen' }
3889 * # => nil
3890 *
3891 * Output (from the first two blocks above):
3892 *
3893 * #<MatchData "abra">
3894 * #<MatchData "abra">
3895 *
3896 * /(.)(.)(.)/.match("abc")[2] # => "b"
3897 * /(.)(.)/.match("abc", 1)[2] # => "c"
3898 *
3899 */
3900
3901static VALUE
3902rb_reg_match_m(int argc, VALUE *argv, VALUE re)
3903{
3904 VALUE result = Qnil, str, initpos;
3905 long pos;
3906
3907 if (rb_scan_args(argc, argv, "11", &str, &initpos) == 2) {
3908 pos = NUM2LONG(initpos);
3909 }
3910 else {
3911 pos = 0;
3912 }
3913
3914 pos = reg_match_pos(re, &str, pos, &result);
3915 if (pos < 0) {
3916 rb_backref_set(Qnil);
3917 return Qnil;
3918 }
3919 rb_match_busy(result);
3920 if (!NIL_P(result) && rb_block_given_p()) {
3921 return rb_yield(result);
3922 }
3923 return result;
3924}
3925
3926/*
3927 * call-seq:
3928 * match?(string) -> true or false
3929 * match?(string, offset = 0) -> true or false
3930 *
3931 * Returns <code>true</code> or <code>false</code> to indicate whether the
3932 * regexp is matched or not without updating $~ and other related variables.
3933 * If the second parameter is present, it specifies the position in the string
3934 * to begin the search.
3935 *
3936 * /R.../.match?("Ruby") # => true
3937 * /R.../.match?("Ruby", 1) # => false
3938 * /P.../.match?("Ruby") # => false
3939 * $& # => nil
3940 */
3941
3942static VALUE
3943rb_reg_match_m_p(int argc, VALUE *argv, VALUE re)
3944{
3945 long pos = rb_check_arity(argc, 1, 2) > 1 ? NUM2LONG(argv[1]) : 0;
3946 return rb_reg_match_p(re, argv[0], pos);
3947}
3948
3949VALUE
3950rb_reg_match_p(VALUE re, VALUE str, long pos)
3951{
3952 if (NIL_P(str)) return Qfalse;
3953 str = SYMBOL_P(str) ? rb_sym2str(str) : StringValue(str);
3954 if (pos) {
3955 if (pos < 0) {
3956 pos += NUM2LONG(rb_str_length(str));
3957 if (pos < 0) return Qfalse;
3958 }
3959 if (pos > 0) {
3960 long len = 1;
3961 const char *beg = rb_str_subpos(str, pos, &len);
3962 if (!beg) return Qfalse;
3963 pos = beg - RSTRING_PTR(str);
3964 }
3965 }
3966
3967 struct reg_onig_search_args args = {
3968 .pos = pos,
3969 .range = RSTRING_LEN(str),
3970 };
3971
3972 return rb_reg_onig_match(re, str, reg_onig_search, &args, NULL) == ONIG_MISMATCH ? Qfalse : Qtrue;
3973}
3974
3975/*
3976 * Document-method: compile
3977 *
3978 * Alias for Regexp.new
3979 */
3980
3981static int
3982str_to_option(VALUE str)
3983{
3984 int flag = 0;
3985 const char *ptr;
3986 long len;
3987 str = rb_check_string_type(str);
3988 if (NIL_P(str)) return -1;
3989 RSTRING_GETMEM(str, ptr, len);
3990 for (long i = 0; i < len; ++i) {
3991 int f = char_to_option(ptr[i]);
3992 if (!f) {
3993 rb_raise(rb_eArgError, "unknown regexp option: %"PRIsVALUE, str);
3994 }
3995 flag |= f;
3996 }
3997 return flag;
3998}
3999
4000static void
4001set_timeout(rb_hrtime_t *hrt, VALUE timeout)
4002{
4003 double timeout_d = NIL_P(timeout) ? 0.0 : NUM2DBL(timeout);
4004 if (!NIL_P(timeout) && timeout_d <= 0) {
4005 rb_raise(rb_eArgError, "invalid timeout: %"PRIsVALUE, timeout);
4006 }
4007 double2hrtime(hrt, timeout_d);
4008}
4009
4010static VALUE
4011reg_copy(VALUE copy, VALUE orig)
4012{
4013 int r;
4014 regex_t *re;
4015
4016 rb_reg_initialize_check(copy);
4017 if ((r = onig_reg_copy(&re, RREGEXP_PTR(orig))) != 0) {
4018 /* ONIGERR_MEMORY only */
4019 rb_raise(rb_eRegexpError, "%s", onig_error_code_to_format(r));
4020 }
4021 RREGEXP_PTR(copy) = re;
4022 RB_OBJ_WRITE(copy, &RREGEXP(copy)->src, RREGEXP(orig)->src);
4023 RREGEXP_PTR(copy)->timelimit = RREGEXP_PTR(orig)->timelimit;
4024 rb_enc_copy(copy, orig);
4025 FL_SET_RAW(copy, FL_TEST_RAW(orig, KCODE_FIXED|REG_ENCODING_NONE));
4026 if (RBASIC_CLASS(copy) == rb_cRegexp) {
4027 OBJ_FREEZE(copy);
4028 }
4029
4030 return copy;
4031}
4032
4033struct reg_init_args {
4034 VALUE str;
4035 VALUE timeout;
4036 rb_encoding *enc;
4037 int flags;
4038};
4039
4040static VALUE reg_extract_args(int argc, VALUE *argv, struct reg_init_args *args);
4041static VALUE reg_init_args(VALUE self, VALUE str, rb_encoding *enc, int flags);
4042
4043/*
4044 * call-seq:
4045 * Regexp.new(string, options = 0, timeout: nil) -> regexp
4046 * Regexp.new(regexp, timeout: nil) -> regexp
4047 *
4048 * With argument +string+ given, returns a new regexp with the given string
4049 * and options:
4050 *
4051 * r = Regexp.new('foo') # => /foo/
4052 * r.source # => "foo"
4053 * r.options # => 0
4054 *
4055 * Optional argument +options+ is one of the following:
4056 *
4057 * - A String of options:
4058 *
4059 * Regexp.new('foo', 'i') # => /foo/i
4060 * Regexp.new('foo', 'im') # => /foo/im
4061 *
4062 * - The bit-wise OR of one or more of the constants
4063 * Regexp::EXTENDED, Regexp::IGNORECASE, Regexp::MULTILINE, and
4064 * Regexp::NOENCODING:
4065 *
4066 * Regexp.new('foo', Regexp::IGNORECASE) # => /foo/i
4067 * Regexp.new('foo', Regexp::EXTENDED) # => /foo/x
4068 * Regexp.new('foo', Regexp::MULTILINE) # => /foo/m
4069 * Regexp.new('foo', Regexp::NOENCODING) # => /foo/n
4070 * flags = Regexp::IGNORECASE | Regexp::EXTENDED | Regexp::MULTILINE
4071 * Regexp.new('foo', flags) # => /foo/mix
4072 *
4073 * - +nil+ or +false+, which is ignored.
4074 * - Any other truthy value, in which case the regexp will be
4075 * case-insensitive.
4076 *
4077 * If optional keyword argument +timeout+ is given,
4078 * its float value overrides the timeout interval for the class,
4079 * Regexp.timeout.
4080 * If +nil+ is passed as +timeout, it uses the timeout interval
4081 * for the class, Regexp.timeout.
4082 *
4083 * With argument +regexp+ given, returns a new regexp. The source,
4084 * options, timeout are the same as +regexp+. +options+ and +n_flag+
4085 * arguments are ineffective. The timeout can be overridden by
4086 * +timeout+ keyword.
4087 *
4088 * options = Regexp::MULTILINE
4089 * r = Regexp.new('foo', options, timeout: 1.1) # => /foo/m
4090 * r2 = Regexp.new(r) # => /foo/m
4091 * r2.timeout # => 1.1
4092 * r3 = Regexp.new(r, timeout: 3.14) # => /foo/m
4093 * r3.timeout # => 3.14
4094 *
4095 */
4096
4097static VALUE
4098rb_reg_initialize_m(int argc, VALUE *argv, VALUE self)
4099{
4100 struct reg_init_args args;
4101 VALUE re = reg_extract_args(argc, argv, &args);
4102
4103 if (NIL_P(re)) {
4104 reg_init_args(self, args.str, args.enc, args.flags);
4105 }
4106 else {
4107 reg_copy(self, re);
4108 }
4109
4110 set_timeout(&RREGEXP_PTR(self)->timelimit, args.timeout);
4111 if (RBASIC_CLASS(self) == rb_cRegexp) {
4112 OBJ_FREEZE(self);
4113 }
4114
4115 return self;
4116}
4117
4118static VALUE
4119reg_extract_args(int argc, VALUE *argv, struct reg_init_args *args)
4120{
4121 int flags = 0;
4122 rb_encoding *enc = 0;
4123 VALUE str, src, opts = Qundef, kwargs;
4124 VALUE re = Qnil;
4125
4126 rb_scan_args(argc, argv, "11:", &src, &opts, &kwargs);
4127
4128 args->timeout = Qnil;
4129 if (!NIL_P(kwargs)) {
4130 static ID keywords[1];
4131 if (!keywords[0]) {
4132 keywords[0] = rb_intern_const("timeout");
4133 }
4134 rb_get_kwargs(kwargs, keywords, 0, 1, &args->timeout);
4135 }
4136
4137 if (RB_TYPE_P(src, T_REGEXP)) {
4138 re = src;
4139
4140 if (!NIL_P(opts)) {
4141 rb_warn("flags ignored");
4142 }
4143 rb_reg_check(re);
4144 flags = rb_reg_options(re);
4145 str = RREGEXP_SRC(re);
4146 }
4147 else {
4148 if (!NIL_P(opts)) {
4149 int f;
4150 if (FIXNUM_P(opts)) flags = FIX2INT(opts);
4151 else if ((f = str_to_option(opts)) >= 0) flags = f;
4152 else if (rb_bool_expected(opts, "ignorecase", FALSE))
4153 flags = ONIG_OPTION_IGNORECASE;
4154 }
4155 str = StringValue(src);
4156 }
4157 args->str = str;
4158 args->enc = enc;
4159 args->flags = flags;
4160 return re;
4161}
4162
4163static VALUE
4164reg_init_args(VALUE self, VALUE str, rb_encoding *enc, int flags)
4165{
4166 if (enc && rb_enc_get(str) != enc)
4167 rb_reg_init_str_enc(self, str, enc, flags);
4168 else
4169 rb_reg_init_str(self, str, flags);
4170 return self;
4171}
4172
4173VALUE
4174rb_reg_quote(VALUE str)
4175{
4176 rb_encoding *enc = rb_enc_get(str);
4177 char *s, *send, *t;
4178 VALUE tmp;
4179 int c, clen;
4180 int ascii_only = rb_enc_str_asciionly_p(str);
4181
4182 s = RSTRING_PTR(str);
4183 send = s + RSTRING_LEN(str);
4184 while (s < send) {
4185 c = rb_enc_ascget(s, send, &clen, enc);
4186 if (c == -1) {
4187 s += mbclen(s, send, enc);
4188 continue;
4189 }
4190 switch (c) {
4191 case '[': case ']': case '{': case '}':
4192 case '(': case ')': case '|': case '-':
4193 case '*': case '.': case '\\':
4194 case '?': case '+': case '^': case '$':
4195 case ' ': case '#':
4196 case '\t': case '\f': case '\v': case '\n': case '\r':
4197 goto meta_found;
4198 }
4199 s += clen;
4200 }
4201 tmp = rb_str_new3(str);
4202 if (ascii_only) {
4203 rb_enc_associate(tmp, rb_usascii_encoding());
4204 }
4205 return tmp;
4206
4207 meta_found:
4208 tmp = rb_str_new(0, RSTRING_LEN(str)*2);
4209 if (ascii_only) {
4210 rb_enc_associate(tmp, rb_usascii_encoding());
4211 }
4212 else {
4213 rb_enc_copy(tmp, str);
4214 }
4215 t = RSTRING_PTR(tmp);
4216 /* copy upto metacharacter */
4217 const char *p = RSTRING_PTR(str);
4218 memcpy(t, p, s - p);
4219 t += s - p;
4220
4221 while (s < send) {
4222 c = rb_enc_ascget(s, send, &clen, enc);
4223 if (c == -1) {
4224 int n = mbclen(s, send, enc);
4225
4226 while (n--)
4227 *t++ = *s++;
4228 continue;
4229 }
4230 s += clen;
4231 switch (c) {
4232 case '[': case ']': case '{': case '}':
4233 case '(': case ')': case '|': case '-':
4234 case '*': case '.': case '\\':
4235 case '?': case '+': case '^': case '$':
4236 case '#':
4237 t += rb_enc_mbcput('\\', t, enc);
4238 break;
4239 case ' ':
4240 t += rb_enc_mbcput('\\', t, enc);
4241 t += rb_enc_mbcput(' ', t, enc);
4242 continue;
4243 case '\t':
4244 t += rb_enc_mbcput('\\', t, enc);
4245 t += rb_enc_mbcput('t', t, enc);
4246 continue;
4247 case '\n':
4248 t += rb_enc_mbcput('\\', t, enc);
4249 t += rb_enc_mbcput('n', t, enc);
4250 continue;
4251 case '\r':
4252 t += rb_enc_mbcput('\\', t, enc);
4253 t += rb_enc_mbcput('r', t, enc);
4254 continue;
4255 case '\f':
4256 t += rb_enc_mbcput('\\', t, enc);
4257 t += rb_enc_mbcput('f', t, enc);
4258 continue;
4259 case '\v':
4260 t += rb_enc_mbcput('\\', t, enc);
4261 t += rb_enc_mbcput('v', t, enc);
4262 continue;
4263 }
4264 t += rb_enc_mbcput(c, t, enc);
4265 }
4266 rb_str_resize(tmp, t - RSTRING_PTR(tmp));
4267 return tmp;
4268}
4269
4270
4271/*
4272 * call-seq:
4273 * Regexp.escape(string) -> new_string
4274 *
4275 * Returns a new string that escapes any characters
4276 * that have special meaning in a regular expression:
4277 *
4278 * s = Regexp.escape('\*?{}.') # => "\\\\\\*\\?\\{\\}\\."
4279 *
4280 * For any string +s+, this call returns a MatchData object:
4281 *
4282 * r = Regexp.new(Regexp.escape(s)) # => /\\\\\\\*\\\?\\\{\\\}\\\./
4283 * r.match(s) # => #<MatchData "\\\\\\*\\?\\{\\}\\.">
4284 *
4285 */
4286
4287static VALUE
4288rb_reg_s_quote(VALUE c, VALUE str)
4289{
4290 return rb_reg_quote(reg_operand(str, TRUE));
4291}
4292
4293int
4294rb_reg_options(VALUE re)
4295{
4296 int options;
4297
4298 rb_reg_check(re);
4299 options = RREGEXP_PTR(re)->options & ARG_REG_OPTION_MASK;
4300 if (RBASIC(re)->flags & KCODE_FIXED) options |= ARG_ENCODING_FIXED;
4301 if (RBASIC(re)->flags & REG_ENCODING_NONE) options |= ARG_ENCODING_NONE;
4302 return options;
4303}
4304
4305static VALUE
4306rb_check_regexp_type(VALUE re)
4307{
4308 return rb_check_convert_type(re, T_REGEXP, "Regexp", "to_regexp");
4309}
4310
4311/*
4312 * call-seq:
4313 * Regexp.try_convert(object) -> regexp or nil
4314 *
4315 * Returns +object+ if it is a regexp:
4316 *
4317 * Regexp.try_convert(/re/) # => /re/
4318 *
4319 * Otherwise if +object+ responds to <tt>:to_regexp</tt>,
4320 * calls <tt>object.to_regexp</tt> and returns the result.
4321 *
4322 * Returns +nil+ if +object+ does not respond to <tt>:to_regexp</tt>.
4323 *
4324 * Regexp.try_convert('re') # => nil
4325 *
4326 * Raises an exception unless <tt>object.to_regexp</tt> returns a regexp.
4327 *
4328 */
4329static VALUE
4330rb_reg_s_try_convert(VALUE dummy, VALUE re)
4331{
4332 return rb_check_regexp_type(re);
4333}
4334
4335static VALUE
4336rb_reg_s_union(VALUE self, VALUE args0)
4337{
4338 long argc = RARRAY_LEN(args0);
4339
4340 if (argc == 0) {
4341 VALUE args[1];
4342 args[0] = rb_str_new2("(?!)");
4343 return rb_class_new_instance(1, args, rb_cRegexp);
4344 }
4345 else if (argc == 1) {
4346 VALUE arg = rb_ary_entry(args0, 0);
4347 VALUE re = rb_check_regexp_type(arg);
4348 if (!NIL_P(re))
4349 return re;
4350 else {
4351 VALUE quoted;
4352 quoted = rb_reg_s_quote(Qnil, arg);
4353 return rb_reg_new_str(quoted, 0);
4354 }
4355 }
4356 else {
4357 int i;
4358 VALUE source = rb_str_buf_new(0);
4359 rb_encoding *result_enc;
4360
4361 int has_asciionly = 0;
4362 rb_encoding *has_ascii_compat_fixed = 0;
4363 rb_encoding *has_ascii_incompat = 0;
4364
4365 for (i = 0; i < argc; i++) {
4366 volatile VALUE v;
4367 VALUE e = rb_ary_entry(args0, i);
4368
4369 if (0 < i)
4370 rb_str_buf_cat_ascii(source, "|");
4371
4372 v = rb_check_regexp_type(e);
4373 if (!NIL_P(v)) {
4374 rb_encoding *enc = rb_enc_get(v);
4375 if (!rb_enc_asciicompat(enc)) {
4376 if (!has_ascii_incompat)
4377 has_ascii_incompat = enc;
4378 else if (has_ascii_incompat != enc)
4379 rb_raise(rb_eArgError, "incompatible encodings: %s and %s",
4380 rb_enc_name(has_ascii_incompat), rb_enc_name(enc));
4381 }
4382 else if (rb_reg_fixed_encoding_p(v)) {
4383 if (!has_ascii_compat_fixed)
4384 has_ascii_compat_fixed = enc;
4385 else if (has_ascii_compat_fixed != enc)
4386 rb_raise(rb_eArgError, "incompatible encodings: %s and %s",
4387 rb_enc_name(has_ascii_compat_fixed), rb_enc_name(enc));
4388 }
4389 else {
4390 has_asciionly = 1;
4391 }
4392 v = rb_reg_str_with_term(v, -1);
4393 }
4394 else {
4395 rb_encoding *enc;
4396 StringValue(e);
4397 enc = rb_enc_get(e);
4398 if (!rb_enc_asciicompat(enc)) {
4399 if (!has_ascii_incompat)
4400 has_ascii_incompat = enc;
4401 else if (has_ascii_incompat != enc)
4402 rb_raise(rb_eArgError, "incompatible encodings: %s and %s",
4403 rb_enc_name(has_ascii_incompat), rb_enc_name(enc));
4404 }
4405 else if (rb_enc_str_asciionly_p(e)) {
4406 has_asciionly = 1;
4407 }
4408 else {
4409 if (!has_ascii_compat_fixed)
4410 has_ascii_compat_fixed = enc;
4411 else if (has_ascii_compat_fixed != enc)
4412 rb_raise(rb_eArgError, "incompatible encodings: %s and %s",
4413 rb_enc_name(has_ascii_compat_fixed), rb_enc_name(enc));
4414 }
4415 v = rb_reg_s_quote(Qnil, e);
4416 }
4417 if (has_ascii_incompat) {
4418 if (has_asciionly) {
4419 rb_raise(rb_eArgError, "ASCII incompatible encoding: %s",
4420 rb_enc_name(has_ascii_incompat));
4421 }
4422 if (has_ascii_compat_fixed) {
4423 rb_raise(rb_eArgError, "incompatible encodings: %s and %s",
4424 rb_enc_name(has_ascii_incompat), rb_enc_name(has_ascii_compat_fixed));
4425 }
4426 }
4427
4428 if (i == 0) {
4429 rb_enc_copy(source, v);
4430 }
4431 rb_str_append(source, v);
4432 }
4433
4434 if (has_ascii_incompat) {
4435 result_enc = has_ascii_incompat;
4436 }
4437 else if (has_ascii_compat_fixed) {
4438 result_enc = has_ascii_compat_fixed;
4439 }
4440 else {
4441 result_enc = rb_ascii8bit_encoding();
4442 }
4443
4444 rb_enc_associate(source, result_enc);
4445 return rb_class_new_instance(1, &source, rb_cRegexp);
4446 }
4447}
4448
4449/*
4450 * call-seq:
4451 * Regexp.union(*patterns) -> regexp
4452 * Regexp.union(array_of_patterns) -> regexp
4453 *
4454 * Returns a new regexp that is the union of the given patterns:
4455 *
4456 * r = Regexp.union(%w[cat dog]) # => /cat|dog/
4457 * r.match('cat') # => #<MatchData "cat">
4458 * r.match('dog') # => #<MatchData "dog">
4459 * r.match('cog') # => nil
4460 *
4461 * For each pattern that is a string, <tt>Regexp.new(pattern)</tt> is used:
4462 *
4463 * Regexp.union('penzance') # => /penzance/
4464 * Regexp.union('a+b*c') # => /a\+b\*c/
4465 * Regexp.union('skiing', 'sledding') # => /skiing|sledding/
4466 * Regexp.union(['skiing', 'sledding']) # => /skiing|sledding/
4467 *
4468 * For each pattern that is a regexp, it is used as is,
4469 * including its flags:
4470 *
4471 * Regexp.union(/foo/i, /bar/m, /baz/x)
4472 * # => /(?i-mx:foo)|(?m-ix:bar)|(?x-mi:baz)/
4473 * Regexp.union([/foo/i, /bar/m, /baz/x])
4474 * # => /(?i-mx:foo)|(?m-ix:bar)|(?x-mi:baz)/
4475 *
4476 * With no arguments, returns <tt>/(?!)/</tt>:
4477 *
4478 * Regexp.union # => /(?!)/
4479 *
4480 * If any regexp pattern contains captures, the behavior is unspecified.
4481 *
4482 */
4483static VALUE
4484rb_reg_s_union_m(VALUE self, VALUE args)
4485{
4486 VALUE v;
4487 if (RARRAY_LEN(args) == 1 &&
4488 !NIL_P(v = rb_check_array_type(rb_ary_entry(args, 0)))) {
4489 return rb_reg_s_union(self, v);
4490 }
4491 return rb_reg_s_union(self, args);
4492}
4493
4494/*
4495 * call-seq:
4496 * Regexp.linear_time?(re)
4497 * Regexp.linear_time?(string, options = 0)
4498 *
4499 * Returns +true+ if matching against <tt>re</tt> can be
4500 * done in linear time to the input string.
4501 *
4502 * Regexp.linear_time?(/re/) # => true
4503 *
4504 * Note that this is a property of the ruby interpreter, not of the argument
4505 * regular expression. Identical regexp can or cannot run in linear time
4506 * depending on your ruby binary. Neither forward nor backward compatibility
4507 * is guaranteed about the return value of this method. Our current algorithm
4508 * is (*1) but this is subject to change in the future. Alternative
4509 * implementations can also behave differently. They might always return
4510 * false for everything.
4511 *
4512 * (*1): https://doi.org/10.1109/SP40001.2021.00032
4513 *
4514 */
4515static VALUE
4516rb_reg_s_linear_time_p(int argc, VALUE *argv, VALUE self)
4517{
4518 struct reg_init_args args;
4519 VALUE re = reg_extract_args(argc, argv, &args);
4520
4521 if (NIL_P(re)) {
4522 re = reg_init_args(rb_reg_alloc(), args.str, args.enc, args.flags);
4523 }
4524
4525 return RBOOL(onig_check_linear_time(RREGEXP_PTR(re)));
4526}
4527
4528/* :nodoc: */
4529static VALUE
4530rb_reg_init_copy(VALUE copy, VALUE re)
4531{
4532 if (!OBJ_INIT_COPY(copy, re)) return copy;
4533 rb_reg_check(re);
4534 return reg_copy(copy, re);
4535}
4536
4537VALUE
4538rb_reg_regsub(VALUE str, VALUE src, struct re_registers *regs, VALUE regexp)
4539{
4540 VALUE val = 0;
4541 char *p, *s, *e;
4542 int no, clen;
4543 rb_encoding *str_enc = rb_enc_get(str);
4544 rb_encoding *src_enc = rb_enc_get(src);
4545 int acompat = rb_enc_asciicompat(str_enc);
4546 long n;
4547#define ASCGET(s,e,cl) (acompat ? (*(cl)=1,ISASCII((s)[0])?(s)[0]:-1) : rb_enc_ascget((s), (e), (cl), str_enc))
4548
4549 RSTRING_GETMEM(str, s, n);
4550 p = s;
4551 e = s + n;
4552
4553 while (s < e) {
4554 int c = ASCGET(s, e, &clen);
4555 char *ss;
4556
4557 if (c == -1) {
4558 s += mbclen(s, e, str_enc);
4559 continue;
4560 }
4561 ss = s;
4562 s += clen;
4563
4564 if (c != '\\' || s == e) continue;
4565
4566 if (!val) {
4567 val = rb_str_buf_new(ss-p);
4568 }
4569 rb_enc_str_buf_cat(val, p, ss-p, str_enc);
4570
4571 c = ASCGET(s, e, &clen);
4572 if (c == -1) {
4573 s += mbclen(s, e, str_enc);
4574 rb_enc_str_buf_cat(val, ss, s-ss, str_enc);
4575 p = s;
4576 continue;
4577 }
4578 s += clen;
4579
4580 p = s;
4581 switch (c) {
4582 case '1': case '2': case '3': case '4':
4583 case '5': case '6': case '7': case '8': case '9':
4584 if (!NIL_P(regexp) && onig_noname_group_capture_is_active(RREGEXP_PTR(regexp))) {
4585 no = c - '0';
4586 }
4587 else {
4588 continue;
4589 }
4590 break;
4591
4592 case 'k':
4593 if (s < e && ASCGET(s, e, &clen) == '<') {
4594 char *name, *name_end;
4595
4596 name_end = name = s + clen;
4597 while (name_end < e) {
4598 c = ASCGET(name_end, e, &clen);
4599 if (c == '>') break;
4600 name_end += c == -1 ? mbclen(name_end, e, str_enc) : clen;
4601 }
4602 if (name_end < e) {
4603 VALUE n = rb_str_subseq(str, (long)(name - RSTRING_PTR(str)),
4604 (long)(name_end - name));
4605 if ((no = NAME_TO_NUMBER(regs, regexp, n, name, name_end)) < 1) {
4606 name_to_backref_error(n);
4607 }
4608 p = s = name_end + clen;
4609 break;
4610 }
4611 else {
4612 rb_raise(rb_eRuntimeError, "invalid group name reference format");
4613 }
4614 }
4615
4616 rb_enc_str_buf_cat(val, ss, s-ss, str_enc);
4617 continue;
4618
4619 case '0':
4620 case '&':
4621 no = 0;
4622 break;
4623
4624 case '`':
4625 rb_enc_str_buf_cat(val, RSTRING_PTR(src), BEG(0), src_enc);
4626 continue;
4627
4628 case '\'':
4629 rb_enc_str_buf_cat(val, RSTRING_PTR(src)+END(0), RSTRING_LEN(src)-END(0), src_enc);
4630 continue;
4631
4632 case '+':
4633 no = regs->num_regs-1;
4634 while (BEG(no) == -1 && no > 0) no--;
4635 if (no == 0) continue;
4636 break;
4637
4638 case '\\':
4639 rb_enc_str_buf_cat(val, s-clen, clen, str_enc);
4640 continue;
4641
4642 default:
4643 rb_enc_str_buf_cat(val, ss, s-ss, str_enc);
4644 continue;
4645 }
4646
4647 if (no >= 0) {
4648 if (no >= regs->num_regs) continue;
4649 if (BEG(no) == -1) continue;
4650 rb_enc_str_buf_cat(val, RSTRING_PTR(src)+BEG(no), END(no)-BEG(no), src_enc);
4651 }
4652 }
4653
4654 if (!val) return str;
4655 if (p < e) {
4656 rb_enc_str_buf_cat(val, p, e-p, str_enc);
4657 }
4658
4659 return val;
4660}
4661
4662static VALUE
4663ignorecase_getter(ID _x, VALUE *_y)
4664{
4665 rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "variable $= is no longer effective");
4666 return Qfalse;
4667}
4668
4669static void
4670ignorecase_setter(VALUE val, ID id, VALUE *_)
4671{
4672 rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "variable $= is no longer effective; ignored");
4673}
4674
4675static VALUE
4676match_getter(void)
4677{
4678 VALUE match = rb_backref_get();
4679
4680 if (NIL_P(match)) return Qnil;
4681 rb_match_busy(match);
4682 return match;
4683}
4684
4685static VALUE
4686get_LAST_MATCH_INFO(ID _x, VALUE *_y)
4687{
4688 return match_getter();
4689}
4690
4691static void
4692match_setter(VALUE val, ID _x, VALUE *_y)
4693{
4694 if (!NIL_P(val)) {
4695 Check_Type(val, T_MATCH);
4696 }
4697 rb_backref_set(val);
4698}
4699
4700/*
4701 * call-seq:
4702 * Regexp.last_match -> matchdata or nil
4703 * Regexp.last_match(n) -> string or nil
4704 * Regexp.last_match(name) -> string or nil
4705 *
4706 * With no argument, returns the value of <tt>$~</tt>,
4707 * which is the result of the most recent pattern match
4708 * (see {Regexp global variables}[rdoc-ref:Regexp@Global+Variables]):
4709 *
4710 * /c(.)t/ =~ 'cat' # => 0
4711 * Regexp.last_match # => #<MatchData "cat" 1:"a">
4712 * /a/ =~ 'foo' # => nil
4713 * Regexp.last_match # => nil
4714 *
4715 * With non-negative integer argument +n+, returns the _n_th field in the
4716 * matchdata, if any, or nil if none:
4717 *
4718 * /c(.)t/ =~ 'cat' # => 0
4719 * Regexp.last_match(0) # => "cat"
4720 * Regexp.last_match(1) # => "a"
4721 * Regexp.last_match(2) # => nil
4722 *
4723 * With negative integer argument +n+, counts backwards from the last field:
4724 *
4725 * Regexp.last_match(-1) # => "a"
4726 *
4727 * With string or symbol argument +name+,
4728 * returns the string value for the named capture, if any:
4729 *
4730 * /(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/ =~ 'var = val'
4731 * Regexp.last_match # => #<MatchData "var = val" lhs:"var"rhs:"val">
4732 * Regexp.last_match(:lhs) # => "var"
4733 * Regexp.last_match('rhs') # => "val"
4734 * Regexp.last_match('foo') # Raises IndexError.
4735 *
4736 */
4737
4738static VALUE
4739rb_reg_s_last_match(int argc, VALUE *argv, VALUE _)
4740{
4741 if (rb_check_arity(argc, 0, 1) == 1) {
4742 VALUE match = rb_backref_get();
4743 int n;
4744 if (NIL_P(match)) return Qnil;
4745 n = match_backref_number(match, argv[0]);
4746 return rb_reg_nth_match(n, match);
4747 }
4748 return match_getter();
4749}
4750
4751static void
4752re_warn(const char *s)
4753{
4754 rb_warn("%s", s);
4755}
4756
4757// This function is periodically called during regexp matching
4758bool
4759rb_reg_timeout_p(regex_t *reg, void *end_time_)
4760{
4761 rb_hrtime_t *end_time = (rb_hrtime_t *)end_time_;
4762
4763 if (*end_time == 0) {
4764 // This is the first time to check interrupts;
4765 // just measure the current time and determine the end time
4766 // if timeout is set.
4767 rb_hrtime_t timelimit = reg->timelimit;
4768
4769 if (!timelimit) {
4770 // no per-object timeout.
4771 timelimit = rb_reg_match_time_limit;
4772 }
4773
4774 if (timelimit) {
4775 *end_time = rb_hrtime_add(timelimit, rb_hrtime_now());
4776 }
4777 else {
4778 // no timeout is set
4779 *end_time = RB_HRTIME_MAX;
4780 }
4781 }
4782 else {
4783 if (*end_time < rb_hrtime_now()) {
4784 // Timeout has exceeded
4785 return true;
4786 }
4787 }
4788
4789 return false;
4790}
4791
4792/*
4793 * call-seq:
4794 * Regexp.timeout -> float or nil
4795 *
4796 * It returns the current default timeout interval for Regexp matching in second.
4797 * +nil+ means no default timeout configuration.
4798 */
4799
4800static VALUE
4801rb_reg_s_timeout_get(VALUE dummy)
4802{
4803 double d = hrtime2double(rb_reg_match_time_limit);
4804 if (d == 0.0) return Qnil;
4805 return DBL2NUM(d);
4806}
4807
4808/*
4809 * call-seq:
4810 * Regexp.timeout = float or nil
4811 *
4812 * It sets the default timeout interval for Regexp matching in second.
4813 * +nil+ means no default timeout configuration.
4814 * This configuration is process-global. If you want to set timeout for
4815 * each Regexp, use +timeout+ keyword for <code>Regexp.new</code>.
4816 *
4817 * Regexp.timeout = 1
4818 * /^a*b?a*$/ =~ "a" * 100000 + "x" #=> regexp match timeout (RuntimeError)
4819 */
4820
4821static VALUE
4822rb_reg_s_timeout_set(VALUE dummy, VALUE timeout)
4823{
4824 rb_ractor_ensure_main_ractor("can not access Regexp.timeout from non-main Ractors");
4825
4826 set_timeout(&rb_reg_match_time_limit, timeout);
4827
4828 return timeout;
4829}
4830
4831/*
4832 * call-seq:
4833 * rxp.timeout -> float or nil
4834 *
4835 * It returns the timeout interval for Regexp matching in second.
4836 * +nil+ means no default timeout configuration.
4837 *
4838 * This configuration is per-object. The global configuration set by
4839 * Regexp.timeout= is ignored if per-object configuration is set.
4840 *
4841 * re = Regexp.new("^a*b?a*$", timeout: 1)
4842 * re.timeout #=> 1.0
4843 * re =~ "a" * 100000 + "x" #=> regexp match timeout (RuntimeError)
4844 */
4845
4846static VALUE
4847rb_reg_timeout_get(VALUE re)
4848{
4849 rb_reg_check(re);
4850 double d = hrtime2double(RREGEXP_PTR(re)->timelimit);
4851 if (d == 0.0) return Qnil;
4852 return DBL2NUM(d);
4853}
4854
4855/*
4856 * Document-class: RegexpError
4857 *
4858 * Raised when given an invalid regexp expression.
4859 *
4860 * Regexp.new("?")
4861 *
4862 * <em>raises the exception:</em>
4863 *
4864 * RegexpError: target of repeat operator is not specified: /?/
4865 */
4866
4867/*
4868 * Document-class: Regexp
4869 *
4870 * :include: doc/_regexp.rdoc
4871 */
4872
4873void
4874Init_Regexp(void)
4875{
4876 rb_eRegexpError = rb_define_class("RegexpError", rb_eStandardError);
4877
4878 onigenc_set_default_encoding(ONIG_ENCODING_ASCII);
4879 onig_set_warn_func(re_warn);
4880 onig_set_verb_warn_func(re_warn);
4881
4882 rb_define_virtual_variable("$~", get_LAST_MATCH_INFO, match_setter);
4883 rb_define_virtual_variable("$&", last_match_getter, 0);
4884 rb_define_virtual_variable("$`", prematch_getter, 0);
4885 rb_define_virtual_variable("$'", postmatch_getter, 0);
4886 rb_define_virtual_variable("$+", last_paren_match_getter, 0);
4887
4888 rb_gvar_ractor_local("$~");
4889 rb_gvar_ractor_local("$&");
4890 rb_gvar_ractor_local("$`");
4891 rb_gvar_ractor_local("$'");
4892 rb_gvar_ractor_local("$+");
4893 rb_gvar_box_dynamic("$~");
4894 rb_gvar_box_ready("$&");
4895 rb_gvar_box_ready("$`");
4896 rb_gvar_box_ready("$'");
4897 rb_gvar_box_ready("$+");
4898
4899 rb_define_virtual_variable("$=", ignorecase_getter, ignorecase_setter);
4900
4901 rb_cRegexp = rb_define_class("Regexp", rb_cObject);
4902 rb_define_alloc_func(rb_cRegexp, rb_reg_s_alloc);
4903 rb_define_singleton_method(rb_cRegexp, "compile", rb_class_new_instance_pass_kw, -1);
4904 rb_define_singleton_method(rb_cRegexp, "quote", rb_reg_s_quote, 1);
4905 rb_define_singleton_method(rb_cRegexp, "escape", rb_reg_s_quote, 1);
4906 rb_define_singleton_method(rb_cRegexp, "union", rb_reg_s_union_m, -2);
4907 rb_define_singleton_method(rb_cRegexp, "last_match", rb_reg_s_last_match, -1);
4908 rb_define_singleton_method(rb_cRegexp, "try_convert", rb_reg_s_try_convert, 1);
4909 rb_define_singleton_method(rb_cRegexp, "linear_time?", rb_reg_s_linear_time_p, -1);
4910
4911 rb_define_method(rb_cRegexp, "initialize", rb_reg_initialize_m, -1);
4912 rb_define_method(rb_cRegexp, "initialize_copy", rb_reg_init_copy, 1);
4913 rb_define_method(rb_cRegexp, "hash", rb_reg_hash, 0);
4914 rb_define_method(rb_cRegexp, "eql?", rb_reg_equal, 1);
4915 rb_define_method(rb_cRegexp, "==", rb_reg_equal, 1);
4916 rb_define_method(rb_cRegexp, "=~", rb_reg_match, 1);
4917 rb_define_method(rb_cRegexp, "===", rb_reg_eqq, 1);
4918 rb_define_method(rb_cRegexp, "~", rb_reg_match2, 0);
4919 rb_define_method(rb_cRegexp, "match", rb_reg_match_m, -1);
4920 rb_define_method(rb_cRegexp, "match?", rb_reg_match_m_p, -1);
4921 rb_define_method(rb_cRegexp, "to_s", rb_reg_to_s, 0);
4922 rb_define_method(rb_cRegexp, "inspect", rb_reg_inspect, 0);
4923 rb_define_method(rb_cRegexp, "source", rb_reg_source, 0);
4924 rb_define_method(rb_cRegexp, "casefold?", rb_reg_casefold_p, 0);
4925 rb_define_method(rb_cRegexp, "options", rb_reg_options_m, 0);
4926 rb_define_method(rb_cRegexp, "encoding", rb_obj_encoding, 0); /* in encoding.c */
4927 rb_define_method(rb_cRegexp, "fixed_encoding?", rb_reg_fixed_encoding_p, 0);
4928 rb_define_method(rb_cRegexp, "names", rb_reg_names, 0);
4929 rb_define_method(rb_cRegexp, "named_captures", rb_reg_named_captures, 0);
4930 rb_define_method(rb_cRegexp, "timeout", rb_reg_timeout_get, 0);
4931
4932 /* Raised when regexp matching timed out. */
4933 rb_eRegexpTimeoutError = rb_define_class_under(rb_cRegexp, "TimeoutError", rb_eRegexpError);
4934 rb_define_singleton_method(rb_cRegexp, "timeout", rb_reg_s_timeout_get, 0);
4935 rb_define_singleton_method(rb_cRegexp, "timeout=", rb_reg_s_timeout_set, 1);
4936
4937 /* see Regexp.options and Regexp.new */
4938 rb_define_const(rb_cRegexp, "IGNORECASE", INT2FIX(ONIG_OPTION_IGNORECASE));
4939 /* see Regexp.options and Regexp.new */
4940 rb_define_const(rb_cRegexp, "EXTENDED", INT2FIX(ONIG_OPTION_EXTEND));
4941 /* see Regexp.options and Regexp.new */
4942 rb_define_const(rb_cRegexp, "MULTILINE", INT2FIX(ONIG_OPTION_MULTILINE));
4943 /* see Regexp.options and Regexp.new */
4944 rb_define_const(rb_cRegexp, "FIXEDENCODING", INT2FIX(ARG_ENCODING_FIXED));
4945 /* see Regexp.options and Regexp.new */
4946 rb_define_const(rb_cRegexp, "NOENCODING", INT2FIX(ARG_ENCODING_NONE));
4947
4948 rb_global_variable(&reg_cache);
4949
4950 rb_cMatch = rb_define_class("MatchData", rb_cObject);
4951 rb_define_alloc_func(rb_cMatch, match_alloc);
4952 rb_undef_method(CLASS_OF(rb_cMatch), "new");
4953 rb_undef_method(CLASS_OF(rb_cMatch), "allocate");
4954
4955 rb_define_method(rb_cMatch, "initialize_copy", match_init_copy, 1);
4956 rb_define_method(rb_cMatch, "regexp", match_regexp, 0);
4957 rb_define_method(rb_cMatch, "names", match_names, 0);
4958 rb_define_method(rb_cMatch, "size", match_size, 0);
4959 rb_define_method(rb_cMatch, "length", match_size, 0);
4960 rb_define_method(rb_cMatch, "offset", match_offset, 1);
4961 rb_define_method(rb_cMatch, "byteoffset", match_byteoffset, 1);
4962 rb_define_method(rb_cMatch, "bytebegin", match_bytebegin, 1);
4963 rb_define_method(rb_cMatch, "byteend", match_byteend, 1);
4964 rb_define_method(rb_cMatch, "begin", match_begin, 1);
4965 rb_define_method(rb_cMatch, "end", match_end, 1);
4966 rb_define_method(rb_cMatch, "match", match_nth, 1);
4967 rb_define_method(rb_cMatch, "match_length", match_nth_length, 1);
4968 rb_define_method(rb_cMatch, "to_a", match_to_a, 0);
4969 rb_define_method(rb_cMatch, "[]", match_aref, -1);
4970 rb_define_method(rb_cMatch, "captures", match_captures, 0);
4971 rb_define_alias(rb_cMatch, "deconstruct", "captures");
4972 rb_define_method(rb_cMatch, "named_captures", match_named_captures, -1);
4973 rb_define_method(rb_cMatch, "deconstruct_keys", match_deconstruct_keys, 1);
4974 rb_define_method(rb_cMatch, "values_at", match_values_at, -1);
4975 rb_define_method(rb_cMatch, "pre_match", rb_reg_match_pre, 0);
4976 rb_define_method(rb_cMatch, "post_match", rb_reg_match_post, 0);
4977 rb_define_method(rb_cMatch, "to_s", match_to_s, 0);
4978 rb_define_method(rb_cMatch, "inspect", match_inspect, 0);
4979 rb_define_method(rb_cMatch, "string", match_string, 0);
4980 rb_define_method(rb_cMatch, "hash", match_hash, 0);
4981 rb_define_method(rb_cMatch, "eql?", match_equal, 1);
4982 rb_define_method(rb_cMatch, "==", match_equal, 1);
4983 rb_define_method(rb_cMatch, "integer_at", match_integer_at, -1);
4984}
#define RUBY_ASSERT(...)
Asserts that the given expression is truthy if and only if RUBY_DEBUG is truthy.
Definition assert.h:219
static bool rb_enc_isprint(OnigCodePoint c, rb_encoding *enc)
Identical to rb_isprint(), except it additionally takes an encoding.
Definition ctype.h:180
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Retrieves argument from argc and argv to given VALUE references according to the format string.
Definition class.c:3154
int rb_get_kwargs(VALUE keyword_hash, const ID *table, int required, int optional, VALUE *values)
Keyword argument deconstructor.
Definition class.c:2943
#define ENC_CODERANGE_7BIT
Old name of RUBY_ENC_CODERANGE_7BIT.
Definition coderange.h:180
#define rb_str_buf_cat2
Old name of rb_usascii_str_new_cstr.
Definition string.h:1683
#define OBJ_INIT_COPY(obj, orig)
Old name of RB_OBJ_INIT_COPY.
Definition object.h:41
#define ISSPACE
Old name of rb_isspace.
Definition ctype.h:88
#define T_STRING
Old name of RUBY_T_STRING.
Definition value_type.h:78
#define ENC_CODERANGE_CLEAN_P(cr)
Old name of RB_ENC_CODERANGE_CLEAN_P.
Definition coderange.h:183
#define INT2FIX
Old name of RB_INT2FIX.
Definition long.h:48
#define rb_str_buf_new2
Old name of rb_str_buf_new_cstr.
Definition string.h:1680
#define OBJ_FREEZE
Old name of RB_OBJ_FREEZE.
Definition fl_type.h:131
#define ENC_CODERANGE_UNKNOWN
Old name of RUBY_ENC_CODERANGE_UNKNOWN.
Definition coderange.h:179
#define ENCODING_GET(obj)
Old name of RB_ENCODING_GET.
Definition encoding.h:109
#define FIX2INT
Old name of RB_FIX2INT.
Definition int.h:41
#define rb_str_new3
Old name of rb_str_new_shared.
Definition string.h:1677
#define MBCLEN_CHARFOUND_LEN(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_LEN.
Definition encoding.h:517
#define FL_SET
Old name of RB_FL_SET.
Definition fl_type.h:125
#define LONG2NUM
Old name of RB_LONG2NUM.
Definition long.h:50
#define rb_exc_new3
Old name of rb_exc_new_str.
Definition error.h:38
#define MBCLEN_INVALID_P(ret)
Old name of ONIGENC_MBCLEN_INVALID_P.
Definition encoding.h:518
#define Qtrue
Old name of RUBY_Qtrue.
#define ST2FIX
Old name of RB_ST2FIX.
Definition st_data_t.h:33
#define MBCLEN_NEEDMORE_P(ret)
Old name of ONIGENC_MBCLEN_NEEDMORE_P.
Definition encoding.h:519
#define NUM2INT
Old name of RB_NUM2INT.
Definition int.h:44
#define INT2NUM
Old name of RB_INT2NUM.
Definition int.h:43
#define Qnil
Old name of RUBY_Qnil.
#define Qfalse
Old name of RUBY_Qfalse.
#define ENC_CODERANGE_BROKEN
Old name of RUBY_ENC_CODERANGE_BROKEN.
Definition coderange.h:182
#define T_ARRAY
Old name of RUBY_T_ARRAY.
Definition value_type.h:56
#define scan_hex(s, l, e)
Old name of ruby_scan_hex.
Definition util.h:108
#define NIL_P
Old name of RB_NIL_P.
#define MBCLEN_CHARFOUND_P(ret)
Old name of ONIGENC_MBCLEN_CHARFOUND_P.
Definition encoding.h:516
#define FL_WB_PROTECTED
Old name of RUBY_FL_WB_PROTECTED.
Definition fl_type.h:59
#define T_SYMBOL
Old name of RUBY_T_SYMBOL.
Definition value_type.h:80
#define T_MATCH
Old name of RUBY_T_MATCH.
Definition value_type.h:69
#define FL_TEST
Old name of RB_FL_TEST.
Definition fl_type.h:127
#define NUM2LONG
Old name of RB_NUM2LONG.
Definition long.h:51
#define FL_UNSET
Old name of RB_FL_UNSET.
Definition fl_type.h:129
#define FIXNUM_P
Old name of RB_FIXNUM_P.
#define scan_oct(s, l, e)
Old name of ruby_scan_oct.
Definition util.h:85
#define rb_ary_new2
Old name of rb_ary_new_capa.
Definition array.h:657
#define rb_str_new4
Old name of rb_str_new_frozen.
Definition string.h:1678
#define SYMBOL_P
Old name of RB_SYMBOL_P.
Definition value_type.h:88
#define T_REGEXP
Old name of RUBY_T_REGEXP.
Definition value_type.h:77
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition eval.c:661
VALUE rb_eRegexpError
RegexpError exception.
Definition re.c:35
#define ruby_verbose
This variable controls whether the interpreter is in debug mode.
Definition error.h:476
VALUE rb_eTypeError
TypeError exception.
Definition error.c:1427
VALUE rb_eEncCompatError
Encoding::CompatibilityError exception.
Definition error.c:1434
void rb_warn(const char *fmt,...)
Identical to rb_warning(), except it reports unless $VERBOSE is nil.
Definition error.c:467
VALUE rb_eIndexError
IndexError exception.
Definition error.c:1429
VALUE rb_obj_reveal(VALUE obj, VALUE klass)
Make a hidden object visible again.
Definition object.c:104
VALUE rb_any_to_s(VALUE obj)
Generates a textual representation of the given object.
Definition object.c:646
VALUE rb_cMatch
MatchData class.
Definition re.c:965
VALUE rb_obj_hide(VALUE obj)
Make the object invisible from Ruby code.
Definition object.c:95
VALUE rb_cRegexp
Regexp class.
Definition re.c:2654
VALUE rb_obj_class(VALUE obj)
Queries the class of an object.
Definition object.c:235
#define RB_OBJ_WRITE(old, slot, young)
Declaration of a "back" pointer.
Definition gc.h:603
Encoding relates APIs.
static char * rb_enc_left_char_head(const char *s, const char *p, const char *e, rb_encoding *enc)
Queries the left boundary of a character.
Definition encoding.h:683
int rb_char_to_option_kcode(int c, int *option, int *kcode)
Converts a character option to its encoding.
Definition re.c:331
static int rb_enc_mbmaxlen(rb_encoding *enc)
Queries the maximum number of bytes that the passed encoding needs to represent a character.
Definition encoding.h:447
VALUE rb_enc_reg_new(const char *ptr, long len, rb_encoding *enc, int opts)
Identical to rb_reg_new(), except it additionally takes an encoding.
Definition re.c:3460
long rb_memsearch(const void *x, long m, const void *y, long n, rb_encoding *enc)
Looks for the passed string in the passed buffer.
Definition re.c:255
long rb_enc_strlen(const char *head, const char *tail, rb_encoding *enc)
Counts the number of characters of the passed string, according to the passed encoding.
Definition string.c:2341
long rb_str_coderange_scan_restartable(const char *str, const char *end, rb_encoding *enc, int *cr)
Scans the passed string until it finds something odd.
Definition string.c:829
VALUE rb_str_encode(VALUE str, VALUE to, int ecflags, VALUE ecopts)
Converts the contents of the passed string from its encoding to the passed one.
Definition transcode.c:2977
#define RGENGC_WB_PROTECTED_MATCH
This is a compile-time flag to enable/disable write barrier for struct RMatch.
Definition gc.h:512
#define RGENGC_WB_PROTECTED_REGEXP
This is a compile-time flag to enable/disable write barrier for struct RRegexp.
Definition gc.h:501
VALUE rb_ary_new_capa(long capa)
Identical to rb_ary_new(), except it additionally specifies how many rooms of objects it should alloc...
VALUE rb_ary_resize(VALUE ary, long len)
Expands or shrinks the passed array to the passed length.
VALUE rb_ary_push(VALUE ary, VALUE elem)
Special case of rb_ary_cat() that it adds only one element.
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Identical to rb_ary_new_from_values(), except it expects exactly two parameters.
void rb_ary_store(VALUE ary, long key, VALUE val)
Destructively stores the passed value to the passed array's passed index.
int rb_uv_to_utf8(char buf[6], unsigned long uv)
Encodes a Unicode codepoint into its UTF-8 representation.
Definition pack.c:1690
static int rb_check_arity(int argc, int min, int max)
Ensures that the passed integer is in the passed range.
Definition error.h:284
VALUE rb_backref_get(void)
Queries the last match, or Regexp.last_match, or the $~.
Definition vm.c:2043
void rb_backref_set(VALUE md)
Updates $~.
Definition vm.c:2049
VALUE rb_range_beg_len(VALUE range, long *begp, long *lenp, long len, int err)
Deconstructs a numerical range.
Definition range.c:1949
int rb_reg_backref_number(VALUE match, VALUE backref)
Queries the index of the given named capture.
Definition re.c:1236
int rb_reg_options(VALUE re)
Queries the options of the passed regular expression.
Definition re.c:4294
VALUE rb_reg_last_match(VALUE md)
This just returns the argument, stringified.
Definition re.c:1942
void rb_match_busy(VALUE md)
Asserts that the given MatchData is "occupied".
Definition re.c:1490
VALUE rb_reg_nth_match(int n, VALUE md)
Queries the nth captured substring.
Definition re.c:1917
VALUE rb_reg_match_post(VALUE md)
The portion of the original string after the given match.
Definition re.c:1999
VALUE rb_reg_nth_defined(int n, VALUE md)
Identical to rb_reg_nth_match(), except it just returns Boolean.
Definition re.c:1900
VALUE rb_reg_match_pre(VALUE md)
The portion of the original string before the given match.
Definition re.c:1966
VALUE rb_reg_new_str(VALUE src, int opts)
Identical to rb_reg_new(), except it takes the expression in Ruby's string instead of C's.
Definition re.c:3422
VALUE rb_reg_match_last(VALUE md)
The portion of the original string that captured at the very last.
Definition re.c:2032
VALUE rb_reg_new(const char *src, long len, int opts)
Creates a new Regular expression.
Definition re.c:3474
#define rb_hash_uint(h, i)
Just another name of st_hash_uint.
Definition string.h:943
#define rb_hash_end(h)
Just another name of st_hash_end.
Definition string.h:946
VALUE rb_str_append(VALUE dst, VALUE src)
Identical to rb_str_buf_append(), except it converts the right hand side before concatenating.
Definition string.c:3843
VALUE rb_str_subseq(VALUE str, long beg, long len)
Identical to rb_str_substr(), except the numbers are interpreted as byte offsets instead of character...
Definition string.c:3196
st_index_t rb_memhash(const void *ptr, long len)
This is a universal hash function.
Definition random.c:1791
#define rb_str_new(str, len)
Allocates an instance of rb_cString.
Definition string.h:1499
#define rb_str_buf_cat
Just another name of rb_str_cat.
Definition string.h:1682
VALUE rb_str_dup(VALUE str)
Duplicates a string.
Definition string.c:2003
st_index_t rb_str_hash(VALUE str)
Calculates a hash value of a string.
Definition string.c:4192
VALUE rb_str_buf_append(VALUE dst, VALUE src)
Identical to rb_str_cat_cstr(), except it takes Ruby's string instead of C's.
Definition string.c:3809
VALUE rb_str_equal(VALUE str1, VALUE str2)
Equality of two strings.
Definition string.c:4313
st_index_t rb_hash_start(st_index_t i)
Starts a series of hashing.
Definition random.c:1785
VALUE rb_str_inspect(VALUE str)
Generates a "readable" version of the receiver.
Definition string.c:7281
VALUE rb_str_buf_new(long capa)
Allocates a "string buffer".
Definition string.c:1725
VALUE rb_str_intern(VALUE str)
Identical to rb_to_symbol(), except it assumes the receiver being an instance of RString.
Definition symbol.c:968
VALUE rb_class_path(VALUE mod)
Identical to rb_mod_name(), except it returns #<Class: ...> style inspection for anonymous modules.
Definition variable.c:380
static ID rb_intern_const(const char *str)
This is a "tiny optimisation" over rb_intern().
Definition symbol.h:285
VALUE rb_sym2str(VALUE symbol)
Obtain a frozen string representation of a symbol (not including the leading colon).
Definition symbol.c:1024
int len
Length of the buffer.
Definition io.h:8
long rb_reg_search(VALUE re, VALUE str, long pos, int dir)
Runs the passed regular expression over the passed string.
Definition re.c:1856
regex_t * rb_reg_prepare_re(VALUE re, VALUE str)
Exercises various checks and preprocesses so that the given regular expression can be applied to the ...
Definition re.c:1627
long rb_reg_adjust_startpos(VALUE re, VALUE str, long pos, int dir)
Tell us if this is a wrong idea, but it seems this function has no usage at all.
Definition re.c:1731
OnigPosition rb_reg_onig_match(VALUE re, VALUE str, OnigPosition(*match)(regex_t *reg, VALUE str, struct re_registers *regs, void *args), void *args, struct re_registers *regs)
Runs a regular expression match using function match.
Definition re.c:1695
VALUE rb_reg_regcomp(VALUE str)
Creates a new instance of rb_cRegexp.
Definition re.c:3496
VALUE rb_reg_quote(VALUE str)
Escapes any characters that would have special meaning in a regular expression.
Definition re.c:4174
int rb_reg_region_copy(struct re_registers *dst, const struct re_registers *src)
Duplicates a match data.
Definition re.c:982
#define MEMCPY(p1, p2, type, n)
Handy macro to call memcpy.
Definition memory.h:372
#define ALLOCA_N(type, n)
Definition memory.h:292
#define RB_ALLOCV_N(type, v, n)
Allocates a memory region, possibly on stack.
Definition memory.h:336
#define MEMZERO(p, type, n)
Handy macro to erase a region of memory.
Definition memory.h:360
#define RB_GC_GUARD(v)
Prevents premature destruction of local objects.
Definition memory.h:167
#define RB_ALLOCV_END(v)
Polite way to declare that the given array is not used any longer.
Definition memory.h:349
#define RARRAY_LEN
Just another name of rb_array_len.
Definition rarray.h:51
#define RARRAY_AREF(a, i)
Definition rarray.h:403
static VALUE RBASIC_CLASS(VALUE obj)
Queries the class of an object.
Definition rbasic.h:166
#define RBASIC(obj)
Convenient casting macro.
Definition rbasic.h:40
#define RMATCH(obj)
Convenient casting macro.
Definition rmatch.h:37
static struct re_registers * RMATCH_REGS(VALUE match)
Queries the raw re_registers.
Definition rmatch.h:138
#define RREGEXP(obj)
Convenient casting macro.
Definition rregexp.h:37
static VALUE RREGEXP_SRC(VALUE rexp)
Convenient getter function.
Definition rregexp.h:103
#define RREGEXP_PTR(obj)
Convenient accessor macro.
Definition rregexp.h:45
static long RREGEXP_SRC_LEN(VALUE rexp)
Convenient getter function.
Definition rregexp.h:144
static char * RREGEXP_SRC_PTR(VALUE rexp)
Convenient getter function.
Definition rregexp.h:125
#define StringValue(v)
Ensures that the parameter object is a String.
Definition rstring.h:66
static char * RSTRING_END(VALUE str)
Queries the end of the contents pointer of the string.
Definition rstring.h:409
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Convenient macro to obtain the contents and length at once.
Definition rstring.h:450
#define StringValueCStr(v)
Identical to StringValuePtr, except it additionally checks for the contents for viability as a C stri...
Definition rstring.h:89
#define RTEST
This is an old name of RB_TEST.
#define _(args)
This was a transition path from K&R to ANSI.
Definition stdarg.h:35
MEMO.
Definition imemo.h:104
VALUE flags
Per-object flags.
Definition rbasic.h:81
Regular expression execution context.
Definition rmatch.h:96
VALUE regexp
The expression of this match.
Definition rmatch.h:109
VALUE str
The target string that the match was made against.
Definition rmatch.h:104
Ruby's regular expression.
Definition rregexp.h:60
struct RBasic basic
Basic part, including flags and class.
Definition rregexp.h:63
const VALUE src
Source code of this expression.
Definition rregexp.h:74
unsigned long usecnt
Reference count.
Definition rregexp.h:90
struct re_pattern_buffer * ptr
The pattern buffer.
Definition rregexp.h:71
Definition re.c:992
Represents a match.
Definition rmatch.h:71
struct rmatch_offset * char_offset
Capture group offsets, in C array.
Definition rmatch.h:79
int char_offset_num_allocated
Number of rmatch_offset that ::rmatch::char_offset holds.
Definition rmatch.h:82
struct re_registers regs
"Registers" of a match.
Definition rmatch.h:76
Represents the region of a capture group.
Definition rmatch.h:65
long beg
Beginning of a group.
Definition rmatch.h:66
long end
End of a group.
Definition rmatch.h:67
uintptr_t ID
Type that represents a Ruby identifier such as a variable name.
Definition value.h:52
#define SIZEOF_VALUE
Identical to sizeof(VALUE), except it is a macro that can also be used inside of preprocessor directi...
Definition value.h:69
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40
static void Check_Type(VALUE v, enum ruby_value_type t)
Identical to RB_TYPE_P(), except it raises exceptions on predication failure.
Definition value_type.h:433
static bool RB_TYPE_P(VALUE obj, enum ruby_value_type t)
Queries if the given object is of given type.
Definition value_type.h:376