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