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