Ruby: re.c Source File

Ruby 3.2.5p208 (2024-07-26 revision 31d0f1a2e7dbfb60731d1f05b868e1d578cda493)

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