~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to re.c

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2010-07-31 17:08:39 UTC
  • mfrom: (1.1.4 upstream) (8.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20100731170839-j034dmpdqt1cc4p6
Tags: 1.9.2~svn28788-1
* New release based on upstream snapshot from the 1.9.2 branch,
  after 1.9.2 RC2. That branch is (supposed to be) binary-compatible
  with the 1.9.1 branch.
  + Builds fine on i386. Closes: #580852.
* Upgrade to Standards-Version: 3.9.1. No changes needed.
* Updated generated incs.
* Patches that still need work:
  + Unclear status, need more investigation:
   090729_fix_Makefile_deps.dpatch
   090803_exclude_rdoc.dpatch
   203_adjust_base_of_search_path.dpatch
   902_define_YAML_in_yaml_stringio.rb.dpatch
   919_common.mk_tweaks.dpatch
   931_libruby_suffix.dpatch
   940_test_thread_mutex_sync_shorter.dpatch
  + Maybe not needed anymore, keeping but not applying.
   102_skip_test_copy_stream.dpatch (test doesn't block anymore?)
   104_skip_btest_io.dpatch (test doesn't block anymore?)
   201_gem_prelude.dpatch (we don't use that rubygems anyway?)
   202_gem_default_dir.dpatch (we don't use that rubygems anyway?)
   940_test_file_exhaustive_fails_as_root.dpatch
   940_test_priority_fails.dpatch
   100518_load_libc_libm.dpatch
* Add disable-tests.diff: disable some tests that cause failures on FreeBSD.
  Closes: #590002, #543805, #542927.
* However, many new failures on FreeBSD. Since that version is still an
  improvement, add the check that makes test suite failures non-fatal on
  FreeBSD again. That still needs to be investigated.
* Re-add 903_skip_base_ruby_check.dpatch
* Add build-dependency on ruby1.8 and drop all pre-generated files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
113
113
    if (m > SIZEOF_VALUE)
114
114
        rb_bug("!!too long pattern string!!");
115
115
 
116
 
        /* Prepare hash value */
 
116
    /* Prepare hash value */
117
117
    for (hx = *x++, hy = *y++; x < xe; ++x, ++y) {
118
118
        hx <<= CHAR_BIT;
119
119
        hy <<= CHAR_BIT;
287
287
 
288
288
    switch (c) {
289
289
      case 'n':
290
 
        *kcode = -1;
 
290
        *kcode = rb_ascii8bit_encindex();
291
291
        return (*option = ARG_ENCODING_NONE);
292
292
      case 'e':
293
293
        *kcode = rb_enc_find_index("EUC-JP");
296
296
        *kcode = rb_enc_find_index("Windows-31J");
297
297
        break;
298
298
      case 'u':
299
 
        *kcode = rb_enc_find_index("UTF-8");
 
299
        *kcode = rb_utf8_encindex();
300
300
        break;
301
301
      default:
302
302
        *kcode = -1;
314
314
    }
315
315
}
316
316
 
 
317
int rb_str_buf_cat_escaped_char(VALUE result, unsigned int c, int unicode_p);
 
318
 
317
319
static void
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)
319
322
{
320
 
    rb_encoding *enc = rb_enc_get(str);
321
323
    const char *p, *pend;
322
324
    int need_escape = 0;
323
325
    int c, clen;
324
326
 
325
327
    p = s; pend = p + len;
326
 
    while (p<pend) {
327
 
        c = rb_enc_ascget(p, pend, &clen, enc);
328
 
        if (c == -1) {
329
 
            p += mbclen(p, pend, enc);
330
 
        }
331
 
        else if (c != '/' && rb_enc_isprint(c, enc)) {
332
 
            p += clen;
333
 
        }
334
 
        else {
335
 
            need_escape = 1;
336
 
            break;
337
 
        }
338
 
    }
 
328
    if (rb_enc_asciicompat(enc)) {
 
329
        while (p < pend) {
 
330
            c = rb_enc_ascget(p, pend, &clen, enc);
 
331
            if (c == -1) {
 
332
                if (enc == resenc) {
 
333
                    p += mbclen(p, pend, enc);
 
334
                }
 
335
                else {
 
336
                    need_escape = 1;
 
337
                    break;
 
338
                }
 
339
            }
 
340
            else if (c != '/' && rb_enc_isprint(c, enc)) {
 
341
                p += clen;
 
342
            }
 
343
            else {
 
344
                need_escape = 1;
 
345
                break;
 
346
            }
 
347
        }
 
348
    }
 
349
    else {
 
350
        need_escape = 1;
 
351
    }
 
352
 
339
353
    if (!need_escape) {
340
354
        rb_str_buf_cat(str, s, len);
341
355
    }
342
356
    else {
 
357
        int unicode_p = rb_enc_unicode_p(enc);
343
358
        p = s;
344
359
        while (p<pend) {
345
360
            c = rb_enc_ascget(p, pend, &clen, enc);
355
370
                rb_str_buf_cat(str, p, clen);
356
371
            }
357
372
            else if (c == -1) {
358
 
                int l = mbclen(p, pend, enc);
359
 
                rb_str_buf_cat(str, p, l);
360
 
                p += l;
361
 
                continue;
 
373
                clen = rb_enc_precise_mbclen(p, pend, enc);
 
374
                if (!MBCLEN_CHARFOUND_P(clen)) {
 
375
                    c = (unsigned char)*p;
 
376
                    clen = 1;
 
377
                    goto hex;
 
378
                }
 
379
                if (resenc) {
 
380
                    unsigned int c = rb_enc_mbc_to_codepoint(p, pend, enc);
 
381
                    rb_str_buf_cat_escaped_char(str, c, unicode_p);
 
382
                }
 
383
                else {
 
384
                    clen = MBCLEN_CHARFOUND_LEN(clen);
 
385
                    rb_str_buf_cat(str, p, clen);
 
386
                }
362
387
            }
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)) {
367
392
                char b[8];
368
393
 
369
 
                sprintf(b, "\\x%02X", c);
 
394
              hex:
 
395
                snprintf(b, sizeof(b), "\\x%02X", c);
370
396
                rb_str_buf_cat(str, b, 4);
371
397
            }
372
398
            else {
380
406
static VALUE
381
407
rb_reg_desc(const char *s, long len, VALUE re)
382
408
{
 
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();
 
413
 
 
414
    if (re && rb_enc_asciicompat(enc)) {
385
415
        rb_enc_copy(str, re);
386
416
    }
387
417
    else {
388
418
        rb_enc_associate(str, rb_usascii_encoding());
389
419
    }
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, "/");
392
422
    if (re) {
393
423
        char opts[4];
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");
397
429
    }
398
430
    OBJ_INFECT(str, re);
399
431
    return str;
402
434
 
403
435
/*
404
436
 *  call-seq:
405
 
 *      rxp.source   => str
 
437
 *      rxp.source   -> str
406
438
 *
407
439
 *  Returns the original string of the pattern.
408
440
 *
427
459
 
428
460
/*
429
461
 * call-seq:
430
 
 *    rxp.inspect   => string
 
462
 *    rxp.inspect   -> string
431
463
 *
432
464
 * Produce a nicely formatted string-version of _rxp_. Perhaps surprisingly,
433
465
 * <code>#inspect</code> actually produces the more natural version of
449
481
 
450
482
/*
451
483
 *  call-seq:
452
 
 *     rxp.to_s   => str
 
484
 *     rxp.to_s   -> str
453
485
 *
454
486
 *  Returns a string containing the regular expression and its options (using the
455
487
 *  <code>(?opts:source)</code> notation. This string can be fed back in to
476
508
    const UChar* ptr;
477
509
    VALUE str = rb_str_buf_new2("(?");
478
510
    char optbuf[5];
 
511
    rb_encoding *enc = rb_enc_get(re);
479
512
 
480
513
    rb_reg_check(re);
481
514
 
519
552
            goto again;
520
553
        }
521
554
        if (*ptr == ':' && ptr[len-1] == ')') {
522
 
            int r;
523
555
            Regexp *rp;
524
 
            r = onig_alloc_init(&rp, ONIG_OPTION_DEFAULT,
525
 
                                ONIGENC_CASE_FOLD_DEFAULT,
526
 
                                rb_enc_get(re),
527
 
                                OnigDefaultSyntax);
528
 
            if (r == 0) {
529
 
                 ++ptr;
530
 
                 len -= 2;
531
 
                 err = (onig_compile(rp, ptr, ptr + len, NULL) != 0);
532
 
            }
 
556
 
 
557
            ++ptr;
 
558
            len -= 2;
 
559
            err = onig_new(&rp, ptr, ptr + len, ONIG_OPTION_DEFAULT,
 
560
                           enc, OnigDefaultSyntax, NULL);
533
561
            onig_free(rp);
534
562
        }
535
563
        if (err) {
548
576
    }
549
577
 
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);
554
582
 
569
597
{
570
598
    char opts[6];
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();
572
602
 
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);
576
606
    opts[0] = '/';
577
607
    option_to_str(opts + 1, options);
578
608
    rb_str_buf_cat2(desc, opts);
601
631
 
602
632
/*
603
633
 *  call-seq:
604
 
 *     rxp.casefold?   => true or false
 
634
 *     rxp.casefold?   -> true or false
605
635
 *
606
636
 *  Returns the value of the case-insensitive flag.
607
637
 *
621
651
 
622
652
/*
623
653
 *  call-seq:
624
 
 *     rxp.options   => fixnum
 
654
 *     rxp.options   -> fixnum
625
655
 *
626
656
 *  Returns the set of bits corresponding to the options used when creating this
627
657
 *  Regexp (see <code>Regexp::new</code> for details. Note that additional bits
660
690
 
661
691
/*
662
692
 * call-seq:
663
 
 *    rxp.names   => [name1, name2, ...]
 
693
 *    rxp.names   -> [name1, name2, ...]
664
694
 *
665
695
 * Returns a list of names of captures as an array of strings.
666
696
 *
701
731
 
702
732
/*
703
733
 * call-seq:
704
 
 *    rxp.named_captures  => hash
 
734
 *    rxp.named_captures  -> hash
705
735
 *
706
736
 * Returns a hash representing information about named captures of <i>rxp</i>.
707
737
 *
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.
711
 
 * 
 
741
 *
712
742
 *    /(?<foo>.)(?<bar>.)/.named_captures
713
743
 *    #=> {"foo"=>[1], "bar"=>[2]}
714
744
 *
730
760
    return hash;
731
761
}
732
762
 
 
763
static int
 
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)
 
767
{
 
768
  int r;
 
769
 
 
770
  *reg = (regex_t* )xmalloc(sizeof(regex_t));
 
771
  if (IS_NULL(*reg)) return ONIGERR_MEMORY;
 
772
 
 
773
  r = onig_reg_init(*reg, option, ONIGENC_CASE_FOLD_DEFAULT, enc, syntax);
 
774
  if (r) goto err;
 
775
 
 
776
  r = onig_compile(*reg, pattern, pattern_end, einfo, sourcefile, sourceline);
 
777
  if (r) {
 
778
  err:
 
779
    onig_free(*reg);
 
780
    *reg = NULL;
 
781
  }
 
782
  return r;
 
783
}
 
784
 
733
785
static Regexp*
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)
735
788
{
736
789
    Regexp *rp;
737
790
    int r;
744
797
       from that.
745
798
    */
746
799
 
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);
749
802
    if (r) {
750
 
        onig_error_code_to_str((UChar*)err, r);
751
 
        return 0;
752
 
    }
753
 
 
754
 
    r = onig_compile(rp, (UChar*)s, (UChar*)(s + len), &einfo);
755
 
 
756
 
    if (r != 0) {
757
 
        onig_free(rp);
758
 
        (void )onig_error_code_to_str((UChar*)err, r, &einfo);
 
803
        onig_error_code_to_str((UChar*)err, r, &einfo);
759
804
        return 0;
760
805
    }
761
806
    return rp;
792
837
}
793
838
 
794
839
typedef struct {
795
 
    int byte_pos;
796
 
    int char_pos;
 
840
    long byte_pos;
 
841
    long char_pos;
797
842
} pair_t;
798
843
 
799
844
static int
800
845
pair_byte_cmp(const void *pair1, const void *pair2)
801
846
{
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;
 
850
#else
 
851
    return (int)diff;
 
852
#endif
803
853
}
804
854
 
805
855
static void
807
857
{
808
858
    struct rmatch *rm = RMATCH(match)->rmatch;
809
859
    struct re_registers *regs;
810
 
    int num_regs;
811
 
    int i, num_pos, c;
 
860
    int i, num_regs, num_pos;
 
861
    long c;
812
862
    char *s, *p, *q, *e;
813
863
    rb_encoding *enc;
814
864
    pair_t *pairs;
918
968
 
919
969
/*
920
970
 * call-seq:
921
 
 *    mtch.regexp   => regexp
 
971
 *    mtch.regexp   -> regexp
922
972
 *
923
973
 * Returns the regexp.
924
974
 *
935
985
 
936
986
/*
937
987
 * call-seq:
938
 
 *    mtch.names   => [name1, name2, ...]
 
988
 *    mtch.names   -> [name1, name2, ...]
939
989
 *
940
990
 * Returns a list of names of captures as an array of strings.
941
991
 * It is same as mtch.regexp.names.
956
1006
 
957
1007
/*
958
1008
 *  call-seq:
959
 
 *     mtch.length   => integer
960
 
 *     mtch.size     => integer
 
1009
 *     mtch.length   -> integer
 
1010
 *     mtch.size     -> integer
961
1011
 *
962
1012
 *  Returns the number of elements in the match array.
963
1013
 *
1008
1058
    return num;
1009
1059
}
1010
1060
 
 
1061
int
 
1062
rb_reg_backref_number(VALUE match, VALUE backref)
 
1063
{
 
1064
    return match_backref_number(match, backref);
 
1065
}
1011
1066
 
1012
1067
/*
1013
1068
 *  call-seq:
1014
 
 *     mtch.offset(n)   => array
 
1069
 *     mtch.offset(n)   -> array
1015
1070
 *
1016
1071
 *  Returns a two-element array containing the beginning and ending offsets of
1017
1072
 *  the <em>n</em>th match.
1048
1103
 
1049
1104
/*
1050
1105
 *  call-seq:
1051
 
 *     mtch.begin(n)   => integer
 
1106
 *     mtch.begin(n)   -> integer
1052
1107
 *
1053
1108
 *  Returns the offset of the start of the <em>n</em>th element of the match
1054
1109
 *  array in the string.
1083
1138
 
1084
1139
/*
1085
1140
 *  call-seq:
1086
 
 *     mtch.end(n)   => integer
 
1141
 *     mtch.end(n)   -> integer
1087
1142
 *
1088
1143
 *  Returns the offset of the character immediately following the end of the
1089
1144
 *  <em>n</em>th element of the match array in the string.
1125
1180
 
1126
1181
/*
1127
1182
 *  call-seq:
1128
 
 *     rxp.fixed_encoding?   => true or false
 
1183
 *     rxp.fixed_encoding?   -> true or false
1129
1184
 *
1130
1185
 *  Returns false if rxp is applicable to
1131
1186
 *  a string with any ASCII compatible encoding.
1249
1304
    return reg;
1250
1305
}
1251
1306
 
1252
 
int
1253
 
rb_reg_adjust_startpos(VALUE re, VALUE str, int pos, int reverse)
 
1307
long
 
1308
rb_reg_adjust_startpos(VALUE re, VALUE str, long pos, int reverse)
1254
1309
{
1255
 
    int range;
 
1310
    long range;
1256
1311
    rb_encoding *enc;
1257
1312
    UChar *p, *string;
1258
1313
 
1280
1335
    return pos;
1281
1336
}
1282
1337
 
1283
 
int
1284
 
rb_reg_search(VALUE re, VALUE str, int pos, int reverse)
 
1338
long
 
1339
rb_reg_search(VALUE re, VALUE str, long pos, int reverse)
1285
1340
{
1286
 
    int result;
 
1341
    long result;
1287
1342
    VALUE match;
1288
1343
    struct re_registers regi, *regs = &regi;
1289
1344
    char *range = RSTRING_PTR(str);
1339
1394
        }
1340
1395
        else {
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);
1344
1399
        }
1345
1400
    }
1420
1475
 
1421
1476
/*
1422
1477
 *  call-seq:
1423
 
 *     mtch.pre_match   => str
 
1478
 *     mtch.pre_match   -> str
1424
1479
 *
1425
1480
 *  Returns the portion of the original string before the current match.
1426
1481
 *  Equivalent to the special variable <code>$`</code>.
1447
1502
 
1448
1503
/*
1449
1504
 *  call-seq:
1450
 
 *     mtch.post_match   => str
 
1505
 *     mtch.post_match   -> str
1451
1506
 *
1452
1507
 *  Returns the portion of the original string after the current match.
1453
1508
 *  Equivalent to the special variable <code>$'</code>.
1518
1573
static VALUE
1519
1574
match_array(VALUE match, int start)
1520
1575
{
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;
 
1577
    VALUE ary;
 
1578
    VALUE target;
1524
1579
    int i;
1525
1580
    int taint = OBJ_TAINTED(match);
1526
1581
 
1527
1582
    match_check(match);
 
1583
    regs = RMATCH_REGS(match);
 
1584
    ary = rb_ary_new2(regs->num_regs);
 
1585
    target = RMATCH(match)->str;
 
1586
 
1528
1587
    for (i=start; i<regs->num_regs; i++) {
1529
1588
        if (regs->beg[i] == -1) {
1530
1589
            rb_ary_push(ary, Qnil);
1545
1604
 
1546
1605
/*
1547
1606
 *  call-seq:
1548
 
 *     mtch.to_a   => anArray
 
1607
 *     mtch.to_a   -> anArray
1549
1608
 *
1550
1609
 *  Returns the array of matches.
1551
1610
 *
1574
1633
 
1575
1634
/*
1576
1635
 *  call-seq:
1577
 
 *     mtch.captures   => array
 
1636
 *     mtch.captures   -> array
1578
1637
 *
1579
1638
 *  Returns the array of captures; equivalent to <code>mtch.to_a[1..-1]</code>.
1580
1639
 *
1609
1668
 
1610
1669
/*
1611
1670
 *  call-seq:
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
1616
1675
 *
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);
1643
1702
 
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);
1648
 
        }
1649
 
      }
1650
 
      else {
1651
 
        const char *p;
1652
 
        int num;
1653
 
 
1654
 
        switch (TYPE(idx)) {
1655
 
          case T_SYMBOL:
1656
 
            p = rb_id2name(SYM2ID(idx));
1657
 
            goto name_to_backref;
1658
 
            break;
1659
 
          case T_STRING:
1660
 
            p = StringValuePtr(idx);
1661
 
 
1662
 
          name_to_backref:
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);
1666
 
            break;
1667
 
 
1668
 
          default:
1669
 
            break;
1670
 
        }
1671
 
      }
 
1704
        if (FIXNUM_P(idx)) {
 
1705
            if (FIX2INT(idx) >= 0) {
 
1706
                return rb_reg_nth_match(FIX2INT(idx), match);
 
1707
            }
 
1708
        }
 
1709
        else {
 
1710
            const char *p;
 
1711
            int num;
 
1712
 
 
1713
            switch (TYPE(idx)) {
 
1714
              case T_SYMBOL:
 
1715
                p = rb_id2name(SYM2ID(idx));
 
1716
                goto name_to_backref;
 
1717
                break;
 
1718
              case T_STRING:
 
1719
                p = StringValuePtr(idx);
 
1720
 
 
1721
              name_to_backref:
 
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);
 
1725
                break;
 
1726
 
 
1727
              default:
 
1728
                break;
 
1729
            }
 
1730
        }
1672
1731
    }
1673
1732
 
1674
1733
    return rb_ary_aref(argc, argv, match_to_a(match));
1677
1736
static VALUE
1678
1737
match_entry(VALUE match, long n)
1679
1738
{
1680
 
    return rb_reg_nth_match(n, match);
 
1739
    /* n should not exceed num_regs */
 
1740
    return rb_reg_nth_match((int)n, match);
1681
1741
}
1682
1742
 
1683
1743
 
1684
1744
/*
1685
1745
 *  call-seq:
1686
1746
 *
1687
 
 *     mtch.values_at([index]*)   => array
 
1747
 *     mtch.values_at([index]*)   -> array
1688
1748
 *
1689
1749
 *  Uses each <i>index</i> to access the matching values, returning an array of
1690
1750
 *  the corresponding matches.
1697
1757
static VALUE
1698
1758
match_values_at(int argc, VALUE *argv, VALUE match)
1699
1759
{
1700
 
    struct re_registers *regs = RMATCH_REGS(match);
 
1760
    struct re_registers *regs;
 
1761
 
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);
1703
1765
}
1704
1766
 
1705
1767
 
1706
1768
/*
1707
1769
 *  call-seq:
1708
 
 *     mtch.to_s   => str
 
1770
 *     mtch.to_s   -> str
1709
1771
 *
1710
1772
 *  Returns the entire matched string.
1711
1773
 *
1728
1790
 
1729
1791
/*
1730
1792
 *  call-seq:
1731
 
 *     mtch.string   => str
 
1793
 *     mtch.string   -> str
1732
1794
 *
1733
1795
 *  Returns a frozen copy of the string passed in to <code>match</code>.
1734
1796
 *
1764
1826
 
1765
1827
/*
1766
1828
 * call-seq:
1767
 
 *    mtch.inspect   => str
 
1829
 *    mtch.inspect   -> str
1768
1830
 *
1769
1831
 * Returns a printable version of <i>mtch</i>.
1770
1832
 *
1836
1898
    const char *p = *pp;
1837
1899
    int code;
1838
1900
    int meta_prefix = 0, ctrl_prefix = 0;
1839
 
    int len;
 
1901
    size_t len;
1840
1902
    int retbyte;
1841
1903
 
1842
1904
    retbyte = -1;
1864
1926
      case '0': case '1': case '2': case '3':
1865
1927
      case '4': case '5': case '6': case '7':
1866
1928
        p--;
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);
1868
1930
        p += len;
1869
1931
        break;
1870
1932
 
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);
1873
1935
        if (len < 1) {
1874
1936
            errcpy(err, "invalid hex escape");
1875
1937
            return -1;
2035
2097
    const char *p = *pp;
2036
2098
    int has_unicode = 0;
2037
2099
    unsigned long code;
2038
 
    int len;
 
2100
    size_t len;
2039
2101
 
2040
2102
    while (p < end && ISSPACE(*p)) p++;
2041
2103
 
2070
2132
        VALUE buf, rb_encoding **encp, onig_errmsg_buffer err)
2071
2133
{
2072
2134
    const char *p = *pp;
2073
 
    int len;
 
2135
    size_t len;
2074
2136
    unsigned long code;
2075
2137
 
2076
2138
    if (end < p+4) {
2125
2187
              case '1': case '2': case '3':
2126
2188
              case '4': case '5': case '6': case '7': /* \O, \OO, \OOO or backref */
2127
2189
                {
2128
 
                    int octlen;
 
2190
                    size_t octlen;
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.
2133
2195
                        goto escape_asis;
2134
2196
                    }
2135
2197
                }
2136
 
                /* xxx: How about more than 199 subexpressions? */ 
 
2198
                /* xxx: How about more than 199 subexpressions? */
2137
2199
 
2138
2200
              case '0': /* \0, \0O, \0OO */
2139
2201
 
2170
2232
                }
2171
2233
 
2172
2234
              case 'p': /* \p{Hiragana} */
 
2235
              case 'P':
2173
2236
                if (!*encp) {
2174
2237
                    *has_property = 1;
2175
2238
                }
2247
2310
}
2248
2311
 
2249
2312
static VALUE
2250
 
rb_reg_preprocess_dregexp(VALUE ary)
 
2313
rb_reg_preprocess_dregexp(VALUE ary, int options)
2251
2314
{
2252
2315
    rb_encoding *fixed_enc = 0;
2253
2316
    rb_encoding *regexp_enc = 0;
2254
2317
    onig_errmsg_buffer err = "";
2255
2318
    int i;
2256
2319
    VALUE result = 0;
 
2320
    rb_encoding *ascii8bit = rb_ascii8bit_encoding();
2257
2321
 
2258
2322
    if (RARRAY_LEN(ary) == 0) {
2259
2323
        rb_raise(rb_eArgError, "no arguments given");
2265
2329
        char *p, *end;
2266
2330
        rb_encoding *src_enc;
2267
2331
 
 
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");
 
2337
            else
 
2338
                src_enc = ascii8bit;
 
2339
        }
 
2340
 
2268
2341
        StringValue(str);
2269
2342
        p = RSTRING_PTR(str);
2270
2343
        end = p + RSTRING_LEN(str);
2271
 
        src_enc = rb_enc_get(str);
2272
2344
 
2273
2345
        buf = rb_reg_preprocess(p, end, src_enc, &fixed_enc, err);
2274
2346
 
2277
2349
 
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));
2282
2354
            }
2283
2355
            regexp_enc = fixed_enc;
2296
2368
}
2297
2369
 
2298
2370
static int
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)
2301
2374
{
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;
2346
2419
    }
2347
 
    
 
2420
 
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);
2355
2429
}
2356
2430
 
2357
2431
static int
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)
2359
2434
{
2360
2435
    int ret;
2361
2436
    rb_encoding *enc = rb_enc_get(str);
2370
2445
        }
2371
2446
    }
2372
2447
    ret = rb_reg_initialize(obj, RSTRING_PTR(str), RSTRING_LEN(str), enc,
2373
 
                            options, err);
 
2448
                            options, err, sourcefile, sourceline);
2374
2449
    RB_GC_GUARD(str);
2375
2450
    return ret;
2376
2451
}
2389
2464
}
2390
2465
 
2391
2466
VALUE
 
2467
rb_reg_alloc(void)
 
2468
{
 
2469
    return rb_reg_s_alloc(rb_cRegexp);
 
2470
}
 
2471
 
 
2472
VALUE
2392
2473
rb_reg_new_str(VALUE s, int options)
2393
2474
{
2394
 
    VALUE re = rb_reg_s_alloc(rb_cRegexp);
 
2475
    return rb_reg_init_str(rb_reg_alloc(), s, options);
 
2476
}
 
2477
 
 
2478
VALUE
 
2479
rb_reg_init_str(VALUE re, VALUE s, int options)
 
2480
{
2395
2481
    onig_errmsg_buffer err = "";
2396
2482
 
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);
2399
2485
    }
2400
2486
 
2404
2490
VALUE
2405
2491
rb_reg_new_ary(VALUE ary, int opt)
2406
2492
{
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);
2408
2494
}
2409
2495
 
2410
2496
VALUE
2411
2497
rb_enc_reg_new(const char *s, long len, rb_encoding *enc, int options)
2412
2498
{
2413
 
    VALUE re = rb_reg_s_alloc(rb_cRegexp);
 
2499
    VALUE re = rb_reg_alloc();
2414
2500
    onig_errmsg_buffer err = "";
2415
2501
 
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);
2418
2504
    }
2419
2505
 
2427
2513
}
2428
2514
 
2429
2515
VALUE
2430
 
rb_reg_compile(VALUE str, int options)
 
2516
rb_reg_compile(VALUE str, int options, const char *sourcefile, int sourceline)
2431
2517
{
2432
 
    VALUE re = rb_reg_s_alloc(rb_cRegexp);
 
2518
    VALUE re = rb_reg_alloc();
2433
2519
    onig_errmsg_buffer err = "";
2434
2520
 
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));
2438
2524
        return Qnil;
2439
2525
    }
2455
2541
    return reg_cache = rb_reg_new_str(save_str, 0);
2456
2542
}
2457
2543
 
 
2544
static st_index_t reg_hash(VALUE re);
2458
2545
/*
2459
2546
 * call-seq:
2460
 
 *   rxp.hash   => fixnum
 
2547
 *   rxp.hash   -> fixnum
2461
2548
 *
2462
2549
 * Produce a hash based on the text and options of this regular expression.
2463
2550
 */
2465
2552
static VALUE
2466
2553
rb_reg_hash(VALUE re)
2467
2554
{
2468
 
    int hashval, len;
2469
 
    char *p;
 
2555
    st_index_t hashval = reg_hash(re);
 
2556
    return LONG2FIX(hashval);
 
2557
}
 
2558
 
 
2559
static st_index_t
 
2560
reg_hash(VALUE re)
 
2561
{
 
2562
    st_index_t hashval;
2470
2563
 
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);
2475
 
    while (len--) {
2476
 
        hashval = hashval * 33 + *p++;
2477
 
    }
2478
 
    hashval = hashval + (hashval>>5);
2479
 
 
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);
2481
2568
}
2482
2569
 
2483
2570
 
2484
2571
/*
2485
2572
 *  call-seq:
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
2488
2575
 *
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
2512
2599
    return Qfalse;
2513
2600
}
2514
2601
 
 
2602
/*
 
2603
 * call-seq:
 
2604
 *    mtch.hash   -> integer
 
2605
 *
 
2606
 * Produce a hash based on the target string, regexp and matched
 
2607
 * positions of this matchdata.
 
2608
 */
 
2609
 
 
2610
static VALUE
 
2611
match_hash(VALUE match)
 
2612
{
 
2613
    const struct re_registers *regs;
 
2614
    st_index_t hashval = rb_hash_start(rb_str_hash(RMATCH(match)->str));
 
2615
 
 
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);
 
2623
}
 
2624
 
 
2625
/*
 
2626
 * call-seq:
 
2627
 *    mtch == mtch2   -> true or false
 
2628
 *
 
2629
 *  Equality---Two matchdata are equal if their target strings,
 
2630
 *  patterns, and matched positions are identical.
 
2631
 */
 
2632
 
 
2633
static VALUE
 
2634
match_equal(VALUE match1, VALUE match2)
 
2635
{
 
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;
 
2646
    return Qtrue;
 
2647
}
 
2648
 
2515
2649
static VALUE
2516
2650
reg_operand(VALUE s, int check)
2517
2651
{
2537
2671
        rb_backref_set(Qnil);
2538
2672
        return -1;
2539
2673
    }
2540
 
    *strp = str = reg_operand(str, Qtrue);
 
2674
    *strp = str = reg_operand(str, TRUE);
2541
2675
    if (pos != 0) {
2542
2676
        if (pos < 0) {
2543
2677
            VALUE l = rb_str_length(str);
2553
2687
 
2554
2688
/*
2555
2689
 *  call-seq:
2556
 
 *     rxp =~ str    => integer or nil
 
2690
 *     rxp =~ str    -> integer or nil
2557
2691
 *
2558
2692
 *  Match---Matches <i>rxp</i> against <i>str</i>.
2559
2693
 *
2570
2704
 *
2571
2705
 *  If it is not matched, nil is assigned for the variables.
2572
2706
 *
2573
 
 *     /(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/ =~ "  x = "   
 
2707
 *     /(?<lhs>\w+)\s*=\s*(?<rhs>\w+)/ =~ "  x = "
2574
2708
 *     p lhs    #=> nil
2575
2709
 *     p rhs    #=> nil
2576
2710
 *
2610
2744
 
2611
2745
/*
2612
2746
 *  call-seq:
2613
 
 *     rxp === str   => true or false
 
2747
 *     rxp === str   -> true or false
2614
2748
 *
2615
2749
 *  Case Equality---Synonym for <code>Regexp#=~</code> used in case statements.
2616
2750
 *
2631
2765
{
2632
2766
    long start;
2633
2767
 
2634
 
    str = reg_operand(str, Qfalse);
 
2768
    str = reg_operand(str, FALSE);
2635
2769
    if (NIL_P(str)) {
2636
2770
        rb_backref_set(Qnil);
2637
2771
        return Qfalse;
2646
2780
 
2647
2781
/*
2648
2782
 *  call-seq:
2649
 
 *     ~ rxp   => integer or nil
 
2783
 *     ~ rxp   -> integer or nil
2650
2784
 *
2651
2785
 *  Match---Matches <i>rxp</i> against the contents of <code>$_</code>.
2652
2786
 *  Equivalent to <code><i>rxp</i> =~ $_</code>.
2677
2811
 
2678
2812
/*
2679
2813
 *  call-seq:
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
2682
2816
 *
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
2688
2822
 *
2689
2823
 *     /(.)(.)(.)/.match("abc")[2]   #=> "b"
2690
2824
 *     /(.)(.)/.match("abc", 1)[2]   #=> "c"
2691
 
 *     
 
2825
 *
2692
2826
 *  If a block is given, invoke the block with MatchData if match succeed, so
2693
2827
 *  that you can write
2694
 
 *     
 
2828
 *
2695
2829
 *     pat.match(str) {|m| ...}
2696
 
 *     
 
2830
 *
2697
2831
 *  instead of
2698
 
 *      
 
2832
 *
2699
2833
 *     if m = pat.match(str)
2700
2834
 *       ...
2701
2835
 *     end
2702
 
 *      
 
2836
 *
2703
2837
 *  The return value is a value from block execution in this case.
2704
2838
 */
2705
2839
 
2738
2872
 
2739
2873
/*
2740
2874
 *  call-seq:
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
2745
2879
 *
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.
2754
2889
 *
2755
2890
 *     r1 = Regexp.new('^a-z+:\\s+\w+')           #=> /^a-z+:\s+\w+/
2756
2891
 *     r2 = Regexp.new('cat', true)               #=> /cat/i
2769
2904
    long len;
2770
2905
 
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);
2773
2908
    }
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);
2788
2923
        }
2795
2930
        enc = 0;
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;
2801
2936
            }
2806
2941
        str = argv[0];
2807
2942
        ptr = StringValuePtr(str);
2808
2943
        if (enc
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);
2812
2947
        }
2813
2948
    }
2914
3049
 
2915
3050
/*
2916
3051
 *  call-seq:
2917
 
 *     Regexp.escape(str)   => string
2918
 
 *     Regexp.quote(str)    => string
 
3052
 *     Regexp.escape(str)   -> string
 
3053
 *     Regexp.quote(str)    -> string
2919
3054
 *
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
2929
3064
static VALUE
2930
3065
rb_reg_s_quote(VALUE c, VALUE str)
2931
3066
{
2932
 
    return rb_reg_quote(reg_operand(str, Qtrue));
 
3067
    return rb_reg_quote(reg_operand(str, TRUE));
2933
3068
}
2934
3069
 
2935
3070
int
3089
3224
 
3090
3225
/*
3091
3226
 *  call-seq:
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
3094
3229
 *
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.
3099
3235
 *
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);
3137
3274
    }
3138
3275
    return copy;
3194
3331
          case 'k':
3195
3332
            if (s < e && ASCGET(s, e, &clen) == '<') {
3196
3333
                char *name, *name_end;
3197
 
               
 
3334
 
3198
3335
                name_end = name = s + clen;
3199
3336
                while (name_end < e) {
3200
3337
                    c = ASCGET(name_end, e, &clen);
3304
3441
 
3305
3442
/*
3306
3443
 *  call-seq:
3307
 
 *     Regexp.last_match           => matchdata
3308
 
 *     Regexp.last_match(n)        => str
 
3444
 *     Regexp.last_match           -> matchdata
 
3445
 *     Regexp.last_match(n)        -> str
3309
3446
 *
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.
3315
3452
 *
 
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.
 
3455
 *
3316
3456
 *     /c(.)t/ =~ 'cat'        #=> 0
3317
3457
 *     Regexp.last_match       #=> #<MatchData "cat" 1:"a">
3318
3458
 *     Regexp.last_match(0)    #=> "cat"
3347
3487
}
3348
3488
 
3349
3489
/*
 
3490
 *  Document-class: RegexpError
 
3491
 *
 
3492
 *  Raised when given an invalid regexp expression.
 
3493
 *
 
3494
 *     Regexp.new("?")
 
3495
 *
 
3496
 *  <em>raises the exception:</em>
 
3497
 *
 
3498
 *     RegexpError: target of repeat operator is not specified: /?/
 
3499
 */
 
3500
 
 
3501
/*
3350
3502
 *  Document-class: Regexp
3351
3503
 *
3352
3504
 *  A <code>Regexp</code> holds a regular expression, used to match a pattern
3354
3506
 *  <code>%r{...}</code> literals, and by the <code>Regexp::new</code>
3355
3507
 *  constructor.
3356
3508
 *
 
3509
 *  :include: doc/re.rdoc
3357
3510
 */
3358
3511
 
3359
3512
void
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));
3410
3564
 
3411
3565
    rb_global_variable(&reg_cache);
3412
3566
 
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);
3434
3591
}