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