317
int rb_str_buf_cat_escaped_char(VALUE result, unsigned int c, int unicode_p);
318
rb_reg_expr_str(VALUE str, const char *s, long len)
320
rb_reg_expr_str(VALUE str, const char *s, long len,
321
rb_encoding *enc, rb_encoding *resenc)
320
rb_encoding *enc = rb_enc_get(str);
321
323
const char *p, *pend;
322
324
int need_escape = 0;
325
327
p = s; pend = p + len;
327
c = rb_enc_ascget(p, pend, &clen, enc);
329
p += mbclen(p, pend, enc);
331
else if (c != '/' && rb_enc_isprint(c, enc)) {
328
if (rb_enc_asciicompat(enc)) {
330
c = rb_enc_ascget(p, pend, &clen, enc);
333
p += mbclen(p, pend, enc);
340
else if (c != '/' && rb_enc_isprint(c, enc)) {
339
353
if (!need_escape) {
340
354
rb_str_buf_cat(str, s, len);
357
int unicode_p = rb_enc_unicode_p(enc);
345
360
c = rb_enc_ascget(p, pend, &clen, enc);
355
370
rb_str_buf_cat(str, p, clen);
357
372
else if (c == -1) {
358
int l = mbclen(p, pend, enc);
359
rb_str_buf_cat(str, p, l);
373
clen = rb_enc_precise_mbclen(p, pend, enc);
374
if (!MBCLEN_CHARFOUND_P(clen)) {
375
c = (unsigned char)*p;
380
unsigned int c = rb_enc_mbc_to_codepoint(p, pend, enc);
381
rb_str_buf_cat_escaped_char(str, c, unicode_p);
384
clen = MBCLEN_CHARFOUND_LEN(clen);
385
rb_str_buf_cat(str, p, clen);
363
388
else if (rb_enc_isprint(c, enc)) {
364
389
rb_str_buf_cat(str, p, clen);
366
391
else if (!rb_enc_isspace(c, enc)) {
369
sprintf(b, "\\x%02X", c);
395
snprintf(b, sizeof(b), "\\x%02X", c);
370
396
rb_str_buf_cat(str, b, 4);
381
407
rb_reg_desc(const char *s, long len, VALUE re)
409
rb_encoding *enc = rb_enc_get(re);
383
410
VALUE str = rb_str_buf_new2("/");
384
if (re && rb_enc_asciicompat(rb_enc_get(re))) {
411
rb_encoding *resenc = rb_default_internal_encoding();
412
if (resenc == NULL) resenc = rb_default_external_encoding();
414
if (re && rb_enc_asciicompat(enc)) {
385
415
rb_enc_copy(str, re);
388
418
rb_enc_associate(str, rb_usascii_encoding());
390
rb_reg_expr_str(str, s, len);
420
rb_reg_expr_str(str, s, len, enc, resenc);
391
421
rb_str_buf_cat2(str, "/");
394
424
rb_reg_check(re);
395
425
if (*option_to_str(opts, RREGEXP(re)->ptr->options))
396
426
rb_str_buf_cat2(str, opts);
427
if (RBASIC(re)->flags & REG_ENCODING_NONE)
428
rb_str_buf_cat2(str, "n");
398
430
OBJ_INFECT(str, re);
521
554
if (*ptr == ':' && ptr[len-1] == ')') {
524
r = onig_alloc_init(&rp, ONIG_OPTION_DEFAULT,
525
ONIGENC_CASE_FOLD_DEFAULT,
531
err = (onig_compile(rp, ptr, ptr + len, NULL) != 0);
559
err = onig_new(&rp, ptr, ptr + len, ONIG_OPTION_DEFAULT,
560
enc, OnigDefaultSyntax, NULL);
550
578
rb_str_buf_cat2(str, ":");
551
rb_reg_expr_str(str, (char*)ptr, len);
579
rb_reg_expr_str(str, (char*)ptr, len, enc, NULL);
552
580
rb_str_buf_cat2(str, ")");
553
581
rb_enc_copy(str, re);
571
599
VALUE desc = rb_str_buf_new2(err);
600
rb_encoding *resenc = rb_default_internal_encoding();
601
if (resenc == NULL) resenc = rb_default_external_encoding();
573
603
rb_enc_associate(desc, enc);
574
604
rb_str_buf_cat2(desc, ": /");
575
rb_reg_expr_str(desc, s, len);
605
rb_reg_expr_str(desc, s, len, enc, resenc);
577
607
option_to_str(opts + 1, options);
578
608
rb_str_buf_cat2(desc, opts);
663
* rxp.names => [name1, name2, ...]
693
* rxp.names -> [name1, name2, ...]
665
695
* Returns a list of names of captures as an array of strings.
704
* rxp.named_captures => hash
734
* rxp.named_captures -> hash
706
736
* Returns a hash representing information about named captures of <i>rxp</i>.
708
738
* A key of the hash is a name of the named captures.
709
739
* A value of the hash is an array which is list of indexes of corresponding
710
740
* named captures.
712
742
* /(?<foo>.)(?<bar>.)/.named_captures
713
743
* #=> {"foo"=>[1], "bar"=>[2]}
764
onig_new_with_source(regex_t** reg, const UChar* pattern, const UChar* pattern_end,
765
OnigOptionType option, OnigEncoding enc, const OnigSyntaxType* syntax,
766
OnigErrorInfo* einfo, const char *sourcefile, int sourceline)
770
*reg = (regex_t* )xmalloc(sizeof(regex_t));
771
if (IS_NULL(*reg)) return ONIGERR_MEMORY;
773
r = onig_reg_init(*reg, option, ONIGENC_CASE_FOLD_DEFAULT, enc, syntax);
776
r = onig_compile(*reg, pattern, pattern_end, einfo, sourcefile, sourceline);
734
make_regexp(const char *s, long len, rb_encoding *enc, int flags, onig_errmsg_buffer err)
786
make_regexp(const char *s, long len, rb_encoding *enc, int flags, onig_errmsg_buffer err,
787
const char *sourcefile, int sourceline)
747
r = onig_alloc_init(&rp, flags, ONIGENC_CASE_FOLD_DEFAULT,
748
enc, OnigDefaultSyntax);
800
r = onig_new_with_source(&rp, (UChar*)s, (UChar*)(s + len), flags,
801
enc, OnigDefaultSyntax, &einfo, sourcefile, sourceline);
750
onig_error_code_to_str((UChar*)err, r);
754
r = onig_compile(rp, (UChar*)s, (UChar*)(s + len), &einfo);
758
(void )onig_error_code_to_str((UChar*)err, r, &einfo);
803
onig_error_code_to_str((UChar*)err, r, &einfo);
800
845
pair_byte_cmp(const void *pair1, const void *pair2)
802
return ((pair_t*)pair1)->byte_pos - ((pair_t*)pair2)->byte_pos;
847
long diff = ((pair_t*)pair1)->byte_pos - ((pair_t*)pair2)->byte_pos;
848
#if SIZEOF_LONG > SIZEOF_INT
849
return diff ? diff > 0 ? 1 : -1 : 0;
938
* mtch.names => [name1, name2, ...]
988
* mtch.names -> [name1, name2, ...]
940
990
* Returns a list of names of captures as an array of strings.
941
991
* It is same as mtch.regexp.names.
1062
rb_reg_backref_number(VALUE match, VALUE backref)
1064
return match_backref_number(match, backref);
1014
* mtch.offset(n) => array
1069
* mtch.offset(n) -> array
1016
1071
* Returns a two-element array containing the beginning and ending offsets of
1017
1072
* the <em>n</em>th match.
1253
rb_reg_adjust_startpos(VALUE re, VALUE str, int pos, int reverse)
1308
rb_reg_adjust_startpos(VALUE re, VALUE str, long pos, int reverse)
1256
1311
rb_encoding *enc;
1257
1312
UChar *p, *string;
1284
rb_reg_search(VALUE re, VALUE str, int pos, int reverse)
1339
rb_reg_search(VALUE re, VALUE str, long pos, int reverse)
1288
1343
struct re_registers regi, *regs = ®i;
1289
1344
char *range = RSTRING_PTR(str);
1341
1396
onig_errmsg_buffer err = "";
1342
onig_error_code_to_str((UChar*)err, result);
1397
onig_error_code_to_str((UChar*)err, (int)result);
1343
1398
rb_reg_raise(RREGEXP_SRC_PTR(re), RREGEXP_SRC_LEN(re), err, 0);
1519
1574
match_array(VALUE match, int start)
1521
struct re_registers *regs = RMATCH_REGS(match);
1522
VALUE ary = rb_ary_new2(regs->num_regs);
1523
VALUE target = RMATCH(match)->str;
1576
struct re_registers *regs;
1525
1580
int taint = OBJ_TAINTED(match);
1527
1582
match_check(match);
1583
regs = RMATCH_REGS(match);
1584
ary = rb_ary_new2(regs->num_regs);
1585
target = RMATCH(match)->str;
1528
1587
for (i=start; i<regs->num_regs; i++) {
1529
1588
if (regs->beg[i] == -1) {
1530
1589
rb_ary_push(ary, Qnil);
1612
* mtch[i] => str or nil
1613
* mtch[start, length] => array
1614
* mtch[range] => array
1615
* mtch[name] => str or nil
1671
* mtch[i] -> str or nil
1672
* mtch[start, length] -> array
1673
* mtch[range] -> array
1674
* mtch[name] -> str or nil
1617
1676
* Match Reference---<code>MatchData</code> acts as an array, and may be
1618
1677
* accessed using the normal array indexing techniques. <i>mtch</i>[0] is
1642
1701
rb_scan_args(argc, argv, "11", &idx, &rest);
1644
1703
if (NIL_P(rest)) {
1645
if (FIXNUM_P(idx)) {
1646
if (FIX2INT(idx) >= 0) {
1647
return rb_reg_nth_match(FIX2INT(idx), match);
1654
switch (TYPE(idx)) {
1656
p = rb_id2name(SYM2ID(idx));
1657
goto name_to_backref;
1660
p = StringValuePtr(idx);
1663
num = name_to_backref_number(RMATCH_REGS(match),
1664
RMATCH(match)->regexp, p, p + strlen(p));
1665
return rb_reg_nth_match(num, match);
1704
if (FIXNUM_P(idx)) {
1705
if (FIX2INT(idx) >= 0) {
1706
return rb_reg_nth_match(FIX2INT(idx), match);
1713
switch (TYPE(idx)) {
1715
p = rb_id2name(SYM2ID(idx));
1716
goto name_to_backref;
1719
p = StringValuePtr(idx);
1722
num = name_to_backref_number(RMATCH_REGS(match),
1723
RMATCH(match)->regexp, p, p + strlen(p));
1724
return rb_reg_nth_match(num, match);
1674
1733
return rb_ary_aref(argc, argv, match_to_a(match));
1678
1737
match_entry(VALUE match, long n)
1680
return rb_reg_nth_match(n, match);
1739
/* n should not exceed num_regs */
1740
return rb_reg_nth_match((int)n, match);
1687
* mtch.values_at([index]*) => array
1747
* mtch.values_at([index]*) -> array
1689
1749
* Uses each <i>index</i> to access the matching values, returning an array of
1690
1750
* the corresponding matches.
1698
1758
match_values_at(int argc, VALUE *argv, VALUE match)
1700
struct re_registers *regs = RMATCH_REGS(match);
1760
struct re_registers *regs;
1701
1762
match_check(match);
1763
regs = RMATCH_REGS(match);
1702
1764
return rb_get_values_at(match, regs->num_regs, argc, argv, match_entry);
1710
1772
* Returns the entire matched string.
1864
1926
case '0': case '1': case '2': case '3':
1865
1927
case '4': case '5': case '6': case '7':
1867
code = ruby_scan_oct(p, end < p+3 ? end-p : 3, &len);
1929
code = scan_oct(p, end < p+3 ? end-p : 3, &len);
1871
1933
case 'x': /* \xHH */
1872
code = ruby_scan_hex(p, end < p+2 ? end-p : 2, &len);
1934
code = scan_hex(p, end < p+2 ? end-p : 2, &len);
1874
1936
errcpy(err, "invalid hex escape");
2125
2187
case '1': case '2': case '3':
2126
2188
case '4': case '5': case '6': case '7': /* \O, \OO, \OOO or backref */
2129
2191
if (ruby_scan_oct(p-1, end-(p-1), &octlen) <= 0177) {
2130
2192
/* backref or 7bit octal.
2131
2193
no need to unescape anyway.
2250
rb_reg_preprocess_dregexp(VALUE ary)
2313
rb_reg_preprocess_dregexp(VALUE ary, int options)
2252
2315
rb_encoding *fixed_enc = 0;
2253
2316
rb_encoding *regexp_enc = 0;
2254
2317
onig_errmsg_buffer err = "";
2256
2319
VALUE result = 0;
2320
rb_encoding *ascii8bit = rb_ascii8bit_encoding();
2258
2322
if (RARRAY_LEN(ary) == 0) {
2259
2323
rb_raise(rb_eArgError, "no arguments given");
2266
2330
rb_encoding *src_enc;
2332
src_enc = rb_enc_get(str);
2333
if (options & ARG_ENCODING_NONE &&
2334
src_enc != ascii8bit) {
2335
if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT)
2336
rb_raise(rb_eRegexpError, "/.../n has a non escaped non ASCII character in non ASCII-8BIT script");
2338
src_enc = ascii8bit;
2268
2341
StringValue(str);
2269
2342
p = RSTRING_PTR(str);
2270
2343
end = p + RSTRING_LEN(str);
2271
src_enc = rb_enc_get(str);
2273
2345
buf = rb_reg_preprocess(p, end, src_enc, &fixed_enc, err);
2278
2350
if (fixed_enc != 0) {
2279
2351
if (regexp_enc != 0 && regexp_enc != fixed_enc) {
2280
rb_raise(rb_eArgError, "encoding mismatch in dynamic regexp : %s and %s",
2352
rb_raise(rb_eRegexpError, "encoding mismatch in dynamic regexp : %s and %s",
2281
2353
rb_enc_name(regexp_enc), rb_enc_name(fixed_enc));
2283
2355
regexp_enc = fixed_enc;
2299
rb_reg_initialize(VALUE obj, const char *s, int len, rb_encoding *enc,
2300
int options, onig_errmsg_buffer err)
2371
rb_reg_initialize(VALUE obj, const char *s, long len, rb_encoding *enc,
2372
int options, onig_errmsg_buffer err,
2373
const char *sourcefile, int sourceline)
2302
2375
struct RRegexp *re = RREGEXP(obj);
2303
2376
VALUE unescaped;
2344
2417
if (options & ARG_ENCODING_NONE) {
2345
2418
re->basic.flags |= REG_ENCODING_NONE;
2348
2421
re->ptr = make_regexp(RSTRING_PTR(unescaped), RSTRING_LEN(unescaped), enc,
2349
options & ARG_REG_OPTION_MASK, err);
2422
options & ARG_REG_OPTION_MASK, err,
2423
sourcefile, sourceline);
2350
2424
if (!re->ptr) return -1;
2351
2425
re->src = rb_enc_str_new(s, len, enc);
2352
2426
OBJ_FREEZE(re->src);
2358
rb_reg_initialize_str(VALUE obj, VALUE str, int options, onig_errmsg_buffer err)
2432
rb_reg_initialize_str(VALUE obj, VALUE str, int options, onig_errmsg_buffer err,
2433
const char *sourcefile, int sourceline)
2361
2436
rb_encoding *enc = rb_enc_get(str);
2372
2447
ret = rb_reg_initialize(obj, RSTRING_PTR(str), RSTRING_LEN(str), enc,
2448
options, err, sourcefile, sourceline);
2374
2449
RB_GC_GUARD(str);
2469
return rb_reg_s_alloc(rb_cRegexp);
2392
2473
rb_reg_new_str(VALUE s, int options)
2394
VALUE re = rb_reg_s_alloc(rb_cRegexp);
2475
return rb_reg_init_str(rb_reg_alloc(), s, options);
2479
rb_reg_init_str(VALUE re, VALUE s, int options)
2395
2481
onig_errmsg_buffer err = "";
2397
if (rb_reg_initialize_str(re, s, options, err) != 0) {
2483
if (rb_reg_initialize_str(re, s, options, err, NULL, 0) != 0) {
2398
2484
rb_reg_raise_str(s, options, err);
2405
2491
rb_reg_new_ary(VALUE ary, int opt)
2407
return rb_reg_new_str(rb_reg_preprocess_dregexp(ary), opt);
2493
return rb_reg_new_str(rb_reg_preprocess_dregexp(ary, opt), opt);
2411
2497
rb_enc_reg_new(const char *s, long len, rb_encoding *enc, int options)
2413
VALUE re = rb_reg_s_alloc(rb_cRegexp);
2499
VALUE re = rb_reg_alloc();
2414
2500
onig_errmsg_buffer err = "";
2416
if (rb_reg_initialize(re, s, len, enc, options, err) != 0) {
2502
if (rb_reg_initialize(re, s, len, enc, options, err, NULL, 0) != 0) {
2417
2503
rb_enc_reg_raise(s, len, enc, options, err);
2430
rb_reg_compile(VALUE str, int options)
2516
rb_reg_compile(VALUE str, int options, const char *sourcefile, int sourceline)
2432
VALUE re = rb_reg_s_alloc(rb_cRegexp);
2518
VALUE re = rb_reg_alloc();
2433
2519
onig_errmsg_buffer err = "";
2435
2521
if (!str) str = rb_str_new(0,0);
2436
if (rb_reg_initialize_str(re, str, options, err) != 0) {
2522
if (rb_reg_initialize_str(re, str, options, err, sourcefile, sourceline) != 0) {
2437
2523
rb_set_errinfo(rb_reg_error_desc(str, options, err));
2466
2553
rb_reg_hash(VALUE re)
2555
st_index_t hashval = reg_hash(re);
2556
return LONG2FIX(hashval);
2471
2564
rb_reg_check(re);
2472
2565
hashval = RREGEXP(re)->ptr->options;
2473
len = RREGEXP_SRC_LEN(re);
2474
p = RREGEXP_SRC_PTR(re);
2476
hashval = hashval * 33 + *p++;
2478
hashval = hashval + (hashval>>5);
2480
return INT2FIX(hashval);
2566
hashval = rb_hash_uint(hashval, rb_memhash(RREGEXP_SRC_PTR(re), RREGEXP_SRC_LEN(re)));
2567
return rb_hash_end(hashval);
2486
* rxp == other_rxp => true or false
2487
* rxp.eql?(other_rxp) => true or false
2573
* rxp == other_rxp -> true or false
2574
* rxp.eql?(other_rxp) -> true or false
2489
2576
* Equality---Two regexps are equal if their patterns are identical, they have
2490
2577
* the same character set code, and their <code>casefold?</code> values are the
2604
* mtch.hash -> integer
2606
* Produce a hash based on the target string, regexp and matched
2607
* positions of this matchdata.
2611
match_hash(VALUE match)
2613
const struct re_registers *regs;
2614
st_index_t hashval = rb_hash_start(rb_str_hash(RMATCH(match)->str));
2616
rb_hash_uint(hashval, reg_hash(RMATCH(match)->regexp));
2617
regs = RMATCH_REGS(match);
2618
hashval = rb_hash_uint(hashval, regs->num_regs);
2619
hashval = rb_hash_uint(hashval, rb_memhash(regs->beg, regs->num_regs * sizeof(*regs->beg)));
2620
hashval = rb_hash_uint(hashval, rb_memhash(regs->end, regs->num_regs * sizeof(*regs->end)));
2621
hashval = rb_hash_end(hashval);
2622
return LONG2FIX(hashval);
2627
* mtch == mtch2 -> true or false
2629
* Equality---Two matchdata are equal if their target strings,
2630
* patterns, and matched positions are identical.
2634
match_equal(VALUE match1, VALUE match2)
2636
const struct re_registers *regs1, *regs2;
2637
if (match1 == match2) return Qtrue;
2638
if (TYPE(match2) != T_MATCH) return Qfalse;
2639
if (!rb_str_equal(RMATCH(match1)->str, RMATCH(match2)->str)) return Qfalse;
2640
if (!rb_reg_equal(RMATCH(match1)->regexp, RMATCH(match2)->regexp)) return Qfalse;
2641
regs1 = RMATCH_REGS(match1);
2642
regs2 = RMATCH_REGS(match2);
2643
if (regs1->num_regs != regs2->num_regs) return Qfalse;
2644
if (memcmp(regs1->beg, regs2->beg, regs1->num_regs * sizeof(*regs1->beg))) return Qfalse;
2645
if (memcmp(regs1->end, regs2->end, regs1->num_regs * sizeof(*regs1->end))) return Qfalse;
2516
2650
reg_operand(VALUE s, int check)
2680
* rxp.match(str) => matchdata or nil
2681
* rxp.match(str,pos) => matchdata or nil
2814
* rxp.match(str) -> matchdata or nil
2815
* rxp.match(str,pos) -> matchdata or nil
2683
2817
* Returns a <code>MatchData</code> object describing the match, or
2684
2818
* <code>nil</code> if there was no match. This is equivalent to retrieving the
2689
2823
* /(.)(.)(.)/.match("abc")[2] #=> "b"
2690
2824
* /(.)(.)/.match("abc", 1)[2] #=> "c"
2692
2826
* If a block is given, invoke the block with MatchData if match succeed, so
2693
2827
* that you can write
2695
2829
* pat.match(str) {|m| ...}
2699
2833
* if m = pat.match(str)
2703
2837
* The return value is a value from block execution in this case.
2741
* Regexp.new(string [, options]) => regexp
2742
* Regexp.new(regexp) => regexp
2743
* Regexp.compile(string [, options]) => regexp
2744
* Regexp.compile(regexp) => regexp
2875
* Regexp.new(string, [options [, lang]]) -> regexp
2876
* Regexp.new(regexp) -> regexp
2877
* Regexp.compile(string, [options [, lang]]) -> regexp
2878
* Regexp.compile(regexp) -> regexp
2746
2880
* Constructs a new regular expression from <i>pattern</i>, which can be either
2747
2881
* a <code>String</code> or a <code>Regexp</code> (in which case that regexp's
2751
2885
* <code>Regexp::IGNORECASE</code>, and <code>Regexp::MULTILINE</code>,
2752
2886
* <em>or</em>-ed together. Otherwise, if <i>options</i> is not
2753
2887
* <code>nil</code>, the regexp will be case insensitive.
2888
* When the <i>lang</i> parameter is `n' or `N' sets the regexp no encoding.
2755
2890
* r1 = Regexp.new('^a-z+:\\s+\w+') #=> /^a-z+:\s+\w+/
2756
2891
* r2 = Regexp.new('cat', true) #=> /cat/i
2771
2906
if (argc == 0 || argc > 3) {
2772
rb_raise(rb_eArgError, "wrong number of arguments");
2907
rb_raise(rb_eArgError, "wrong number of arguments (%d for 1..3)", argc);
2774
2909
if (TYPE(argv[0]) == T_REGEXP) {
2775
2910
VALUE re = argv[0];
2782
2917
ptr = RREGEXP_SRC_PTR(re);
2783
2918
len = RREGEXP_SRC_LEN(re);
2784
2919
enc = rb_enc_get(re);
2785
if (rb_reg_initialize(self, ptr, len, enc, flags, err)) {
2920
if (rb_reg_initialize(self, ptr, len, enc, flags, err, NULL, 0)) {
2786
2921
str = rb_enc_str_new(ptr, len, enc);
2787
2922
rb_reg_raise_str(str, flags, err);
2796
2931
if (argc == 3 && !NIL_P(argv[2])) {
2797
2932
char *kcode = StringValuePtr(argv[2]);
2798
if (kcode[0] == 'n' || kcode[1] == 'N') {
2933
if (kcode[0] == 'n' || kcode[0] == 'N') {
2799
2934
enc = rb_ascii8bit_encoding();
2800
2935
flags |= ARG_ENCODING_NONE;
2807
2942
ptr = StringValuePtr(str);
2809
? rb_reg_initialize(self, ptr, RSTRING_LEN(str), enc, flags, err)
2810
: rb_reg_initialize_str(self, str, flags, err)) {
2944
? rb_reg_initialize(self, ptr, RSTRING_LEN(str), enc, flags, err, NULL, 0)
2945
: rb_reg_initialize_str(self, str, flags, err, NULL, 0)) {
2811
2946
rb_reg_raise_str(str, flags, err);
2917
* Regexp.escape(str) => string
2918
* Regexp.quote(str) => string
3052
* Regexp.escape(str) -> string
3053
* Regexp.quote(str) -> string
2920
3055
* Escapes any characters that would have special meaning in a regular
2921
3056
* expression. Returns a new escaped string, or self if no characters are
2930
3065
rb_reg_s_quote(VALUE c, VALUE str)
2932
return rb_reg_quote(reg_operand(str, Qtrue));
3067
return rb_reg_quote(reg_operand(str, TRUE));
3092
* Regexp.union(pat1, pat2, ...) => new_regexp
3093
* Regexp.union(pats_ary) => new_regexp
3227
* Regexp.union(pat1, pat2, ...) -> new_regexp
3228
* Regexp.union(pats_ary) -> new_regexp
3095
3230
* Return a <code>Regexp</code> object that is the union of the given
3096
3231
* <em>pattern</em>s, i.e., will match any of its parts. The <em>pattern</em>s
3097
3232
* can be Regexp objects, in which case their options will be preserved, or
3098
3233
* Strings. If no patterns are given, returns <code>/(?!)/</code>.
3234
* The behavior is unspecified if any given <em>pattern</em> contains capture.
3100
3236
* Regexp.union #=> /(?!)/
3101
3237
* Regexp.union("penzance") #=> /penzance/
3132
3268
rb_reg_check(re);
3133
3269
s = RREGEXP_SRC_PTR(re);
3134
3270
len = RREGEXP_SRC_LEN(re);
3135
if (rb_reg_initialize(copy, s, len, rb_enc_get(re), rb_reg_options(re), err) != 0) {
3271
if (rb_reg_initialize(copy, s, len, rb_enc_get(re), rb_reg_options(re),
3272
err, NULL, 0) != 0) {
3136
3273
rb_reg_raise(s, len, err, re);
3195
3332
if (s < e && ASCGET(s, e, &clen) == '<') {
3196
3333
char *name, *name_end;
3198
3335
name_end = name = s + clen;
3199
3336
while (name_end < e) {
3200
3337
c = ASCGET(name_end, e, &clen);
3307
* Regexp.last_match => matchdata
3308
* Regexp.last_match(n) => str
3444
* Regexp.last_match -> matchdata
3445
* Regexp.last_match(n) -> str
3310
3447
* The first form returns the <code>MatchData</code> object generated by the
3311
3448
* last successful pattern match. Equivalent to reading the global variable
3313
3450
* <code>MatchData</code> object.
3314
3451
* <em>n</em> can be a string or symbol to reference a named capture.
3453
* Note that the <code>last_match</code> is local to the thread and method scope
3454
* of the method that did the pattern match.
3316
3456
* /c(.)t/ =~ 'cat' #=> 0
3317
3457
* Regexp.last_match #=> #<MatchData "cat" 1:"a">
3318
3458
* Regexp.last_match(0) #=> "cat"
3490
* Document-class: RegexpError
3492
* Raised when given an invalid regexp expression.
3496
* <em>raises the exception:</em>
3498
* RegexpError: target of repeat operator is not specified: /?/
3350
3502
* Document-class: Regexp
3352
3504
* A <code>Regexp</code> holds a regular expression, used to match a pattern
3407
3560
rb_define_const(rb_cRegexp, "IGNORECASE", INT2FIX(ONIG_OPTION_IGNORECASE));
3408
3561
rb_define_const(rb_cRegexp, "EXTENDED", INT2FIX(ONIG_OPTION_EXTEND));
3409
3562
rb_define_const(rb_cRegexp, "MULTILINE", INT2FIX(ONIG_OPTION_MULTILINE));
3563
rb_define_const(rb_cRegexp, "FIXEDENCODING", INT2FIX(ARG_ENCODING_FIXED));
3411
3565
rb_global_variable(®_cache);
3431
3585
rb_define_method(rb_cMatch, "to_s", match_to_s, 0);
3432
3586
rb_define_method(rb_cMatch, "inspect", match_inspect, 0);
3433
3587
rb_define_method(rb_cMatch, "string", match_string, 0);
3588
rb_define_method(rb_cMatch, "hash", match_hash, 0);
3589
rb_define_method(rb_cMatch, "eql?", match_equal, 1);
3590
rb_define_method(rb_cMatch, "==", match_equal, 1);