Ruby: re.c Source File

Ruby 3.4.5p51 (2025-07-16 revision 20cda200d3ce092571d0b5d342dadca69636cb0f)

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