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