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