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