Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)
re.c
Go to the documentation of this file.
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
13
14#include <ctype.h>
15
16#include "encindex.h"
17#include "internal.h"
18#include "internal/hash.h"
19#include "internal/imemo.h"
20#include "internal/re.h"
21#include "internal/string.h"
22#include "internal/variable.h"
23#include "regint.h"
24#include "ruby/encoding.h"
25#include "ruby/re.h"
26#include "ruby/util.h"
27
29
31#define errcpy(err, msg) strlcpy((err), (msg), ONIG_MAX_ERROR_MESSAGE_LEN)
32
33#define BEG(no) (regs->beg[(no)])
34#define END(no) (regs->end[(no)])
35
36#if 'a' == 97 /* it's ascii */
37static const char casetable[] = {
38 '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
39 '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
40 '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
41 '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
42 /* ' ' '!' '"' '#' '$' '%' '&' ''' */
43 '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
44 /* '(' ')' '*' '+' ',' '-' '.' '/' */
45 '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
46 /* '0' '1' '2' '3' '4' '5' '6' '7' */
47 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
48 /* '8' '9' ':' ';' '<' '=' '>' '?' */
49 '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
50 /* '@' 'A' 'B' 'C' 'D' 'E' 'F' 'G' */
51 '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
52 /* 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O' */
53 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
54 /* 'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' */
55 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
56 /* 'X' 'Y' 'Z' '[' '\' ']' '^' '_' */
57 '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
58 /* '`' 'a' 'b' 'c' 'd' 'e' 'f' 'g' */
59 '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
60 /* 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' */
61 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
62 /* 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' */
63 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
64 /* 'x' 'y' 'z' '{' '|' '}' '~' */
65 '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
66 '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
67 '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
68 '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
69 '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
70 '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
71 '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
72 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
73 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
74 '\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
75 '\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317',
76 '\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327',
77 '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
78 '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
79 '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
80 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
81 '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
82};
83#else
84# error >>> "You lose. You will need a translation table for your character set." <<<
85#endif
86
87int
88rb_memcicmp(const void *x, const void *y, long len)
89{
90 const unsigned char *p1 = x, *p2 = y;
91 int tmp;
92
93 while (len--) {
94 if ((tmp = casetable[(unsigned)*p1++] - casetable[(unsigned)*p2++]))
95 return tmp;
96 }
97 return 0;
98}
99
100#ifdef HAVE_MEMMEM
101static inline long
102rb_memsearch_ss(const unsigned char *xs, long m, const unsigned char *ys, long n)
103{
104 const unsigned char *y;
105
106 if ((y = memmem(ys, n, xs, m)) != NULL)
107 return y - ys;
108 else
109 return -1;
110}
111#else
112static inline long
113rb_memsearch_ss(const unsigned char *xs, long m, const unsigned char *ys, long n)
114{
115 const unsigned char *x = xs, *xe = xs + m;
116 const unsigned char *y = ys, *ye = ys + n;
117#define VALUE_MAX ((VALUE)~(VALUE)0)
118 VALUE hx, hy, mask = VALUE_MAX >> ((SIZEOF_VALUE - m) * CHAR_BIT);
119
120 if (m > SIZEOF_VALUE)
121 rb_bug("!!too long pattern string!!");
122
123 if (!(y = memchr(y, *x, n - m + 1)))
124 return -1;
125
126 /* Prepare hash value */
127 for (hx = *x++, hy = *y++; x < xe; ++x, ++y) {
128 hx <<= CHAR_BIT;
129 hy <<= CHAR_BIT;
130 hx |= *x;
131 hy |= *y;
132 }
133 /* Searching */
134 while (hx != hy) {
135 if (y == ye)
136 return -1;
137 hy <<= CHAR_BIT;
138 hy |= *y;
139 hy &= mask;
140 y++;
141 }
142 return y - ys - m;
143}
144#endif
145
146static inline long
147rb_memsearch_qs(const unsigned char *xs, long m, const unsigned char *ys, long n)
148{
149 const unsigned char *x = xs, *xe = xs + m;
150 const unsigned char *y = ys;
151 VALUE i, qstable[256];
152
153 /* Preprocessing */
154 for (i = 0; i < 256; ++i)
155 qstable[i] = m + 1;
156 for (; x < xe; ++x)
157 qstable[*x] = xe - x;
158 /* Searching */
159 for (; y + m <= ys + n; y += *(qstable + y[m])) {
160 if (*xs == *y && memcmp(xs, y, m) == 0)
161 return y - ys;
162 }
163 return -1;
164}
165
166static inline unsigned int
167rb_memsearch_qs_utf8_hash(const unsigned char *x)
168{
169 register const unsigned int mix = 8353;
170 register unsigned int h = *x;
171 if (h < 0xC0) {
172 return h + 256;
173 }
174 else if (h < 0xE0) {
175 h *= mix;
176 h += x[1];
177 }
178 else if (h < 0xF0) {
179 h *= mix;
180 h += x[1];
181 h *= mix;
182 h += x[2];
183 }
184 else if (h < 0xF5) {
185 h *= mix;
186 h += x[1];
187 h *= mix;
188 h += x[2];
189 h *= mix;
190 h += x[3];
191 }
192 else {
193 return h + 256;
194 }
195 return (unsigned char)h;
196}
197
198static inline long
199rb_memsearch_qs_utf8(const unsigned char *xs, long m, const unsigned char *ys, long n)
200{
201 const unsigned char *x = xs, *xe = xs + m;
202 const unsigned char *y = ys;
203 VALUE i, qstable[512];
204
205 /* Preprocessing */
206 for (i = 0; i < 512; ++i) {
207 qstable[i] = m + 1;
208 }
209 for (; x < xe; ++x) {
210 qstable[rb_memsearch_qs_utf8_hash(x)] = xe - x;
211 }
212 /* Searching */
213 for (; y + m <= ys + n; y += qstable[rb_memsearch_qs_utf8_hash(y+m)]) {
214 if (*xs == *y && memcmp(xs, y, m) == 0)
215 return y - ys;
216 }
217 return -1;
218}
219
220static inline long
221rb_memsearch_wchar(const unsigned char *xs, long m, const unsigned char *ys, long n)
222{
223 const unsigned char *x = xs, x0 = *xs, *y = ys;
224 enum {char_size = 2};
225
226 for (n -= m; n >= 0; n -= char_size, y += char_size) {
227 if (x0 == *y && memcmp(x+1, y+1, m-1) == 0)
228 return y - ys;
229 }
230 return -1;
231}
232
233static inline long
234rb_memsearch_qchar(const unsigned char *xs, long m, const unsigned char *ys, long n)
235{
236 const unsigned char *x = xs, x0 = *xs, *y = ys;
237 enum {char_size = 4};
238
239 for (n -= m; n >= 0; n -= char_size, y += char_size) {
240 if (x0 == *y && memcmp(x+1, y+1, m-1) == 0)
241 return y - ys;
242 }
243 return -1;
244}
245
246long
247rb_memsearch(const void *x0, long m, const void *y0, long n, rb_encoding *enc)
248{
249 const unsigned char *x = x0, *y = y0;
250
251 if (m > n) return -1;
252 else if (m == n) {
253 return memcmp(x0, y0, m) == 0 ? 0 : -1;
254 }
255 else if (m < 1) {
256 return 0;
257 }
258 else if (m == 1) {
259 const unsigned char *ys = memchr(y, *x, n);
260
261 if (ys)
262 return ys - y;
263 else
264 return -1;
265 }
266 else if (LIKELY(rb_enc_mbminlen(enc) == 1)) {
267 if (m <= SIZEOF_VALUE) {
268 return rb_memsearch_ss(x0, m, y0, n);
269 }
270 else if (enc == rb_utf8_encoding()){
271 return rb_memsearch_qs_utf8(x0, m, y0, n);
272 }
273 }
274 else if (LIKELY(rb_enc_mbminlen(enc) == 2)) {
275 return rb_memsearch_wchar(x0, m, y0, n);
276 }
277 else if (LIKELY(rb_enc_mbminlen(enc) == 4)) {
278 return rb_memsearch_qchar(x0, m, y0, n);
279 }
280 return rb_memsearch_qs(x0, m, y0, n);
281}
282
283#define REG_LITERAL FL_USER5
284#define REG_ENCODING_NONE FL_USER6
285
286#define KCODE_FIXED FL_USER4
287
288#define ARG_REG_OPTION_MASK \
289 (ONIG_OPTION_IGNORECASE|ONIG_OPTION_MULTILINE|ONIG_OPTION_EXTEND)
290#define ARG_ENCODING_FIXED 16
291#define ARG_ENCODING_NONE 32
292
293static int
294char_to_option(int c)
295{
296 int val;
297
298 switch (c) {
299 case 'i':
301 break;
302 case 'x':
303 val = ONIG_OPTION_EXTEND;
304 break;
305 case 'm':
307 break;
308 default:
309 val = 0;
310 break;
311 }
312 return val;
313}
314
315enum { OPTBUF_SIZE = 4 };
316
317static char *
318option_to_str(char str[OPTBUF_SIZE], int options)
319{
320 char *p = str;
321 if (options & ONIG_OPTION_MULTILINE) *p++ = 'm';
322 if (options & ONIG_OPTION_IGNORECASE) *p++ = 'i';
323 if (options & ONIG_OPTION_EXTEND) *p++ = 'x';
324 *p = 0;
325 return str;
326}
327
328extern int
329rb_char_to_option_kcode(int c, int *option, int *kcode)
330{
331 *option = 0;
332
333 switch (c) {
334 case 'n':
335 *kcode = rb_ascii8bit_encindex();
336 return (*option = ARG_ENCODING_NONE);
337 case 'e':
338 *kcode = ENCINDEX_EUC_JP;
339 break;
340 case 's':
341 *kcode = ENCINDEX_Windows_31J;
342 break;
343 case 'u':
344 *kcode = rb_utf8_encindex();
345 break;
346 default:
347 *kcode = -1;
348 return (*option = char_to_option(c));
349 }
350 *option = ARG_ENCODING_FIXED;
351 return 1;
352}
353
354static void
355rb_reg_check(VALUE re)
356{
357 if (!RREGEXP_PTR(re) || !RREGEXP_SRC(re) || !RREGEXP_SRC_PTR(re)) {
358 rb_raise(rb_eTypeError, "uninitialized Regexp");
359 }
360}
361
362static void
363rb_reg_expr_str(VALUE str, const char *s, long len,
364 rb_encoding *enc, rb_encoding *resenc, int term)
365{
366 const char *p, *pend;
367 int cr = ENC_CODERANGE_UNKNOWN;
368 int need_escape = 0;
369 int c, clen;
370
371 p = s; pend = p + len;
372 rb_str_coderange_scan_restartable(p, pend, enc, &cr);
374 while (p < pend) {
375 c = rb_enc_ascget(p, pend, &clen, enc);
376 if (c == -1) {
377 if (enc == resenc) {
378 p += mbclen(p, pend, enc);
379 }
380 else {
381 need_escape = 1;
382 break;
383 }
384 }
385 else if (c != term && rb_enc_isprint(c, enc)) {
386 p += clen;
387 }
388 else {
389 need_escape = 1;
390 break;
391 }
392 }
393 }
394 else {
395 need_escape = 1;
396 }
397
398 if (!need_escape) {
400 }
401 else {
402 int unicode_p = rb_enc_unicode_p(enc);
403 p = s;
404 while (p<pend) {
405 c = rb_enc_ascget(p, pend, &clen, enc);
406 if (c == '\\' && p+clen < pend) {
407 int n = clen + mbclen(p+clen, pend, enc);
408 rb_str_buf_cat(str, p, n);
409 p += n;
410 continue;
411 }
412 else if (c == -1) {
413 clen = rb_enc_precise_mbclen(p, pend, enc);
414 if (!MBCLEN_CHARFOUND_P(clen)) {
415 c = (unsigned char)*p;
416 clen = 1;
417 goto hex;
418 }
419 if (resenc) {
420 unsigned int c = rb_enc_mbc_to_codepoint(p, pend, enc);
421 rb_str_buf_cat_escaped_char(str, c, unicode_p);
422 }
423 else {
424 clen = MBCLEN_CHARFOUND_LEN(clen);
425 rb_str_buf_cat(str, p, clen);
426 }
427 }
428 else if (c == term) {
429 char c = '\\';
430 rb_str_buf_cat(str, &c, 1);
431 rb_str_buf_cat(str, p, clen);
432 }
433 else if (rb_enc_isprint(c, enc)) {
434 rb_str_buf_cat(str, p, clen);
435 }
436 else if (!rb_enc_isspace(c, enc)) {
437 char b[8];
438
439 hex:
440 snprintf(b, sizeof(b), "\\x%02X", c);
441 rb_str_buf_cat(str, b, 4);
442 }
443 else {
444 rb_str_buf_cat(str, p, clen);
445 }
446 p += clen;
447 }
448 }
449}
450
451static VALUE
452rb_reg_desc(const char *s, long len, VALUE re)
453{
454 rb_encoding *enc = rb_enc_get(re);
457 if (resenc == NULL) resenc = rb_default_external_encoding();
458
459 if (re && rb_enc_asciicompat(enc)) {
460 rb_enc_copy(str, re);
461 }
462 else {
464 }
465 rb_reg_expr_str(str, s, len, enc, resenc, '/');
466 rb_str_buf_cat2(str, "/");
467 if (re) {
468 char opts[OPTBUF_SIZE];
469 rb_reg_check(re);
470 if (*option_to_str(opts, RREGEXP_PTR(re)->options))
471 rb_str_buf_cat2(str, opts);
472 if (RBASIC(re)->flags & REG_ENCODING_NONE)
473 rb_str_buf_cat2(str, "n");
474 }
475 return str;
476}
477
478
479/*
480 * call-seq:
481 * rxp.source -> str
482 *
483 * Returns the original string of the pattern.
484 *
485 * /ab+c/ix.source #=> "ab+c"
486 *
487 * Note that escape sequences are retained as is.
488 *
489 * /\x20\+/.source #=> "\\x20\\+"
490 *
491 */
492
493static VALUE
494rb_reg_source(VALUE re)
495{
496 VALUE str;
497
498 rb_reg_check(re);
499 str = rb_str_dup(RREGEXP_SRC(re));
500 return str;
501}
502
503/*
504 * call-seq:
505 * rxp.inspect -> string
506 *
507 * Produce a nicely formatted string-version of _rxp_. Perhaps surprisingly,
508 * <code>#inspect</code> actually produces the more natural version of
509 * the string than <code>#to_s</code>.
510 *
511 * /ab+c/ix.inspect #=> "/ab+c/ix"
512 *
513 */
514
515static VALUE
516rb_reg_inspect(VALUE re)
517{
518 if (!RREGEXP_PTR(re) || !RREGEXP_SRC(re) || !RREGEXP_SRC_PTR(re)) {
519 return rb_any_to_s(re);
520 }
521 return rb_reg_desc(RREGEXP_SRC_PTR(re), RREGEXP_SRC_LEN(re), re);
522}
523
524static VALUE rb_reg_str_with_term(VALUE re, int term);
525
526/*
527 * call-seq:
528 * rxp.to_s -> str
529 *
530 * Returns a string containing the regular expression and its options (using the
531 * <code>(?opts:source)</code> notation. This string can be fed back in to
532 * Regexp::new to a regular expression with the same semantics as the
533 * original. (However, <code>Regexp#==</code> may not return true
534 * when comparing the two, as the source of the regular expression
535 * itself may differ, as the example shows). Regexp#inspect produces
536 * a generally more readable version of <i>rxp</i>.
537 *
538 * r1 = /ab+c/ix #=> /ab+c/ix
539 * s1 = r1.to_s #=> "(?ix-m:ab+c)"
540 * r2 = Regexp.new(s1) #=> /(?ix-m:ab+c)/
541 * r1 == r2 #=> false
542 * r1.source #=> "ab+c"
543 * r2.source #=> "(?ix-m:ab+c)"
544 */
545
546static VALUE
547rb_reg_to_s(VALUE re)
548{
549 return rb_reg_str_with_term(re, '/');
550}
551
552static VALUE
553rb_reg_str_with_term(VALUE re, int term)
554{
555 int options, opt;
557 long len;
558 const UChar* ptr;
559 VALUE str = rb_str_buf_new2("(?");
560 char optbuf[OPTBUF_SIZE + 1]; /* for '-' */
561 rb_encoding *enc = rb_enc_get(re);
562
563 rb_reg_check(re);
564
565 rb_enc_copy(str, re);
566 options = RREGEXP_PTR(re)->options;
567 ptr = (UChar*)RREGEXP_SRC_PTR(re);
568 len = RREGEXP_SRC_LEN(re);
569 again:
570 if (len >= 4 && ptr[0] == '(' && ptr[1] == '?') {
571 int err = 1;
572 ptr += 2;
573 if ((len -= 2) > 0) {
574 do {
575 opt = char_to_option((int )*ptr);
576 if (opt != 0) {
577 options |= opt;
578 }
579 else {
580 break;
581 }
582 ++ptr;
583 } while (--len > 0);
584 }
585 if (len > 1 && *ptr == '-') {
586 ++ptr;
587 --len;
588 do {
589 opt = char_to_option((int )*ptr);
590 if (opt != 0) {
591 options &= ~opt;
592 }
593 else {
594 break;
595 }
596 ++ptr;
597 } while (--len > 0);
598 }
599 if (*ptr == ')') {
600 --len;
601 ++ptr;
602 goto again;
603 }
604 if (*ptr == ':' && ptr[len-1] == ')') {
605 Regexp *rp;
606 VALUE verbose = ruby_verbose;
608
609 ++ptr;
610 len -= 2;
611 err = onig_new(&rp, ptr, ptr + len, options,
612 enc, OnigDefaultSyntax, NULL);
613 onig_free(rp);
614 ruby_verbose = verbose;
615 }
616 if (err) {
617 options = RREGEXP_PTR(re)->options;
618 ptr = (UChar*)RREGEXP_SRC_PTR(re);
619 len = RREGEXP_SRC_LEN(re);
620 }
621 }
622
623 if (*option_to_str(optbuf, options)) rb_str_buf_cat2(str, optbuf);
624
625 if ((options & embeddable) != embeddable) {
626 optbuf[0] = '-';
627 option_to_str(optbuf + 1, ~options);
628 rb_str_buf_cat2(str, optbuf);
629 }
630
631 rb_str_buf_cat2(str, ":");
632 if (rb_enc_asciicompat(enc)) {
633 rb_reg_expr_str(str, (char*)ptr, len, enc, NULL, term);
634 rb_str_buf_cat2(str, ")");
635 }
636 else {
637 const char *s, *e;
638 char *paren;
639 ptrdiff_t n;
640 rb_str_buf_cat2(str, ")");
643
644 /* backup encoded ")" to paren */
645 s = RSTRING_PTR(str);
646 e = RSTRING_END(str);
647 s = rb_enc_left_char_head(s, e-1, e, enc);
648 n = e - s;
649 paren = ALLOCA_N(char, n);
650 memcpy(paren, s, n);
652
653 rb_reg_expr_str(str, (char*)ptr, len, enc, NULL, term);
654 rb_str_buf_cat(str, paren, n);
655 }
656 rb_enc_copy(str, re);
657
658 return str;
659}
660
661NORETURN(static void rb_reg_raise(const char *s, long len, const char *err, VALUE re));
662
663static void
664rb_reg_raise(const char *s, long len, const char *err, VALUE re)
665{
666 VALUE desc = rb_reg_desc(s, len, re);
667
668 rb_raise(rb_eRegexpError, "%s: %"PRIsVALUE, err, desc);
669}
670
671static VALUE
672rb_enc_reg_error_desc(const char *s, long len, rb_encoding *enc, int options, const char *err)
673{
674 char opts[OPTBUF_SIZE + 1]; /* for '/' */
675 VALUE desc = rb_str_buf_new2(err);
677 if (resenc == NULL) resenc = rb_default_external_encoding();
678
679 rb_enc_associate(desc, enc);
680 rb_str_buf_cat2(desc, ": /");
681 rb_reg_expr_str(desc, s, len, enc, resenc, '/');
682 opts[0] = '/';
683 option_to_str(opts + 1, options);
684 rb_str_buf_cat2(desc, opts);
685 return rb_exc_new3(rb_eRegexpError, desc);
686}
687
688NORETURN(static void rb_enc_reg_raise(const char *s, long len, rb_encoding *enc, int options, const char *err));
689
690static void
691rb_enc_reg_raise(const char *s, long len, rb_encoding *enc, int options, const char *err)
692{
693 rb_exc_raise(rb_enc_reg_error_desc(s, len, enc, options, err));
694}
695
696static VALUE
697rb_reg_error_desc(VALUE str, int options, const char *err)
698{
699 return rb_enc_reg_error_desc(RSTRING_PTR(str), RSTRING_LEN(str),
700 rb_enc_get(str), options, err);
701}
702
703NORETURN(static void rb_reg_raise_str(VALUE str, int options, const char *err));
704
705static void
706rb_reg_raise_str(VALUE str, int options, const char *err)
707{
708 rb_exc_raise(rb_reg_error_desc(str, options, err));
709}
710
711
712/*
713 * call-seq:
714 * rxp.casefold? -> true or false
715 *
716 * Returns the value of the case-insensitive flag.
717 *
718 * /a/.casefold? #=> false
719 * /a/i.casefold? #=> true
720 * /(?i:a)/.casefold? #=> false
721 */
722
723static VALUE
724rb_reg_casefold_p(VALUE re)
725{
726 rb_reg_check(re);
727 if (RREGEXP_PTR(re)->options & ONIG_OPTION_IGNORECASE) return Qtrue;
728 return Qfalse;
729}
730
731
732/*
733 * call-seq:
734 * rxp.options -> integer
735 *
736 * Returns the set of bits corresponding to the options used when
737 * creating this Regexp (see Regexp::new for details. Note that
738 * additional bits may be set in the returned options: these are used
739 * internally by the regular expression code. These extra bits are
740 * ignored if the options are passed to Regexp::new.
741 *
742 * Regexp::IGNORECASE #=> 1
743 * Regexp::EXTENDED #=> 2
744 * Regexp::MULTILINE #=> 4
745 *
746 * /cat/.options #=> 0
747 * /cat/ix.options #=> 3
748 * Regexp.new('cat', true).options #=> 1
749 * /\xa1\xa2/e.options #=> 16
750 *
751 * r = /cat/ix
752 * Regexp.new(r.source, r.options) #=> /cat/ix
753 */
754
755static VALUE
756rb_reg_options_m(VALUE re)
757{
758 int options = rb_reg_options(re);
759 return INT2NUM(options);
760}
761
762static int
763reg_names_iter(const OnigUChar *name, const OnigUChar *name_end,
764 int back_num, int *back_refs, OnigRegex regex, void *arg)
765{
766 VALUE ary = (VALUE)arg;
767 rb_ary_push(ary, rb_enc_str_new((const char *)name, name_end-name, regex->enc));
768 return 0;
769}
770
771/*
772 * call-seq:
773 * rxp.names -> [name1, name2, ...]
774 *
775 * Returns a list of names of captures as an array of strings.
776 *
777 * /(?<foo>.)(?<bar>.)(?<baz>.)/.names
778 * #=> ["foo", "bar", "baz"]
779 *
780 * /(?<foo>.)(?<foo>.)/.names
781 * #=> ["foo"]
782 *
783 * /(.)(.)/.names
784 * #=> []
785 */
786
787static VALUE
788rb_reg_names(VALUE re)
789{
790 VALUE ary;
791 rb_reg_check(re);
793 onig_foreach_name(RREGEXP_PTR(re), reg_names_iter, (void*)ary);
794 return ary;
795}
796
797static int
798reg_named_captures_iter(const OnigUChar *name, const OnigUChar *name_end,
799 int back_num, int *back_refs, OnigRegex regex, void *arg)
800{
801 VALUE hash = (VALUE)arg;
802 VALUE ary = rb_ary_new2(back_num);
803 int i;
804
805 for (i = 0; i < back_num; i++)
806 rb_ary_store(ary, i, INT2NUM(back_refs[i]));
807
808 rb_hash_aset(hash, rb_str_new((const char*)name, name_end-name),ary);
809
810 return 0;
811}
812
813/*
814 * call-seq:
815 * rxp.named_captures -> hash
816 *
817 * Returns a hash representing information about named captures of <i>rxp</i>.
818 *
819 * A key of the hash is a name of the named captures.
820 * A value of the hash is an array which is list of indexes of corresponding
821 * named captures.
822 *
823 * /(?<foo>.)(?<bar>.)/.named_captures
824 * #=> {"foo"=>[1], "bar"=>[2]}
825 *
826 * /(?<foo>.)(?<foo>.)/.named_captures
827 * #=> {"foo"=>[1, 2]}
828 *
829 * If there are no named captures, an empty hash is returned.
830 *
831 * /(.)(.)/.named_captures
832 * #=> {}
833 */
834
835static VALUE
836rb_reg_named_captures(VALUE re)
837{
838 regex_t *reg = (rb_reg_check(re), RREGEXP_PTR(re));
840 onig_foreach_name(reg, reg_named_captures_iter, (void*)hash);
841 return hash;
842}
843
844static int
845onig_new_with_source(regex_t** reg, const UChar* pattern, const UChar* pattern_end,
846 OnigOptionType option, OnigEncoding enc, const OnigSyntaxType* syntax,
847 OnigErrorInfo* einfo, const char *sourcefile, int sourceline)
848{
849 int r;
850
851 *reg = (regex_t* )malloc(sizeof(regex_t));
852 if (IS_NULL(*reg)) return ONIGERR_MEMORY;
853
854 r = onig_reg_init(*reg, option, ONIGENC_CASE_FOLD_DEFAULT, enc, syntax);
855 if (r) goto err;
856
857 r = onig_compile_ruby(*reg, pattern, pattern_end, einfo, sourcefile, sourceline);
858 if (r) {
859 err:
860 onig_free(*reg);
861 *reg = NULL;
862 }
863 return r;
864}
865
866static Regexp*
867make_regexp(const char *s, long len, rb_encoding *enc, int flags, onig_errmsg_buffer err,
868 const char *sourcefile, int sourceline)
869{
870 Regexp *rp;
871 int r;
872 OnigErrorInfo einfo;
873
874 /* Handle escaped characters first. */
875
876 /* Build a copy of the string (in dest) with the
877 escaped characters translated, and generate the regex
878 from that.
879 */
880
881 r = onig_new_with_source(&rp, (UChar*)s, (UChar*)(s + len), flags,
882 enc, OnigDefaultSyntax, &einfo, sourcefile, sourceline);
883 if (r) {
884 onig_error_code_to_str((UChar*)err, r, &einfo);
885 return 0;
886 }
887 return rp;
888}
889
890
891/*
892 * Document-class: MatchData
893 *
894 * MatchData encapsulates the result of matching a Regexp against
895 * string. It is returned by Regexp#match and String#match, and also
896 * stored in a global variable returned by Regexp.last_match.
897 *
898 * Usage:
899 *
900 * url = 'https://docs.ruby-lang.org/en/2.5.0/MatchData.html'
901 * m = url.match(/(\d\.?)+/) # => #<MatchData "2.5.0" 1:"0">
902 * m.string # => "https://docs.ruby-lang.org/en/2.5.0/MatchData.html"
903 * m.regexp # => /(\d\.?)+/
904 * # entire matched substring:
905 * m[0] # => "2.5.0"
906 *
907 * # Working with unnamed captures
908 * m = url.match(%r{([^/]+)/([^/]+)\.html$})
909 * m.captures # => ["2.5.0", "MatchData"]
910 * m[1] # => "2.5.0"
911 * m.values_at(1, 2) # => ["2.5.0", "MatchData"]
912 *
913 * # Working with named captures
914 * m = url.match(%r{(?<version>[^/]+)/(?<module>[^/]+)\.html$})
915 * m.captures # => ["2.5.0", "MatchData"]
916 * m.named_captures # => {"version"=>"2.5.0", "module"=>"MatchData"}
917 * m[:version] # => "2.5.0"
918 * m.values_at(:version, :module)
919 * # => ["2.5.0", "MatchData"]
920 * # Numerical indexes are working, too
921 * m[1] # => "2.5.0"
922 * m.values_at(1, 2) # => ["2.5.0", "MatchData"]
923 *
924 * == Global variables equivalence
925 *
926 * Parts of last MatchData (returned by Regexp.last_match) are also
927 * aliased as global variables:
928 *
929 * * <code>$~</code> is Regexp.last_match;
930 * * <code>$&</code> is Regexp.last_match<code>[ 0 ]</code>;
931 * * <code>$1</code>, <code>$2</code>, and so on are
932 * Regexp.last_match<code>[ i ]</code> (captures by number);
933 * * <code>$`</code> is Regexp.last_match<code>.pre_match</code>;
934 * * <code>$'</code> is Regexp.last_match<code>.post_match</code>;
935 * * <code>$+</code> is Regexp.last_match<code>[ -1 ]</code> (the last capture).
936 *
937 * See also "Special global variables" section in Regexp documentation.
938 */
939
941
942static VALUE
943match_alloc(VALUE klass)
944{
945 NEWOBJ_OF(match, struct RMatch, klass, T_MATCH);
946
947 match->str = 0;
948 match->rmatch = 0;
949 match->regexp = 0;
950 match->rmatch = ZALLOC(struct rmatch);
951
952 return (VALUE)match;
953}
954
955int
956rb_reg_region_copy(struct re_registers *to, const struct re_registers *from)
957{
958 onig_region_copy(to, (OnigRegion *)from);
959 if (to->allocated) return 0;
960 rb_gc();
961 onig_region_copy(to, (OnigRegion *)from);
962 if (to->allocated) return 0;
963 return ONIGERR_MEMORY;
964}
965
966typedef struct {
969} pair_t;
970
971static int
972pair_byte_cmp(const void *pair1, const void *pair2)
973{
974 long diff = ((pair_t*)pair1)->byte_pos - ((pair_t*)pair2)->byte_pos;
975#if SIZEOF_LONG > SIZEOF_INT
976 return diff ? diff > 0 ? 1 : -1 : 0;
977#else
978 return (int)diff;
979#endif
980}
981
982static void
983update_char_offset(VALUE match)
984{
985 struct rmatch *rm = RMATCH(match)->rmatch;
986 struct re_registers *regs;
987 int i, num_regs, num_pos;
988 long c;
989 char *s, *p, *q;
990 rb_encoding *enc;
991 pair_t *pairs;
992
994 return;
995
996 regs = &rm->regs;
997 num_regs = rm->regs.num_regs;
998
1002 }
1003
1004 enc = rb_enc_get(RMATCH(match)->str);
1005 if (rb_enc_mbmaxlen(enc) == 1) {
1006 for (i = 0; i < num_regs; i++) {
1007 rm->char_offset[i].beg = BEG(i);
1008 rm->char_offset[i].end = END(i);
1009 }
1010 return;
1011 }
1012
1013 pairs = ALLOCA_N(pair_t, num_regs*2);
1014 num_pos = 0;
1015 for (i = 0; i < num_regs; i++) {
1016 if (BEG(i) < 0)
1017 continue;
1018 pairs[num_pos++].byte_pos = BEG(i);
1019 pairs[num_pos++].byte_pos = END(i);
1020 }
1021 qsort(pairs, num_pos, sizeof(pair_t), pair_byte_cmp);
1022
1023 s = p = RSTRING_PTR(RMATCH(match)->str);
1024 c = 0;
1025 for (i = 0; i < num_pos; i++) {
1026 q = s + pairs[i].byte_pos;
1027 c += rb_enc_strlen(p, q, enc);
1028 pairs[i].char_pos = c;
1029 p = q;
1030 }
1031
1032 for (i = 0; i < num_regs; i++) {
1033 pair_t key, *found;
1034 if (BEG(i) < 0) {
1035 rm->char_offset[i].beg = -1;
1036 rm->char_offset[i].end = -1;
1037 continue;
1038 }
1039
1040 key.byte_pos = BEG(i);
1041 found = bsearch(&key, pairs, num_pos, sizeof(pair_t), pair_byte_cmp);
1042 rm->char_offset[i].beg = found->char_pos;
1043
1044 key.byte_pos = END(i);
1045 found = bsearch(&key, pairs, num_pos, sizeof(pair_t), pair_byte_cmp);
1046 rm->char_offset[i].end = found->char_pos;
1047 }
1048}
1049
1050static void
1051match_check(VALUE match)
1052{
1053 if (!RMATCH(match)->regexp) {
1054 rb_raise(rb_eTypeError, "uninitialized MatchData");
1055 }
1056}
1057
1058/* :nodoc: */
1059static VALUE
1060match_init_copy(VALUE obj, VALUE orig)
1061{
1062 struct rmatch *rm;
1063
1064 if (!OBJ_INIT_COPY(obj, orig)) return obj;
1065
1066 RMATCH(obj)->str = RMATCH(orig)->str;
1067 RMATCH(obj)->regexp = RMATCH(orig)->regexp;
1068
1069 rm = RMATCH(obj)->rmatch;
1070 if (rb_reg_region_copy(&rm->regs, RMATCH_REGS(orig)))
1071 rb_memerror();
1072
1074 if (rm->char_offset_num_allocated < rm->regs.num_regs) {
1077 }
1079 struct rmatch_offset, rm->regs.num_regs);
1080 RB_GC_GUARD(orig);
1081 }
1082
1083 return obj;
1084}
1085
1086
1087/*
1088 * call-seq:
1089 * mtch.regexp -> regexp
1090 *
1091 * Returns the regexp.
1092 *
1093 * m = /a.*b/.match("abc")
1094 * m.regexp #=> /a.*b/
1095 */
1096
1097static VALUE
1098match_regexp(VALUE match)
1099{
1100 VALUE regexp;
1101 match_check(match);
1102 regexp = RMATCH(match)->regexp;
1103 if (NIL_P(regexp)) {
1105 regexp = rb_reg_regcomp(rb_reg_quote(str));
1106 RMATCH(match)->regexp = regexp;
1107 }
1108 return regexp;
1109}
1110
1111/*
1112 * call-seq:
1113 * mtch.names -> [name1, name2, ...]
1114 *
1115 * Returns a list of names of captures as an array of strings.
1116 * It is same as mtch.regexp.names.
1117 *
1118 * /(?<foo>.)(?<bar>.)(?<baz>.)/.match("hoge").names
1119 * #=> ["foo", "bar", "baz"]
1120 *
1121 * m = /(?<x>.)(?<y>.)?/.match("a") #=> #<MatchData "a" x:"a" y:nil>
1122 * m.names #=> ["x", "y"]
1123 */
1124
1125static VALUE
1126match_names(VALUE match)
1127{
1128 match_check(match);
1129 if (NIL_P(RMATCH(match)->regexp))
1130 return rb_ary_new_capa(0);
1131 return rb_reg_names(RMATCH(match)->regexp);
1132}
1133
1134/*
1135 * call-seq:
1136 * mtch.length -> integer
1137 * mtch.size -> integer
1138 *
1139 * Returns the number of elements in the match array.
1140 *
1141 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1142 * m.length #=> 5
1143 * m.size #=> 5
1144 */
1145
1146static VALUE
1147match_size(VALUE match)
1148{
1149 match_check(match);
1150 return INT2FIX(RMATCH_REGS(match)->num_regs);
1151}
1152
1153static int name_to_backref_number(struct re_registers *, VALUE, const char*, const char*);
1154NORETURN(static void name_to_backref_error(VALUE name));
1155
1156static void
1157name_to_backref_error(VALUE name)
1158{
1159 rb_raise(rb_eIndexError, "undefined group name reference: % "PRIsVALUE,
1160 name);
1161}
1162
1163static int
1164match_backref_number(VALUE match, VALUE backref)
1165{
1166 const char *name;
1167 int num;
1168
1169 struct re_registers *regs = RMATCH_REGS(match);
1170 VALUE regexp = RMATCH(match)->regexp;
1171
1172 match_check(match);
1173 if (SYMBOL_P(backref)) {
1174 backref = rb_sym2str(backref);
1175 }
1176 else if (!RB_TYPE_P(backref, T_STRING)) {
1177 return NUM2INT(backref);
1178 }
1179 name = StringValueCStr(backref);
1180
1181 num = name_to_backref_number(regs, regexp, name, name + RSTRING_LEN(backref));
1182
1183 if (num < 1) {
1184 name_to_backref_error(backref);
1185 }
1186
1187 return num;
1188}
1189
1190int
1192{
1193 return match_backref_number(match, backref);
1194}
1195
1196/*
1197 * call-seq:
1198 * mtch.offset(n) -> array
1199 *
1200 * Returns a two-element array containing the beginning and ending offsets of
1201 * the <em>n</em>th match.
1202 * <em>n</em> can be a string or symbol to reference a named capture.
1203 *
1204 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1205 * m.offset(0) #=> [1, 7]
1206 * m.offset(4) #=> [6, 7]
1207 *
1208 * m = /(?<foo>.)(.)(?<bar>.)/.match("hoge")
1209 * p m.offset(:foo) #=> [0, 1]
1210 * p m.offset(:bar) #=> [2, 3]
1211 *
1212 */
1213
1214static VALUE
1215match_offset(VALUE match, VALUE n)
1216{
1217 int i = match_backref_number(match, n);
1218 struct re_registers *regs = RMATCH_REGS(match);
1219
1220 match_check(match);
1221 if (i < 0 || regs->num_regs <= i)
1222 rb_raise(rb_eIndexError, "index %d out of matches", i);
1223
1224 if (BEG(i) < 0)
1225 return rb_assoc_new(Qnil, Qnil);
1226
1227 update_char_offset(match);
1230}
1231
1232
1233/*
1234 * call-seq:
1235 * mtch.begin(n) -> integer
1236 *
1237 * Returns the offset of the start of the <em>n</em>th element of the match
1238 * array in the string.
1239 * <em>n</em> can be a string or symbol to reference a named capture.
1240 *
1241 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1242 * m.begin(0) #=> 1
1243 * m.begin(2) #=> 2
1244 *
1245 * m = /(?<foo>.)(.)(?<bar>.)/.match("hoge")
1246 * p m.begin(:foo) #=> 0
1247 * p m.begin(:bar) #=> 2
1248 */
1249
1250static VALUE
1251match_begin(VALUE match, VALUE n)
1252{
1253 int i = match_backref_number(match, n);
1254 struct re_registers *regs = RMATCH_REGS(match);
1255
1256 match_check(match);
1257 if (i < 0 || regs->num_regs <= i)
1258 rb_raise(rb_eIndexError, "index %d out of matches", i);
1259
1260 if (BEG(i) < 0)
1261 return Qnil;
1262
1263 update_char_offset(match);
1264 return INT2FIX(RMATCH(match)->rmatch->char_offset[i].beg);
1265}
1266
1267
1268/*
1269 * call-seq:
1270 * mtch.end(n) -> integer
1271 *
1272 * Returns the offset of the character immediately following the end of the
1273 * <em>n</em>th element of the match array in the string.
1274 * <em>n</em> can be a string or symbol to reference a named capture.
1275 *
1276 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1277 * m.end(0) #=> 7
1278 * m.end(2) #=> 3
1279 *
1280 * m = /(?<foo>.)(.)(?<bar>.)/.match("hoge")
1281 * p m.end(:foo) #=> 1
1282 * p m.end(:bar) #=> 3
1283 */
1284
1285static VALUE
1286match_end(VALUE match, VALUE n)
1287{
1288 int i = match_backref_number(match, n);
1289 struct re_registers *regs = RMATCH_REGS(match);
1290
1291 match_check(match);
1292 if (i < 0 || regs->num_regs <= i)
1293 rb_raise(rb_eIndexError, "index %d out of matches", i);
1294
1295 if (BEG(i) < 0)
1296 return Qnil;
1297
1298 update_char_offset(match);
1299 return INT2FIX(RMATCH(match)->rmatch->char_offset[i].end);
1300}
1301
1302#define MATCH_BUSY FL_USER2
1303
1304void
1306{
1308}
1309
1310void
1312{
1314}
1315
1316int
1318{
1319 struct re_registers *regs;
1320 if (NIL_P(match)) return -1;
1321 regs = RMATCH_REGS(match);
1322 if (!regs) return -1;
1323 return regs->num_regs;
1324}
1325
1326int
1328{
1329 struct re_registers *regs;
1330 if (NIL_P(match)) return FALSE;
1331 regs = RMATCH_REGS(match);
1332 if (!regs) return FALSE;
1333 if (nth >= regs->num_regs) {
1334 return FALSE;
1335 }
1336 if (nth < 0) {
1337 nth += regs->num_regs;
1338 if (nth <= 0) return FALSE;
1339 }
1340 return (BEG(nth) != -1);
1341}
1342
1343static void
1344match_set_string(VALUE m, VALUE string, long pos, long len)
1345{
1346 struct RMatch *match = (struct RMatch *)m;
1347 struct rmatch *rmatch = match->rmatch;
1348
1349 match->str = string;
1350 match->regexp = Qnil;
1351 int err = onig_region_resize(&rmatch->regs, 1);
1352 if (err) rb_memerror();
1353 rmatch->regs.beg[0] = pos;
1354 rmatch->regs.end[0] = pos + len;
1355}
1356
1357void
1358rb_backref_set_string(VALUE string, long pos, long len)
1359{
1361 if (NIL_P(match) || FL_TEST(match, MATCH_BUSY)) {
1362 match = match_alloc(rb_cMatch);
1363 }
1364 match_set_string(match, string, pos, len);
1366}
1367
1368/*
1369 * call-seq:
1370 * rxp.fixed_encoding? -> true or false
1371 *
1372 * Returns false if rxp is applicable to
1373 * a string with any ASCII compatible encoding.
1374 * Returns true otherwise.
1375 *
1376 * r = /a/
1377 * r.fixed_encoding? #=> false
1378 * r =~ "\u{6666} a" #=> 2
1379 * r =~ "\xa1\xa2 a".force_encoding("euc-jp") #=> 2
1380 * r =~ "abc".force_encoding("euc-jp") #=> 0
1381 *
1382 * r = /a/u
1383 * r.fixed_encoding? #=> true
1384 * r.encoding #=> #<Encoding:UTF-8>
1385 * r =~ "\u{6666} a" #=> 2
1386 * r =~ "\xa1\xa2".force_encoding("euc-jp") #=> Encoding::CompatibilityError
1387 * r =~ "abc".force_encoding("euc-jp") #=> 0
1388 *
1389 * r = /\u{6666}/
1390 * r.fixed_encoding? #=> true
1391 * r.encoding #=> #<Encoding:UTF-8>
1392 * r =~ "\u{6666} a" #=> 0
1393 * r =~ "\xa1\xa2".force_encoding("euc-jp") #=> Encoding::CompatibilityError
1394 * r =~ "abc".force_encoding("euc-jp") #=> nil
1395 */
1396
1397static VALUE
1398rb_reg_fixed_encoding_p(VALUE re)
1399{
1400 if (FL_TEST(re, KCODE_FIXED))
1401 return Qtrue;
1402 else
1403 return Qfalse;
1404}
1405
1406static VALUE
1407rb_reg_preprocess(const char *p, const char *end, rb_encoding *enc,
1408 rb_encoding **fixed_enc, onig_errmsg_buffer err);
1409
1410NORETURN(static void reg_enc_error(VALUE re, VALUE str));
1411
1412static void
1413reg_enc_error(VALUE re, VALUE str)
1414{
1416 "incompatible encoding regexp match (%s regexp with %s string)",
1419}
1420
1421static inline int
1422str_coderange(VALUE str)
1423{
1424 int cr = ENC_CODERANGE(str);
1425 if (cr == ENC_CODERANGE_UNKNOWN) {
1427 }
1428 return cr;
1429}
1430
1431static rb_encoding*
1432rb_reg_prepare_enc(VALUE re, VALUE str, int warn)
1433{
1434 rb_encoding *enc = 0;
1435 int cr = str_coderange(str);
1436
1437 if (cr == ENC_CODERANGE_BROKEN) {
1439 "invalid byte sequence in %s",
1441 }
1442
1443 rb_reg_check(re);
1444 enc = rb_enc_get(str);
1445 if (RREGEXP_PTR(re)->enc == enc) {
1446 }
1447 else if (cr == ENC_CODERANGE_7BIT &&
1448 RREGEXP_PTR(re)->enc == rb_usascii_encoding()) {
1449 enc = RREGEXP_PTR(re)->enc;
1450 }
1451 else if (!rb_enc_asciicompat(enc)) {
1452 reg_enc_error(re, str);
1453 }
1454 else if (rb_reg_fixed_encoding_p(re)) {
1455 if ((!rb_enc_asciicompat(RREGEXP_PTR(re)->enc) ||
1456 cr != ENC_CODERANGE_7BIT)) {
1457 reg_enc_error(re, str);
1458 }
1459 enc = RREGEXP_PTR(re)->enc;
1460 }
1461 else if (warn && (RBASIC(re)->flags & REG_ENCODING_NONE) &&
1462 enc != rb_ascii8bit_encoding() &&
1463 cr != ENC_CODERANGE_7BIT) {
1464 rb_warn("historical binary regexp match /.../n against %s string",
1465 rb_enc_name(enc));
1466 }
1467 return enc;
1468}
1469
1470regex_t *
1472{
1473 regex_t *reg = RREGEXP_PTR(re);
1474 int r;
1475 OnigErrorInfo einfo;
1476 const char *pattern;
1477 VALUE unescaped;
1478 rb_encoding *fixed_enc = 0;
1479 rb_encoding *enc = rb_reg_prepare_enc(re, str, 1);
1480
1481 if (reg->enc == enc) return reg;
1482
1483 rb_reg_check(re);
1484 reg = RREGEXP_PTR(re);
1485 pattern = RREGEXP_SRC_PTR(re);
1486
1487 unescaped = rb_reg_preprocess(
1488 pattern, pattern + RREGEXP_SRC_LEN(re), enc,
1489 &fixed_enc, err);
1490
1491 if (unescaped == Qnil) {
1492 rb_raise(rb_eArgError, "regexp preprocess failed: %s", err);
1493 }
1494
1495 const char *ptr;
1496 long len;
1497 RSTRING_GETMEM(unescaped, ptr, len);
1498 r = onig_new(&reg, (UChar *)ptr, (UChar *)(ptr + len),
1499 reg->options, enc,
1500 OnigDefaultSyntax, &einfo);
1501 if (r) {
1502 onig_error_code_to_str((UChar*)err, r, &einfo);
1503 rb_reg_raise(pattern, RREGEXP_SRC_LEN(re), err, re);
1504 }
1505
1506 RB_GC_GUARD(unescaped);
1507 return reg;
1508}
1509
1510regex_t *
1512{
1514 return rb_reg_prepare_re0(re, str, err);
1515}
1516
1517long
1518rb_reg_adjust_startpos(VALUE re, VALUE str, long pos, int reverse)
1519{
1520 long range;
1521 rb_encoding *enc;
1522 UChar *p, *string;
1523
1524 enc = rb_reg_prepare_enc(re, str, 0);
1525
1526 if (reverse) {
1527 range = -pos;
1528 }
1529 else {
1530 range = RSTRING_LEN(str) - pos;
1531 }
1532
1533 if (pos > 0 && ONIGENC_MBC_MAXLEN(enc) != 1 && pos < RSTRING_LEN(str)) {
1534 string = (UChar*)RSTRING_PTR(str);
1535
1536 if (range > 0) {
1537 p = onigenc_get_right_adjust_char_head(enc, string, string + pos, string + RSTRING_LEN(str));
1538 }
1539 else {
1540 p = ONIGENC_LEFT_ADJUST_CHAR_HEAD(enc, string, string + pos, string + RSTRING_LEN(str));
1541 }
1542 return p - string;
1543 }
1544
1545 return pos;
1546}
1547
1548/* returns byte offset */
1549static long
1550rb_reg_search_set_match(VALUE re, VALUE str, long pos, int reverse, int set_backref_str, VALUE *set_match)
1551{
1552 long result;
1553 VALUE match;
1554 struct re_registers regi, *regs = &regi;
1555 char *start, *range;
1556 long len;
1557 regex_t *reg;
1558 int tmpreg;
1560
1561 RSTRING_GETMEM(str, start, len);
1562 range = start;
1563 if (pos > len || pos < 0) {
1565 return -1;
1566 }
1567
1568 reg = rb_reg_prepare_re0(re, str, err);
1569 tmpreg = reg != RREGEXP_PTR(re);
1570 if (!tmpreg) RREGEXP(re)->usecnt++;
1571
1572 MEMZERO(regs, struct re_registers, 1);
1573 if (!reverse) {
1574 range += len;
1575 }
1576 result = onig_search(reg,
1577 (UChar*)start,
1578 ((UChar*)(start + len)),
1579 ((UChar*)(start + pos)),
1580 ((UChar*)range),
1581 regs, ONIG_OPTION_NONE);
1582 if (!tmpreg) RREGEXP(re)->usecnt--;
1583 if (tmpreg) {
1584 if (RREGEXP(re)->usecnt) {
1585 onig_free(reg);
1586 }
1587 else {
1589 RREGEXP_PTR(re) = reg;
1590 }
1591 }
1592 if (result < 0) {
1593 if (regs == &regi)
1594 onig_region_free(regs, 0);
1595 if (result == ONIG_MISMATCH) {
1597 return result;
1598 }
1599 else {
1600 onig_error_code_to_str((UChar*)err, (int)result);
1601 rb_reg_raise(RREGEXP_SRC_PTR(re), RREGEXP_SRC_LEN(re), err, re);
1602 }
1603 }
1604
1605 match = match_alloc(rb_cMatch);
1606 int copy_err = rb_reg_region_copy(RMATCH_REGS(match), regs);
1607 onig_region_free(regs, 0);
1608 if (copy_err) rb_memerror();
1609
1610 if (set_backref_str) {
1611 RMATCH(match)->str = rb_str_new4(str);
1612 }
1613
1614 RMATCH(match)->regexp = re;
1616 if (set_match) *set_match = match;
1617
1618 return result;
1619}
1620
1621long
1622rb_reg_search0(VALUE re, VALUE str, long pos, int reverse, int set_backref_str)
1623{
1624 return rb_reg_search_set_match(re, str, pos, reverse, set_backref_str, NULL);
1625}
1626
1627long
1628rb_reg_search(VALUE re, VALUE str, long pos, int reverse)
1629{
1630 return rb_reg_search0(re, str, pos, reverse, 1);
1631}
1632
1633bool
1635{
1636 long result;
1637 VALUE match;
1638 struct re_registers regi, *regs = &regi;
1639 regex_t *reg;
1640 int tmpreg;
1642
1643 reg = rb_reg_prepare_re0(re, str, err);
1644 tmpreg = reg != RREGEXP_PTR(re);
1645 if (!tmpreg) RREGEXP(re)->usecnt++;
1646
1648 if (!NIL_P(match)) {
1649 if (FL_TEST(match, MATCH_BUSY)) {
1650 match = Qnil;
1651 }
1652 else {
1653 regs = RMATCH_REGS(match);
1654 }
1655 }
1656 if (NIL_P(match)) {
1657 MEMZERO(regs, struct re_registers, 1);
1658 }
1659 const char *ptr;
1660 long len;
1662 result = onig_match(reg,
1663 (UChar*)(ptr),
1664 ((UChar*)(ptr + len)),
1665 (UChar*)(ptr),
1666 regs, ONIG_OPTION_NONE);
1667 if (!tmpreg) RREGEXP(re)->usecnt--;
1668 if (tmpreg) {
1669 if (RREGEXP(re)->usecnt) {
1670 onig_free(reg);
1671 }
1672 else {
1674 RREGEXP_PTR(re) = reg;
1675 }
1676 }
1677 if (result < 0) {
1678 if (regs == &regi)
1679 onig_region_free(regs, 0);
1680 if (result == ONIG_MISMATCH) {
1682 return false;
1683 }
1684 else {
1685 onig_error_code_to_str((UChar*)err, (int)result);
1686 rb_reg_raise(RREGEXP_SRC_PTR(re), RREGEXP_SRC_LEN(re), err, re);
1687 }
1688 }
1689
1690 if (NIL_P(match)) {
1691 int err;
1692 match = match_alloc(rb_cMatch);
1693 err = rb_reg_region_copy(RMATCH_REGS(match), regs);
1694 onig_region_free(regs, 0);
1695 if (err) rb_memerror();
1696 }
1697
1698 RMATCH(match)->str = rb_str_new4(str);
1699
1700 RMATCH(match)->regexp = re;
1702
1703 return true;
1704}
1705
1706VALUE
1708{
1709 struct re_registers *regs;
1710 if (NIL_P(match)) return Qnil;
1711 match_check(match);
1712 regs = RMATCH_REGS(match);
1713 if (nth >= regs->num_regs) {
1714 return Qnil;
1715 }
1716 if (nth < 0) {
1717 nth += regs->num_regs;
1718 if (nth <= 0) return Qnil;
1719 }
1720 if (BEG(nth) == -1) return Qfalse;
1721 return Qtrue;
1722}
1723
1724VALUE
1726{
1727 VALUE str;
1728 long start, end, len;
1729 struct re_registers *regs;
1730
1731 if (NIL_P(match)) return Qnil;
1732 match_check(match);
1733 regs = RMATCH_REGS(match);
1734 if (nth >= regs->num_regs) {
1735 return Qnil;
1736 }
1737 if (nth < 0) {
1738 nth += regs->num_regs;
1739 if (nth <= 0) return Qnil;
1740 }
1741 start = BEG(nth);
1742 if (start == -1) return Qnil;
1743 end = END(nth);
1744 len = end - start;
1745 str = rb_str_subseq(RMATCH(match)->str, start, len);
1746 return str;
1747}
1748
1749VALUE
1751{
1752 return rb_reg_nth_match(0, match);
1753}
1754
1755
1756/*
1757 * call-seq:
1758 * mtch.pre_match -> str
1759 *
1760 * Returns the portion of the original string before the current match.
1761 * Equivalent to the special variable <code>$`</code>.
1762 *
1763 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1764 * m.pre_match #=> "T"
1765 */
1766
1767VALUE
1769{
1770 VALUE str;
1771 struct re_registers *regs;
1772
1773 if (NIL_P(match)) return Qnil;
1774 match_check(match);
1775 regs = RMATCH_REGS(match);
1776 if (BEG(0) == -1) return Qnil;
1777 str = rb_str_subseq(RMATCH(match)->str, 0, BEG(0));
1778 return str;
1779}
1780
1781
1782/*
1783 * call-seq:
1784 * mtch.post_match -> str
1785 *
1786 * Returns the portion of the original string after the current match.
1787 * Equivalent to the special variable <code>$'</code>.
1788 *
1789 * m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie")
1790 * m.post_match #=> ": The Movie"
1791 */
1792
1793VALUE
1795{
1796 VALUE str;
1797 long pos;
1798 struct re_registers *regs;
1799
1800 if (NIL_P(match)) return Qnil;
1801 match_check(match);
1802 regs = RMATCH_REGS(match);
1803 if (BEG(0) == -1) return Qnil;
1804 str = RMATCH(match)->str;
1805 pos = END(0);
1806 str = rb_str_subseq(str, pos, RSTRING_LEN(str) - pos);
1807 return str;
1808}
1809
1810VALUE
1812{
1813 int i;
1814 struct re_registers *regs;
1815
1816 if (NIL_P(match)) return Qnil;
1817 match_check(match);
1818 regs = RMATCH_REGS(match);
1819 if (BEG(0) == -1) return Qnil;
1820
1821 for (i=regs->num_regs-1; BEG(i) == -1 && i > 0; i--)
1822 ;
1823 if (i == 0) return Qnil;
1824 return rb_reg_nth_match(i, match);
1825}
1826
1827static VALUE
1828last_match_getter(ID _x, VALUE *_y)
1829{
1831}
1832
1833static VALUE
1834prematch_getter(ID _x, VALUE *_y)
1835{
1837}
1838
1839static VALUE
1840postmatch_getter(ID _x, VALUE *_y)
1841{
1843}
1844
1845static VALUE
1846last_paren_match_getter(ID _x, VALUE *_y)
1847{
1849}
1850
1851static VALUE
1852match_array(VALUE match, int start)
1853{
1854 struct re_registers *regs;
1855 VALUE ary;
1856 VALUE target;
1857 int i;
1858
1859 match_check(match);
1860 regs = RMATCH_REGS(match);
1861 ary = rb_ary_new2(regs->num_regs);
1862 target = RMATCH(match)->str;
1863
1864 for (i=start; i<regs->num_regs; i++) {
1865 if (regs->beg[i] == -1) {
1866 rb_ary_push(ary, Qnil);
1867 }
1868 else {
1869 VALUE str = rb_str_subseq(target, regs->beg[i], regs->end[i]-regs->beg[i]);
1870 rb_ary_push(ary, str);
1871 }
1872 }
1873 return ary;
1874}
1875
1876
1877/*
1878 * call-seq:
1879 * mtch.to_a -> anArray
1880 *
1881 * Returns the array of matches.
1882 *
1883 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1884 * m.to_a #=> ["HX1138", "H", "X", "113", "8"]
1885 *
1886 * Because <code>to_a</code> is called when expanding
1887 * <code>*</code><em>variable</em>, there's a useful assignment
1888 * shortcut for extracting matched fields. This is slightly slower than
1889 * accessing the fields directly (as an intermediate array is
1890 * generated).
1891 *
1892 * all,f1,f2,f3 = * /(.)(.)(\d+)(\d)/.match("THX1138.")
1893 * all #=> "HX1138"
1894 * f1 #=> "H"
1895 * f2 #=> "X"
1896 * f3 #=> "113"
1897 */
1898
1899static VALUE
1900match_to_a(VALUE match)
1901{
1902 return match_array(match, 0);
1903}
1904
1905
1906/*
1907 * call-seq:
1908 * mtch.captures -> array
1909 *
1910 * Returns the array of captures; equivalent to <code>mtch.to_a[1..-1]</code>.
1911 *
1912 * f1,f2,f3,f4 = /(.)(.)(\d+)(\d)/.match("THX1138.").captures
1913 * f1 #=> "H"
1914 * f2 #=> "X"
1915 * f3 #=> "113"
1916 * f4 #=> "8"
1917 */
1918static VALUE
1919match_captures(VALUE match)
1920{
1921 return match_array(match, 1);
1922}
1923
1924static int
1925name_to_backref_number(struct re_registers *regs, VALUE regexp, const char* name, const char* name_end)
1926{
1927 if (NIL_P(regexp)) return -1;
1929 (const unsigned char *)name, (const unsigned char *)name_end, regs);
1930}
1931
1932#define NAME_TO_NUMBER(regs, re, name, name_ptr, name_end) \
1933 (NIL_P(re) ? 0 : \
1934 !rb_enc_compatible(RREGEXP_SRC(re), (name)) ? 0 : \
1935 name_to_backref_number((regs), (re), (name_ptr), (name_end)))
1936
1937static int
1938namev_to_backref_number(struct re_registers *regs, VALUE re, VALUE name)
1939{
1940 int num;
1941
1942 if (SYMBOL_P(name)) {
1943 name = rb_sym2str(name);
1944 }
1945 else if (!RB_TYPE_P(name, T_STRING)) {
1946 return -1;
1947 }
1948 num = NAME_TO_NUMBER(regs, re, name,
1949 RSTRING_PTR(name), RSTRING_END(name));
1950 if (num < 1) {
1951 name_to_backref_error(name);
1952 }
1953 return num;
1954}
1955
1956static VALUE
1957match_ary_subseq(VALUE match, long beg, long len, VALUE result)
1958{
1959 long olen = RMATCH_REGS(match)->num_regs;
1960 long j, end = olen < beg+len ? olen : beg+len;
1961 if (NIL_P(result)) result = rb_ary_new_capa(len);
1962 if (len == 0) return result;
1963
1964 for (j = beg; j < end; j++) {
1965 rb_ary_push(result, rb_reg_nth_match((int)j, match));
1966 }
1967 if (beg + len > j) {
1968 rb_ary_resize(result, RARRAY_LEN(result) + (beg + len) - j);
1969 }
1970 return result;
1971}
1972
1973static VALUE
1974match_ary_aref(VALUE match, VALUE idx, VALUE result)
1975{
1976 long beg, len;
1977 int num_regs = RMATCH_REGS(match)->num_regs;
1978
1979 /* check if idx is Range */
1980 switch (rb_range_beg_len(idx, &beg, &len, (long)num_regs, !NIL_P(result))) {
1981 case Qfalse:
1982 if (NIL_P(result)) return rb_reg_nth_match(NUM2INT(idx), match);
1983 rb_ary_push(result, rb_reg_nth_match(NUM2INT(idx), match));
1984 return result;
1985 case Qnil:
1986 return Qnil;
1987 default:
1988 return match_ary_subseq(match, beg, len, result);
1989 }
1990}
1991
1992/*
1993 * call-seq:
1994 * mtch[i] -> str or nil
1995 * mtch[start, length] -> array
1996 * mtch[range] -> array
1997 * mtch[name] -> str or nil
1998 *
1999 * Match Reference -- MatchData acts as an array, and may be accessed
2000 * using the normal array indexing techniques. <code>mtch[0]</code>
2001 * is equivalent to the special variable <code>$&</code>, and returns
2002 * the entire matched string. <code>mtch[1]</code>,
2003 * <code>mtch[2]</code>, and so on return the values of the matched
2004 * backreferences (portions of the pattern between parentheses).
2005 *
2006 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
2007 * m #=> #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8">
2008 * m[0] #=> "HX1138"
2009 * m[1, 2] #=> ["H", "X"]
2010 * m[1..3] #=> ["H", "X", "113"]
2011 * m[-3, 2] #=> ["X", "113"]
2012 *
2013 * m = /(?<foo>a+)b/.match("ccaaab")
2014 * m #=> #<MatchData "aaab" foo:"aaa">
2015 * m["foo"] #=> "aaa"
2016 * m[:foo] #=> "aaa"
2017 */
2018
2019static VALUE
2020match_aref(int argc, VALUE *argv, VALUE match)
2021{
2022 VALUE idx, length;
2023
2024 match_check(match);
2025 rb_scan_args(argc, argv, "11", &idx, &length);
2026
2027 if (NIL_P(length)) {
2028 if (FIXNUM_P(idx)) {
2029 return rb_reg_nth_match(FIX2INT(idx), match);
2030 }
2031 else {
2032 int num = namev_to_backref_number(RMATCH_REGS(match), RMATCH(match)->regexp, idx);
2033 if (num >= 0) {
2034 return rb_reg_nth_match(num, match);
2035 }
2036 else {
2037 return match_ary_aref(match, idx, Qnil);
2038 }
2039 }
2040 }
2041 else {
2042 long beg = NUM2LONG(idx);
2043 long len = NUM2LONG(length);
2044 long num_regs = RMATCH_REGS(match)->num_regs;
2045 if (len < 0) {
2046 return Qnil;
2047 }
2048 if (beg < 0) {
2049 beg += num_regs;
2050 if (beg < 0) return Qnil;
2051 }
2052 else if (beg > num_regs) {
2053 return Qnil;
2054 }
2055 else if (beg+len > num_regs) {
2056 len = num_regs - beg;
2057 }
2058 return match_ary_subseq(match, beg, len, Qnil);
2059 }
2060}
2061
2062/*
2063 * call-seq:
2064 *
2065 * mtch.values_at(index, ...) -> array
2066 *
2067 * Uses each <i>index</i> to access the matching values, returning an array of
2068 * the corresponding matches.
2069 *
2070 * m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie")
2071 * m.to_a #=> ["HX1138", "H", "X", "113", "8"]
2072 * m.values_at(0, 2, -2) #=> ["HX1138", "X", "113"]
2073 *
2074 * m = /(?<a>\d+) *(?<op>[+\-*\/]) *(?<b>\d+)/.match("1 + 2")
2075 * m.to_a #=> ["1 + 2", "1", "+", "2"]
2076 * m.values_at(:a, :b, :op) #=> ["1", "2", "+"]
2077 */
2078
2079static VALUE
2080match_values_at(int argc, VALUE *argv, VALUE match)
2081{
2082 VALUE result;
2083 int i;
2084
2085 match_check(match);
2086 result = rb_ary_new2(argc);
2087
2088 for (i=0; i<argc; i++) {
2089 if (FIXNUM_P(argv[i])) {
2091 }
2092 else {
2093 int num = namev_to_backref_number(RMATCH_REGS(match), RMATCH(match)->regexp, argv[i]);
2094 if (num >= 0) {
2096 }
2097 else {
2098 match_ary_aref(match, argv[i], result);
2099 }
2100 }
2101 }
2102 return result;
2103}
2104
2105
2106/*
2107 * call-seq:
2108 * mtch.to_s -> str
2109 *
2110 * Returns the entire matched string.
2111 *
2112 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
2113 * m.to_s #=> "HX1138"
2114 */
2115
2116static VALUE
2117match_to_s(VALUE match)
2118{
2120
2121 match_check(match);
2122 if (NIL_P(str)) str = rb_str_new(0,0);
2123 return str;
2124}
2125
2126static int
2127match_named_captures_iter(const OnigUChar *name, const OnigUChar *name_end,
2128 int back_num, int *back_refs, OnigRegex regex, void *arg) {
2129 struct MEMO *memo = MEMO_CAST(arg);
2130 VALUE hash = memo->v1;
2131 VALUE match = memo->v2;
2132
2133 VALUE key = rb_enc_str_new((const char *)name, name_end-name, regex->enc);
2134 VALUE value;
2135
2136 int i;
2137 int found = 0;
2138
2139 for (i = 0; i < back_num; i++) {
2140 value = rb_reg_nth_match(back_refs[i], match);
2141 if (RTEST(value)) {
2142 rb_hash_aset(hash, key, value);
2143 found = 1;
2144 }
2145 }
2146
2147 if (found == 0) {
2148 rb_hash_aset(hash, key, Qnil);
2149 }
2150
2151 return 0;
2152}
2153
2154/*
2155 * call-seq:
2156 * mtch.named_captures -> hash
2157 *
2158 * Returns a Hash using named capture.
2159 *
2160 * A key of the hash is a name of the named captures.
2161 * A value of the hash is a string of last successful capture of corresponding
2162 * group.
2163 *
2164 * m = /(?<a>.)(?<b>.)/.match("01")
2165 * m.named_captures #=> {"a" => "0", "b" => "1"}
2166 *
2167 * m = /(?<a>.)(?<b>.)?/.match("0")
2168 * m.named_captures #=> {"a" => "0", "b" => nil}
2169 *
2170 * m = /(?<a>.)(?<a>.)/.match("01")
2171 * m.named_captures #=> {"a" => "1"}
2172 *
2173 * m = /(?<a>x)|(?<a>y)/.match("x")
2174 * m.named_captures #=> {"a" => "x"}
2175 */
2176
2177static VALUE
2178match_named_captures(VALUE match)
2179{
2180 VALUE hash;
2181 struct MEMO *memo;
2182
2183 match_check(match);
2184 if (NIL_P(RMATCH(match)->regexp))
2185 return rb_hash_new();
2186
2187 hash = rb_hash_new();
2188 memo = MEMO_NEW(hash, match, 0);
2189
2190 onig_foreach_name(RREGEXP(RMATCH(match)->regexp)->ptr, match_named_captures_iter, (void*)memo);
2191
2192 return hash;
2193}
2194
2195/*
2196 * call-seq:
2197 * mtch.string -> str
2198 *
2199 * Returns a frozen copy of the string passed in to <code>match</code>.
2200 *
2201 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
2202 * m.string #=> "THX1138."
2203 */
2204
2205static VALUE
2206match_string(VALUE match)
2207{
2208 match_check(match);
2209 return RMATCH(match)->str; /* str is frozen */
2210}
2211
2213 const UChar *name;
2214 long len;
2215};
2216
2217static int
2218match_inspect_name_iter(const OnigUChar *name, const OnigUChar *name_end,
2219 int back_num, int *back_refs, OnigRegex regex, void *arg0)
2220{
2221 struct backref_name_tag *arg = (struct backref_name_tag *)arg0;
2222 int i;
2223
2224 for (i = 0; i < back_num; i++) {
2225 arg[back_refs[i]].name = name;
2226 arg[back_refs[i]].len = name_end - name;
2227 }
2228 return 0;
2229}
2230
2231/*
2232 * call-seq:
2233 * mtch.inspect -> str
2234 *
2235 * Returns a printable version of <i>mtch</i>.
2236 *
2237 * puts /.$/.match("foo").inspect
2238 * #=> #<MatchData "o">
2239 *
2240 * puts /(.)(.)(.)/.match("foo").inspect
2241 * #=> #<MatchData "foo" 1:"f" 2:"o" 3:"o">
2242 *
2243 * puts /(.)(.)?(.)/.match("fo").inspect
2244 * #=> #<MatchData "fo" 1:"f" 2:nil 3:"o">
2245 *
2246 * puts /(?<foo>.)(?<bar>.)(?<baz>.)/.match("hoge").inspect
2247 * #=> #<MatchData "hog" foo:"h" bar:"o" baz:"g">
2248 *
2249 */
2250
2251static VALUE
2252match_inspect(VALUE match)
2253{
2255 VALUE str;
2256 int i;
2257 struct re_registers *regs = RMATCH_REGS(match);
2258 int num_regs = regs->num_regs;
2259 struct backref_name_tag *names;
2260 VALUE regexp = RMATCH(match)->regexp;
2261
2262 if (regexp == 0) {
2263 return rb_sprintf("#<%"PRIsVALUE":%p>", cname, (void*)match);
2264 }
2265 else if (NIL_P(regexp)) {
2266 return rb_sprintf("#<%"PRIsVALUE": %"PRIsVALUE">",
2267 cname, rb_reg_nth_match(0, match));
2268 }
2269
2270 names = ALLOCA_N(struct backref_name_tag, num_regs);
2271 MEMZERO(names, struct backref_name_tag, num_regs);
2272
2274 match_inspect_name_iter, names);
2275
2276 str = rb_str_buf_new2("#<");
2277 rb_str_append(str, cname);
2278
2279 for (i = 0; i < num_regs; i++) {
2280 VALUE v;
2281 rb_str_buf_cat2(str, " ");
2282 if (0 < i) {
2283 if (names[i].name)
2284 rb_str_buf_cat(str, (const char *)names[i].name, names[i].len);
2285 else {
2286 rb_str_catf(str, "%d", i);
2287 }
2288 rb_str_buf_cat2(str, ":");
2289 }
2290 v = rb_reg_nth_match(i, match);
2291 if (v == Qnil)
2292 rb_str_buf_cat2(str, "nil");
2293 else
2295 }
2296 rb_str_buf_cat2(str, ">");
2297
2298 return str;
2299}
2300
2302
2303static int
2304read_escaped_byte(const char **pp, const char *end, onig_errmsg_buffer err)
2305{
2306 const char *p = *pp;
2307 int code;
2308 int meta_prefix = 0, ctrl_prefix = 0;
2309 size_t len;
2310
2311 if (p == end || *p++ != '\\') {
2312 errcpy(err, "too short escaped multibyte character");
2313 return -1;
2314 }
2315
2316again:
2317 if (p == end) {
2318 errcpy(err, "too short escape sequence");
2319 return -1;
2320 }
2321 switch (*p++) {
2322 case '\\': code = '\\'; break;
2323 case 'n': code = '\n'; break;
2324 case 't': code = '\t'; break;
2325 case 'r': code = '\r'; break;
2326 case 'f': code = '\f'; break;
2327 case 'v': code = '\013'; break;
2328 case 'a': code = '\007'; break;
2329 case 'e': code = '\033'; break;
2330
2331 /* \OOO */
2332 case '0': case '1': case '2': case '3':
2333 case '4': case '5': case '6': case '7':
2334 p--;
2335 code = scan_oct(p, end < p+3 ? end-p : 3, &len);
2336 p += len;
2337 break;
2338
2339 case 'x': /* \xHH */
2340 code = scan_hex(p, end < p+2 ? end-p : 2, &len);
2341 if (len < 1) {
2342 errcpy(err, "invalid hex escape");
2343 return -1;
2344 }
2345 p += len;
2346 break;
2347
2348 case 'M': /* \M-X, \M-\C-X, \M-\cX */
2349 if (meta_prefix) {
2350 errcpy(err, "duplicate meta escape");
2351 return -1;
2352 }
2353 meta_prefix = 1;
2354 if (p+1 < end && *p++ == '-' && (*p & 0x80) == 0) {
2355 if (*p == '\\') {
2356 p++;
2357 goto again;
2358 }
2359 else {
2360 code = *p++;
2361 break;
2362 }
2363 }
2364 errcpy(err, "too short meta escape");
2365 return -1;
2366
2367 case 'C': /* \C-X, \C-\M-X */
2368 if (p == end || *p++ != '-') {
2369 errcpy(err, "too short control escape");
2370 return -1;
2371 }
2372 case 'c': /* \cX, \c\M-X */
2373 if (ctrl_prefix) {
2374 errcpy(err, "duplicate control escape");
2375 return -1;
2376 }
2377 ctrl_prefix = 1;
2378 if (p < end && (*p & 0x80) == 0) {
2379 if (*p == '\\') {
2380 p++;
2381 goto again;
2382 }
2383 else {
2384 code = *p++;
2385 break;
2386 }
2387 }
2388 errcpy(err, "too short control escape");
2389 return -1;
2390
2391 default:
2392 errcpy(err, "unexpected escape sequence");
2393 return -1;
2394 }
2395 if (code < 0 || 0xff < code) {
2396 errcpy(err, "invalid escape code");
2397 return -1;
2398 }
2399
2400 if (ctrl_prefix)
2401 code &= 0x1f;
2402 if (meta_prefix)
2403 code |= 0x80;
2404
2405 *pp = p;
2406 return code;
2407}
2408
2409static int
2410unescape_escaped_nonascii(const char **pp, const char *end, rb_encoding *enc,
2412{
2413 const char *p = *pp;
2414 int chmaxlen = rb_enc_mbmaxlen(enc);
2415 unsigned char *area = ALLOCA_N(unsigned char, chmaxlen);
2416 char *chbuf = (char *)area;
2417 int chlen = 0;
2418 int byte;
2419 int l;
2420
2421 memset(chbuf, 0, chmaxlen);
2422
2423 byte = read_escaped_byte(&p, end, err);
2424 if (byte == -1) {
2425 return -1;
2426 }
2427
2428 area[chlen++] = byte;
2429 while (chlen < chmaxlen &&
2430 MBCLEN_NEEDMORE_P(rb_enc_precise_mbclen(chbuf, chbuf+chlen, enc))) {
2431 byte = read_escaped_byte(&p, end, err);
2432 if (byte == -1) {
2433 return -1;
2434 }
2435 area[chlen++] = byte;
2436 }
2437
2438 l = rb_enc_precise_mbclen(chbuf, chbuf+chlen, enc);
2439 if (MBCLEN_INVALID_P(l)) {
2440 errcpy(err, "invalid multibyte escape");
2441 return -1;
2442 }
2443 if (1 < chlen || (area[0] & 0x80)) {
2444 rb_str_buf_cat(buf, chbuf, chlen);
2445
2446 if (*encp == 0)
2447 *encp = enc;
2448 else if (*encp != enc) {
2449 errcpy(err, "escaped non ASCII character in UTF-8 regexp");
2450 return -1;
2451 }
2452 }
2453 else {
2454 char escbuf[5];
2455 snprintf(escbuf, sizeof(escbuf), "\\x%02X", area[0]&0xff);
2456 rb_str_buf_cat(buf, escbuf, 4);
2457 }
2458 *pp = p;
2459 return 0;
2460}
2461
2462static int
2463check_unicode_range(unsigned long code, onig_errmsg_buffer err)
2464{
2465 if ((0xd800 <= code && code <= 0xdfff) || /* Surrogates */
2466 0x10ffff < code) {
2467 errcpy(err, "invalid Unicode range");
2468 return -1;
2469 }
2470 return 0;
2471}
2472
2473static int
2474append_utf8(unsigned long uv,
2476{
2477 if (check_unicode_range(uv, err) != 0)
2478 return -1;
2479 if (uv < 0x80) {
2480 char escbuf[5];
2481 snprintf(escbuf, sizeof(escbuf), "\\x%02X", (int)uv);
2482 rb_str_buf_cat(buf, escbuf, 4);
2483 }
2484 else {
2485 int len;
2486 char utf8buf[6];
2487 len = rb_uv_to_utf8(utf8buf, uv);
2488 rb_str_buf_cat(buf, utf8buf, len);
2489
2490 if (*encp == 0)
2491 *encp = rb_utf8_encoding();
2492 else if (*encp != rb_utf8_encoding()) {
2493 errcpy(err, "UTF-8 character in non UTF-8 regexp");
2494 return -1;
2495 }
2496 }
2497 return 0;
2498}
2499
2500static int
2501unescape_unicode_list(const char **pp, const char *end,
2503{
2504 const char *p = *pp;
2505 int has_unicode = 0;
2506 unsigned long code;
2507 size_t len;
2508
2509 while (p < end && ISSPACE(*p)) p++;
2510
2511 while (1) {
2512 code = ruby_scan_hex(p, end-p, &len);
2513 if (len == 0)
2514 break;
2515 if (6 < len) { /* max 10FFFF */
2516 errcpy(err, "invalid Unicode range");
2517 return -1;
2518 }
2519 p += len;
2520 if (append_utf8(code, buf, encp, err) != 0)
2521 return -1;
2522 has_unicode = 1;
2523
2524 while (p < end && ISSPACE(*p)) p++;
2525 }
2526
2527 if (has_unicode == 0) {
2528 errcpy(err, "invalid Unicode list");
2529 return -1;
2530 }
2531
2532 *pp = p;
2533
2534 return 0;
2535}
2536
2537static int
2538unescape_unicode_bmp(const char **pp, const char *end,
2540{
2541 const char *p = *pp;
2542 size_t len;
2543 unsigned long code;
2544
2545 if (end < p+4) {
2546 errcpy(err, "invalid Unicode escape");
2547 return -1;
2548 }
2549 code = ruby_scan_hex(p, 4, &len);
2550 if (len != 4) {
2551 errcpy(err, "invalid Unicode escape");
2552 return -1;
2553 }
2554 if (append_utf8(code, buf, encp, err) != 0)
2555 return -1;
2556 *pp = p + 4;
2557 return 0;
2558}
2559
2560static int
2561unescape_nonascii(const char *p, const char *end, rb_encoding *enc,
2562 VALUE buf, rb_encoding **encp, int *has_property,
2564{
2565 unsigned char c;
2566 char smallbuf[2];
2567
2568 while (p < end) {
2569 int chlen = rb_enc_precise_mbclen(p, end, enc);
2570 if (!MBCLEN_CHARFOUND_P(chlen)) {
2571 invalid_multibyte:
2572 errcpy(err, "invalid multibyte character");
2573 return -1;
2574 }
2575 chlen = MBCLEN_CHARFOUND_LEN(chlen);
2576 if (1 < chlen || (*p & 0x80)) {
2577 multibyte:
2578 rb_str_buf_cat(buf, p, chlen);
2579 p += chlen;
2580 if (*encp == 0)
2581 *encp = enc;
2582 else if (*encp != enc) {
2583 errcpy(err, "non ASCII character in UTF-8 regexp");
2584 return -1;
2585 }
2586 continue;
2587 }
2588
2589 switch (c = *p++) {
2590 case '\\':
2591 if (p == end) {
2592 errcpy(err, "too short escape sequence");
2593 return -1;
2594 }
2595 chlen = rb_enc_precise_mbclen(p, end, enc);
2596 if (!MBCLEN_CHARFOUND_P(chlen)) {
2597 goto invalid_multibyte;
2598 }
2599 if ((chlen = MBCLEN_CHARFOUND_LEN(chlen)) > 1) {
2600 /* include the previous backslash */
2601 --p;
2602 ++chlen;
2603 goto multibyte;
2604 }
2605 switch (c = *p++) {
2606 case '1': case '2': case '3':
2607 case '4': case '5': case '6': case '7': /* \O, \OO, \OOO or backref */
2608 {
2609 size_t len = end-(p-1), octlen;
2610 if (ruby_scan_oct(p-1, len < 3 ? len : 3, &octlen) <= 0177) {
2611 /* backref or 7bit octal.
2612 no need to unescape anyway.
2613 re-escaping may break backref */
2614 goto escape_asis;
2615 }
2616 }
2617 /* xxx: How about more than 199 subexpressions? */
2618
2619 case '0': /* \0, \0O, \0OO */
2620
2621 case 'x': /* \xHH */
2622 case 'c': /* \cX, \c\M-X */
2623 case 'C': /* \C-X, \C-\M-X */
2624 case 'M': /* \M-X, \M-\C-X, \M-\cX */
2625 p = p-2;
2626 if (enc == rb_usascii_encoding()) {
2627 const char *pbeg = p;
2628 int byte = read_escaped_byte(&p, end, err);
2629 if (byte == -1) return -1;
2630 c = byte;
2631 rb_str_buf_cat(buf, pbeg, p-pbeg);
2632 }
2633 else {
2634 if (unescape_escaped_nonascii(&p, end, enc, buf, encp, err) != 0)
2635 return -1;
2636 }
2637 break;
2638
2639 case 'u':
2640 if (p == end) {
2641 errcpy(err, "too short escape sequence");
2642 return -1;
2643 }
2644 if (*p == '{') {
2645 /* \u{H HH HHH HHHH HHHHH HHHHHH ...} */
2646 p++;
2647 if (unescape_unicode_list(&p, end, buf, encp, err) != 0)
2648 return -1;
2649 if (p == end || *p++ != '}') {
2650 errcpy(err, "invalid Unicode list");
2651 return -1;
2652 }
2653 break;
2654 }
2655 else {
2656 /* \uHHHH */
2657 if (unescape_unicode_bmp(&p, end, buf, encp, err) != 0)
2658 return -1;
2659 break;
2660 }
2661
2662 case 'p': /* \p{Hiragana} */
2663 case 'P':
2664 if (!*encp) {
2665 *has_property = 1;
2666 }
2667 goto escape_asis;
2668
2669 default: /* \n, \\, \d, \9, etc. */
2670escape_asis:
2671 smallbuf[0] = '\\';
2672 smallbuf[1] = c;
2673 rb_str_buf_cat(buf, smallbuf, 2);
2674 break;
2675 }
2676 break;
2677
2678 default:
2679 rb_str_buf_cat(buf, (char *)&c, 1);
2680 break;
2681 }
2682 }
2683
2684 return 0;
2685}
2686
2687static VALUE
2688rb_reg_preprocess(const char *p, const char *end, rb_encoding *enc,
2689 rb_encoding **fixed_enc, onig_errmsg_buffer err)
2690{
2691 VALUE buf;
2692 int has_property = 0;
2693
2694 buf = rb_str_buf_new(0);
2695
2696 if (rb_enc_asciicompat(enc))
2697 *fixed_enc = 0;
2698 else {
2699 *fixed_enc = enc;
2700 rb_enc_associate(buf, enc);
2701 }
2702
2703 if (unescape_nonascii(p, end, enc, buf, fixed_enc, &has_property, err) != 0)
2704 return Qnil;
2705
2706 if (has_property && !*fixed_enc) {
2707 *fixed_enc = enc;
2708 }
2709
2710 if (*fixed_enc) {
2711 rb_enc_associate(buf, *fixed_enc);
2712 }
2713
2714 return buf;
2715}
2716
2717VALUE
2719{
2720 rb_encoding *fixed_enc = 0;
2722 VALUE buf;
2723 char *p, *end;
2724 rb_encoding *enc;
2725
2727 p = RSTRING_PTR(str);
2728 end = p + RSTRING_LEN(str);
2729 enc = rb_enc_get(str);
2730
2731 buf = rb_reg_preprocess(p, end, enc, &fixed_enc, err);
2733
2734 if (buf == Qnil) {
2735 return rb_reg_error_desc(str, 0, err);
2736 }
2737 return Qnil;
2738}
2739
2740static VALUE
2741rb_reg_preprocess_dregexp(VALUE ary, int options)
2742{
2743 rb_encoding *fixed_enc = 0;
2744 rb_encoding *regexp_enc = 0;
2746 int i;
2747 VALUE result = 0;
2748 rb_encoding *ascii8bit = rb_ascii8bit_encoding();
2749
2750 if (RARRAY_LEN(ary) == 0) {
2751 rb_raise(rb_eArgError, "no arguments given");
2752 }
2753
2754 for (i = 0; i < RARRAY_LEN(ary); i++) {
2755 VALUE str = RARRAY_AREF(ary, i);
2756 VALUE buf;
2757 char *p, *end;
2758 rb_encoding *src_enc;
2759
2760 src_enc = rb_enc_get(str);
2761 if (options & ARG_ENCODING_NONE &&
2762 src_enc != ascii8bit) {
2763 if (str_coderange(str) != ENC_CODERANGE_7BIT)
2764 rb_raise(rb_eRegexpError, "/.../n has a non escaped non ASCII character in non ASCII-8BIT script");
2765 else
2766 src_enc = ascii8bit;
2767 }
2768
2770 p = RSTRING_PTR(str);
2771 end = p + RSTRING_LEN(str);
2772
2773 buf = rb_reg_preprocess(p, end, src_enc, &fixed_enc, err);
2774
2775 if (buf == Qnil)
2776 rb_raise(rb_eArgError, "%s", err);
2777
2778 if (fixed_enc != 0) {
2779 if (regexp_enc != 0 && regexp_enc != fixed_enc) {
2780 rb_raise(rb_eRegexpError, "encoding mismatch in dynamic regexp : %s and %s",
2781 rb_enc_name(regexp_enc), rb_enc_name(fixed_enc));
2782 }
2783 regexp_enc = fixed_enc;
2784 }
2785
2786 if (!result)
2787 result = rb_str_new3(str);
2788 else
2789 rb_str_buf_append(result, str);
2790 }
2791 if (regexp_enc) {
2792 rb_enc_associate(result, regexp_enc);
2793 }
2794
2795 return result;
2796}
2797
2798static int
2799rb_reg_initialize(VALUE obj, const char *s, long len, rb_encoding *enc,
2800 int options, onig_errmsg_buffer err,
2801 const char *sourcefile, int sourceline)
2802{
2803 struct RRegexp *re = RREGEXP(obj);
2804 VALUE unescaped;
2805 rb_encoding *fixed_enc = 0;
2807
2808 rb_check_frozen(obj);
2809 if (FL_TEST(obj, REG_LITERAL))
2810 rb_raise(rb_eSecurityError, "can't modify literal regexp");
2811 if (re->ptr)
2812 rb_raise(rb_eTypeError, "already initialized regexp");
2813 re->ptr = 0;
2814
2815 if (rb_enc_dummy_p(enc)) {
2816 errcpy(err, "can't make regexp with dummy encoding");
2817 return -1;
2818 }
2819
2820 unescaped = rb_reg_preprocess(s, s+len, enc, &fixed_enc, err);
2821 if (unescaped == Qnil)
2822 return -1;
2823
2824 if (fixed_enc) {
2825 if ((fixed_enc != enc && (options & ARG_ENCODING_FIXED)) ||
2826 (fixed_enc != a_enc && (options & ARG_ENCODING_NONE))) {
2827 errcpy(err, "incompatible character encoding");
2828 return -1;
2829 }
2830 if (fixed_enc != a_enc) {
2831 options |= ARG_ENCODING_FIXED;
2832 enc = fixed_enc;
2833 }
2834 }
2835 else if (!(options & ARG_ENCODING_FIXED)) {
2836 enc = rb_usascii_encoding();
2837 }
2838
2839 rb_enc_associate((VALUE)re, enc);
2840 if ((options & ARG_ENCODING_FIXED) || fixed_enc) {
2841 re->basic.flags |= KCODE_FIXED;
2842 }
2843 if (options & ARG_ENCODING_NONE) {
2845 }
2846
2847 re->ptr = make_regexp(RSTRING_PTR(unescaped), RSTRING_LEN(unescaped), enc,
2848 options & ARG_REG_OPTION_MASK, err,
2849 sourcefile, sourceline);
2850 if (!re->ptr) return -1;
2851 RB_GC_GUARD(unescaped);
2852 return 0;
2853}
2854
2855static void
2856reg_set_source(VALUE reg, VALUE str, rb_encoding *enc)
2857{
2858 rb_encoding *regenc = rb_enc_get(reg);
2859 if (regenc != enc) {
2860 str = rb_enc_associate(rb_str_dup(str), enc = regenc);
2861 }
2862 RB_OBJ_WRITE(reg, &RREGEXP(reg)->src, rb_fstring(str));
2863}
2864
2865static int
2866rb_reg_initialize_str(VALUE obj, VALUE str, int options, onig_errmsg_buffer err,
2867 const char *sourcefile, int sourceline)
2868{
2869 int ret;
2870 rb_encoding *str_enc = rb_enc_get(str), *enc = str_enc;
2871 if (options & ARG_ENCODING_NONE) {
2872 rb_encoding *ascii8bit = rb_ascii8bit_encoding();
2873 if (enc != ascii8bit) {
2874 if (str_coderange(str) != ENC_CODERANGE_7BIT) {
2875 errcpy(err, "/.../n has a non escaped non ASCII character in non ASCII-8BIT script");
2876 return -1;
2877 }
2878 enc = ascii8bit;
2879 }
2880 }
2881 ret = rb_reg_initialize(obj, RSTRING_PTR(str), RSTRING_LEN(str), enc,
2882 options, err, sourcefile, sourceline);
2883 if (ret == 0) reg_set_source(obj, str, str_enc);
2884 return ret;
2885}
2886
2887static VALUE
2888rb_reg_s_alloc(VALUE klass)
2889{
2891
2892 re->ptr = 0;
2893 RB_OBJ_WRITE(re, &re->src, 0);
2894 re->usecnt = 0;
2895
2896 return (VALUE)re;
2897}
2898
2899VALUE
2901{
2902 return rb_reg_s_alloc(rb_cRegexp);
2903}
2904
2905VALUE
2906rb_reg_new_str(VALUE s, int options)
2907{
2908 return rb_reg_init_str(rb_reg_alloc(), s, options);
2909}
2910
2911VALUE
2912rb_reg_init_str(VALUE re, VALUE s, int options)
2913{
2915
2916 if (rb_reg_initialize_str(re, s, options, err, NULL, 0) != 0) {
2917 rb_reg_raise_str(s, options, err);
2918 }
2919
2920 return re;
2921}
2922
2923static VALUE
2924rb_reg_init_str_enc(VALUE re, VALUE s, rb_encoding *enc, int options)
2925{
2927
2928 if (rb_reg_initialize(re, RSTRING_PTR(s), RSTRING_LEN(s),
2929 enc, options, err, NULL, 0) != 0) {
2930 rb_reg_raise_str(s, options, err);
2931 }
2932 reg_set_source(re, s, enc);
2933
2934 return re;
2935}
2936
2939{
2940 VALUE re = rb_reg_new_str(rb_reg_preprocess_dregexp(ary, opt), opt);
2941 rb_obj_freeze(re);
2942 return re;
2943}
2944
2945VALUE
2946rb_enc_reg_new(const char *s, long len, rb_encoding *enc, int options)
2947{
2948 VALUE re = rb_reg_alloc();
2950
2951 if (rb_reg_initialize(re, s, len, enc, options, err, NULL, 0) != 0) {
2952 rb_enc_reg_raise(s, len, enc, options, err);
2953 }
2954 RB_OBJ_WRITE(re, &RREGEXP(re)->src, rb_fstring(rb_enc_str_new(s, len, enc)));
2955
2956 return re;
2957}
2958
2959VALUE
2960rb_reg_new(const char *s, long len, int options)
2961{
2962 return rb_enc_reg_new(s, len, rb_ascii8bit_encoding(), options);
2963}
2964
2965VALUE
2966rb_reg_compile(VALUE str, int options, const char *sourcefile, int sourceline)
2967{
2968 VALUE re = rb_reg_alloc();
2970
2971 if (!str) str = rb_str_new(0,0);
2972 if (rb_reg_initialize_str(re, str, options, err, sourcefile, sourceline) != 0) {
2973 rb_set_errinfo(rb_reg_error_desc(str, options, err));
2974 return Qnil;
2975 }
2976 FL_SET(re, REG_LITERAL);
2977 rb_obj_freeze(re);
2978 return re;
2979}
2980
2981static VALUE reg_cache;
2982
2983VALUE
2985{
2986 if (reg_cache && RREGEXP_SRC_LEN(reg_cache) == RSTRING_LEN(str)
2987 && ENCODING_GET(reg_cache) == ENCODING_GET(str)
2988 && memcmp(RREGEXP_SRC_PTR(reg_cache), RSTRING_PTR(str), RSTRING_LEN(str)) == 0)
2989 return reg_cache;
2990
2991 return reg_cache = rb_reg_new_str(str, 0);
2992}
2993
2994static st_index_t reg_hash(VALUE re);
2995/*
2996 * call-seq:
2997 * rxp.hash -> integer
2998 *
2999 * Produce a hash based on the text and options of this regular expression.
3000 *
3001 * See also Object#hash.
3002 */
3003
3004static VALUE
3005rb_reg_hash(VALUE re)
3006{
3007 st_index_t hashval = reg_hash(re);
3008 return ST2FIX(hashval);
3009}
3010
3011static st_index_t
3012reg_hash(VALUE re)
3013{
3014 st_index_t hashval;
3015
3016 rb_reg_check(re);
3017 hashval = RREGEXP_PTR(re)->options;
3018 hashval = rb_hash_uint(hashval, rb_memhash(RREGEXP_SRC_PTR(re), RREGEXP_SRC_LEN(re)));
3019 return rb_hash_end(hashval);
3020}
3021
3022
3023/*
3024 * call-seq:
3025 * rxp == other_rxp -> true or false
3026 * rxp.eql?(other_rxp) -> true or false
3027 *
3028 * Equality---Two regexps are equal if their patterns are identical, they have
3029 * the same character set code, and their <code>casefold?</code> values are the
3030 * same.
3031 *
3032 * /abc/ == /abc/x #=> false
3033 * /abc/ == /abc/i #=> false
3034 * /abc/ == /abc/u #=> false
3035 * /abc/u == /abc/n #=> false
3036 */
3037
3038static VALUE
3039rb_reg_equal(VALUE re1, VALUE re2)
3040{
3041 if (re1 == re2) return Qtrue;
3042 if (!RB_TYPE_P(re2, T_REGEXP)) return Qfalse;
3043 rb_reg_check(re1); rb_reg_check(re2);
3044 if (FL_TEST(re1, KCODE_FIXED) != FL_TEST(re2, KCODE_FIXED)) return Qfalse;
3045 if (RREGEXP_PTR(re1)->options != RREGEXP_PTR(re2)->options) return Qfalse;
3046 if (RREGEXP_SRC_LEN(re1) != RREGEXP_SRC_LEN(re2)) return Qfalse;
3047 if (ENCODING_GET(re1) != ENCODING_GET(re2)) return Qfalse;
3048 if (memcmp(RREGEXP_SRC_PTR(re1), RREGEXP_SRC_PTR(re2), RREGEXP_SRC_LEN(re1)) == 0) {
3049 return Qtrue;
3050 }
3051 return Qfalse;
3052}
3053
3054/*
3055 * call-seq:
3056 * mtch.hash -> integer
3057 *
3058 * Produce a hash based on the target string, regexp and matched
3059 * positions of this matchdata.
3060 *
3061 * See also Object#hash.
3062 */
3063
3064static VALUE
3065match_hash(VALUE match)
3066{
3067 const struct re_registers *regs;
3068 st_index_t hashval;
3069
3070 match_check(match);
3072 hashval = rb_hash_uint(hashval, reg_hash(match_regexp(match)));
3073 regs = RMATCH_REGS(match);
3074 hashval = rb_hash_uint(hashval, regs->num_regs);
3075 hashval = rb_hash_uint(hashval, rb_memhash(regs->beg, regs->num_regs * sizeof(*regs->beg)));
3076 hashval = rb_hash_uint(hashval, rb_memhash(regs->end, regs->num_regs * sizeof(*regs->end)));
3077 hashval = rb_hash_end(hashval);
3078 return ST2FIX(hashval);
3079}
3080
3081/*
3082 * call-seq:
3083 * mtch == mtch2 -> true or false
3084 * mtch.eql?(mtch2) -> true or false
3085 *
3086 * Equality---Two matchdata are equal if their target strings,
3087 * patterns, and matched positions are identical.
3088 */
3089
3090static VALUE
3091match_equal(VALUE match1, VALUE match2)
3092{
3093 const struct re_registers *regs1, *regs2;
3094
3095 if (match1 == match2) return Qtrue;
3096 if (!RB_TYPE_P(match2, T_MATCH)) return Qfalse;
3097 if (!RMATCH(match1)->regexp || !RMATCH(match2)->regexp) return Qfalse;
3098 if (!rb_str_equal(RMATCH(match1)->str, RMATCH(match2)->str)) return Qfalse;
3099 if (!rb_reg_equal(match_regexp(match1), match_regexp(match2))) return Qfalse;
3100 regs1 = RMATCH_REGS(match1);
3101 regs2 = RMATCH_REGS(match2);
3102 if (regs1->num_regs != regs2->num_regs) return Qfalse;
3103 if (memcmp(regs1->beg, regs2->beg, regs1->num_regs * sizeof(*regs1->beg))) return Qfalse;
3104 if (memcmp(regs1->end, regs2->end, regs1->num_regs * sizeof(*regs1->end))) return Qfalse;
3105 return Qtrue;
3106}
3107
3108static VALUE
3109reg_operand(VALUE s, int check)
3110{
3111 if (SYMBOL_P(s)) {
3112 return rb_sym2str(s);
3113 }
3114 else if (RB_TYPE_P(s, T_STRING)) {
3115 return s;
3116 }
3117 else {
3118 return check ? rb_str_to_str(s) : rb_check_string_type(s);
3119 }
3120}
3121
3122static long
3123reg_match_pos(VALUE re, VALUE *strp, long pos, VALUE* set_match)
3124{
3125 VALUE str = *strp;
3126
3127 if (NIL_P(str)) {
3129 return -1;
3130 }
3131 *strp = str = reg_operand(str, TRUE);
3132 if (pos != 0) {
3133 if (pos < 0) {
3134 VALUE l = rb_str_length(str);
3135 pos += NUM2INT(l);
3136 if (pos < 0) {
3137 return pos;
3138 }
3139 }
3140 pos = rb_str_offset(str, pos);
3141 }
3142 return rb_reg_search_set_match(re, str, pos, 0, 1, set_match);
3143}
3144
3145/*
3146 * call-seq:
3147 * rxp =~ str -> integer or nil
3148 *
3149 * Match---Matches <i>rxp</i> against <i>str</i>.
3150 *
3151 * /at/ =~ "input data" #=> 7
3152 * /ax/ =~ "input data" #=> nil
3153 *
3154 * If <code>=~</code> is used with a regexp literal with named captures,
3155 * captured strings (or nil) is assigned to local variables named by
3156 * the capture names.
3157 *
3158 * /(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/ =~ " x = y "
3159 * p lhs #=> "x"
3160 * p rhs #=> "y"
3161 *
3162 * If it is not matched, nil is assigned for the variables.
3163 *
3164 * /(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/ =~ " x = "
3165 * p lhs #=> nil
3166 * p rhs #=> nil
3167 *
3168 * This assignment is implemented in the Ruby parser.
3169 * The parser detects 'regexp-literal =~ expression' for the assignment.
3170 * The regexp must be a literal without interpolation and placed at left hand side.
3171 *
3172 * The assignment does not occur if the regexp is not a literal.
3173 *
3174 * re = /(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/
3175 * re =~ " x = y "
3176 * p lhs # undefined local variable
3177 * p rhs # undefined local variable
3178 *
3179 * A regexp interpolation, <code>#{}</code>, also disables
3180 * the assignment.
3181 *
3182 * rhs_pat = /(?<rhs>\w+)/
3183 * /(?<lhs>\w+)\s*=\s*#{rhs_pat}/ =~ "x = y"
3184 * p lhs # undefined local variable
3185 *
3186 * The assignment does not occur if the regexp is placed at the right hand side.
3187 *
3188 * " x = y " =~ /(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/
3189 * p lhs, rhs # undefined local variable
3190 *
3191 */
3192
3193VALUE
3195{
3196 long pos = reg_match_pos(re, &str, 0, NULL);
3197 if (pos < 0) return Qnil;
3198 pos = rb_str_sublen(str, pos);
3199 return LONG2FIX(pos);
3200}
3201
3202/*
3203 * call-seq:
3204 * rxp === str -> true or false
3205 *
3206 * Case Equality---Used in case statements.
3207 *
3208 * a = "HELLO"
3209 * case a
3210 * when /\A[a-z]*\z/; print "Lower case\n"
3211 * when /\A[A-Z]*\z/; print "Upper case\n"
3212 * else; print "Mixed case\n"
3213 * end
3214 * #=> "Upper case"
3215 *
3216 * Following a regular expression literal with the #=== operator allows you to
3217 * compare against a String.
3218 *
3219 * /^[a-z]*$/ === "HELLO" #=> false
3220 * /^[A-Z]*$/ === "HELLO" #=> true
3221 */
3222
3223VALUE
3225{
3226 long start;
3227
3228 str = reg_operand(str, FALSE);
3229 if (NIL_P(str)) {
3231 return Qfalse;
3232 }
3233 start = rb_reg_search(re, str, 0, 0);
3234 if (start < 0) {
3235 return Qfalse;
3236 }
3237 return Qtrue;
3238}
3239
3240
3241/*
3242 * call-seq:
3243 * ~ rxp -> integer or nil
3244 *
3245 * Match---Matches <i>rxp</i> against the contents of <code>$_</code>.
3246 * Equivalent to <code><i>rxp</i> =~ $_</code>.
3247 *
3248 * $_ = "input data"
3249 * ~ /at/ #=> 7
3250 */
3251
3252VALUE
3254{
3255 long start;
3256 VALUE line = rb_lastline_get();
3257
3258 if (!RB_TYPE_P(line, T_STRING)) {
3260 return Qnil;
3261 }
3262
3263 start = rb_reg_search(re, line, 0, 0);
3264 if (start < 0) {
3265 return Qnil;
3266 }
3267 start = rb_str_sublen(line, start);
3268 return LONG2FIX(start);
3269}
3270
3271
3272/*
3273 * call-seq:
3274 * rxp.match(str, pos=0) -> matchdata or nil
3275 * rxp.match(str, pos=0) {|match| block } -> obj
3276 *
3277 * Returns a MatchData object describing the match, or
3278 * <code>nil</code> if there was no match. This is equivalent to
3279 * retrieving the value of the special variable <code>$~</code>
3280 * following a normal match. If the second parameter is present, it
3281 * specifies the position in the string to begin the search.
3282 *
3283 * /(.)(.)(.)/.match("abc")[2] #=> "b"
3284 * /(.)(.)/.match("abc", 1)[2] #=> "c"
3285 *
3286 * If a block is given, invoke the block with MatchData if match succeed, so
3287 * that you can write
3288 *
3289 * /M(.*)/.match("Matz") do |m|
3290 * puts m[0]
3291 * puts m[1]
3292 * end
3293 *
3294 * instead of
3295 *
3296 * if m = /M(.*)/.match("Matz")
3297 * puts m[0]
3298 * puts m[1]
3299 * end
3300 *
3301 * The return value is a value from block execution in this case.
3302 */
3303
3304static VALUE
3305rb_reg_match_m(int argc, VALUE *argv, VALUE re)
3306{
3307 VALUE result = Qnil, str, initpos;
3308 long pos;
3309
3310 if (rb_scan_args(argc, argv, "11", &str, &initpos) == 2) {
3311 pos = NUM2LONG(initpos);
3312 }
3313 else {
3314 pos = 0;
3315 }
3316
3317 pos = reg_match_pos(re, &str, pos, &result);
3318 if (pos < 0) {
3320 return Qnil;
3321 }
3322 rb_match_busy(result);
3323 if (!NIL_P(result) && rb_block_given_p()) {
3324 return rb_yield(result);
3325 }
3326 return result;
3327}
3328
3329/*
3330 * call-seq:
3331 * rxp.match?(str) -> true or false
3332 * rxp.match?(str,pos) -> true or false
3333 *
3334 * Returns a <code>true</code> or <code>false</code> indicates whether the
3335 * regexp is matched or not without updating $~ and other related variables.
3336 * If the second parameter is present, it specifies the position in the string
3337 * to begin the search.
3338 *
3339 * /R.../.match?("Ruby") #=> true
3340 * /R.../.match?("Ruby", 1) #=> false
3341 * /P.../.match?("Ruby") #=> false
3342 * $& #=> nil
3343 */
3344
3345static VALUE
3346rb_reg_match_m_p(int argc, VALUE *argv, VALUE re)
3347{
3348 long pos = rb_check_arity(argc, 1, 2) > 1 ? NUM2LONG(argv[1]) : 0;
3349 return rb_reg_match_p(re, argv[0], pos);
3350}
3351
3352VALUE
3354{
3355 regex_t *reg;
3357 OnigPosition result;
3358 const UChar *start, *end;
3359 int tmpreg;
3360
3361 if (NIL_P(str)) return Qfalse;
3363 if (pos) {
3364 if (pos < 0) {
3365 pos += NUM2LONG(rb_str_length(str));
3366 if (pos < 0) return Qfalse;
3367 }
3368 if (pos > 0) {
3369 long len = 1;
3370 const char *beg = rb_str_subpos(str, pos, &len);
3371 if (!beg) return Qfalse;
3372 pos = beg - RSTRING_PTR(str);
3373 }
3374 }
3375 reg = rb_reg_prepare_re0(re, str, err);
3376 tmpreg = reg != RREGEXP_PTR(re);
3377 if (!tmpreg) RREGEXP(re)->usecnt++;
3378 start = ((UChar*)RSTRING_PTR(str));
3379 end = start + RSTRING_LEN(str);
3380 result = onig_search(reg, start, end, start + pos, end,
3382 if (!tmpreg) RREGEXP(re)->usecnt--;
3383 if (tmpreg) {
3384 if (RREGEXP(re)->usecnt) {
3385 onig_free(reg);
3386 }
3387 else {
3389 RREGEXP_PTR(re) = reg;
3390 }
3391 }
3392 if (result < 0) {
3393 if (result == ONIG_MISMATCH) {
3394 return Qfalse;
3395 }
3396 else {
3397 onig_error_code_to_str((UChar*)err, (int)result);
3398 rb_reg_raise(RREGEXP_SRC_PTR(re), RREGEXP_SRC_LEN(re), err, re);
3399 }
3400 }
3401 return Qtrue;
3402}
3403
3404/*
3405 * Document-method: compile
3406 *
3407 * Alias for Regexp.new
3408 */
3409
3410/*
3411 * call-seq:
3412 * Regexp.new(string, [options]) -> regexp
3413 * Regexp.new(regexp) -> regexp
3414 * Regexp.compile(string, [options]) -> regexp
3415 * Regexp.compile(regexp) -> regexp
3416 *
3417 * Constructs a new regular expression from +pattern+, which can be either a
3418 * String or a Regexp (in which case that regexp's options are propagated),
3419 * and new options may not be specified (a change as of Ruby 1.8).
3420 *
3421 * If +options+ is an Integer, it should be one or more of the constants
3422 * Regexp::EXTENDED, Regexp::IGNORECASE, and Regexp::MULTILINE,
3423 * <em>or</em>-ed together. Otherwise, if +options+ is not
3424 * +nil+ or +false+, the regexp will be case insensitive.
3425 *
3426 * r1 = Regexp.new('^a-z+:\\s+\w+') #=> /^a-z+:\s+\w+/
3427 * r2 = Regexp.new('cat', true) #=> /cat/i
3428 * r3 = Regexp.new(r2) #=> /cat/i
3429 * r4 = Regexp.new('dog', Regexp::EXTENDED | Regexp::IGNORECASE) #=> /dog/ix
3430 */
3431
3432static VALUE
3433rb_reg_initialize_m(int argc, VALUE *argv, VALUE self)
3434{
3435 int flags = 0;
3436 VALUE str;
3437 rb_encoding *enc = 0;
3438
3439 rb_check_arity(argc, 1, 3);
3440 if (RB_TYPE_P(argv[0], T_REGEXP)) {
3441 VALUE re = argv[0];
3442
3443 if (argc > 1) {
3444 rb_warn("flags ignored");
3445 }
3446 rb_reg_check(re);
3447 flags = rb_reg_options(re);
3448 str = RREGEXP_SRC(re);
3449 }
3450 else {
3451 if (argc >= 2) {
3452 if (FIXNUM_P(argv[1])) flags = FIX2INT(argv[1]);
3453 else if (RTEST(argv[1])) flags = ONIG_OPTION_IGNORECASE;
3454 }
3455 if (argc == 3 && !NIL_P(argv[2])) {
3456 char *kcode = StringValuePtr(argv[2]);
3457 if (kcode[0] == 'n' || kcode[0] == 'N') {
3458 enc = rb_ascii8bit_encoding();
3459 flags |= ARG_ENCODING_NONE;
3460 }
3461 else {
3462 rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "encoding option is ignored - %s", kcode);
3463 }
3464 }
3465 str = StringValue(argv[0]);
3466 }
3467 if (enc && rb_enc_get(str) != enc)
3468 rb_reg_init_str_enc(self, str, enc, flags);
3469 else
3470 rb_reg_init_str(self, str, flags);
3471 return self;
3472}
3473
3474VALUE
3476{
3477 rb_encoding *enc = rb_enc_get(str);
3478 char *s, *send, *t;
3479 VALUE tmp;
3480 int c, clen;
3481 int ascii_only = rb_enc_str_asciionly_p(str);
3482
3483 s = RSTRING_PTR(str);
3484 send = s + RSTRING_LEN(str);
3485 while (s < send) {
3486 c = rb_enc_ascget(s, send, &clen, enc);
3487 if (c == -1) {
3488 s += mbclen(s, send, enc);
3489 continue;
3490 }
3491 switch (c) {
3492 case '[': case ']': case '{': case '}':
3493 case '(': case ')': case '|': case '-':
3494 case '*': case '.': case '\\':
3495 case '?': case '+': case '^': case '$':
3496 case ' ': case '#':
3497 case '\t': case '\f': case '\v': case '\n': case '\r':
3498 goto meta_found;
3499 }
3500 s += clen;
3501 }
3502 tmp = rb_str_new3(str);
3503 if (ascii_only) {
3505 }
3506 return tmp;
3507
3508 meta_found:
3509 tmp = rb_str_new(0, RSTRING_LEN(str)*2);
3510 if (ascii_only) {
3512 }
3513 else {
3514 rb_enc_copy(tmp, str);
3515 }
3516 t = RSTRING_PTR(tmp);
3517 /* copy upto metacharacter */
3518 const char *p = RSTRING_PTR(str);
3519 memcpy(t, p, s - p);
3520 t += s - p;
3521
3522 while (s < send) {
3523 c = rb_enc_ascget(s, send, &clen, enc);
3524 if (c == -1) {
3525 int n = mbclen(s, send, enc);
3526
3527 while (n--)
3528 *t++ = *s++;
3529 continue;
3530 }
3531 s += clen;
3532 switch (c) {
3533 case '[': case ']': case '{': case '}':
3534 case '(': case ')': case '|': case '-':
3535 case '*': case '.': case '\\':
3536 case '?': case '+': case '^': case '$':
3537 case '#':
3538 t += rb_enc_mbcput('\\', t, enc);
3539 break;
3540 case ' ':
3541 t += rb_enc_mbcput('\\', t, enc);
3542 t += rb_enc_mbcput(' ', t, enc);
3543 continue;
3544 case '\t':
3545 t += rb_enc_mbcput('\\', t, enc);
3546 t += rb_enc_mbcput('t', t, enc);
3547 continue;
3548 case '\n':
3549 t += rb_enc_mbcput('\\', t, enc);
3550 t += rb_enc_mbcput('n', t, enc);
3551 continue;
3552 case '\r':
3553 t += rb_enc_mbcput('\\', t, enc);
3554 t += rb_enc_mbcput('r', t, enc);
3555 continue;
3556 case '\f':
3557 t += rb_enc_mbcput('\\', t, enc);
3558 t += rb_enc_mbcput('f', t, enc);
3559 continue;
3560 case '\v':
3561 t += rb_enc_mbcput('\\', t, enc);
3562 t += rb_enc_mbcput('v', t, enc);
3563 continue;
3564 }
3565 t += rb_enc_mbcput(c, t, enc);
3566 }
3567 rb_str_resize(tmp, t - RSTRING_PTR(tmp));
3568 return tmp;
3569}
3570
3571
3572/*
3573 * call-seq:
3574 * Regexp.escape(str) -> string
3575 * Regexp.quote(str) -> string
3576 *
3577 * Escapes any characters that would have special meaning in a regular
3578 * expression. Returns a new escaped string with the same or compatible
3579 * encoding. For any string,
3580 * <code>Regexp.new(Regexp.escape(<i>str</i>))=~<i>str</i></code> will be true.
3581 *
3582 * Regexp.escape('\*?{}.') #=> \\\*\?\{\}\.
3583 *
3584 */
3585
3586static VALUE
3587rb_reg_s_quote(VALUE c, VALUE str)
3588{
3589 return rb_reg_quote(reg_operand(str, TRUE));
3590}
3591
3592int
3594{
3595 int options;
3596
3597 rb_reg_check(re);
3598 options = RREGEXP_PTR(re)->options & ARG_REG_OPTION_MASK;
3599 if (RBASIC(re)->flags & KCODE_FIXED) options |= ARG_ENCODING_FIXED;
3600 if (RBASIC(re)->flags & REG_ENCODING_NONE) options |= ARG_ENCODING_NONE;
3601 return options;
3602}
3603
3604VALUE
3606{
3607 return rb_check_convert_type(re, T_REGEXP, "Regexp", "to_regexp");
3608}
3609
3610/*
3611 * call-seq:
3612 * Regexp.try_convert(obj) -> re or nil
3613 *
3614 * Try to convert <i>obj</i> into a Regexp, using to_regexp method.
3615 * Returns converted regexp or nil if <i>obj</i> cannot be converted
3616 * for any reason.
3617 *
3618 * Regexp.try_convert(/re/) #=> /re/
3619 * Regexp.try_convert("re") #=> nil
3620 *
3621 * o = Object.new
3622 * Regexp.try_convert(o) #=> nil
3623 * def o.to_regexp() /foo/ end
3624 * Regexp.try_convert(o) #=> /foo/
3625 *
3626 */
3627static VALUE
3628rb_reg_s_try_convert(VALUE dummy, VALUE re)
3629{
3630 return rb_check_regexp_type(re);
3631}
3632
3633static VALUE
3634rb_reg_s_union(VALUE self, VALUE args0)
3635{
3636 long argc = RARRAY_LEN(args0);
3637
3638 if (argc == 0) {
3639 VALUE args[1];
3640 args[0] = rb_str_new2("(?!)");
3641 return rb_class_new_instance(1, args, rb_cRegexp);
3642 }
3643 else if (argc == 1) {
3644 VALUE arg = rb_ary_entry(args0, 0);
3645 VALUE re = rb_check_regexp_type(arg);
3646 if (!NIL_P(re))
3647 return re;
3648 else {
3649 VALUE quoted;
3650 quoted = rb_reg_s_quote(Qnil, arg);
3651 return rb_reg_new_str(quoted, 0);
3652 }
3653 }
3654 else {
3655 int i;
3656 VALUE source = rb_str_buf_new(0);
3657 rb_encoding *result_enc;
3658
3659 int has_asciionly = 0;
3660 rb_encoding *has_ascii_compat_fixed = 0;
3661 rb_encoding *has_ascii_incompat = 0;
3662
3663 for (i = 0; i < argc; i++) {
3664 volatile VALUE v;
3665 VALUE e = rb_ary_entry(args0, i);
3666
3667 if (0 < i)
3668 rb_str_buf_cat_ascii(source, "|");
3669
3670 v = rb_check_regexp_type(e);
3671 if (!NIL_P(v)) {
3672 rb_encoding *enc = rb_enc_get(v);
3673 if (!rb_enc_asciicompat(enc)) {
3674 if (!has_ascii_incompat)
3675 has_ascii_incompat = enc;
3676 else if (has_ascii_incompat != enc)
3677 rb_raise(rb_eArgError, "incompatible encodings: %s and %s",
3678 rb_enc_name(has_ascii_incompat), rb_enc_name(enc));
3679 }
3680 else if (rb_reg_fixed_encoding_p(v)) {
3681 if (!has_ascii_compat_fixed)
3682 has_ascii_compat_fixed = enc;
3683 else if (has_ascii_compat_fixed != enc)
3684 rb_raise(rb_eArgError, "incompatible encodings: %s and %s",
3685 rb_enc_name(has_ascii_compat_fixed), rb_enc_name(enc));
3686 }
3687 else {
3688 has_asciionly = 1;
3689 }
3690 v = rb_reg_str_with_term(v, -1);
3691 }
3692 else {
3693 rb_encoding *enc;
3694 StringValue(e);
3695 enc = rb_enc_get(e);
3696 if (!rb_enc_asciicompat(enc)) {
3697 if (!has_ascii_incompat)
3698 has_ascii_incompat = enc;
3699 else if (has_ascii_incompat != enc)
3700 rb_raise(rb_eArgError, "incompatible encodings: %s and %s",
3701 rb_enc_name(has_ascii_incompat), rb_enc_name(enc));
3702 }
3703 else if (rb_enc_str_asciionly_p(e)) {
3704 has_asciionly = 1;
3705 }
3706 else {
3707 if (!has_ascii_compat_fixed)
3708 has_ascii_compat_fixed = enc;
3709 else if (has_ascii_compat_fixed != enc)
3710 rb_raise(rb_eArgError, "incompatible encodings: %s and %s",
3711 rb_enc_name(has_ascii_compat_fixed), rb_enc_name(enc));
3712 }
3713 v = rb_reg_s_quote(Qnil, e);
3714 }
3715 if (has_ascii_incompat) {
3716 if (has_asciionly) {
3717 rb_raise(rb_eArgError, "ASCII incompatible encoding: %s",
3718 rb_enc_name(has_ascii_incompat));
3719 }
3720 if (has_ascii_compat_fixed) {
3721 rb_raise(rb_eArgError, "incompatible encodings: %s and %s",
3722 rb_enc_name(has_ascii_incompat), rb_enc_name(has_ascii_compat_fixed));
3723 }
3724 }
3725
3726 if (i == 0) {
3727 rb_enc_copy(source, v);
3728 }
3729 rb_str_append(source, v);
3730 }
3731
3732 if (has_ascii_incompat) {
3733 result_enc = has_ascii_incompat;
3734 }
3735 else if (has_ascii_compat_fixed) {
3736 result_enc = has_ascii_compat_fixed;
3737 }
3738 else {
3739 result_enc = rb_ascii8bit_encoding();
3740 }
3741
3742 rb_enc_associate(source, result_enc);
3743 return rb_class_new_instance(1, &source, rb_cRegexp);
3744 }
3745}
3746
3747/*
3748 * call-seq:
3749 * Regexp.union(pat1, pat2, ...) -> new_regexp
3750 * Regexp.union(pats_ary) -> new_regexp
3751 *
3752 * Return a Regexp object that is the union of the given
3753 * <em>pattern</em>s, i.e., will match any of its parts. The
3754 * <em>pattern</em>s can be Regexp objects, in which case their
3755 * options will be preserved, or Strings. If no patterns are given,
3756 * returns <code>/(?!)/</code>. The behavior is unspecified if any
3757 * given <em>pattern</em> contains capture.
3758 *
3759 * Regexp.union #=> /(?!)/
3760 * Regexp.union("penzance") #=> /penzance/
3761 * Regexp.union("a+b*c") #=> /a\+b\*c/
3762 * Regexp.union("skiing", "sledding") #=> /skiing|sledding/
3763 * Regexp.union(["skiing", "sledding"]) #=> /skiing|sledding/
3764 * Regexp.union(/dogs/, /cats/i) #=> /(?-mix:dogs)|(?i-mx:cats)/
3765 *
3766 * Note: the arguments for ::union will try to be converted into a regular
3767 * expression literal via #to_regexp.
3768 */
3769static VALUE
3770rb_reg_s_union_m(VALUE self, VALUE args)
3771{
3772 VALUE v;
3773 if (RARRAY_LEN(args) == 1 &&
3774 !NIL_P(v = rb_check_array_type(rb_ary_entry(args, 0)))) {
3775 return rb_reg_s_union(self, v);
3776 }
3777 return rb_reg_s_union(self, args);
3778}
3779
3780/* :nodoc: */
3781static VALUE
3782rb_reg_init_copy(VALUE copy, VALUE re)
3783{
3784 if (!OBJ_INIT_COPY(copy, re)) return copy;
3785 rb_reg_check(re);
3786 return rb_reg_init_str(copy, RREGEXP_SRC(re), rb_reg_options(re));
3787}
3788
3789VALUE
3790rb_reg_regsub(VALUE str, VALUE src, struct re_registers *regs, VALUE regexp)
3791{
3792 VALUE val = 0;
3793 char *p, *s, *e;
3794 int no, clen;
3795 rb_encoding *str_enc = rb_enc_get(str);
3796 rb_encoding *src_enc = rb_enc_get(src);
3797 int acompat = rb_enc_asciicompat(str_enc);
3798 long n;
3799#define ASCGET(s,e,cl) (acompat ? (*(cl)=1,ISASCII((s)[0])?(s)[0]:-1) : rb_enc_ascget((s), (e), (cl), str_enc))
3800
3801 RSTRING_GETMEM(str, s, n);
3802 p = s;
3803 e = s + n;
3804
3805 while (s < e) {
3806 int c = ASCGET(s, e, &clen);
3807 char *ss;
3808
3809 if (c == -1) {
3810 s += mbclen(s, e, str_enc);
3811 continue;
3812 }
3813 ss = s;
3814 s += clen;
3815
3816 if (c != '\\' || s == e) continue;
3817
3818 if (!val) {
3819 val = rb_str_buf_new(ss-p);
3820 }
3821 rb_enc_str_buf_cat(val, p, ss-p, str_enc);
3822
3823 c = ASCGET(s, e, &clen);
3824 if (c == -1) {
3825 s += mbclen(s, e, str_enc);
3826 rb_enc_str_buf_cat(val, ss, s-ss, str_enc);
3827 p = s;
3828 continue;
3829 }
3830 s += clen;
3831
3832 p = s;
3833 switch (c) {
3834 case '1': case '2': case '3': case '4':
3835 case '5': case '6': case '7': case '8': case '9':
3836 if (!NIL_P(regexp) && onig_noname_group_capture_is_active(RREGEXP_PTR(regexp))) {
3837 no = c - '0';
3838 }
3839 else {
3840 continue;
3841 }
3842 break;
3843
3844 case 'k':
3845 if (s < e && ASCGET(s, e, &clen) == '<') {
3846 char *name, *name_end;
3847
3848 name_end = name = s + clen;
3849 while (name_end < e) {
3850 c = ASCGET(name_end, e, &clen);
3851 if (c == '>') break;
3852 name_end += c == -1 ? mbclen(name_end, e, str_enc) : clen;
3853 }
3854 if (name_end < e) {
3855 VALUE n = rb_str_subseq(str, (long)(name - RSTRING_PTR(str)),
3856 (long)(name_end - name));
3857 if ((no = NAME_TO_NUMBER(regs, regexp, n, name, name_end)) < 1) {
3858 name_to_backref_error(n);
3859 }
3860 p = s = name_end + clen;
3861 break;
3862 }
3863 else {
3864 rb_raise(rb_eRuntimeError, "invalid group name reference format");
3865 }
3866 }
3867
3868 rb_enc_str_buf_cat(val, ss, s-ss, str_enc);
3869 continue;
3870
3871 case '0':
3872 case '&':
3873 no = 0;
3874 break;
3875
3876 case '`':
3877 rb_enc_str_buf_cat(val, RSTRING_PTR(src), BEG(0), src_enc);
3878 continue;
3879
3880 case '\'':
3881 rb_enc_str_buf_cat(val, RSTRING_PTR(src)+END(0), RSTRING_LEN(src)-END(0), src_enc);
3882 continue;
3883
3884 case '+':
3885 no = regs->num_regs-1;
3886 while (BEG(no) == -1 && no > 0) no--;
3887 if (no == 0) continue;
3888 break;
3889
3890 case '\\':
3891 rb_enc_str_buf_cat(val, s-clen, clen, str_enc);
3892 continue;
3893
3894 default:
3895 rb_enc_str_buf_cat(val, ss, s-ss, str_enc);
3896 continue;
3897 }
3898
3899 if (no >= 0) {
3900 if (no >= regs->num_regs) continue;
3901 if (BEG(no) == -1) continue;
3902 rb_enc_str_buf_cat(val, RSTRING_PTR(src)+BEG(no), END(no)-BEG(no), src_enc);
3903 }
3904 }
3905
3906 if (!val) return str;
3907 if (p < e) {
3908 rb_enc_str_buf_cat(val, p, e-p, str_enc);
3909 }
3910
3911 return val;
3912}
3913
3914static VALUE
3915ignorecase_getter(ID _x, VALUE *_y)
3916{
3917 rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "variable $= is no longer effective");
3918 return Qfalse;
3919}
3920
3921static void
3922ignorecase_setter(VALUE val, ID id, VALUE *_)
3923{
3924 rb_category_warn(RB_WARN_CATEGORY_DEPRECATED, "variable $= is no longer effective; ignored");
3925}
3926
3927static VALUE
3928match_getter(void)
3929{
3931
3932 if (NIL_P(match)) return Qnil;
3934 return match;
3935}
3936
3937static VALUE
3938get_LAST_MATCH_INFO(ID _x, VALUE *_y)
3939{
3940 return match_getter();
3941}
3942
3943static void
3944match_setter(VALUE val, ID _x, VALUE *_y)
3945{
3946 if (!NIL_P(val)) {
3947 Check_Type(val, T_MATCH);
3948 }
3949 rb_backref_set(val);
3950}
3951
3952/*
3953 * call-seq:
3954 * Regexp.last_match -> matchdata
3955 * Regexp.last_match(n) -> str
3956 *
3957 * The first form returns the MatchData object generated by the
3958 * last successful pattern match. Equivalent to reading the special global
3959 * variable <code>$~</code> (see Special global variables in Regexp for
3960 * details).
3961 *
3962 * The second form returns the <i>n</i>th field in this MatchData object.
3963 * _n_ can be a string or symbol to reference a named capture.
3964 *
3965 * Note that the last_match is local to the thread and method scope of the
3966 * method that did the pattern match.
3967 *
3968 * /c(.)t/ =~ 'cat' #=> 0
3969 * Regexp.last_match #=> #<MatchData "cat" 1:"a">
3970 * Regexp.last_match(0) #=> "cat"
3971 * Regexp.last_match(1) #=> "a"
3972 * Regexp.last_match(2) #=> nil
3973 *
3974 * /(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/ =~ "var = val"
3975 * Regexp.last_match #=> #<MatchData "var = val" lhs:"var" rhs:"val">
3976 * Regexp.last_match(:lhs) #=> "var"
3977 * Regexp.last_match(:rhs) #=> "val"
3978 */
3979
3980static VALUE
3981rb_reg_s_last_match(int argc, VALUE *argv, VALUE _)
3982{
3983 if (rb_check_arity(argc, 0, 1) == 1) {
3985 int n;
3986 if (NIL_P(match)) return Qnil;
3987 n = match_backref_number(match, argv[0]);
3988 return rb_reg_nth_match(n, match);
3989 }
3990 return match_getter();
3991}
3992
3993static void
3994re_warn(const char *s)
3995{
3996 rb_warn("%s", s);
3997}
3998
3999/*
4000 * Document-class: RegexpError
4001 *
4002 * Raised when given an invalid regexp expression.
4003 *
4004 * Regexp.new("?")
4005 *
4006 * <em>raises the exception:</em>
4007 *
4008 * RegexpError: target of repeat operator is not specified: /?/
4009 */
4010
4011/*
4012 * Document-class: Regexp
4013 *
4014 * A Regexp holds a regular expression, used to match a pattern
4015 * against strings. Regexps are created using the <code>/.../</code>
4016 * and <code>%r{...}</code> literals, and by the Regexp::new
4017 * constructor.
4018 *
4019 * :include: doc/regexp.rdoc
4020 */
4021
4022void
4024{
4026
4028 onig_set_warn_func(re_warn);
4029 onig_set_verb_warn_func(re_warn);
4030
4031 rb_define_virtual_variable("$~", get_LAST_MATCH_INFO, match_setter);
4032 rb_define_virtual_variable("$&", last_match_getter, 0);
4033 rb_define_virtual_variable("$`", prematch_getter, 0);
4034 rb_define_virtual_variable("$'", postmatch_getter, 0);
4035 rb_define_virtual_variable("$+", last_paren_match_getter, 0);
4036
4042
4043 rb_define_virtual_variable("$=", ignorecase_getter, ignorecase_setter);
4044
4046 rb_define_alloc_func(rb_cRegexp, rb_reg_s_alloc);
4048 rb_define_singleton_method(rb_cRegexp, "quote", rb_reg_s_quote, 1);
4049 rb_define_singleton_method(rb_cRegexp, "escape", rb_reg_s_quote, 1);
4050 rb_define_singleton_method(rb_cRegexp, "union", rb_reg_s_union_m, -2);
4051 rb_define_singleton_method(rb_cRegexp, "last_match", rb_reg_s_last_match, -1);
4052 rb_define_singleton_method(rb_cRegexp, "try_convert", rb_reg_s_try_convert, 1);
4053
4054 rb_define_method(rb_cRegexp, "initialize", rb_reg_initialize_m, -1);
4055 rb_define_method(rb_cRegexp, "initialize_copy", rb_reg_init_copy, 1);
4056 rb_define_method(rb_cRegexp, "hash", rb_reg_hash, 0);
4057 rb_define_method(rb_cRegexp, "eql?", rb_reg_equal, 1);
4058 rb_define_method(rb_cRegexp, "==", rb_reg_equal, 1);
4062 rb_define_method(rb_cRegexp, "match", rb_reg_match_m, -1);
4063 rb_define_method(rb_cRegexp, "match?", rb_reg_match_m_p, -1);
4064 rb_define_method(rb_cRegexp, "to_s", rb_reg_to_s, 0);
4065 rb_define_method(rb_cRegexp, "inspect", rb_reg_inspect, 0);
4066 rb_define_method(rb_cRegexp, "source", rb_reg_source, 0);
4067 rb_define_method(rb_cRegexp, "casefold?", rb_reg_casefold_p, 0);
4068 rb_define_method(rb_cRegexp, "options", rb_reg_options_m, 0);
4069 rb_define_method(rb_cRegexp, "encoding", rb_obj_encoding, 0); /* in encoding.c */
4070 rb_define_method(rb_cRegexp, "fixed_encoding?", rb_reg_fixed_encoding_p, 0);
4071 rb_define_method(rb_cRegexp, "names", rb_reg_names, 0);
4072 rb_define_method(rb_cRegexp, "named_captures", rb_reg_named_captures, 0);
4073
4074 /* see Regexp.options and Regexp.new */
4076 /* see Regexp.options and Regexp.new */
4078 /* see Regexp.options and Regexp.new */
4080 /* see Regexp.options and Regexp.new */
4082 /* see Regexp.options and Regexp.new */
4084
4085 rb_global_variable(&reg_cache);
4086
4087 rb_cMatch = rb_define_class("MatchData", rb_cObject);
4088 rb_define_alloc_func(rb_cMatch, match_alloc);
4090 rb_undef_method(CLASS_OF(rb_cMatch), "allocate");
4091
4092 rb_define_method(rb_cMatch, "initialize_copy", match_init_copy, 1);
4093 rb_define_method(rb_cMatch, "regexp", match_regexp, 0);
4094 rb_define_method(rb_cMatch, "names", match_names, 0);
4095 rb_define_method(rb_cMatch, "size", match_size, 0);
4096 rb_define_method(rb_cMatch, "length", match_size, 0);
4097 rb_define_method(rb_cMatch, "offset", match_offset, 1);
4098 rb_define_method(rb_cMatch, "begin", match_begin, 1);
4099 rb_define_method(rb_cMatch, "end", match_end, 1);
4100 rb_define_method(rb_cMatch, "to_a", match_to_a, 0);
4101 rb_define_method(rb_cMatch, "[]", match_aref, -1);
4102 rb_define_method(rb_cMatch, "captures", match_captures, 0);
4103 rb_define_method(rb_cMatch, "named_captures", match_named_captures, 0);
4104 rb_define_method(rb_cMatch, "values_at", match_values_at, -1);
4106 rb_define_method(rb_cMatch, "post_match", rb_reg_match_post, 0);
4107 rb_define_method(rb_cMatch, "to_s", match_to_s, 0);
4108 rb_define_method(rb_cMatch, "inspect", match_inspect, 0);
4109 rb_define_method(rb_cMatch, "string", match_string, 0);
4110 rb_define_method(rb_cMatch, "hash", match_hash, 0);
4111 rb_define_method(rb_cMatch, "eql?", match_equal, 1);
4112 rb_define_method(rb_cMatch, "==", match_equal, 1);
4113}
void rb_ary_store(VALUE ary, long idx, VALUE val)
Definition: array.c:1141
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:1301
VALUE rb_ary_new_capa(long capa)
Definition: array.c:743
VALUE rb_check_array_type(VALUE ary)
Definition: array.c:988
VALUE rb_ary_resize(VALUE ary, long len)
expands or shrinks ary to len elements.
Definition: array.c:2235
VALUE rb_ary_entry(VALUE ary, long offset)
Definition: array.c:1672
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Definition: array.c:975
#define NORETURN(x)
Definition: attributes.h:152
#define rb_category_warn(category,...)
Definition: bigdecimal.h:163
Our own, locale independent, character handling routines.
#define ISSPACE
Definition: ctype.h:38
#define rb_define_method(klass, mid, func, arity)
Defines klass#mid.
Definition: cxxanyargs.hpp:653
#define rb_define_singleton_method(klass, mid, func, arity)
Defines klass.mid.
Definition: cxxanyargs.hpp:668
#define range(low, item, hi)
Definition: date_strftime.c:21
enum @11::@13::@14 mask
struct RIMemo * ptr
Definition: debug.c:88
#define MJIT_FUNC_EXPORTED
Definition: dllexport.h:55
#define ENCINDEX_Windows_31J
Definition: encindex.h:54
#define ENCINDEX_EUC_JP
Definition: encindex.h:53
#define rb_ascii8bit_encindex()
Definition: encindex.h:57
#define rb_utf8_encindex()
Definition: encindex.h:58
int rb_enc_dummy_p(rb_encoding *enc)
Definition: encoding.c:203
int rb_enc_precise_mbclen(const char *p, const char *e, rb_encoding *enc)
Definition: encoding.c:1230
VALUE rb_enc_associate(VALUE obj, rb_encoding *enc)
Definition: encoding.c:1064
rb_encoding * rb_utf8_encoding(void)
Definition: encoding.c:1537
rb_encoding * rb_ascii8bit_encoding(void)
Definition: encoding.c:1525
rb_encoding * rb_default_internal_encoding(void)
Definition: encoding.c:1734
rb_encoding * rb_enc_get(VALUE obj)
Definition: encoding.c:1070
int rb_enc_unicode_p(rb_encoding *enc)
Definition: encoding.c:688
void rb_enc_copy(VALUE obj1, VALUE obj2)
Definition: encoding.c:1188
rb_encoding * rb_default_external_encoding(void)
Definition: encoding.c:1647
VALUE rb_obj_encoding(VALUE obj)
Definition: encoding.c:1202
rb_encoding * rb_usascii_encoding(void)
Definition: encoding.c:1549
VALUE rb_enc_from_encoding(rb_encoding *encoding)
Definition: encoding.c:188
int rb_enc_ascget(const char *p, const char *e, int *len, rb_encoding *enc)
Definition: encoding.c:1242
big_t * num
Definition: enough.c:232
uint8_t len
Definition: escape.c:17
char str[HTML_ESCAPE_MAX_LEN+1]
Definition: escape.c:18
#define RSTRING_LEN(string)
Definition: fbuffer.h:22
#define RSTRING_PTR(string)
Definition: fbuffer.h:19
#define memcpy(d, s, n)
Definition: ffi_common.h:55
#define LIKELY(x)
Definition: ffi_common.h:125
#define FL_WB_PROTECTED
Definition: fl_type.h:50
#define PRIsVALUE
Definition: function.c:10
void rb_memerror(void)
Definition: gc.c:10309
void rb_gc(void)
Definition: gc.c:9497
#define CLASS_OF
Definition: globals.h:153
VALUE rb_define_class(const char *name, VALUE super)
Defines a top-level class.
Definition: class.c:748
void rb_undef_method(VALUE klass, const char *name)
Definition: class.c:1777
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:2296
int rb_block_given_p(void)
Determines if the current method is given a block.
Definition: eval.c:935
#define FL_SET
Definition: fl_type.h:128
#define FL_TEST
Definition: fl_type.h:130
#define FL_UNSET
Definition: fl_type.h:132
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:2917
void rb_exc_raise(VALUE mesg)
Raises an exception in the current thread.
Definition: eval.c:712
void rb_bug(const char *fmt,...)
Definition: error.c:768
VALUE rb_eStandardError
Definition: error.c:1054
void rb_set_errinfo(VALUE err)
Sets the current exception ($!) to the given value.
Definition: eval.c:1925
VALUE rb_eTypeError
Definition: error.c:1057
VALUE rb_eEncCompatError
Definition: error.c:1064
VALUE rb_eRuntimeError
Definition: error.c:1055
void rb_warn(const char *fmt,...)
Definition: error.c:408
VALUE rb_eArgError
Definition: error.c:1058
VALUE rb_eIndexError
Definition: error.c:1059
VALUE rb_eSecurityError
Definition: error.c:1066
VALUE rb_check_convert_type(VALUE, int, const char *, const char *)
Tries to convert an object into another type.
Definition: object.c:2971
VALUE rb_cObject
Object class.
Definition: object.c:49
VALUE rb_any_to_s(VALUE)
Default implementation of #to_s.
Definition: object.c:561
VALUE rb_class_new_instance(int, const VALUE *, VALUE)
Allocates and initializes an instance of klass.
Definition: object.c:1953
VALUE rb_obj_class(VALUE)
Definition: object.c:245
VALUE rb_obj_freeze(VALUE)
Make the object unmodifiable.
Definition: object.c:1101
unsigned char match[65280+2]
Definition: gun.c:165
VALUE rb_hash_new_with_size(st_index_t size)
Definition: hash.c:1544
VALUE rb_hash_aset(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:2901
VALUE rb_hash_new(void)
Definition: hash.c:1538
const char term
Definition: id.c:37
IMEMO: Internal memo object.
#define MEMO_NEW(a, b, c)
Definition: imemo.h:122
#define MEMO_CAST(m)
Definition: imemo.h:121
#define ENC_CODERANGE_7BIT
Definition: encoding.h:93
#define rb_enc_left_char_head(s, p, e, enc)
Definition: encoding.h:213
#define rb_enc_mbcput(c, buf, enc)
Definition: encoding.h:208
#define ENC_CODERANGE_CLEAN_P(cr)
Definition: encoding.h:96
int rb_enc_str_coderange(VALUE)
Definition: string.c:725
#define ENC_CODERANGE(obj)
Definition: encoding.h:97
#define ENC_CODERANGE_UNKNOWN
Definition: encoding.h:92
VALUE rb_enc_str_new(const char *, long, rb_encoding *)
Definition: string.c:857
#define rb_enc_name(enc)
Definition: encoding.h:168
#define rb_enc_mbmaxlen(enc)
Definition: encoding.h:172
#define ENCODING_GET(obj)
Definition: encoding.h:51
#define rb_enc_mbc_to_codepoint(p, e, enc)
Definition: encoding.h:199
VALUE rb_enc_str_buf_cat(VALUE str, const char *ptr, long len, rb_encoding *enc)
Definition: string.c:3072
#define MBCLEN_CHARFOUND_LEN(ret)
Definition: encoding.h:183
#define rb_enc_asciicompat(enc)
Definition: encoding.h:236
VALUE rb_str_encode(VALUE str, VALUE to, int ecflags, VALUE ecopts)
Definition: transcode.c:2892
int rb_enc_str_asciionly_p(VALUE)
Definition: string.c:739
#define MBCLEN_INVALID_P(ret)
Definition: encoding.h:184
#define rb_enc_isprint(c, enc)
Definition: encoding.h:227
#define MBCLEN_NEEDMORE_P(ret)
Definition: encoding.h:185
#define rb_enc_mbminlen(enc)
Definition: encoding.h:171
long rb_enc_strlen(const char *, const char *, rb_encoding *)
Definition: string.c:1887
#define ENC_CODERANGE_BROKEN
Definition: encoding.h:95
long rb_str_coderange_scan_restartable(const char *, const char *, rb_encoding *, int *)
Definition: string.c:617
#define MBCLEN_CHARFOUND_P(ret)
Definition: encoding.h:182
#define rb_enc_isspace(c, enc)
Definition: encoding.h:228
Thin wrapper to ruby/config.h.
@ RB_WARN_CATEGORY_DEPRECATED
Definition: error.h:34
#define ruby_verbose
Definition: error.h:68
#define rb_ary_new2
Definition: array.h:72
int rb_uv_to_utf8(char[6], unsigned long)
Definition: pack.c:1660
#define rb_check_frozen
Definition: error.h:72
#define rb_exc_new3
Definition: error.h:31
#define rb_check_arity
Definition: error.h:34
#define OBJ_INIT_COPY(obj, orig)
Definition: object.h:31
VALUE rb_backref_get(void)
Definition: vm.c:1544
VALUE rb_lastline_get(void)
Definition: vm.c:1556
void rb_backref_set(VALUE)
Definition: vm.c:1550
VALUE rb_range_beg_len(VALUE, long *, long *, long, int)
Definition: range.c:1398
#define rb_str_new2
Definition: string.h:276
VALUE rb_str_resize(VALUE, long)
Definition: string.c:2859
#define rb_str_buf_cat2
Definition: string.h:284
#define rb_hash_uint(h, i)
Definition: string.h:117
long rb_str_offset(VALUE, long)
Definition: string.c:2566
#define rb_hash_end(h)
Definition: string.h:118
char * rb_str_subpos(VALUE, long, long *)
Definition: string.c:2647
#define rb_str_buf_new2
Definition: string.h:281
st_index_t rb_memhash(const void *ptr, long len)
Definition: random.c:1629
#define rb_str_new(str, len)
Definition: string.h:213
#define rb_str_buf_cat
Definition: string.h:283
st_index_t rb_hash_start(st_index_t)
Definition: random.c:1623
VALUE rb_str_buf_new(long)
Definition: string.c:1398
VALUE rb_check_string_type(VALUE)
Definition: string.c:2462
VALUE rb_str_inspect(VALUE)
Definition: string.c:6199
#define rb_str_new3
Definition: string.h:277
VALUE rb_str_equal(VALUE str1, VALUE str2)
Definition: string.c:3423
VALUE rb_str_subseq(VALUE, long, long)
Definition: string.c:2624
VALUE rb_str_append(VALUE, VALUE)
Definition: string.c:3118
VALUE rb_str_buf_cat_ascii(VALUE, const char *)
Definition: string.c:3079
VALUE rb_str_buf_append(VALUE, VALUE)
Definition: string.c:3103
long rb_str_sublen(VALUE, long)
Definition: string.c:2613
VALUE rb_str_dup(VALUE)
Definition: string.c:1631
st_index_t rb_str_hash(VALUE)
Definition: string.c:3314
VALUE rb_str_length(VALUE)
Definition: string.c:1995
#define rb_str_new4
Definition: string.h:278
VALUE rb_class_path(VALUE)
Definition: variable.c:169
void rb_define_alloc_func(VALUE, rb_alloc_func_t)
VALUE rb_sym2str(VALUE)
Definition: symbol.c:927
void rb_define_const(VALUE, const char *, VALUE)
Definition: variable.c:3150
unsigned long ruby_scan_oct(const char *, size_t, size_t *)
Definition: util.c:38
unsigned long ruby_scan_hex(const char *, size_t, size_t *)
Definition: util.c:56
#define scan_hex(s, l, e)
Definition: util.h:24
#define scan_oct(s, l, e)
Definition: util.h:22
#define FIX2INT
Definition: int.h:41
#define NUM2INT
Definition: int.h:44
#define INT2NUM
Definition: int.h:43
Internal header for Hash.
Internal header for Regexp.
Internal header for String.
VALUE rb_fstring(VALUE)
Definition: string.c:353
int rb_str_buf_cat_escaped_char(VALUE result, unsigned int c, int unicode_p)
Definition: string.c:6084
void rb_gvar_ractor_local(const char *name)
Definition: variable.c:359
#define rp(obj)
Definition: internal.h:95
voidpf void * buf
Definition: ioapi.h:138
VALUE rb_yield(VALUE)
Definition: vm_eval.c:1341
#define CHAR_BIT
Definition: limits.h:44
#define INT2FIX
Definition: long.h:48
#define LONG2FIX
Definition: long.h:49
#define NUM2LONG
Definition: long.h:51
int memcmp(const void *s1, const void *s2, size_t len)
Definition: memcmp.c:7
#define MEMCPY(p1, p2, type, n)
Definition: memory.h:129
#define REALLOC_N
Definition: memory.h:137
#define ALLOCA_N(type, n)
Definition: memory.h:112
#define MEMZERO(p, type, n)
Definition: memory.h:128
#define RB_GC_GUARD(v)
Definition: memory.h:91
void rb_define_virtual_variable(const char *q, type *w, void_type *e)
Define a function-backended global variable.
Definition: cxxanyargs.hpp:73
#define NEWOBJ_OF
Definition: newobj.h:35
#define char_size(c2, c1)
Definition: nkf.c:3824
const char * name
Definition: nkf.c:208
#define TRUE
Definition: nkf.h:175
#define FALSE
Definition: nkf.h:174
#define ONIG_MISMATCH
Definition: onigmo.h:625
#define ONIGENC_LEFT_ADJUST_CHAR_HEAD(enc, start, s, end)
Definition: onigmo.h:336
ONIG_EXTERN int onig_reg_init(OnigRegex reg, OnigOptionType option, OnigCaseFoldType case_fold_flag, OnigEncoding enc, const OnigSyntaxType *syntax)
ONIG_EXTERN int onig_error_code_to_str(OnigUChar *s, OnigPosition err_code,...)
ONIG_EXTERN OnigUChar * onigenc_get_right_adjust_char_head(OnigEncoding enc, const OnigUChar *start, const OnigUChar *s, const OnigUChar *end)
unsigned char OnigUChar
Definition: onigmo.h:79
#define ONIG_MAX_ERROR_MESSAGE_LEN
Definition: onigmo.h:443
ONIG_EXTERN int onig_new(OnigRegex *, const OnigUChar *pattern, const OnigUChar *pattern_end, OnigOptionType option, OnigEncoding enc, const OnigSyntaxType *syntax, OnigErrorInfo *einfo)
ONIG_EXTERN int onig_region_resize(OnigRegion *region, int n)
Definition: regexec.c:248
ONIG_EXTERN void onig_region_free(OnigRegion *region, int free_self)
Definition: regexec.c:343
#define ONIG_OPTION_MULTILINE
Definition: onigmo.h:453
#define UChar
Definition: onigmo.h:76
ONIG_EXTERN OnigPosition onig_search(OnigRegex, const OnigUChar *str, const OnigUChar *end, const OnigUChar *start, const OnigUChar *range, OnigRegion *region, OnigOptionType option)
ptrdiff_t OnigPosition
Definition: onigmo.h:83
#define ONIGENC_CASE_FOLD_DEFAULT
Definition: onigmo.h:131
#define ONIG_OPTION_IGNORECASE
Definition: onigmo.h:451
#define ONIGERR_MEMORY
Definition: onigmo.h:629
#define ONIG_ENCODING_ASCII
Definition: onigmo.h:225
ONIG_EXTERN void onig_free(OnigRegex)
ONIG_EXTERN int onigenc_set_default_encoding(OnigEncoding enc)
Definition: regenc.c:48
ONIG_EXTERN const OnigSyntaxType * OnigDefaultSyntax
Definition: onigmo.h:515
#define ONIGENC_MBC_MAXLEN(enc)
Definition: onigmo.h:362
ONIG_EXTERN void onig_set_verb_warn_func(OnigWarnFunc f)
Definition: regparse.c:106
ONIG_EXTERN OnigPosition onig_match(OnigRegex, const OnigUChar *str, const OnigUChar *end, const OnigUChar *at, OnigRegion *region, OnigOptionType option)
unsigned int OnigOptionType
Definition: onigmo.h:445
ONIG_EXTERN int onig_foreach_name(OnigRegex reg, int(*func)(const OnigUChar *, const OnigUChar *, int, int *, OnigRegex, void *), void *arg)
ONIG_EXTERN void onig_region_copy(OnigRegion *to, const OnigRegion *from)
Definition: regexec.c:359
ONIG_EXTERN int onig_name_to_backref_number(OnigRegex reg, const OnigUChar *name, const OnigUChar *name_end, const OnigRegion *region)
ONIG_EXTERN int onig_noname_group_capture_is_active(const OnigRegexType *reg)
Definition: regparse.c:963
#define ONIG_OPTION_EXTEND
Definition: onigmo.h:452
ONIG_EXTERN int onig_number_of_names(const OnigRegexType *reg)
Definition: regparse.c:623
#define ONIG_OPTION_NONE
Definition: onigmo.h:450
ONIG_EXTERN void onig_set_warn_func(OnigWarnFunc f)
Definition: regparse.c:101
#define RARRAY_AREF(a, i)
Definition: psych_emitter.c:7
#define RARRAY_LEN
Definition: rarray.h:52
#define RBASIC(obj)
Definition: rbasic.h:34
int rb_reg_backref_number(VALUE match, VALUE backref)
Definition: re.c:1191
void rb_backref_set_string(VALUE string, long pos, long len)
Definition: re.c:1358
VALUE rb_reg_nth_defined(int nth, VALUE match)
Definition: re.c:1707
VALUE rb_reg_match_last(VALUE match)
Definition: re.c:1811
int rb_reg_options(VALUE re)
Definition: re.c:3593
VALUE rb_reg_match(VALUE re, VALUE str)
Definition: re.c:3194
#define REG_ENCODING_NONE
Definition: re.c:284
VALUE rb_reg_new_ary(VALUE ary, int opt)
Definition: re.c:2938
VALUE rb_reg_eqq(VALUE re, VALUE str)
Definition: re.c:3224
#define NAME_TO_NUMBER(regs, re, name, name_ptr, name_end)
Definition: re.c:1932
regex_t * rb_reg_prepare_re(VALUE re, VALUE str)
Definition: re.c:1511
VALUE rb_cMatch
Definition: re.c:940
VALUE rb_eRegexpError
Definition: re.c:28
VALUE rb_reg_regsub(VALUE str, VALUE src, struct re_registers *regs, VALUE regexp)
Definition: re.c:3790
#define ARG_ENCODING_NONE
Definition: re.c:291
VALUE rb_enc_reg_new(const char *s, long len, rb_encoding *enc, int options)
Definition: re.c:2946
#define ARG_ENCODING_FIXED
Definition: re.c:290
VALUE rb_reg_match_post(VALUE match)
Definition: re.c:1794
VALUE rb_reg_last_match(VALUE match)
Definition: re.c:1750
VALUE rb_reg_match_p(VALUE re, VALUE str, long pos)
Definition: re.c:3353
void Init_Regexp(void)
Definition: re.c:4023
VALUE rb_cRegexp
Definition: re.c:2301
VALUE rb_reg_match_pre(VALUE match)
Definition: re.c:1768
#define BEG(no)
Definition: re.c:33
@ OPTBUF_SIZE
Definition: re.c:315
char onig_errmsg_buffer[ONIG_MAX_ERROR_MESSAGE_LEN]
Definition: re.c:30
#define END(no)
Definition: re.c:34
VALUE rb_reg_new(const char *s, long len, int options)
Definition: re.c:2960
bool rb_reg_start_with_p(VALUE re, VALUE str)
Definition: re.c:1634
int rb_char_to_option_kcode(int c, int *option, int *kcode)
Definition: re.c:329
void rb_match_busy(VALUE match)
Definition: re.c:1305
#define REG_LITERAL
Definition: re.c:283
long rb_memsearch(const void *x0, long m, const void *y0, long n, rb_encoding *enc)
Definition: re.c:247
regex_t * rb_reg_prepare_re0(VALUE re, VALUE str, onig_errmsg_buffer err)
Definition: re.c:1471
#define ARG_REG_OPTION_MASK
Definition: re.c:288
#define MATCH_BUSY
Definition: re.c:1302
#define VALUE_MAX
long rb_reg_search(VALUE re, VALUE str, long pos, int reverse)
Definition: re.c:1628
long rb_reg_adjust_startpos(VALUE re, VALUE str, long pos, int reverse)
Definition: re.c:1518
VALUE rb_reg_regcomp(VALUE str)
Definition: re.c:2984
VALUE rb_reg_nth_match(int nth, VALUE match)
Definition: re.c:1725
#define errcpy(err, msg)
Definition: re.c:31
int rb_match_nth_defined(int nth, VALUE match)
Definition: re.c:1327
long rb_reg_search0(VALUE re, VALUE str, long pos, int reverse, int set_backref_str)
Definition: re.c:1622
int rb_memcicmp(const void *x, const void *y, long len)
Definition: re.c:88
VALUE rb_reg_init_str(VALUE re, VALUE s, int options)
Definition: re.c:2912
VALUE rb_reg_compile(VALUE str, int options, const char *sourcefile, int sourceline)
Definition: re.c:2966
void rb_match_unbusy(VALUE match)
Definition: re.c:1311
int rb_match_count(VALUE match)
Definition: re.c:1317
VALUE rb_reg_match2(VALUE re)
Definition: re.c:3253
#define KCODE_FIXED
Definition: re.c:286
VALUE rb_reg_quote(VALUE str)
Definition: re.c:3475
VALUE rb_check_regexp_type(VALUE re)
Definition: re.c:3605
VALUE rb_reg_alloc(void)
Definition: re.c:2900
#define ASCGET(s, e, cl)
VALUE rb_reg_check_preprocess(VALUE str)
Definition: re.c:2718
VALUE rb_reg_new_str(VALUE s, int options)
Definition: re.c:2906
int rb_reg_region_copy(struct re_registers *to, const struct re_registers *from)
Definition: re.c:956
int onig_compile_ruby(regex_t *reg, const UChar *pattern, const UChar *pattern_end, OnigErrorInfo *einfo, const char *sourcefile, int sourceline)
Definition: regcomp.c:5734
#define NULL
Definition: regenc.h:69
#define mbclen(p, e, enc)
Definition: regex.h:31
#define IS_NULL(p)
Definition: regint.h:298
#define RB_OBJ_WRITE(a, slot, b)
WB for new reference from ‘a’ to ‘b’.
Definition: rgengc.h:107
#define RGENGC_WB_PROTECTED_REGEXP
Definition: rgengc.h:68
#define RMATCH(obj)
Definition: rmatch.h:32
#define RREGEXP(obj)
Definition: rregexp.h:31
#define RREGEXP_PTR(obj)
Definition: rregexp.h:32
VALUE rb_str_to_str(VALUE)
Definition: string.c:1471
#define StringValue(v)
Definition: rstring.h:50
#define StringValuePtr(v)
Definition: rstring.h:51
#define RSTRING_GETMEM(str, ptrvar, lenvar)
Definition: rstring.h:211
#define StringValueCStr(v)
Definition: rstring.h:52
int argc
Definition: ruby.c:240
char ** argv
Definition: ruby.c:241
#define ST2FIX(h)
Definition: ruby_missing.h:21
#define Qtrue
#define RTEST
#define Qnil
#define Qfalse
#define NIL_P
#define FIXNUM_P
VALUE rb_str_catf(VALUE, const char *,...)
Definition: sprintf.c:1243
VALUE rb_sprintf(const char *,...)
Definition: sprintf.c:1203
#define malloc
Definition: st.c:170
st_data_t st_index_t
Definition: st.h:50
#define _(args)
Definition: stdarg.h:31
MEMO.
Definition: imemo.h:105
const VALUE v2
Definition: imemo.h:109
const VALUE v1
Definition: imemo.h:108
const VALUE value
Definition: imemo.h:113
VALUE flags
Definition: rbasic.h:48
Definition: rmatch.h:55
struct RBasic basic
Definition: rregexp.h:43
const VALUE src
Definition: rregexp.h:45
unsigned long usecnt
Definition: rregexp.h:46
struct re_pattern_buffer * ptr
Definition: rregexp.h:44
long len
Definition: re.c:2214
const UChar * name
Definition: re.c:2213
Definition: inftree9.h:24
Definition: re.c:966
long char_pos
Definition: re.c:968
long byte_pos
Definition: re.c:967
OnigEncoding enc
Definition: onigmo.h:776
OnigOptionType options
Definition: onigmo.h:772
OnigPosition * beg
Definition: onigmo.h:719
int num_regs
Definition: onigmo.h:718
int allocated
Definition: onigmo.h:717
OnigPosition * end
Definition: onigmo.h:720
long beg
Definition: rmatch.h:44
long end
Definition: rmatch.h:45
Definition: rmatch.h:48
int char_offset_num_allocated
Definition: rmatch.h:52
struct rmatch_offset * char_offset
Definition: rmatch.h:51
struct re_registers regs
Definition: rmatch.h:49
#define snprintf
Definition: subst.h:14
#define t
Definition: symbol.c:253
unsigned long VALUE
Definition: value.h:38
#define SIZEOF_VALUE
Definition: value.h:41
unsigned long ID
Definition: value.h:39
#define T_STRING
Definition: value_type.h:77
#define T_MATCH
Definition: value_type.h:68
#define SYMBOL_P
Definition: value_type.h:87
#define T_REGEXP
Definition: value_type.h:76
int err
Definition: win32.c:142
#define ZALLOC(strm, items, size)
Definition: zutil.h:266