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