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