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