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