1 /**********************************************************************
6 created at: Mon Aug 9 18:24:49 JST 1993
8 Copyright (C) 1993-2007 Yukihiro Matsumoto
10 **********************************************************************/
12 #include "ruby/ruby.h"
14 #include "ruby/encoding.h"
15 #include "ruby/util.h"
19 VALUE rb_eRegexpError
;
21 typedef char onig_errmsg_buffer
[ONIG_MAX_ERROR_MESSAGE_LEN
];
23 #define BEG(no) regs->beg[no]
24 #define END(no) regs->end[no]
26 #if 'a' == 97 /* it's ascii */
27 static const char casetable
[] = {
28 '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
29 '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
30 '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
31 '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
32 /* ' ' '!' '"' '#' '$' '%' '&' ''' */
33 '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
34 /* '(' ')' '*' '+' ',' '-' '.' '/' */
35 '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
36 /* '0' '1' '2' '3' '4' '5' '6' '7' */
37 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
38 /* '8' '9' ':' ';' '<' '=' '>' '?' */
39 '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
40 /* '@' 'A' 'B' 'C' 'D' 'E' 'F' 'G' */
41 '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
42 /* 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O' */
43 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
44 /* 'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' */
45 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
46 /* 'X' 'Y' 'Z' '[' '\' ']' '^' '_' */
47 '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
48 /* '`' 'a' 'b' 'c' 'd' 'e' 'f' 'g' */
49 '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
50 /* 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' */
51 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
52 /* 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' */
53 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
54 /* 'x' 'y' 'z' '{' '|' '}' '~' */
55 '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
56 '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
57 '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
58 '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
59 '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
60 '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
61 '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
62 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
63 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
64 '\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
65 '\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317',
66 '\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327',
67 '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
68 '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
69 '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
70 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
71 '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
74 # error >>> "You lose. You will need a translation table for your character set." <<<
78 rb_memcicmp(const void *x
, const void *y
, long len
)
80 const unsigned char *p1
= x
, *p2
= y
;
84 if ((tmp
= casetable
[(unsigned)*p1
++] - casetable
[(unsigned)*p2
++]))
93 rb_memcmp(const void *p1
, const void *p2
, long len
)
95 return memcmp(p1
, p2
, len
);
99 rb_memsearch_ss(const unsigned char *xs
, long m
, const unsigned char *ys
, long n
)
101 const unsigned char *x
= xs
, *xe
= xs
+ m
;
102 const unsigned char *y
= ys
, *ye
= ys
+ n
;
104 # if SIZEOF_VALUE == 8
105 # define VALUE_MAX 0xFFFFFFFFFFFFFFFFULL
106 # elif SIZEOF_VALUE == 4
107 # define VALUE_MAX 0xFFFFFFFFUL
110 VALUE hx
, hy
, mask
= VALUE_MAX
>> ((SIZEOF_VALUE
- m
) * CHAR_BIT
);
112 if (m
> SIZEOF_VALUE
)
113 rb_bug("!!too long pattern string!!");
115 /* Prepare hash value */
116 for (hx
= *x
++, hy
= *y
++; x
< xe
; ++x
, ++y
) {
135 rb_memsearch_qs(const unsigned char *xs
, long m
, const unsigned char *ys
, long n
)
137 const unsigned char *x
= xs
, *xe
= xs
+ m
;
138 const unsigned char *y
= ys
;
139 VALUE i
, qstable
[256];
142 for (i
= 0; i
< 256; ++i
)
145 qstable
[*x
] = xe
- x
;
147 for (; y
+ m
<= ys
+ n
; y
+= *(qstable
+ y
[m
])) {
148 if (*xs
== *y
&& memcmp(xs
, y
, m
) == 0)
154 static inline unsigned int
155 rb_memsearch_qs_utf8_hash(const unsigned char *x
)
157 register const unsigned int mix
= 8353;
158 register unsigned int h
= *x
;
183 return (unsigned char)h
;
187 rb_memsearch_qs_utf8(const unsigned char *xs
, long m
, const unsigned char *ys
, long n
)
189 const unsigned char *x
= xs
, *xe
= xs
+ m
;
190 const unsigned char *y
= ys
;
191 VALUE i
, qstable
[512];
194 for (i
= 0; i
< 512; ++i
) {
197 for (; x
< xe
; ++x
) {
198 qstable
[rb_memsearch_qs_utf8_hash(x
)] = xe
- x
;
201 for (; y
+ m
<= ys
+ n
; y
+= qstable
[rb_memsearch_qs_utf8_hash(y
+m
)]) {
202 if (*xs
== *y
&& memcmp(xs
, y
, m
) == 0)
209 rb_memsearch(const void *x0
, long m
, const void *y0
, long n
, rb_encoding
*enc
)
211 const unsigned char *x
= x0
, *y
= y0
;
213 if (m
> n
) return -1;
215 return memcmp(x0
, y0
, m
) == 0 ? 0 : -1;
221 const unsigned char *ys
= y
, *ye
= ys
+ n
;
222 for (; y
< ye
; ++y
) {
228 else if (m
<= SIZEOF_VALUE
) {
229 return rb_memsearch_ss(x0
, m
, y0
, n
);
231 else if (enc
== rb_utf8_encoding()){
232 return rb_memsearch_qs_utf8(x0
, m
, y0
, n
);
235 return rb_memsearch_qs(x0
, m
, y0
, n
);
239 #define REG_LITERAL FL_USER5
240 #define REG_ENCODING_NONE FL_USER6
242 #define KCODE_FIXED FL_USER4
244 #define ARG_REG_OPTION_MASK \
245 (ONIG_OPTION_IGNORECASE|ONIG_OPTION_MULTILINE|ONIG_OPTION_EXTEND)
246 #define ARG_ENCODING_FIXED 16
247 #define ARG_ENCODING_NONE 32
250 char_to_option(int c
)
256 val
= ONIG_OPTION_IGNORECASE
;
259 val
= ONIG_OPTION_EXTEND
;
262 val
= ONIG_OPTION_MULTILINE
;
272 option_to_str(char str
[4], int options
)
275 if (options
& ONIG_OPTION_MULTILINE
) *p
++ = 'm';
276 if (options
& ONIG_OPTION_IGNORECASE
) *p
++ = 'i';
277 if (options
& ONIG_OPTION_EXTEND
) *p
++ = 'x';
283 rb_char_to_option_kcode(int c
, int *option
, int *kcode
)
290 return (*option
= ARG_ENCODING_NONE
);
292 *kcode
= rb_enc_find_index("EUC-JP");
295 *kcode
= rb_enc_find_index("Windows-31J");
298 *kcode
= rb_enc_find_index("UTF-8");
302 return (*option
= char_to_option(c
));
304 *option
= ARG_ENCODING_FIXED
;
309 rb_reg_check(VALUE re
)
311 if (!RREGEXP(re
)->ptr
|| !RREGEXP_SRC(re
) || !RREGEXP_SRC_PTR(re
)) {
312 rb_raise(rb_eTypeError
, "uninitialized Regexp");
317 rb_reg_expr_str(VALUE str
, const char *s
, long len
)
319 rb_encoding
*enc
= rb_enc_get(str
);
320 const char *p
, *pend
;
324 p
= s
; pend
= p
+ len
;
326 c
= rb_enc_ascget(p
, pend
, &clen
, enc
);
328 p
+= mbclen(p
, pend
, enc
);
330 else if (c
!= '/' && rb_enc_isprint(c
, enc
)) {
339 rb_str_buf_cat(str
, s
, len
);
344 c
= rb_enc_ascget(p
, pend
, &clen
, enc
);
345 if (c
== '\\' && p
+clen
< pend
) {
346 int n
= clen
+ mbclen(p
+clen
, pend
, enc
);
347 rb_str_buf_cat(str
, p
, n
);
353 rb_str_buf_cat(str
, &c
, 1);
354 rb_str_buf_cat(str
, p
, clen
);
357 int l
= mbclen(p
, pend
, enc
);
358 rb_str_buf_cat(str
, p
, l
);
362 else if (rb_enc_isprint(c
, enc
)) {
363 rb_str_buf_cat(str
, p
, clen
);
365 else if (!rb_enc_isspace(c
, enc
)) {
368 sprintf(b
, "\\x%02X", c
);
369 rb_str_buf_cat(str
, b
, 4);
372 rb_str_buf_cat(str
, p
, clen
);
380 rb_reg_desc(const char *s
, long len
, VALUE re
)
382 VALUE str
= rb_str_buf_new2("/");
384 rb_enc_copy(str
, re
);
385 rb_reg_expr_str(str
, s
, len
);
386 rb_str_buf_cat2(str
, "/");
390 if (*option_to_str(opts
, RREGEXP(re
)->ptr
->options
))
391 rb_str_buf_cat2(str
, opts
);
402 * Returns the original string of the pattern.
404 * /ab+c/ix.source #=> "ab+c"
406 * Note that escape sequences are retained as is.
408 * /\x20\+/.source #=> "\\x20\\+"
413 rb_reg_source(VALUE re
)
418 str
= rb_enc_str_new(RREGEXP_SRC_PTR(re
),RREGEXP_SRC_LEN(re
), rb_enc_get(re
));
419 if (OBJ_TAINTED(re
)) OBJ_TAINT(str
);
425 * rxp.inspect => string
427 * Produce a nicely formatted string-version of _rxp_. Perhaps surprisingly,
428 * <code>#inspect</code> actually produces the more natural version of
429 * the string than <code>#to_s</code>.
431 * /ab+c/ix.inspect #=> "/ab+c/ix"
436 rb_reg_inspect(VALUE re
)
439 return rb_reg_desc(RREGEXP_SRC_PTR(re
), RREGEXP_SRC_LEN(re
), re
);
447 * Returns a string containing the regular expression and its options (using the
448 * <code>(?opts:source)</code> notation. This string can be fed back in to
449 * <code>Regexp::new</code> to a regular expression with the same semantics as
450 * the original. (However, <code>Regexp#==</code> may not return true when
451 * comparing the two, as the source of the regular expression itself may
452 * differ, as the example shows). <code>Regexp#inspect</code> produces a
453 * generally more readable version of <i>rxp</i>.
455 * r1 = /ab+c/ix #=> /ab+c/ix
456 * s1 = r1.to_s #=> "(?ix-m:ab+c)"
457 * r2 = Regexp.new(s1) #=> /(?ix-m:ab+c)/
459 * r1.source #=> "ab+c"
460 * r2.source #=> "(?ix-m:ab+c)"
464 rb_reg_to_s(VALUE re
)
467 const int embeddable
= ONIG_OPTION_MULTILINE
|ONIG_OPTION_IGNORECASE
|ONIG_OPTION_EXTEND
;
470 VALUE str
= rb_str_buf_new2("(?");
475 rb_enc_copy(str
, re
);
476 options
= RREGEXP(re
)->ptr
->options
;
477 ptr
= (UChar
*)RREGEXP_SRC_PTR(re
);
478 len
= RREGEXP_SRC_LEN(re
);
480 if (len
>= 4 && ptr
[0] == '(' && ptr
[1] == '?') {
483 if ((len
-= 2) > 0) {
485 opt
= char_to_option((int )*ptr
);
495 if (len
> 1 && *ptr
== '-') {
499 opt
= char_to_option((int )*ptr
);
514 if (*ptr
== ':' && ptr
[len
-1] == ')') {
517 r
= onig_alloc_init(&rp
, ONIG_OPTION_DEFAULT
,
518 ONIGENC_CASE_FOLD_DEFAULT
,
524 err
= (onig_compile(rp
, ptr
, ptr
+ len
, NULL
) != 0);
529 options
= RREGEXP(re
)->ptr
->options
;
530 ptr
= (UChar
*)RREGEXP_SRC_PTR(re
);
531 len
= RREGEXP_SRC_LEN(re
);
535 if (*option_to_str(optbuf
, options
)) rb_str_buf_cat2(str
, optbuf
);
537 if ((options
& embeddable
) != embeddable
) {
539 option_to_str(optbuf
+ 1, ~options
);
540 rb_str_buf_cat2(str
, optbuf
);
543 rb_str_buf_cat2(str
, ":");
544 rb_reg_expr_str(str
, (char*)ptr
, len
);
545 rb_str_buf_cat2(str
, ")");
546 rb_enc_copy(str
, re
);
553 rb_reg_raise(const char *s
, long len
, const char *err
, VALUE re
)
555 VALUE desc
= rb_reg_desc(s
, len
, re
);
557 rb_raise(rb_eRegexpError
, "%s: %s", err
, RSTRING_PTR(desc
));
561 rb_enc_reg_error_desc(const char *s
, long len
, rb_encoding
*enc
, int options
, const char *err
)
564 VALUE desc
= rb_str_buf_new2(err
);
566 rb_enc_associate(desc
, enc
);
567 rb_str_buf_cat2(desc
, ": /");
568 rb_reg_expr_str(desc
, s
, len
);
570 option_to_str(opts
+ 1, options
);
571 rb_str_buf_cat2(desc
, opts
);
572 return rb_exc_new3(rb_eRegexpError
, desc
);
576 rb_enc_reg_raise(const char *s
, long len
, rb_encoding
*enc
, int options
, const char *err
)
578 rb_exc_raise(rb_enc_reg_error_desc(s
, len
, enc
, options
, err
));
582 rb_reg_error_desc(VALUE str
, int options
, const char *err
)
584 return rb_enc_reg_error_desc(RSTRING_PTR(str
), RSTRING_LEN(str
),
585 rb_enc_get(str
), options
, err
);
589 rb_reg_raise_str(VALUE str
, int options
, const char *err
)
591 rb_exc_raise(rb_reg_error_desc(str
, options
, err
));
597 * rxp.casefold? => true or false
599 * Returns the value of the case-insensitive flag.
601 * /a/.casefold? #=> false
602 * /a/i.casefold? #=> true
603 * /(?i:a)/.casefold? #=> false
607 rb_reg_casefold_p(VALUE re
)
610 if (RREGEXP(re
)->ptr
->options
& ONIG_OPTION_IGNORECASE
) return Qtrue
;
617 * rxp.options => fixnum
619 * Returns the set of bits corresponding to the options used when creating this
620 * Regexp (see <code>Regexp::new</code> for details. Note that additional bits
621 * may be set in the returned options: these are used internally by the regular
622 * expression code. These extra bits are ignored if the options are passed to
623 * <code>Regexp::new</code>.
625 * Regexp::IGNORECASE #=> 1
626 * Regexp::EXTENDED #=> 2
627 * Regexp::MULTILINE #=> 4
629 * /cat/.options #=> 0
630 * /cat/ix.options #=> 3
631 * Regexp.new('cat', true).options #=> 1
632 * /\xa1\xa2/e.options #=> 16
635 * Regexp.new(r.source, r.options) #=> /cat/ix
639 rb_reg_options_m(VALUE re
)
641 int options
= rb_reg_options(re
);
642 return INT2NUM(options
);
646 reg_names_iter(const OnigUChar
*name
, const OnigUChar
*name_end
,
647 int back_num
, int *back_refs
, OnigRegex regex
, void *arg
)
649 VALUE ary
= (VALUE
)arg
;
650 rb_ary_push(ary
, rb_str_new((const char *)name
, name_end
-name
));
656 * rxp.names => [name1, name2, ...]
658 * Returns a list of names of captures as an array of strings.
660 * /(?<foo>.)(?<bar>.)(?<baz>.)/.names
661 * #=> ["foo", "bar", "baz"]
663 * /(?<foo>.)(?<foo>.)/.names
671 rb_reg_names(VALUE re
)
673 VALUE ary
= rb_ary_new();
675 onig_foreach_name(RREGEXP(re
)->ptr
, reg_names_iter
, (void*)ary
);
680 reg_named_captures_iter(const OnigUChar
*name
, const OnigUChar
*name_end
,
681 int back_num
, int *back_refs
, OnigRegex regex
, void *arg
)
683 VALUE hash
= (VALUE
)arg
;
684 VALUE ary
= rb_ary_new2(back_num
);
687 for(i
= 0; i
< back_num
; i
++)
688 rb_ary_store(ary
, i
, INT2NUM(back_refs
[i
]));
690 rb_hash_aset(hash
, rb_str_new((const char*)name
, name_end
-name
),ary
);
697 * rxp.named_captures => hash
699 * Returns a hash representing information about named captures of <i>rxp</i>.
701 * A key of the hash is a name of the named captures.
702 * A value of the hash is an array which is list of indexes of corresponding
705 * /(?<foo>.)(?<bar>.)/.named_captures
706 * #=> {"foo"=>[1], "bar"=>[2]}
708 * /(?<foo>.)(?<foo>.)/.named_captures
709 * #=> {"foo"=>[1, 2]}
711 * If there are no named captures, an empty hash is returned.
713 * /(.)(.)/.named_captures
718 rb_reg_named_captures(VALUE re
)
720 VALUE hash
= rb_hash_new();
722 onig_foreach_name(RREGEXP(re
)->ptr
, reg_named_captures_iter
, (void*)hash
);
727 make_regexp(const char *s
, long len
, rb_encoding
*enc
, int flags
, onig_errmsg_buffer err
)
733 /* Handle escaped characters first. */
735 /* Build a copy of the string (in dest) with the
736 escaped characters translated, and generate the regex
740 r
= onig_alloc_init(&rp
, flags
, ONIGENC_CASE_FOLD_DEFAULT
,
741 enc
, OnigDefaultSyntax
);
743 onig_error_code_to_str((UChar
*)err
, r
);
747 r
= onig_compile(rp
, (UChar
*)s
, (UChar
*)(s
+ len
), &einfo
);
751 (void )onig_error_code_to_str((UChar
*)err
, r
, &einfo
);
759 * Document-class: MatchData
761 * <code>MatchData</code> is the type of the special variable <code>$~</code>,
762 * and is the type of the object returned by <code>Regexp#match</code> and
763 * <code>Regexp.last_match</code>. It encapsulates all the results of a pattern
764 * match, results normally accessed through the special variables
765 * <code>$&</code>, <code>$'</code>, <code>$`</code>, <code>$1</code>,
766 * <code>$2</code>, and so on.
773 match_alloc(VALUE klass
)
775 NEWOBJ(match
, struct RMatch
);
776 OBJSETUP(match
, klass
, T_MATCH
);
781 match
->rmatch
= ALLOC(struct rmatch
);
782 MEMZERO(match
->rmatch
, struct rmatch
, 1);
793 pair_byte_cmp(const void *pair1
, const void *pair2
)
795 return ((pair_t
*)pair1
)->byte_pos
- ((pair_t
*)pair2
)->byte_pos
;
799 update_char_offset(VALUE match
)
801 struct rmatch
*rm
= RMATCH(match
)->rmatch
;
802 struct re_registers
*regs
;
809 if (rm
->char_offset_updated
)
813 num_regs
= rm
->regs
.num_regs
;
815 if (rm
->char_offset_num_allocated
< num_regs
) {
816 REALLOC_N(rm
->char_offset
, struct rmatch_offset
, num_regs
);
817 rm
->char_offset_num_allocated
= num_regs
;
820 enc
= rb_enc_get(RMATCH(match
)->str
);
821 if (rb_enc_mbmaxlen(enc
) == 1) {
822 for (i
= 0; i
< num_regs
; i
++) {
823 rm
->char_offset
[i
].beg
= BEG(i
);
824 rm
->char_offset
[i
].end
= END(i
);
826 rm
->char_offset_updated
= 1;
830 pairs
= ALLOCA_N(pair_t
, num_regs
*2);
832 for (i
= 0; i
< num_regs
; i
++) {
835 pairs
[num_pos
++].byte_pos
= BEG(i
);
836 pairs
[num_pos
++].byte_pos
= END(i
);
838 qsort(pairs
, num_pos
, sizeof(pair_t
), pair_byte_cmp
);
840 s
= p
= RSTRING_PTR(RMATCH(match
)->str
);
841 e
= s
+ RSTRING_LEN(RMATCH(match
)->str
);
843 for (i
= 0; i
< num_pos
; i
++) {
844 q
= s
+ pairs
[i
].byte_pos
;
845 c
+= rb_enc_strlen(p
, q
, enc
);
846 pairs
[i
].char_pos
= c
;
850 for (i
= 0; i
< num_regs
; i
++) {
853 rm
->char_offset
[i
].beg
= -1;
854 rm
->char_offset
[i
].end
= -1;
858 key
.byte_pos
= BEG(i
);
859 found
= bsearch(&key
, pairs
, num_pos
, sizeof(pair_t
), pair_byte_cmp
);
860 rm
->char_offset
[i
].beg
= found
->char_pos
;
862 key
.byte_pos
= END(i
);
863 found
= bsearch(&key
, pairs
, num_pos
, sizeof(pair_t
), pair_byte_cmp
);
864 rm
->char_offset
[i
].end
= found
->char_pos
;
867 rm
->char_offset_updated
= 1;
871 match_check(VALUE match
)
873 if (!RMATCH(match
)->regexp
) {
874 rb_raise(rb_eTypeError
, "uninitialized Match");
880 match_init_copy(VALUE obj
, VALUE orig
)
884 if (obj
== orig
) return obj
;
886 if (!rb_obj_is_instance_of(orig
, rb_obj_class(obj
))) {
887 rb_raise(rb_eTypeError
, "wrong argument class");
889 RMATCH(obj
)->str
= RMATCH(orig
)->str
;
890 RMATCH(obj
)->regexp
= RMATCH(orig
)->regexp
;
892 rm
= RMATCH(obj
)->rmatch
;
893 onig_region_copy(&rm
->regs
, RMATCH_REGS(orig
));
895 if (!RMATCH(orig
)->rmatch
->char_offset_updated
) {
896 rm
->char_offset_updated
= 0;
899 if (rm
->char_offset_num_allocated
< rm
->regs
.num_regs
) {
900 REALLOC_N(rm
->char_offset
, struct rmatch_offset
, rm
->regs
.num_regs
);
901 rm
->char_offset_num_allocated
= rm
->regs
.num_regs
;
903 MEMCPY(rm
->char_offset
, RMATCH(orig
)->rmatch
->char_offset
,
904 struct rmatch_offset
, rm
->regs
.num_regs
);
905 rm
->char_offset_updated
= 1;
914 * mtch.regexp => regexp
916 * Returns the regexp.
918 * m = /a.*b/.match("abc")
919 * m.regexp #=> /a.*b/
923 match_regexp(VALUE match
)
926 return RMATCH(match
)->regexp
;
931 * mtch.names => [name1, name2, ...]
933 * Returns a list of names of captures as an array of strings.
934 * It is same as mtch.regexp.names.
936 * /(?<foo>.)(?<bar>.)(?<baz>.)/.match("hoge").names
937 * #=> ["foo", "bar", "baz"]
939 * m = /(?<x>.)(?<y>.)?/.match("a") #=> #<MatchData "a" x:"a" y:nil>
940 * m.names #=> ["x", "y"]
944 match_names(VALUE match
)
947 return rb_reg_names(RMATCH(match
)->regexp
);
952 * mtch.length => integer
953 * mtch.size => integer
955 * Returns the number of elements in the match array.
957 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
963 match_size(VALUE match
)
966 return INT2FIX(RMATCH_REGS(match
)->num_regs
);
970 match_backref_number(VALUE match
, VALUE backref
)
975 struct re_registers
*regs
= RMATCH_REGS(match
);
976 VALUE regexp
= RMATCH(match
)->regexp
;
979 switch(TYPE(backref
)) {
981 return NUM2INT(backref
);
984 name
= rb_id2name(SYM2ID(backref
));
988 name
= StringValueCStr(backref
);
992 num
= onig_name_to_backref_number(RREGEXP(regexp
)->ptr
,
993 (const unsigned char*)name
,
994 (const unsigned char*)name
+ strlen(name
),
998 rb_raise(rb_eIndexError
, "undefined group name reference: %s", name
);
1007 * mtch.offset(n) => array
1009 * Returns a two-element array containing the beginning and ending offsets of
1010 * the <em>n</em>th match.
1011 * <em>n</em> can be a string or symbol to reference a named capture.
1013 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1014 * m.offset(0) #=> [1, 7]
1015 * m.offset(4) #=> [6, 7]
1017 * m = /(?<foo>.)(.)(?<bar>.)/.match("hoge")
1018 * p m.offset(:foo) #=> [0, 1]
1019 * p m.offset(:bar) #=> [2, 3]
1024 match_offset(VALUE match
, VALUE n
)
1026 int i
= match_backref_number(match
, n
);
1027 struct re_registers
*regs
= RMATCH_REGS(match
);
1030 if (i
< 0 || regs
->num_regs
<= i
)
1031 rb_raise(rb_eIndexError
, "index %d out of matches", i
);
1034 return rb_assoc_new(Qnil
, Qnil
);
1036 update_char_offset(match
);
1037 return rb_assoc_new(INT2FIX(RMATCH(match
)->rmatch
->char_offset
[i
].beg
),
1038 INT2FIX(RMATCH(match
)->rmatch
->char_offset
[i
].end
));
1044 * mtch.begin(n) => integer
1046 * Returns the offset of the start of the <em>n</em>th element of the match
1047 * array in the string.
1048 * <em>n</em> can be a string or symbol to reference a named capture.
1050 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1054 * m = /(?<foo>.)(.)(?<bar>.)/.match("hoge")
1055 * p m.begin(:foo) #=> 0
1056 * p m.begin(:bar) #=> 2
1060 match_begin(VALUE match
, VALUE n
)
1062 int i
= match_backref_number(match
, n
);
1063 struct re_registers
*regs
= RMATCH_REGS(match
);
1066 if (i
< 0 || regs
->num_regs
<= i
)
1067 rb_raise(rb_eIndexError
, "index %d out of matches", i
);
1072 update_char_offset(match
);
1073 return INT2FIX(RMATCH(match
)->rmatch
->char_offset
[i
].beg
);
1079 * mtch.end(n) => integer
1081 * Returns the offset of the character immediately following the end of the
1082 * <em>n</em>th element of the match array in the string.
1083 * <em>n</em> can be a string or symbol to reference a named capture.
1085 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1089 * m = /(?<foo>.)(.)(?<bar>.)/.match("hoge")
1090 * p m.end(:foo) #=> 1
1091 * p m.end(:bar) #=> 3
1095 match_end(VALUE match
, VALUE n
)
1097 int i
= match_backref_number(match
, n
);
1098 struct re_registers
*regs
= RMATCH_REGS(match
);
1101 if (i
< 0 || regs
->num_regs
<= i
)
1102 rb_raise(rb_eIndexError
, "index %d out of matches", i
);
1107 update_char_offset(match
);
1108 return INT2FIX(RMATCH(match
)->rmatch
->char_offset
[i
].end
);
1111 #define MATCH_BUSY FL_USER2
1114 rb_match_busy(VALUE match
)
1116 FL_SET(match
, MATCH_BUSY
);
1121 * rxp.fixed_encoding? => true or false
1123 * Returns false if rxp is applicable to
1124 * a string with any ASCII compatible encoding.
1125 * Returns true otherwise.
1128 * r.fixed_encoding? #=> false
1129 * r =~ "\u{6666} a" #=> 2
1130 * r =~ "\xa1\xa2 a".force_encoding("euc-jp") #=> 2
1131 * r =~ "abc".force_encoding("euc-jp") #=> 0
1134 * r.fixed_encoding? #=> true
1135 * r.encoding #=> #<Encoding:UTF-8>
1136 * r =~ "\u{6666} a" #=> 2
1137 * r =~ "\xa1\xa2".force_encoding("euc-jp") #=> ArgumentError
1138 * r =~ "abc".force_encoding("euc-jp") #=> 0
1141 * r.fixed_encoding? #=> true
1142 * r.encoding #=> #<Encoding:UTF-8>
1143 * r =~ "\u{6666} a" #=> 0
1144 * r =~ "\xa1\xa2".force_encoding("euc-jp") #=> ArgumentError
1145 * r =~ "abc".force_encoding("euc-jp") #=> nil
1149 rb_reg_fixed_encoding_p(VALUE re
)
1151 if (FL_TEST(re
, KCODE_FIXED
))
1158 rb_reg_preprocess(const char *p
, const char *end
, rb_encoding
*enc
,
1159 rb_encoding
**fixed_enc
, onig_errmsg_buffer err
);
1163 reg_enc_error(VALUE re
, VALUE str
)
1165 rb_raise(rb_eArgError
,
1166 "incompatible encoding regexp match (%s regexp with %s string)",
1167 rb_enc_name(RREGEXP(re
)->ptr
->enc
),
1168 rb_enc_name(rb_enc_get(str
)));
1172 rb_reg_prepare_enc(VALUE re
, VALUE str
, int warn
)
1174 rb_encoding
*enc
= 0;
1176 if (rb_enc_str_coderange(str
) == ENC_CODERANGE_BROKEN
) {
1177 rb_raise(rb_eArgError
,
1179 rb_enc_name(rb_enc_get(str
)));
1183 enc
= rb_enc_get(str
);
1184 if (!rb_enc_str_asciicompat_p(str
)) {
1185 if (RREGEXP(re
)->ptr
->enc
!= enc
) {
1186 reg_enc_error(re
, str
);
1189 else if (rb_reg_fixed_encoding_p(re
)) {
1190 if (RREGEXP(re
)->ptr
->enc
!= enc
&&
1191 (!rb_enc_asciicompat(RREGEXP(re
)->ptr
->enc
) ||
1192 rb_enc_str_coderange(str
) != ENC_CODERANGE_7BIT
)) {
1193 reg_enc_error(re
, str
);
1195 enc
= RREGEXP(re
)->ptr
->enc
;
1197 if (warn
&& (RBASIC(re
)->flags
& REG_ENCODING_NONE
) &&
1198 enc
!= rb_ascii8bit_encoding() &&
1199 rb_enc_str_coderange(str
) != ENC_CODERANGE_7BIT
) {
1200 rb_warn("regexp match /.../n against to %s string",
1207 rb_reg_prepare_re(VALUE re
, VALUE str
)
1209 regex_t
*reg
= RREGEXP(re
)->ptr
;
1210 onig_errmsg_buffer err
= "";
1212 OnigErrorInfo einfo
;
1213 const char *pattern
;
1215 rb_encoding
*fixed_enc
= 0;
1216 rb_encoding
*enc
= rb_reg_prepare_enc(re
, str
, 1);
1218 if (reg
->enc
== enc
) return reg
;
1221 reg
= RREGEXP(re
)->ptr
;
1222 pattern
= RREGEXP_SRC_PTR(re
);
1224 unescaped
= rb_reg_preprocess(
1225 pattern
, pattern
+ RREGEXP_SRC_LEN(re
), enc
,
1228 if (unescaped
== Qnil
) {
1229 rb_raise(rb_eArgError
, "regexp preprocess failed: %s", err
);
1232 r
= onig_new(®
, (UChar
* )RSTRING_PTR(unescaped
),
1233 (UChar
* )(RSTRING_PTR(unescaped
) + RSTRING_LEN(unescaped
)),
1235 OnigDefaultSyntax
, &einfo
);
1237 onig_error_code_to_str((UChar
*)err
, r
, &einfo
);
1238 rb_reg_raise(pattern
, RREGEXP_SRC_LEN(re
), err
, re
);
1241 RB_GC_GUARD(unescaped
);
1246 rb_reg_adjust_startpos(VALUE re
, VALUE str
, int pos
, int reverse
)
1252 enc
= rb_reg_prepare_enc(re
, str
, 0);
1258 range
= RSTRING_LEN(str
) - pos
;
1261 if (pos
> 0 && ONIGENC_MBC_MAXLEN(enc
) != 1 && pos
< RSTRING_LEN(str
)) {
1262 string
= (UChar
*)RSTRING_PTR(str
);
1265 p
= onigenc_get_right_adjust_char_head(enc
, string
, string
+ pos
);
1268 p
= ONIGENC_LEFT_ADJUST_CHAR_HEAD(enc
, string
, string
+ pos
);
1277 rb_reg_search(VALUE re
, VALUE str
, int pos
, int reverse
)
1281 struct re_registers regi
, *regs
= ®i
;
1282 char *range
= RSTRING_PTR(str
);
1286 if (pos
> RSTRING_LEN(str
) || pos
< 0) {
1287 rb_backref_set(Qnil
);
1291 reg
= rb_reg_prepare_re(re
, str
);
1292 tmpreg
= reg
!= RREGEXP(re
)->ptr
;
1293 if (!tmpreg
) RREGEXP(re
)->usecnt
++;
1295 match
= rb_backref_get();
1296 if (!NIL_P(match
)) {
1297 if (FL_TEST(match
, MATCH_BUSY
)) {
1301 regs
= RMATCH_REGS(match
);
1305 MEMZERO(regs
, struct re_registers
, 1);
1308 range
+= RSTRING_LEN(str
);
1310 result
= onig_search(reg
,
1311 (UChar
*)(RSTRING_PTR(str
)),
1312 ((UChar
*)(RSTRING_PTR(str
)) + RSTRING_LEN(str
)),
1313 ((UChar
*)(RSTRING_PTR(str
)) + pos
),
1315 regs
, ONIG_OPTION_NONE
);
1316 if (!tmpreg
) RREGEXP(re
)->usecnt
--;
1318 if (RREGEXP(re
)->usecnt
) {
1322 onig_free(RREGEXP(re
)->ptr
);
1323 RREGEXP(re
)->ptr
= reg
;
1328 onig_region_free(regs
, 0);
1329 if (result
== ONIG_MISMATCH
) {
1330 rb_backref_set(Qnil
);
1334 onig_errmsg_buffer err
= "";
1335 onig_error_code_to_str((UChar
*)err
, result
);
1336 rb_reg_raise(RREGEXP_SRC_PTR(re
), RREGEXP_SRC_LEN(re
), err
, 0);
1341 match
= match_alloc(rb_cMatch
);
1342 onig_region_copy(RMATCH_REGS(match
), regs
);
1343 onig_region_free(regs
, 0);
1346 if (rb_safe_level() >= 3)
1349 FL_UNSET(match
, FL_TAINT
);
1352 RMATCH(match
)->str
= rb_str_new4(str
);
1353 RMATCH(match
)->regexp
= re
;
1354 RMATCH(match
)->rmatch
->char_offset_updated
= 0;
1355 rb_backref_set(match
);
1357 OBJ_INFECT(match
, re
);
1358 OBJ_INFECT(match
, str
);
1364 rb_reg_nth_defined(int nth
, VALUE match
)
1366 struct re_registers
*regs
;
1367 if (NIL_P(match
)) return Qnil
;
1369 regs
= RMATCH_REGS(match
);
1370 if (nth
>= regs
->num_regs
) {
1374 nth
+= regs
->num_regs
;
1375 if (nth
<= 0) return Qnil
;
1377 if (BEG(nth
) == -1) return Qfalse
;
1382 rb_reg_nth_match(int nth
, VALUE match
)
1385 long start
, end
, len
;
1386 struct re_registers
*regs
;
1388 if (NIL_P(match
)) return Qnil
;
1390 regs
= RMATCH_REGS(match
);
1391 if (nth
>= regs
->num_regs
) {
1395 nth
+= regs
->num_regs
;
1396 if (nth
<= 0) return Qnil
;
1399 if (start
== -1) return Qnil
;
1402 str
= rb_str_subseq(RMATCH(match
)->str
, start
, len
);
1403 OBJ_INFECT(str
, match
);
1408 rb_reg_last_match(VALUE match
)
1410 return rb_reg_nth_match(0, match
);
1416 * mtch.pre_match => str
1418 * Returns the portion of the original string before the current match.
1419 * Equivalent to the special variable <code>$`</code>.
1421 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1422 * m.pre_match #=> "T"
1426 rb_reg_match_pre(VALUE match
)
1429 struct re_registers
*regs
;
1431 if (NIL_P(match
)) return Qnil
;
1433 regs
= RMATCH_REGS(match
);
1434 if (BEG(0) == -1) return Qnil
;
1435 str
= rb_str_subseq(RMATCH(match
)->str
, 0, BEG(0));
1436 if (OBJ_TAINTED(match
)) OBJ_TAINT(str
);
1443 * mtch.post_match => str
1445 * Returns the portion of the original string after the current match.
1446 * Equivalent to the special variable <code>$'</code>.
1448 * m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie")
1449 * m.post_match #=> ": The Movie"
1453 rb_reg_match_post(VALUE match
)
1457 struct re_registers
*regs
;
1459 if (NIL_P(match
)) return Qnil
;
1461 regs
= RMATCH_REGS(match
);
1462 if (BEG(0) == -1) return Qnil
;
1463 str
= RMATCH(match
)->str
;
1465 str
= rb_str_subseq(str
, pos
, RSTRING_LEN(str
) - pos
);
1466 if (OBJ_TAINTED(match
)) OBJ_TAINT(str
);
1471 rb_reg_match_last(VALUE match
)
1474 struct re_registers
*regs
;
1476 if (NIL_P(match
)) return Qnil
;
1478 regs
= RMATCH_REGS(match
);
1479 if (BEG(0) == -1) return Qnil
;
1481 for (i
=regs
->num_regs
-1; BEG(i
) == -1 && i
> 0; i
--)
1483 if (i
== 0) return Qnil
;
1484 return rb_reg_nth_match(i
, match
);
1488 last_match_getter(void)
1490 return rb_reg_last_match(rb_backref_get());
1494 prematch_getter(void)
1496 return rb_reg_match_pre(rb_backref_get());
1500 postmatch_getter(void)
1502 return rb_reg_match_post(rb_backref_get());
1506 last_paren_match_getter(void)
1508 return rb_reg_match_last(rb_backref_get());
1512 match_array(VALUE match
, int start
)
1514 struct re_registers
*regs
= RMATCH_REGS(match
);
1515 VALUE ary
= rb_ary_new2(regs
->num_regs
);
1516 VALUE target
= RMATCH(match
)->str
;
1518 int taint
= OBJ_TAINTED(match
);
1521 for (i
=start
; i
<regs
->num_regs
; i
++) {
1522 if (regs
->beg
[i
] == -1) {
1523 rb_ary_push(ary
, Qnil
);
1526 VALUE str
= rb_str_subseq(target
, regs
->beg
[i
], regs
->end
[i
]-regs
->beg
[i
]);
1527 if (taint
) OBJ_TAINT(str
);
1528 rb_ary_push(ary
, str
);
1535 /* [MG]:FIXME: I put parens around the /.../.match() in the first line of the
1536 second example to prevent the '*' followed by a '/' from ending the
1541 * mtch.to_a => anArray
1543 * Returns the array of matches.
1545 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1546 * m.to_a #=> ["HX1138", "H", "X", "113", "8"]
1548 * Because <code>to_a</code> is called when expanding
1549 * <code>*</code><em>variable</em>, there's a useful assignment
1550 * shortcut for extracting matched fields. This is slightly slower than
1551 * accessing the fields directly (as an intermediate array is
1554 * all,f1,f2,f3 = *(/(.)(.)(\d+)(\d)/.match("THX1138."))
1562 match_to_a(VALUE match
)
1564 return match_array(match
, 0);
1570 * mtch.captures => array
1572 * Returns the array of captures; equivalent to <code>mtch.to_a[1..-1]</code>.
1574 * f1,f2,f3,f4 = /(.)(.)(\d+)(\d)/.match("THX1138.").captures
1581 match_captures(VALUE match
)
1583 return match_array(match
, 1);
1587 name_to_backref_number(struct re_registers
*regs
, VALUE regexp
, const char* name
, const char* name_end
)
1591 num
= onig_name_to_backref_number(RREGEXP(regexp
)->ptr
,
1592 (const unsigned char* )name
, (const unsigned char* )name_end
, regs
);
1597 VALUE s
= rb_str_new(name
, (long )(name_end
- name
));
1598 rb_raise(rb_eIndexError
, "undefined group name reference: %s",
1605 * mtch[i] => str or nil
1606 * mtch[start, length] => array
1607 * mtch[range] => array
1608 * mtch[name] => str or nil
1610 * Match Reference---<code>MatchData</code> acts as an array, and may be
1611 * accessed using the normal array indexing techniques. <i>mtch</i>[0] is
1612 * equivalent to the special variable <code>$&</code>, and returns the entire
1613 * matched string. <i>mtch</i>[1], <i>mtch</i>[2], and so on return the values
1614 * of the matched backreferences (portions of the pattern between parentheses).
1616 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1617 * m #=> #<MatchData "HX1138" 1:"H" 2:"X" 3:"113" 4:"8">
1619 * m[1, 2] #=> ["H", "X"]
1620 * m[1..3] #=> ["H", "X", "113"]
1621 * m[-3, 2] #=> ["X", "113"]
1623 * m = /(?<foo>a+)b/.match("ccaaab")
1624 * m #=> #<MatchData "aaab" foo:"aaa">
1625 * m["foo"] #=> "aaa"
1630 match_aref(int argc
, VALUE
*argv
, VALUE match
)
1635 rb_scan_args(argc
, argv
, "11", &idx
, &rest
);
1638 if (FIXNUM_P(idx
)) {
1639 if (FIX2INT(idx
) >= 0) {
1640 return rb_reg_nth_match(FIX2INT(idx
), match
);
1647 switch (TYPE(idx
)) {
1649 p
= rb_id2name(SYM2ID(idx
));
1650 goto name_to_backref
;
1653 p
= StringValuePtr(idx
);
1656 num
= name_to_backref_number(RMATCH_REGS(match
),
1657 RMATCH(match
)->regexp
, p
, p
+ strlen(p
));
1658 return rb_reg_nth_match(num
, match
);
1667 return rb_ary_aref(argc
, argv
, match_to_a(match
));
1671 match_entry(VALUE match
, long n
)
1673 return rb_reg_nth_match(n
, match
);
1680 * mtch.values_at([index]*) => array
1682 * Uses each <i>index</i> to access the matching values, returning an array of
1683 * the corresponding matches.
1685 * m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie")
1686 * m.to_a #=> ["HX1138", "H", "X", "113", "8"]
1687 * m.values_at(0, 2, -2) #=> ["HX1138", "X", "113"]
1691 match_values_at(int argc
, VALUE
*argv
, VALUE match
)
1693 struct re_registers
*regs
= RMATCH_REGS(match
);
1695 return rb_get_values_at(match
, regs
->num_regs
, argc
, argv
, match_entry
);
1703 * Returns the entire matched string.
1705 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1706 * m.to_s #=> "HX1138"
1710 match_to_s(VALUE match
)
1712 VALUE str
= rb_reg_last_match(match
);
1715 if (NIL_P(str
)) str
= rb_str_new(0,0);
1716 if (OBJ_TAINTED(match
)) OBJ_TAINT(str
);
1717 if (OBJ_TAINTED(RMATCH(match
)->str
)) OBJ_TAINT(str
);
1724 * mtch.string => str
1726 * Returns a frozen copy of the string passed in to <code>match</code>.
1728 * m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1729 * m.string #=> "THX1138."
1733 match_string(VALUE match
)
1736 return RMATCH(match
)->str
; /* str is frozen */
1739 struct backref_name_tag
{
1745 match_inspect_name_iter(const OnigUChar
*name
, const OnigUChar
*name_end
,
1746 int back_num
, int *back_refs
, OnigRegex regex
, void *arg0
)
1748 struct backref_name_tag
*arg
= (struct backref_name_tag
*)arg0
;
1751 for (i
= 0; i
< back_num
; i
++) {
1752 arg
[back_refs
[i
]].name
= name
;
1753 arg
[back_refs
[i
]].len
= name_end
- name
;
1760 * mtch.inspect => str
1762 * Returns a printable version of <i>mtch</i>.
1764 * puts /.$/.match("foo").inspect
1765 * #=> #<MatchData "o">
1767 * puts /(.)(.)(.)/.match("foo").inspect
1768 * #=> #<MatchData "foo" 1:"f" 2:"o" 3:"o">
1770 * puts /(.)(.)?(.)/.match("fo").inspect
1771 * #=> #<MatchData "fo" 1:"f" 2:nil 3:"o">
1773 * puts /(?<foo>.)(?<bar>.)(?<baz>.)/.match("hoge").inspect
1774 * #=> #<MatchData "hog" foo:"h" bar:"o" baz:"g">
1779 match_inspect(VALUE match
)
1781 const char *cname
= rb_obj_classname(match
);
1784 struct re_registers
*regs
= RMATCH_REGS(match
);
1785 int num_regs
= regs
->num_regs
;
1786 struct backref_name_tag
*names
;
1787 VALUE regexp
= RMATCH(match
)->regexp
;
1790 return rb_sprintf("#<%s:%p>", cname
, (void*)match
);
1793 names
= ALLOCA_N(struct backref_name_tag
, num_regs
);
1794 MEMZERO(names
, struct backref_name_tag
, num_regs
);
1796 onig_foreach_name(RREGEXP(regexp
)->ptr
,
1797 match_inspect_name_iter
, names
);
1799 str
= rb_str_buf_new2("#<");
1800 rb_str_buf_cat2(str
, cname
);
1802 for (i
= 0; i
< num_regs
; i
++) {
1804 rb_str_buf_cat2(str
, " ");
1807 rb_str_buf_cat(str
, (const char *)names
[i
].name
, names
[i
].len
);
1809 char buf
[sizeof(i
)*3+1];
1810 snprintf(buf
, sizeof(buf
), "%d", i
);
1811 rb_str_buf_cat2(str
, buf
);
1813 rb_str_buf_cat2(str
, ":");
1815 v
= rb_reg_nth_match(i
, match
);
1817 rb_str_buf_cat2(str
, "nil");
1819 rb_str_buf_append(str
, rb_str_inspect(v
));
1821 rb_str_buf_cat2(str
, ">");
1829 read_escaped_byte(const char **pp
, const char *end
, onig_errmsg_buffer err
)
1831 const char *p
= *pp
;
1833 int meta_prefix
= 0, ctrl_prefix
= 0;
1838 if (p
== end
|| *p
++ != '\\') {
1839 strcpy(err
, "too short escaped multibyte character");
1845 strcpy(err
, "too short escape sequence");
1849 case '\\': code
= '\\'; break;
1850 case 'n': code
= '\n'; break;
1851 case 't': code
= '\t'; break;
1852 case 'r': code
= '\r'; break;
1853 case 'f': code
= '\f'; break;
1854 case 'v': code
= '\013'; break;
1855 case 'a': code
= '\007'; break;
1856 case 'e': code
= '\033'; break;
1859 case '0': case '1': case '2': case '3':
1860 case '4': case '5': case '6': case '7':
1862 code
= ruby_scan_oct(p
, end
< p
+3 ? end
-p
: 3, &len
);
1866 case 'x': /* \xHH */
1867 code
= ruby_scan_hex(p
, end
< p
+2 ? end
-p
: 2, &len
);
1869 strcpy(err
, "invalid hex escape");
1875 case 'M': /* \M-X, \M-\C-X, \M-\cX */
1877 strcpy(err
, "duplicate meta escape");
1881 if (p
+1 < end
&& *p
++ == '-' && (*p
& 0x80) == 0) {
1891 strcpy(err
, "too short meta escape");
1894 case 'C': /* \C-X, \C-\M-X */
1895 if (p
== end
|| *p
++ != '-') {
1896 strcpy(err
, "too short control escape");
1899 case 'c': /* \cX, \c\M-X */
1901 strcpy(err
, "duplicate control escape");
1905 if (p
< end
&& (*p
& 0x80) == 0) {
1915 strcpy(err
, "too short control escape");
1919 strcpy(err
, "unexpected escape sequence");
1922 if (code
< 0 || 0xff < code
) {
1923 strcpy(err
, "invalid escape code");
1937 unescape_escaped_nonascii(const char **pp
, const char *end
, rb_encoding
*enc
,
1938 VALUE buf
, rb_encoding
**encp
, onig_errmsg_buffer err
)
1940 const char *p
= *pp
;
1941 int chmaxlen
= rb_enc_mbmaxlen(enc
);
1942 char *chbuf
= ALLOCA_N(char, chmaxlen
);
1947 memset(chbuf
, 0, chmaxlen
);
1949 byte
= read_escaped_byte(&p
, end
, err
);
1954 chbuf
[chlen
++] = byte
;
1955 while (chlen
< chmaxlen
&&
1956 MBCLEN_NEEDMORE_P(rb_enc_precise_mbclen(chbuf
, chbuf
+chlen
, enc
))) {
1957 byte
= read_escaped_byte(&p
, end
, err
);
1961 chbuf
[chlen
++] = byte
;
1964 l
= rb_enc_precise_mbclen(chbuf
, chbuf
+chlen
, enc
);
1965 if (MBCLEN_INVALID_P(l
)) {
1966 strcpy(err
, "invalid multibyte escape");
1969 if (1 < chlen
|| (chbuf
[0] & 0x80)) {
1970 rb_str_buf_cat(buf
, chbuf
, chlen
);
1974 else if (*encp
!= enc
) {
1975 strcpy(err
, "escaped non ASCII character in UTF-8 regexp");
1981 snprintf(escbuf
, sizeof(escbuf
), "\\x%02X", chbuf
[0]&0xff);
1982 rb_str_buf_cat(buf
, escbuf
, 4);
1989 check_unicode_range(unsigned long code
, onig_errmsg_buffer err
)
1991 if ((0xd800 <= code
&& code
<= 0xdfff) || /* Surrogates */
1993 strcpy(err
, "invalid Unicode range");
2000 append_utf8(unsigned long uv
,
2001 VALUE buf
, rb_encoding
**encp
, onig_errmsg_buffer err
)
2003 if (check_unicode_range(uv
, err
) != 0)
2007 snprintf(escbuf
, sizeof(escbuf
), "\\x%02X", (int)uv
);
2008 rb_str_buf_cat(buf
, escbuf
, 4);
2013 len
= rb_uv_to_utf8(utf8buf
, uv
);
2014 rb_str_buf_cat(buf
, utf8buf
, len
);
2017 *encp
= rb_utf8_encoding();
2018 else if (*encp
!= rb_utf8_encoding()) {
2019 strcpy(err
, "UTF-8 character in non UTF-8 regexp");
2027 unescape_unicode_list(const char **pp
, const char *end
,
2028 VALUE buf
, rb_encoding
**encp
, onig_errmsg_buffer err
)
2030 const char *p
= *pp
;
2031 int has_unicode
= 0;
2035 while (p
< end
&& ISSPACE(*p
)) p
++;
2038 code
= ruby_scan_hex(p
, end
-p
, &len
);
2041 if (6 < len
) { /* max 10FFFF */
2042 strcpy(err
, "invalid Unicode range");
2046 if (append_utf8(code
, buf
, encp
, err
) != 0)
2050 while (p
< end
&& ISSPACE(*p
)) p
++;
2053 if (has_unicode
== 0) {
2054 strcpy(err
, "invalid Unicode list");
2064 unescape_unicode_bmp(const char **pp
, const char *end
,
2065 VALUE buf
, rb_encoding
**encp
, onig_errmsg_buffer err
)
2067 const char *p
= *pp
;
2072 strcpy(err
, "invalid Unicode escape");
2075 code
= ruby_scan_hex(p
, 4, &len
);
2077 strcpy(err
, "invalid Unicode escape");
2080 if (append_utf8(code
, buf
, encp
, err
) != 0)
2087 unescape_nonascii(const char *p
, const char *end
, rb_encoding
*enc
,
2088 VALUE buf
, rb_encoding
**encp
, onig_errmsg_buffer err
)
2094 int chlen
= rb_enc_precise_mbclen(p
, end
, enc
);
2095 if (!MBCLEN_CHARFOUND_P(chlen
)) {
2096 strcpy(err
, "invalid multibyte character");
2099 chlen
= MBCLEN_CHARFOUND_LEN(chlen
);
2100 if (1 < chlen
|| (*p
& 0x80)) {
2101 rb_str_buf_cat(buf
, p
, chlen
);
2105 else if (*encp
!= enc
) {
2106 strcpy(err
, "non ASCII character in UTF-8 regexp");
2115 strcpy(err
, "too short escape sequence");
2119 case '1': case '2': case '3':
2120 case '4': case '5': case '6': case '7': /* \O, \OO, \OOO or backref */
2123 if (ruby_scan_oct(p
-1, end
-(p
-1), &octlen
) <= 0177) {
2124 /* backref or 7bit octal.
2125 no need to unescape anyway.
2126 re-escaping may break backref */
2130 /* xxx: How about more than 199 subexpressions? */
2132 case '0': /* \0, \0O, \0OO */
2134 case 'x': /* \xHH */
2135 case 'c': /* \cX, \c\M-X */
2136 case 'C': /* \C-X, \C-\M-X */
2137 case 'M': /* \M-X, \M-\C-X, \M-\cX */
2139 if (unescape_escaped_nonascii(&p
, end
, enc
, buf
, encp
, err
) != 0)
2145 strcpy(err
, "too short escape sequence");
2149 /* \u{H HH HHH HHHH HHHHH HHHHHH ...} */
2151 if (unescape_unicode_list(&p
, end
, buf
, encp
, err
) != 0)
2153 if (p
== end
|| *p
++ != '}') {
2154 strcpy(err
, "invalid Unicode list");
2161 if (unescape_unicode_bmp(&p
, end
, buf
, encp
, err
) != 0)
2166 default: /* \n, \\, \d, \9, etc. */
2170 rb_str_buf_cat(buf
, smallbuf
, 2);
2176 rb_str_buf_cat(buf
, &c
, 1);
2185 rb_reg_preprocess(const char *p
, const char *end
, rb_encoding
*enc
,
2186 rb_encoding
**fixed_enc
, onig_errmsg_buffer err
)
2190 buf
= rb_str_buf_new(0);
2192 if (rb_enc_asciicompat(enc
))
2196 rb_enc_associate(buf
, enc
);
2199 if (unescape_nonascii(p
, end
, enc
, buf
, fixed_enc
, err
) != 0)
2203 rb_enc_associate(buf
, *fixed_enc
);
2210 rb_reg_check_preprocess(VALUE str
)
2212 rb_encoding
*fixed_enc
= 0;
2213 onig_errmsg_buffer err
= "";
2219 p
= RSTRING_PTR(str
);
2220 end
= p
+ RSTRING_LEN(str
);
2221 enc
= rb_enc_get(str
);
2223 buf
= rb_reg_preprocess(p
, end
, enc
, &fixed_enc
, err
);
2227 return rb_reg_error_desc(str
, 0, err
);
2233 rb_reg_preprocess_dregexp(VALUE ary
)
2235 rb_encoding
*fixed_enc
= 0;
2236 rb_encoding
*regexp_enc
= 0;
2237 onig_errmsg_buffer err
= "";
2240 int argc
= RARRAY_LEN(ary
);
2241 VALUE
*argv
= RARRAY_PTR(ary
);
2244 rb_raise(rb_eArgError
, "no arguments given");
2247 for (i
= 0; i
< argc
; i
++) {
2248 VALUE str
= argv
[i
];
2251 rb_encoding
*src_enc
;
2254 p
= RSTRING_PTR(str
);
2255 end
= p
+ RSTRING_LEN(str
);
2256 src_enc
= rb_enc_get(str
);
2258 buf
= rb_reg_preprocess(p
, end
, src_enc
, &fixed_enc
, err
);
2261 rb_raise(rb_eArgError
, "%s", err
);
2263 if (fixed_enc
!= 0) {
2264 if (regexp_enc
!= 0 && regexp_enc
!= fixed_enc
) {
2265 rb_raise(rb_eArgError
, "encoding mismatch in dynamic regexp : %s and %s",
2266 rb_enc_name(regexp_enc
), rb_enc_name(fixed_enc
));
2268 regexp_enc
= fixed_enc
;
2272 result
= rb_str_new3(str
);
2274 rb_str_buf_append(result
, str
);
2277 rb_enc_associate(result
, regexp_enc
);
2284 rb_reg_initialize(VALUE obj
, const char *s
, int len
, rb_encoding
*enc
,
2285 int options
, onig_errmsg_buffer err
)
2287 struct RRegexp
*re
= RREGEXP(obj
);
2289 rb_encoding
*fixed_enc
= 0;
2290 rb_encoding
*a_enc
= rb_ascii8bit_encoding();
2292 if (!OBJ_TAINTED(obj
) && rb_safe_level() >= 4)
2293 rb_raise(rb_eSecurityError
, "Insecure: can't modify regexp");
2294 rb_check_frozen(obj
);
2295 if (FL_TEST(obj
, REG_LITERAL
))
2296 rb_raise(rb_eSecurityError
, "can't modify literal regexp");
2298 rb_raise(rb_eTypeError
, "already initialized regexp");
2301 unescaped
= rb_reg_preprocess(s
, s
+len
, enc
, &fixed_enc
, err
);
2302 if (unescaped
== Qnil
)
2306 if ((fixed_enc
!= enc
&& (options
& ARG_ENCODING_FIXED
)) ||
2307 (fixed_enc
!= a_enc
&& (options
& ARG_ENCODING_NONE
))) {
2308 strcpy(err
, "incompatible character encoding");
2311 if (fixed_enc
!= a_enc
) {
2312 options
|= ARG_ENCODING_FIXED
;
2316 else if (!(options
& ARG_ENCODING_FIXED
)) {
2317 enc
= rb_usascii_encoding();
2320 rb_enc_associate((VALUE
)re
, enc
);
2321 if ((options
& ARG_ENCODING_FIXED
) || fixed_enc
) {
2322 re
->basic
.flags
|= KCODE_FIXED
;
2324 if (options
& ARG_ENCODING_NONE
) {
2325 re
->basic
.flags
|= REG_ENCODING_NONE
;
2328 re
->ptr
= make_regexp(RSTRING_PTR(unescaped
), RSTRING_LEN(unescaped
), enc
,
2329 options
& ARG_REG_OPTION_MASK
, err
);
2330 if (!re
->ptr
) return -1;
2331 re
->src
= rb_enc_str_new(s
, len
, enc
);
2332 OBJ_FREEZE(re
->src
);
2333 RB_GC_GUARD(unescaped
);
2338 rb_reg_initialize_str(VALUE obj
, VALUE str
, int options
, onig_errmsg_buffer err
)
2341 rb_encoding
*enc
= rb_enc_get(str
);
2342 if (options
& ARG_ENCODING_NONE
) {
2343 rb_encoding
*ascii8bit
= rb_ascii8bit_encoding();
2344 if (enc
!= ascii8bit
) {
2345 if (rb_enc_str_coderange(str
) != ENC_CODERANGE_7BIT
) {
2346 strcpy(err
, "/.../n has a non escaped non ASCII character in non ASCII-8BIT script");
2352 ret
= rb_reg_initialize(obj
, RSTRING_PTR(str
), RSTRING_LEN(str
), enc
,
2359 rb_reg_s_alloc(VALUE klass
)
2361 NEWOBJ(re
, struct RRegexp
);
2362 OBJSETUP(re
, klass
, T_REGEXP
);
2372 rb_reg_new_str(VALUE s
, int options
)
2374 VALUE re
= rb_reg_s_alloc(rb_cRegexp
);
2375 onig_errmsg_buffer err
= "";
2377 if (rb_reg_initialize_str(re
, s
, options
, err
) != 0) {
2378 rb_reg_raise_str(s
, options
, err
);
2385 rb_reg_new_ary(VALUE ary
, int opt
)
2387 return rb_reg_new_str(rb_reg_preprocess_dregexp(ary
), opt
);
2391 rb_enc_reg_new(const char *s
, long len
, rb_encoding
*enc
, int options
)
2393 VALUE re
= rb_reg_s_alloc(rb_cRegexp
);
2394 onig_errmsg_buffer err
= "";
2396 if (rb_reg_initialize(re
, s
, len
, enc
, options
, err
) != 0) {
2397 rb_enc_reg_raise(s
, len
, enc
, options
, err
);
2404 rb_reg_new(const char *s
, long len
, int options
)
2406 return rb_enc_reg_new(s
, len
, rb_ascii8bit_encoding(), options
);
2410 rb_reg_compile(VALUE str
, int options
)
2412 VALUE re
= rb_reg_s_alloc(rb_cRegexp
);
2413 onig_errmsg_buffer err
= "";
2415 if (!str
) str
= rb_str_new(0,0);
2416 if (rb_reg_initialize_str(re
, str
, options
, err
) != 0) {
2417 rb_set_errinfo(rb_reg_error_desc(str
, options
, err
));
2420 FL_SET(re
, REG_LITERAL
);
2424 static VALUE reg_cache
;
2427 rb_reg_regcomp(VALUE str
)
2429 volatile VALUE save_str
= str
;
2430 if (reg_cache
&& RREGEXP_SRC_LEN(reg_cache
) == RSTRING_LEN(str
)
2431 && ENCODING_GET(reg_cache
) == ENCODING_GET(str
)
2432 && memcmp(RREGEXP_SRC_PTR(reg_cache
), RSTRING_PTR(str
), RSTRING_LEN(str
)) == 0)
2435 return reg_cache
= rb_reg_new_str(save_str
, 0);
2440 * rxp.hash => fixnum
2442 * Produce a hash based on the text and options of this regular expression.
2446 rb_reg_hash(VALUE re
)
2452 hashval
= RREGEXP(re
)->ptr
->options
;
2453 len
= RREGEXP_SRC_LEN(re
);
2454 p
= RREGEXP_SRC_PTR(re
);
2456 hashval
= hashval
* 33 + *p
++;
2458 hashval
= hashval
+ (hashval
>>5);
2460 return INT2FIX(hashval
);
2466 * rxp == other_rxp => true or false
2467 * rxp.eql?(other_rxp) => true or false
2469 * Equality---Two regexps are equal if their patterns are identical, they have
2470 * the same character set code, and their <code>casefold?</code> values are the
2473 * /abc/ == /abc/x #=> false
2474 * /abc/ == /abc/i #=> false
2475 * /abc/ == /abc/n #=> false
2476 * /abc/u == /abc/n #=> false
2480 rb_reg_equal(VALUE re1
, VALUE re2
)
2482 if (re1
== re2
) return Qtrue
;
2483 if (TYPE(re2
) != T_REGEXP
) return Qfalse
;
2484 rb_reg_check(re1
); rb_reg_check(re2
);
2485 if (FL_TEST(re1
, KCODE_FIXED
) != FL_TEST(re2
, KCODE_FIXED
)) return Qfalse
;
2486 if (RREGEXP(re1
)->ptr
->options
!= RREGEXP(re2
)->ptr
->options
) return Qfalse
;
2487 if (RREGEXP_SRC_LEN(re1
) != RREGEXP_SRC_LEN(re2
)) return Qfalse
;
2488 if (ENCODING_GET(re1
) != ENCODING_GET(re2
)) return Qfalse
;
2489 if (memcmp(RREGEXP_SRC_PTR(re1
), RREGEXP_SRC_PTR(re2
), RREGEXP_SRC_LEN(re1
)) == 0) {
2496 reg_operand(VALUE s
, int check
)
2499 return rb_sym_to_s(s
);
2502 VALUE tmp
= rb_check_string_type(s
);
2503 if (check
&& NIL_P(tmp
)) {
2504 rb_raise(rb_eTypeError
, "can't convert %s to String",
2505 rb_obj_classname(s
));
2512 reg_match_pos(VALUE re
, VALUE
*strp
, long pos
)
2517 rb_backref_set(Qnil
);
2520 *strp
= str
= reg_operand(str
, Qtrue
);
2523 VALUE l
= rb_str_length(str
);
2529 pos
= rb_reg_adjust_startpos(re
, str
, pos
, 0);
2531 return rb_reg_search(re
, str
, pos
, 0);
2536 * rxp =~ str => integer or nil
2538 * Match---Matches <i>rxp</i> against <i>str</i>.
2540 * /at/ =~ "input data" #=> 7
2541 * /ax/ =~ "input data" #=> nil
2543 * If <code>=~</code> is used with a regexp literal with named captures,
2544 * captured strings (or nil) is assigned to local variables named by
2545 * the capture names.
2547 * /(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/ =~ " x = y "
2551 * If it is not matched, nil is assigned for the variables.
2553 * /(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/ =~ " x = "
2557 * This assignment is implemented in the Ruby parser.
2558 * So a regexp literal is required for the assignment.
2559 * The assignment is not occur if the regexp is not a literal.
2561 * re = /(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/
2563 * p lhs # undefined local variable
2564 * p rhs # undefined local variable
2566 * A regexp interpolation, <code>#{}</code>, also disables
2569 * rhs_pat = /(?<rhs>\w+)/
2570 * /(?<lhs>\w+)\s*=\s*#{rhs_pat}/ =~ "x = y"
2571 * p lhs # undefined local variable
2576 rb_reg_match(VALUE re
, VALUE str
)
2578 long pos
= reg_match_pos(re
, &str
, 0);
2579 if (pos
< 0) return Qnil
;
2580 pos
= rb_str_sublen(str
, pos
);
2581 return LONG2FIX(pos
);
2586 * rxp === str => true or false
2588 * Case Equality---Synonym for <code>Regexp#=~</code> used in case statements.
2592 * when /^[a-z]*$/; print "Lower case\n"
2593 * when /^[A-Z]*$/; print "Upper case\n"
2594 * else; print "Mixed case\n"
2597 * <em>produces:</em>
2603 rb_reg_eqq(VALUE re
, VALUE str
)
2607 str
= reg_operand(str
, Qfalse
);
2609 rb_backref_set(Qnil
);
2612 start
= rb_reg_search(re
, str
, 0, 0);
2622 * ~ rxp => integer or nil
2624 * Match---Matches <i>rxp</i> against the contents of <code>$_</code>.
2625 * Equivalent to <code><i>rxp</i> =~ $_</code>.
2632 rb_reg_match2(VALUE re
)
2635 VALUE line
= rb_lastline_get();
2637 if (TYPE(line
) != T_STRING
) {
2638 rb_backref_set(Qnil
);
2642 start
= rb_reg_search(re
, line
, 0, 0);
2646 start
= rb_str_sublen(line
, start
);
2647 return LONG2FIX(start
);
2653 * rxp.match(str) => matchdata or nil
2654 * rxp.match(str,pos) => matchdata or nil
2656 * Returns a <code>MatchData</code> object describing the match, or
2657 * <code>nil</code> if there was no match. This is equivalent to retrieving the
2658 * value of the special variable <code>$~</code> following a normal match.
2659 * If the second parameter is present, it specifies the position in the string
2660 * to begin the search.
2662 * /(.)(.)(.)/.match("abc")[2] #=> "b"
2663 * /(.)(.)/.match("abc", 1)[2] #=> "c"
2665 * If a block is given, invoke the block with MatchData if match succeed, so
2666 * that you can write
2668 * pat.match(str) {|m| ...}
2672 * if m = pat.match(str)
2676 * The return value is a value from block execution in this case.
2680 rb_reg_match_m(int argc
, VALUE
*argv
, VALUE re
)
2682 VALUE result
, str
, initpos
;
2685 if (rb_scan_args(argc
, argv
, "11", &str
, &initpos
) == 2) {
2686 pos
= NUM2LONG(initpos
);
2692 pos
= reg_match_pos(re
, &str
, pos
);
2694 rb_backref_set(Qnil
);
2697 result
= rb_backref_get();
2698 rb_match_busy(result
);
2699 if (!NIL_P(result
) && rb_block_given_p()) {
2700 return rb_yield(result
);
2706 * Document-method: compile
2708 * Synonym for <code>Regexp.new</code>
2714 * Regexp.new(string [, options]) => regexp
2715 * Regexp.new(regexp) => regexp
2716 * Regexp.compile(string [, options]) => regexp
2717 * Regexp.compile(regexp) => regexp
2719 * Constructs a new regular expression from <i>pattern</i>, which can be either
2720 * a <code>String</code> or a <code>Regexp</code> (in which case that regexp's
2721 * options are propagated, and new options may not be specified (a change as of
2722 * Ruby 1.8). If <i>options</i> is a <code>Fixnum</code>, it should be one or
2723 * more of the constants <code>Regexp::EXTENDED</code>,
2724 * <code>Regexp::IGNORECASE</code>, and <code>Regexp::MULTILINE</code>,
2725 * <em>or</em>-ed together. Otherwise, if <i>options</i> is not
2726 * <code>nil</code>, the regexp will be case insensitive.
2728 * r1 = Regexp.new('^a-z+:\\s+\w+') #=> /^a-z+:\s+\w+/
2729 * r2 = Regexp.new('cat', true) #=> /cat/i
2730 * r3 = Regexp.new('dog', Regexp::EXTENDED) #=> /dog/x
2731 * r4 = Regexp.new(r2) #=> /cat/i
2735 rb_reg_initialize_m(int argc
, VALUE
*argv
, VALUE self
)
2737 onig_errmsg_buffer err
= "";
2744 if (argc
== 0 || argc
> 3) {
2745 rb_raise(rb_eArgError
, "wrong number of arguments");
2747 if (TYPE(argv
[0]) == T_REGEXP
) {
2751 rb_warn("flags ignored");
2754 flags
= rb_reg_options(re
);
2755 ptr
= RREGEXP_SRC_PTR(re
);
2756 len
= RREGEXP_SRC_LEN(re
);
2757 enc
= rb_enc_get(re
);
2758 if (rb_reg_initialize(self
, ptr
, len
, enc
, flags
, err
)) {
2759 str
= rb_enc_str_new(ptr
, len
, enc
);
2760 rb_reg_raise_str(str
, flags
, err
);
2765 if (FIXNUM_P(argv
[1])) flags
= FIX2INT(argv
[1]);
2766 else if (RTEST(argv
[1])) flags
= ONIG_OPTION_IGNORECASE
;
2769 if (argc
== 3 && !NIL_P(argv
[2])) {
2770 char *kcode
= StringValuePtr(argv
[2]);
2771 if (kcode
[0] == 'n' || kcode
[1] == 'N') {
2772 enc
= rb_ascii8bit_encoding();
2773 flags
|= ARG_ENCODING_FIXED
;
2776 rb_warning("encoding option is obsolete - %s", kcode
);
2780 ptr
= StringValuePtr(str
);
2782 ? rb_reg_initialize(self
, ptr
, RSTRING_LEN(str
), enc
, flags
, err
)
2783 : rb_reg_initialize_str(self
, str
, flags
, err
)) {
2784 rb_reg_raise_str(str
, flags
, err
);
2791 rb_reg_quote(VALUE str
)
2793 rb_encoding
*enc
= rb_enc_get(str
);
2797 int ascii_only
= rb_enc_str_asciionly_p(str
);
2799 s
= RSTRING_PTR(str
);
2800 send
= s
+ RSTRING_LEN(str
);
2802 c
= rb_enc_ascget(s
, send
, &clen
, enc
);
2804 s
+= mbclen(s
, send
, enc
);
2808 case '[': case ']': case '{': case '}':
2809 case '(': case ')': case '|': case '-':
2810 case '*': case '.': case '\\':
2811 case '?': case '+': case '^': case '$':
2813 case '\t': case '\f': case '\v': case '\n': case '\r':
2818 tmp
= rb_str_new3(str
);
2820 rb_enc_associate(tmp
, rb_usascii_encoding());
2825 tmp
= rb_str_new(0, RSTRING_LEN(str
)*2);
2827 rb_enc_associate(tmp
, rb_usascii_encoding());
2830 rb_enc_copy(tmp
, str
);
2832 t
= RSTRING_PTR(tmp
);
2833 /* copy upto metacharacter */
2834 memcpy(t
, RSTRING_PTR(str
), s
- RSTRING_PTR(str
));
2835 t
+= s
- RSTRING_PTR(str
);
2838 c
= rb_enc_ascget(s
, send
, &clen
, enc
);
2840 int n
= mbclen(s
, send
, enc
);
2848 case '[': case ']': case '{': case '}':
2849 case '(': case ')': case '|': case '-':
2850 case '*': case '.': case '\\':
2851 case '?': case '+': case '^': case '$':
2882 rb_str_resize(tmp
, t
- RSTRING_PTR(tmp
));
2883 OBJ_INFECT(tmp
, str
);
2890 * Regexp.escape(str) => string
2891 * Regexp.quote(str) => string
2893 * Escapes any characters that would have special meaning in a regular
2894 * expression. Returns a new escaped string, or self if no characters are
2895 * escaped. For any string,
2896 * <code>Regexp.new(Regexp.escape(<i>str</i>))=~<i>str</i></code> will be true.
2898 * Regexp.escape('\*?{}.') #=> \\\*\?\{\}\.
2903 rb_reg_s_quote(VALUE c
, VALUE str
)
2905 return rb_reg_quote(reg_operand(str
, Qtrue
));
2909 rb_reg_options(VALUE re
)
2914 options
= RREGEXP(re
)->ptr
->options
& ARG_REG_OPTION_MASK
;
2915 if (RBASIC(re
)->flags
& KCODE_FIXED
) options
|= ARG_ENCODING_FIXED
;
2916 if (RBASIC(re
)->flags
& REG_ENCODING_NONE
) options
|= ARG_ENCODING_NONE
;
2921 rb_check_regexp_type(VALUE re
)
2923 return rb_check_convert_type(re
, T_REGEXP
, "Regexp", "to_regexp");
2928 * Regexp.try_convert(obj) -> re or nil
2930 * Try to convert <i>obj</i> into a Regexp, using to_regexp method.
2931 * Returns converted regexp or nil if <i>obj</i> cannot be converted
2934 * Regexp.try_convert(/re/) #=> /re/
2935 * Regexp.try_convert("re") #=> nil
2938 * Regexp.try_convert(o) #=> nil
2939 * def o.to_regexp() /foo/ end
2940 * Regexp.try_convert(o) #=> /foo/
2944 rb_reg_s_try_convert(VALUE dummy
, VALUE re
)
2946 return rb_check_regexp_type(re
);
2950 rb_reg_s_union(VALUE self
, VALUE args0
)
2952 long argc
= RARRAY_LEN(args0
);
2956 args
[0] = rb_str_new2("(?!)");
2957 return rb_class_new_instance(1, args
, rb_cRegexp
);
2959 else if (argc
== 1) {
2960 VALUE arg
= rb_ary_entry(args0
, 0);
2961 VALUE re
= rb_check_regexp_type(arg
);
2966 quoted
= rb_reg_s_quote(Qnil
, arg
);
2967 return rb_reg_new_str(quoted
, 0);
2972 VALUE source
= rb_str_buf_new(0);
2973 rb_encoding
*result_enc
;
2975 int has_asciionly
= 0;
2976 rb_encoding
*has_ascii_compat_fixed
= 0;
2977 rb_encoding
*has_ascii_incompat
= 0;
2979 for (i
= 0; i
< argc
; i
++) {
2981 VALUE e
= rb_ary_entry(args0
, i
);
2984 rb_str_buf_cat_ascii(source
, "|");
2986 v
= rb_check_regexp_type(e
);
2988 rb_encoding
*enc
= rb_enc_get(v
);
2989 if (!rb_enc_asciicompat(enc
)) {
2990 if (!has_ascii_incompat
)
2991 has_ascii_incompat
= enc
;
2992 else if (has_ascii_incompat
!= enc
)
2993 rb_raise(rb_eArgError
, "incompatible encodings: %s and %s",
2994 rb_enc_name(has_ascii_incompat
), rb_enc_name(enc
));
2996 else if (rb_reg_fixed_encoding_p(v
)) {
2997 if (!has_ascii_compat_fixed
)
2998 has_ascii_compat_fixed
= enc
;
2999 else if (has_ascii_compat_fixed
!= enc
)
3000 rb_raise(rb_eArgError
, "incompatible encodings: %s and %s",
3001 rb_enc_name(has_ascii_compat_fixed
), rb_enc_name(enc
));
3009 rb_encoding
*enc
= rb_enc_get(e
);
3011 enc
= rb_enc_get(e
);
3012 if (!rb_enc_str_asciicompat_p(e
)) {
3013 if (!has_ascii_incompat
)
3014 has_ascii_incompat
= enc
;
3015 else if (has_ascii_incompat
!= enc
)
3016 rb_raise(rb_eArgError
, "incompatible encodings: %s and %s",
3017 rb_enc_name(has_ascii_incompat
), rb_enc_name(enc
));
3019 else if (rb_enc_str_asciionly_p(e
)) {
3023 if (!has_ascii_compat_fixed
)
3024 has_ascii_compat_fixed
= enc
;
3025 else if (has_ascii_compat_fixed
!= enc
)
3026 rb_raise(rb_eArgError
, "incompatible encodings: %s and %s",
3027 rb_enc_name(has_ascii_compat_fixed
), rb_enc_name(enc
));
3029 v
= rb_reg_s_quote(Qnil
, e
);
3031 if (has_ascii_incompat
) {
3032 if (has_asciionly
) {
3033 rb_raise(rb_eArgError
, "ASCII incompatible encoding: %s",
3034 rb_enc_name(has_ascii_incompat
));
3036 if (has_ascii_compat_fixed
) {
3037 rb_raise(rb_eArgError
, "incompatible encodings: %s and %s",
3038 rb_enc_name(has_ascii_incompat
), rb_enc_name(has_ascii_compat_fixed
));
3043 rb_enc_copy(source
, v
);
3045 rb_str_append(source
, v
);
3048 if (has_ascii_incompat
) {
3049 result_enc
= has_ascii_incompat
;
3051 else if (has_ascii_compat_fixed
) {
3052 result_enc
= has_ascii_compat_fixed
;
3055 result_enc
= rb_ascii8bit_encoding();
3058 rb_enc_associate(source
, result_enc
);
3059 return rb_class_new_instance(1, &source
, rb_cRegexp
);
3065 * Regexp.union(pat1, pat2, ...) => new_regexp
3066 * Regexp.union(pats_ary) => new_regexp
3068 * Return a <code>Regexp</code> object that is the union of the given
3069 * <em>pattern</em>s, i.e., will match any of its parts. The <em>pattern</em>s
3070 * can be Regexp objects, in which case their options will be preserved, or
3071 * Strings. If no patterns are given, returns <code>/(?!)/</code>.
3073 * Regexp.union #=> /(?!)/
3074 * Regexp.union("penzance") #=> /penzance/
3075 * Regexp.union("a+b*c") #=> /a\+b\*c/
3076 * Regexp.union("skiing", "sledding") #=> /skiing|sledding/
3077 * Regexp.union(["skiing", "sledding"]) #=> /skiing|sledding/
3078 * Regexp.union(/dogs/, /cats/i) #=> /(?-mix:dogs)|(?i-mx:cats)/
3081 rb_reg_s_union_m(VALUE self
, VALUE args
)
3084 if (RARRAY_LEN(args
) == 1 &&
3085 !NIL_P(v
= rb_check_array_type(rb_ary_entry(args
, 0)))) {
3086 return rb_reg_s_union(self
, v
);
3088 return rb_reg_s_union(self
, args
);
3093 rb_reg_init_copy(VALUE copy
, VALUE re
)
3095 onig_errmsg_buffer err
= "";
3099 if (copy
== re
) return copy
;
3100 rb_check_frozen(copy
);
3101 /* need better argument type check */
3102 if (!rb_obj_is_instance_of(re
, rb_obj_class(copy
))) {
3103 rb_raise(rb_eTypeError
, "wrong argument type");
3106 s
= RREGEXP_SRC_PTR(re
);
3107 len
= RREGEXP_SRC_LEN(re
);
3108 if (rb_reg_initialize(copy
, s
, len
, rb_enc_get(re
), rb_reg_options(re
), err
) != 0) {
3109 rb_reg_raise(s
, len
, err
, re
);
3115 rb_reg_regsub(VALUE str
, VALUE src
, struct re_registers
*regs
, VALUE regexp
)
3120 rb_encoding
*str_enc
= rb_enc_get(str
);
3121 rb_encoding
*src_enc
= rb_enc_get(src
);
3122 int acompat
= rb_enc_asciicompat(str_enc
);
3123 #define ASCGET(s,e,cl) (acompat ? (*cl=1,s[0]) : rb_enc_ascget(s, e, cl, str_enc))
3125 p
= s
= RSTRING_PTR(str
);
3126 e
= s
+ RSTRING_LEN(str
);
3129 int c
= ASCGET(s
, e
, &clen
);
3133 s
+= mbclen(s
, e
, str_enc
);
3139 if (c
!= '\\' || s
== e
) continue;
3142 val
= rb_str_buf_new(ss
-p
);
3144 rb_enc_str_buf_cat(val
, p
, ss
-p
, str_enc
);
3146 c
= ASCGET(s
, e
, &clen
);
3148 s
+= mbclen(s
, e
, str_enc
);
3149 rb_enc_str_buf_cat(val
, ss
, s
-ss
, str_enc
);
3157 case '1': case '2': case '3': case '4':
3158 case '5': case '6': case '7': case '8': case '9':
3159 if (onig_noname_group_capture_is_active(RREGEXP(regexp
)->ptr
)) {
3168 if (s
< e
&& ASCGET(s
, e
, &clen
) == '<') {
3169 char *name
, *name_end
;
3171 name_end
= name
= s
+ clen
;
3172 while (name_end
< e
) {
3173 c
= ASCGET(name_end
, e
, &clen
);
3174 if (c
== '>') break;
3175 name_end
+= c
== -1 ? mbclen(name_end
, e
, str_enc
) : clen
;
3178 no
= name_to_backref_number(regs
, regexp
, name
, name_end
);
3179 p
= s
= name_end
+ clen
;
3183 rb_raise(rb_eRuntimeError
, "invalid group name reference format");
3187 rb_enc_str_buf_cat(val
, ss
, s
-ss
, str_enc
);
3196 rb_enc_str_buf_cat(val
, RSTRING_PTR(src
), BEG(0), src_enc
);
3200 rb_enc_str_buf_cat(val
, RSTRING_PTR(src
)+END(0), RSTRING_LEN(src
)-END(0), src_enc
);
3204 no
= regs
->num_regs
-1;
3205 while (BEG(no
) == -1 && no
> 0) no
--;
3206 if (no
== 0) continue;
3210 rb_enc_str_buf_cat(val
, s
-clen
, clen
, str_enc
);
3214 rb_enc_str_buf_cat(val
, ss
, s
-ss
, str_enc
);
3219 if (no
>= regs
->num_regs
) continue;
3220 if (BEG(no
) == -1) continue;
3221 rb_enc_str_buf_cat(val
, RSTRING_PTR(src
)+BEG(no
), END(no
)-BEG(no
), src_enc
);
3225 if (!val
) return str
;
3227 rb_enc_str_buf_cat(val
, p
, e
-p
, str_enc
);
3236 rb_warn("variable $KCODE is no longer effective");
3241 kcode_setter(VALUE val
, ID id
)
3243 rb_warn("variable $KCODE is no longer effective; ignored");
3247 ignorecase_getter(void)
3249 rb_warn("variable $= is no longer effective");
3254 ignorecase_setter(VALUE val
, ID id
)
3256 rb_warn("variable $= is no longer effective; ignored");
3262 VALUE match
= rb_backref_get();
3264 if (NIL_P(match
)) return Qnil
;
3265 rb_match_busy(match
);
3270 match_setter(VALUE val
)
3273 Check_Type(val
, T_MATCH
);
3275 rb_backref_set(val
);
3280 * Regexp.last_match => matchdata
3281 * Regexp.last_match(n) => str
3283 * The first form returns the <code>MatchData</code> object generated by the
3284 * last successful pattern match. Equivalent to reading the global variable
3285 * <code>$~</code>. The second form returns the <i>n</i>th field in this
3286 * <code>MatchData</code> object.
3287 * <em>n</em> can be a string or symbol to reference a named capture.
3289 * /c(.)t/ =~ 'cat' #=> 0
3290 * Regexp.last_match #=> #<MatchData "cat" 1:"a">
3291 * Regexp.last_match(0) #=> "cat"
3292 * Regexp.last_match(1) #=> "a"
3293 * Regexp.last_match(2) #=> nil
3295 * /(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/ =~ "var = val"
3296 * Regexp.last_match #=> #<MatchData "var = val" lhs:"var" rhs:"val">
3297 * Regexp.last_match(:lhs) #=> "var"
3298 * Regexp.last_match(:rhs) #=> "val"
3302 rb_reg_s_last_match(int argc
, VALUE
*argv
)
3306 if (argc
> 0 && rb_scan_args(argc
, argv
, "01", &nth
) == 1) {
3307 VALUE match
= rb_backref_get();
3309 if (NIL_P(match
)) return Qnil
;
3310 n
= match_backref_number(match
, nth
);
3311 return rb_reg_nth_match(n
, match
);
3313 return match_getter();
3317 re_warn(const char *s
)
3323 * Document-class: Regexp
3325 * A <code>Regexp</code> holds a regular expression, used to match a pattern
3326 * against strings. Regexps are created using the <code>/.../</code> and
3327 * <code>%r{...}</code> literals, and by the <code>Regexp::new</code>
3335 rb_eRegexpError
= rb_define_class("RegexpError", rb_eStandardError
);
3337 onigenc_set_default_caseconv_table((UChar
*)casetable
);
3338 onigenc_set_default_encoding(ONIG_ENCODING_ASCII
);
3339 onig_set_warn_func(re_warn
);
3340 onig_set_verb_warn_func(re_warn
);
3342 rb_define_virtual_variable("$~", match_getter
, match_setter
);
3343 rb_define_virtual_variable("$&", last_match_getter
, 0);
3344 rb_define_virtual_variable("$`", prematch_getter
, 0);
3345 rb_define_virtual_variable("$'", postmatch_getter
, 0);
3346 rb_define_virtual_variable("$+", last_paren_match_getter
, 0);
3348 rb_define_virtual_variable("$=", ignorecase_getter
, ignorecase_setter
);
3349 rb_define_virtual_variable("$KCODE", kcode_getter
, kcode_setter
);
3350 rb_define_virtual_variable("$-K", kcode_getter
, kcode_setter
);
3352 rb_cRegexp
= rb_define_class("Regexp", rb_cObject
);
3353 rb_define_alloc_func(rb_cRegexp
, rb_reg_s_alloc
);
3354 rb_define_singleton_method(rb_cRegexp
, "compile", rb_class_new_instance
, -1);
3355 rb_define_singleton_method(rb_cRegexp
, "quote", rb_reg_s_quote
, 1);
3356 rb_define_singleton_method(rb_cRegexp
, "escape", rb_reg_s_quote
, 1);
3357 rb_define_singleton_method(rb_cRegexp
, "union", rb_reg_s_union_m
, -2);
3358 rb_define_singleton_method(rb_cRegexp
, "last_match", rb_reg_s_last_match
, -1);
3359 rb_define_singleton_method(rb_cRegexp
, "try_convert", rb_reg_s_try_convert
, 1);
3361 rb_define_method(rb_cRegexp
, "initialize", rb_reg_initialize_m
, -1);
3362 rb_define_method(rb_cRegexp
, "initialize_copy", rb_reg_init_copy
, 1);
3363 rb_define_method(rb_cRegexp
, "hash", rb_reg_hash
, 0);
3364 rb_define_method(rb_cRegexp
, "eql?", rb_reg_equal
, 1);
3365 rb_define_method(rb_cRegexp
, "==", rb_reg_equal
, 1);
3366 rb_define_method(rb_cRegexp
, "=~", rb_reg_match
, 1);
3367 rb_define_method(rb_cRegexp
, "===", rb_reg_eqq
, 1);
3368 rb_define_method(rb_cRegexp
, "~", rb_reg_match2
, 0);
3369 rb_define_method(rb_cRegexp
, "match", rb_reg_match_m
, -1);
3370 rb_define_method(rb_cRegexp
, "to_s", rb_reg_to_s
, 0);
3371 rb_define_method(rb_cRegexp
, "inspect", rb_reg_inspect
, 0);
3372 rb_define_method(rb_cRegexp
, "source", rb_reg_source
, 0);
3373 rb_define_method(rb_cRegexp
, "casefold?", rb_reg_casefold_p
, 0);
3374 rb_define_method(rb_cRegexp
, "options", rb_reg_options_m
, 0);
3375 rb_define_method(rb_cRegexp
, "encoding", rb_obj_encoding
, 0); /* in encoding.c */
3376 rb_define_method(rb_cRegexp
, "fixed_encoding?", rb_reg_fixed_encoding_p
, 0);
3377 rb_define_method(rb_cRegexp
, "names", rb_reg_names
, 0);
3378 rb_define_method(rb_cRegexp
, "named_captures", rb_reg_named_captures
, 0);
3380 rb_define_const(rb_cRegexp
, "IGNORECASE", INT2FIX(ONIG_OPTION_IGNORECASE
));
3381 rb_define_const(rb_cRegexp
, "EXTENDED", INT2FIX(ONIG_OPTION_EXTEND
));
3382 rb_define_const(rb_cRegexp
, "MULTILINE", INT2FIX(ONIG_OPTION_MULTILINE
));
3384 rb_global_variable(®_cache
);
3386 rb_cMatch
= rb_define_class("MatchData", rb_cObject
);
3387 rb_define_alloc_func(rb_cMatch
, match_alloc
);
3388 rb_undef_method(CLASS_OF(rb_cMatch
), "new");
3390 rb_define_method(rb_cMatch
, "initialize_copy", match_init_copy
, 1);
3391 rb_define_method(rb_cMatch
, "regexp", match_regexp
, 0);
3392 rb_define_method(rb_cMatch
, "names", match_names
, 0);
3393 rb_define_method(rb_cMatch
, "size", match_size
, 0);
3394 rb_define_method(rb_cMatch
, "length", match_size
, 0);
3395 rb_define_method(rb_cMatch
, "offset", match_offset
, 1);
3396 rb_define_method(rb_cMatch
, "begin", match_begin
, 1);
3397 rb_define_method(rb_cMatch
, "end", match_end
, 1);
3398 rb_define_method(rb_cMatch
, "to_a", match_to_a
, 0);
3399 rb_define_method(rb_cMatch
, "[]", match_aref
, -1);
3400 rb_define_method(rb_cMatch
, "captures", match_captures
, 0);
3401 rb_define_method(rb_cMatch
, "values_at", match_values_at
, -1);
3402 rb_define_method(rb_cMatch
, "pre_match", rb_reg_match_pre
, 0);
3403 rb_define_method(rb_cMatch
, "post_match", rb_reg_match_post
, 0);
3404 rb_define_method(rb_cMatch
, "to_s", match_to_s
, 0);
3405 rb_define_method(rb_cMatch
, "inspect", match_inspect
, 0);
3406 rb_define_method(rb_cMatch
, "string", match_string
, 0);