~ubuntu-branches/ubuntu/trusty/ruby1.9/trusty

« back to all changes in this revision

Viewing changes to re.c

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-06-06 11:58:24 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20070606115824-qzldkdwq3dvfpf84
Tags: 1.9.0+20070606-1
* new upstream snapshot. (2006-06-06)
* updated debian/generated-incs/* files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
  re.c -
4
4
 
5
 
  $Author: matz $
 
5
  $Author: nobu $
6
6
  created at: Mon Aug  9 18:24:49 JST 1993
7
7
 
8
8
  Copyright (C) 1993-2006 Yukihiro Matsumoto
172
172
 
173
173
static int reg_kcode = DEFAULT_KCODE;
174
174
 
175
 
static int char_to_option(int c)
176
 
{
177
 
  int val;
178
 
 
179
 
  switch (c) {
180
 
    case 'i':
181
 
      val = ONIG_OPTION_IGNORECASE;
182
 
      break;
183
 
    case 'x':
184
 
      val = ONIG_OPTION_EXTEND;
185
 
      break;
186
 
    case 'm':
187
 
      val = ONIG_OPTION_MULTILINE;
188
 
      break;
189
 
    default:
190
 
      val = 0;
191
 
      break;
192
 
  }
193
 
  return val;
194
 
}
195
 
 
196
 
extern int rb_char_to_option_kcode(int c, int *option, int *kcode)
197
 
{
198
 
  *option = 0;
199
 
 
200
 
  switch (c) {
201
 
    case 'n':
202
 
      *kcode = ARG_KCODE_NONE;
203
 
      break;
204
 
    case 'e':
205
 
      *kcode = ARG_KCODE_EUC;
206
 
      break;
207
 
    case 's':
208
 
      *kcode = ARG_KCODE_SJIS;
209
 
      break;
210
 
    case 'u':
211
 
      *kcode = ARG_KCODE_UTF8;
212
 
      break;
213
 
    default:
214
 
      *kcode  = 0;
215
 
      *option = char_to_option(c);
216
 
      break;
217
 
  }
218
 
 
219
 
  return ((*kcode == 0 && *option == 0) ? 0 : 1);
220
 
}
221
 
 
222
 
static int char_to_arg_kcode(int c)
223
 
{
224
 
  int kcode, option;
225
 
 
226
 
  if (ISUPPER(c))  c = tolower(c);
227
 
 
228
 
  (void )rb_char_to_option_kcode(c, &option, &kcode);
229
 
  return kcode;
 
175
static int
 
176
char_to_option(int c)
 
177
{
 
178
    int val;
 
179
 
 
180
    switch (c) {
 
181
      case 'i':
 
182
        val = ONIG_OPTION_IGNORECASE;
 
183
        break;
 
184
      case 'x':
 
185
        val = ONIG_OPTION_EXTEND;
 
186
        break;
 
187
      case 'm':
 
188
        val = ONIG_OPTION_MULTILINE;
 
189
        break;
 
190
      default:
 
191
        val = 0;
 
192
        break;
 
193
    }
 
194
    return val;
 
195
}
 
196
 
 
197
extern int
 
198
rb_char_to_option_kcode(int c, int *option, int *kcode)
 
199
{
 
200
    *option = 0;
 
201
 
 
202
    switch (c) {
 
203
      case 'n':
 
204
        *kcode = ARG_KCODE_NONE;
 
205
        break;
 
206
      case 'e':
 
207
        *kcode = ARG_KCODE_EUC;
 
208
        break;
 
209
      case 's':
 
210
        *kcode = ARG_KCODE_SJIS;
 
211
        break;
 
212
      case 'u':
 
213
        *kcode = ARG_KCODE_UTF8;
 
214
        break;
 
215
      default:
 
216
        *kcode  = 0;
 
217
        *option = char_to_option(c);
 
218
        break;
 
219
    }
 
220
 
 
221
    return ((*kcode == 0 && *option == 0) ? 0 : 1);
 
222
}
 
223
 
 
224
static int
 
225
char_to_arg_kcode(int c)
 
226
{
 
227
    int kcode, option;
 
228
 
 
229
    if (ISUPPER(c))  c = tolower(c);
 
230
 
 
231
    (void )rb_char_to_option_kcode(c, &option, &kcode);
 
232
    return kcode;
230
233
}
231
234
 
232
235
static int
233
236
kcode_to_arg_value(unsigned int kcode)
234
237
{
235
 
  switch (kcode & KCODE_MASK) {
236
 
    case KCODE_NONE:
237
 
      return ARG_KCODE_NONE;
238
 
    case KCODE_EUC:
239
 
      return ARG_KCODE_EUC;
240
 
    case KCODE_SJIS:
241
 
      return ARG_KCODE_SJIS;
242
 
    case KCODE_UTF8:
243
 
      return ARG_KCODE_UTF8;
244
 
    default:
245
 
      return 0;
246
 
  }
 
238
    switch (kcode & KCODE_MASK) {
 
239
      case KCODE_NONE:
 
240
        return ARG_KCODE_NONE;
 
241
      case KCODE_EUC:
 
242
        return ARG_KCODE_EUC;
 
243
      case KCODE_SJIS:
 
244
        return ARG_KCODE_SJIS;
 
245
      case KCODE_UTF8:
 
246
        return ARG_KCODE_UTF8;
 
247
      default:
 
248
        return 0;
 
249
    }
247
250
}
248
251
 
249
252
static void
250
253
set_re_kcode_by_option(struct RRegexp *re, int options)
251
254
{
252
 
  switch (options & ARG_KCODE_MASK) {
253
 
    case ARG_KCODE_NONE:
254
 
      FL_UNSET(re, KCODE_MASK);
255
 
      FL_SET(re, KCODE_FIXED);
256
 
      break;
257
 
    case ARG_KCODE_EUC:
258
 
      FL_UNSET(re, KCODE_MASK);
259
 
      FL_SET(re, KCODE_EUC);
260
 
      FL_SET(re, KCODE_FIXED);
261
 
      break;
262
 
    case ARG_KCODE_SJIS:
263
 
      FL_UNSET(re, KCODE_MASK);
264
 
      FL_SET(re, KCODE_SJIS);
265
 
      FL_SET(re, KCODE_FIXED);
266
 
      break;
267
 
    case ARG_KCODE_UTF8:
268
 
      FL_UNSET(re, KCODE_MASK);
269
 
      FL_SET(re, KCODE_UTF8);
270
 
      FL_SET(re, KCODE_FIXED);
271
 
      break;
 
255
    switch (options & ARG_KCODE_MASK) {
 
256
      case ARG_KCODE_NONE:
 
257
        FL_UNSET(re, KCODE_MASK);
 
258
        FL_SET(re, KCODE_FIXED);
 
259
        break;
 
260
      case ARG_KCODE_EUC:
 
261
        FL_UNSET(re, KCODE_MASK);
 
262
        FL_SET(re, KCODE_EUC);
 
263
        FL_SET(re, KCODE_FIXED);
 
264
        break;
 
265
      case ARG_KCODE_SJIS:
 
266
        FL_UNSET(re, KCODE_MASK);
 
267
        FL_SET(re, KCODE_SJIS);
 
268
        FL_SET(re, KCODE_FIXED);
 
269
        break;
 
270
      case ARG_KCODE_UTF8:
 
271
        FL_UNSET(re, KCODE_MASK);
 
272
        FL_SET(re, KCODE_UTF8);
 
273
        FL_SET(re, KCODE_FIXED);
 
274
        break;
272
275
 
273
 
    case 0:
274
 
    default:
275
 
      FL_SET(re, reg_kcode);
276
 
      break;
 
276
      case 0:
 
277
      default:
 
278
        FL_SET(re, reg_kcode);
 
279
        break;
277
280
    }
278
281
}
279
282
 
280
283
static int
281
284
re_to_kcode_arg_value(VALUE re)
282
285
{
283
 
  return kcode_to_arg_value(RBASIC(re)->flags);
 
286
    return kcode_to_arg_value(RBASIC(re)->flags);
284
287
}
285
288
 
286
289
static int curr_kcode;
444
447
/*
445
448
 *  call-seq:
446
449
 *     rxp.source   => str
447
 
 *  
 
450
 *
448
451
 *  Returns the original string of the pattern.
449
 
 *     
 
452
 *
450
453
 *     /ab+c/ix.source   #=> "ab+c"
451
454
 */
452
455
 
483
486
/*
484
487
 *  call-seq:
485
488
 *     rxp.to_s   => str
486
 
 *  
 
489
 *
487
490
 *  Returns a string containing the regular expression and its options (using the
488
491
 *  <code>(?xxx:yyy)</code> notation. This string can be fed back in to
489
492
 *  <code>Regexp::new</code> to a regular expression with the same semantics as
491
494
 *  comparing the two, as the source of the regular expression itself may
492
495
 *  differ, as the example shows).  <code>Regexp#inspect</code> produces a
493
496
 *  generally more readable version of <i>rxp</i>.
494
 
 *     
 
497
 *
495
498
 *     r1 = /ab+c/ix         #=> /ab+c/ix
496
499
 *     s1 = r1.to_s          #=> "(?ix-m:ab+c)"
497
500
 *     r2 = Regexp.new(s1)   #=> /(?ix-m:ab+c)/
606
609
/*
607
610
 *  call-seq:
608
611
 *     rxp.casefold?   => true or false
609
 
 *  
 
612
 *
610
613
 *  Returns the value of the case-insensitive flag.
611
614
 */
612
615
 
622
625
/*
623
626
 *  call-seq:
624
627
 *     rxp.options   => fixnum
625
 
 *  
 
628
 *
626
629
 *  Returns the set of bits corresponding to the options used when creating this
627
630
 *  Regexp (see <code>Regexp::new</code> for details. Note that additional bits
628
631
 *  may be set in the returned options: these are used internally by the regular
629
632
 *  expression code. These extra bits are ignored if the options are passed to
630
633
 *  <code>Regexp::new</code>.
631
 
 *     
 
634
 *
632
635
 *     Regexp::IGNORECASE                  #=> 1
633
636
 *     Regexp::EXTENDED                    #=> 2
634
637
 *     Regexp::MULTILINE                   #=> 4
635
 
 *     
 
638
 *
636
639
 *     /cat/.options                       #=> 128
637
640
 *     /cat/ix.options                     #=> 131
638
641
 *     Regexp.new('cat', true).options     #=> 129
639
642
 *     Regexp.new('cat', 0, 's').options   #=> 384
640
 
 *     
 
643
 *
641
644
 *     r = /cat/ix
642
645
 *     Regexp.new(r.source, r.options)     #=> /cat/ix
643
646
 */
653
656
/*
654
657
 *  call-seq:
655
658
 *     rxp.kcode   => str
656
 
 *  
 
659
 *
657
660
 *  Returns the character set code for the regexp.
658
661
 */
659
662
 
769
772
 *  call-seq:
770
773
 *     mtch.length   => integer
771
774
 *     mtch.size     => integer
772
 
 *  
 
775
 *
773
776
 *  Returns the number of elements in the match array.
774
 
 *     
 
777
 *
775
778
 *     m = /(.)(.)(\d+)(\d)/.match("THX1138.")
776
779
 *     m.length   #=> 5
777
780
 *     m.size     #=> 5
787
790
/*
788
791
 *  call-seq:
789
792
 *     mtch.offset(n)   => array
790
 
 *  
 
793
 *
791
794
 *  Returns a two-element array containing the beginning and ending offsets of
792
795
 *  the <em>n</em>th match.
793
 
 *     
 
796
 *
794
797
 *     m = /(.)(.)(\d+)(\d)/.match("THX1138.")
795
798
 *     m.offset(0)   #=> [1, 7]
796
799
 *     m.offset(4)   #=> [6, 7]
815
818
/*
816
819
 *  call-seq:
817
820
 *     mtch.begin(n)   => integer
818
 
 *  
 
821
 *
819
822
 *  Returns the offset of the start of the <em>n</em>th element of the match
820
823
 *  array in the string.
821
 
 *     
 
824
 *
822
825
 *     m = /(.)(.)(\d+)(\d)/.match("THX1138.")
823
826
 *     m.begin(0)   #=> 1
824
827
 *     m.begin(2)   #=> 2
842
845
/*
843
846
 *  call-seq:
844
847
 *     mtch.end(n)   => integer
845
 
 *  
 
848
 *
846
849
 *  Returns the offset of the character immediately following the end of the
847
850
 *  <em>n</em>th element of the match array in the string.
848
 
 *     
 
851
 *
849
852
 *     m = /(.)(.)(\d+)(\d)/.match("THX1138.")
850
853
 *     m.end(0)   #=> 7
851
854
 *     m.end(2)   #=> 3
1088
1091
/*
1089
1092
 *  call-seq:
1090
1093
 *     mtch.pre_match   => str
1091
 
 *  
 
1094
 *
1092
1095
 *  Returns the portion of the original string before the current match.
1093
1096
 *  Equivalent to the special variable <code>$`</code>.
1094
 
 *     
 
1097
 *
1095
1098
 *     m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1096
1099
 *     m.pre_match   #=> "T"
1097
1100
 */
1112
1115
/*
1113
1116
 *  call-seq:
1114
1117
 *     mtch.post_match   => str
1115
 
 *  
 
1118
 *
1116
1119
 *  Returns the portion of the original string after the current match.
1117
1120
 *  Equivalent to the special variable <code>$'</code>.
1118
 
 *     
 
1121
 *
1119
1122
 *     m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie")
1120
1123
 *     m.post_match   #=> ": The Movie"
1121
1124
 */
1203
1206
/*
1204
1207
 *  call-seq:
1205
1208
 *     mtch.to_a   => anArray
1206
 
 *  
 
1209
 *
1207
1210
 *  Returns the array of matches.
1208
 
 *     
 
1211
 *
1209
1212
 *     m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1210
1213
 *     m.to_a   #=> ["HX1138", "H", "X", "113", "8"]
1211
 
 *     
 
1214
 *
1212
1215
 *  Because <code>to_a</code> is called when expanding
1213
1216
 *  <code>*</code><em>variable</em>, there's a useful assignment
1214
1217
 *  shortcut for extracting matched fields. This is slightly slower than
1215
1218
 *  accessing the fields directly (as an intermediate array is
1216
1219
 *  generated).
1217
 
 *     
 
1220
 *
1218
1221
 *     all,f1,f2,f3 = *(/(.)(.)(\d+)(\d)/.match("THX1138."))
1219
1222
 *     all   #=> "HX1138"
1220
1223
 *     f1    #=> "H"
1270
1273
 *     mtch[start, length]   => array
1271
1274
 *     mtch[range]           => array
1272
1275
 *     mtch[name]            => str or nil
1273
 
 *  
 
1276
 *
1274
1277
 *  Match Reference---<code>MatchData</code> acts as an array, and may be
1275
1278
 *  accessed using the normal array indexing techniques.  <i>mtch</i>[0] is
1276
1279
 *  equivalent to the special variable <code>$&</code>, and returns the entire
1277
1280
 *  matched string.  <i>mtch</i>[1], <i>mtch</i>[2], and so on return the values
1278
1281
 *  of the matched backreferences (portions of the pattern between parentheses).
1279
 
 *     
 
1282
 *
1280
1283
 *     m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1281
1284
 *     m[0]       #=> "HX1138"
1282
1285
 *     m[1, 2]    #=> ["H", "X"]
1340
1343
    if (!OBJ_TAINTED(obj) && rb_safe_level() >= 4)
1341
1344
        rb_raise(rb_eSecurityError, "Insecure: can't modify regexp");
1342
1345
 *     mtch.select([index]*)   => array
1343
 
 *  
 
1346
 *
1344
1347
 *  Uses each <i>index</i> to access the matching values, returning an array of
1345
1348
 *  the corresponding matches.
1346
 
 *     
 
1349
 *
1347
1350
 *     m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie")
1348
1351
 *     m.to_a               #=> ["HX1138", "H", "X", "113", "8"]
1349
1352
 *     m.select(0, 2, -2)   #=> ["HX1138", "X", "113"]
1359
1362
/*
1360
1363
 *  call-seq:
1361
1364
 *     mtch.select([index]*)   => array
1362
 
 *  
 
1365
 *
1363
1366
 *  Uses each <i>index</i> to access the matching values, returning an
1364
1367
 *  array of the corresponding matches.
1365
 
 *     
 
1368
 *
1366
1369
 *     m = /(.)(.)(\d+)(\d)/.match("THX1138: The Movie")
1367
1370
 *     m.to_a               #=> ["HX1138", "H", "X", "113", "8"]
1368
1371
 *     m.select(0, 2, -2)   #=> ["HX1138", "X", "113"]
1396
1399
/*
1397
1400
 *  call-seq:
1398
1401
 *     mtch.to_s   => str
1399
 
 *  
 
1402
 *
1400
1403
 *  Returns the entire matched string.
1401
 
 *     
 
1404
 *
1402
1405
 *     m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1403
1406
 *     m.to_s   #=> "HX1138"
1404
1407
 */
1418
1421
/*
1419
1422
 *  call-seq:
1420
1423
 *     mtch.string   => str
1421
 
 *  
 
1424
 *
1422
1425
 *  Returns a frozen copy of the string passed in to <code>match</code>.
1423
 
 *     
 
1426
 *
1424
1427
 *     m = /(.)(.)(\d+)(\d)/.match("THX1138.")
1425
1428
 *     m.string   #=> "THX1138."
1426
1429
 */
1560
1563
 *  call-seq:
1561
1564
 *     rxp == other_rxp      => true or false
1562
1565
 *     rxp.eql?(other_rxp)   => true or false
1563
 
 *  
 
1566
 *
1564
1567
 *  Equality---Two regexps are equal if their patterns are identical, they have
1565
1568
 *  the same character set code, and their <code>casefold?</code> values are the
1566
1569
 *  same.
1567
 
 *     
 
1570
 *
1568
1571
 *     /abc/  == /abc/x   #=> false
1569
1572
 *     /abc/  == /abc/i   #=> false
1570
1573
 *     /abc/u == /abc/n   #=> false
1628
1631
/*
1629
1632
 *  call-seq:
1630
1633
 *     rxp =~ str    => integer or nil
1631
 
 *  
 
1634
 *
1632
1635
 *  Match---Matches <i>rxp</i> against <i>str</i>.
1633
1636
 *
1634
1637
 *     /at/ =~ "input data"   #=> 7
1643
1646
/*
1644
1647
 *  call-seq:
1645
1648
 *     rxp === str   => true or false
1646
 
 *  
 
1649
 *
1647
1650
 *  Case Equality---Synonym for <code>Regexp#=~</code> used in case statements.
1648
 
 *     
 
1651
 *
1649
1652
 *     a = "HELLO"
1650
1653
 *     case a
1651
1654
 *     when /^[a-z]*$/; print "Lower case\n"
1652
1655
 *     when /^[A-Z]*$/; print "Upper case\n"
1653
1656
 *     else;            print "Mixed case\n"
1654
1657
 *     end
1655
 
 *     
 
1658
 *
1656
1659
 *  <em>produces:</em>
1657
 
 *     
 
1660
 *
1658
1661
 *     Upper case
1659
1662
 */
1660
1663
 
1679
1682
/*
1680
1683
 *  call-seq:
1681
1684
 *     ~ rxp   => integer or nil
1682
 
 *  
 
1685
 *
1683
1686
 *  Match---Matches <i>rxp</i> against the contents of <code>$_</code>.
1684
1687
 *  Equivalent to <code><i>rxp</i> =~ $_</code>.
1685
 
 *     
 
1688
 *
1686
1689
 *     $_ = "input data"
1687
1690
 *     ~ /at/   #=> 7
1688
1691
 */
1710
1713
 *  call-seq:
1711
1714
 *     rxp.match(str)       => matchdata or nil
1712
1715
 *     rxp.match(str,pos)   => matchdata or nil
1713
 
 *  
 
1716
 *
1714
1717
 *  Returns a <code>MatchData</code> object describing the match, or
1715
1718
 *  <code>nil</code> if there was no match. This is equivalent to retrieving the
1716
1719
 *  value of the special variable <code>$~</code> following a normal match.
1717
1720
 *  If the second parameter is present, it specifies the position in the string
1718
1721
 *  to begin the search.
1719
 
 *        
 
1722
 *
1720
1723
 *     /(.)(.)(.)/.match("abc")[2]   #=> "b"
1721
1724
 *     /(.)(.)/.match("abc", 1)[2]   #=> "c"
1722
1725
 */
1757
1760
 *     Regexp.new(regexp)                            => regexp
1758
1761
 *     Regexp.compile(string [, options [, lang]])   => regexp
1759
1762
 *     Regexp.compile(regexp)                        => regexp
1760
 
 *  
 
1763
 *
1761
1764
 *  Constructs a new regular expression from <i>pattern</i>, which can be either
1762
1765
 *  a <code>String</code> or a <code>Regexp</code> (in which case that regexp's
1763
1766
 *  options are propagated, and new options may not be specified (a change as of
1768
1771
 *  <code>nil</code>, the regexp will be case insensitive. The <i>lang</i>
1769
1772
 *  parameter enables multibyte support for the regexp: `n', `N' = none, `e',
1770
1773
 *  `E' = EUC, `s', `S' = SJIS, `u', `U' = UTF-8.
1771
 
 * 
 
1774
 *
1772
1775
 *     r1 = Regexp.new('^a-z+:\\s+\w+')           #=> /^a-z+:\s+\w+/
1773
1776
 *     r2 = Regexp.new('cat', true)               #=> /cat/i
1774
1777
 *     r3 = Regexp.new('dog', Regexp::EXTENDED)   #=> /dog/x
1904
1907
 *  call-seq:
1905
1908
 *     Regexp.escape(str)   => a_str
1906
1909
 *     Regexp.quote(str)    => a_str
1907
 
 *  
 
1910
 *
1908
1911
 *  Escapes any characters that would have special meaning in a regular
1909
1912
 *  expression. Returns a new escaped string, or self if no characters are
1910
1913
 *  escaped.  For any string,
1911
1914
 *  <code>Regexp.escape(<i>str</i>)=~<i>str</i></code> will be true.
1912
 
 *     
 
1915
 *
1913
1916
 *     Regexp.escape('\\*?{}.')   #=> \\\\\*\?\{\}\.
1914
1917
 */
1915
1918
 
1965
1968
/*
1966
1969
 *  call-seq:
1967
1970
 *     Regexp.union([pattern]*)   => new_str
1968
 
 *  
 
1971
 *
1969
1972
 *  Return a <code>Regexp</code> object that is the union of the given
1970
1973
 *  <em>pattern</em>s, i.e., will match any of its parts. The <em>pattern</em>s
1971
1974
 *  can be Regexp objects, in which case their options will be preserved, or
1972
1975
 *  Strings. If no arguments are given, returns <code>/(?!)/</code>.
1973
 
 *     
 
1976
 *
1974
1977
 *     Regexp.union                         #=> /(?!)/
1975
1978
 *     Regexp.union("penzance")             #=> /penzance/
1976
1979
 *     Regexp.union("skiing", "sledding")   #=> /skiing|sledding/
2279
2282
 *  call-seq:
2280
2283
 *     Regexp.last_match           => matchdata
2281
2284
 *     Regexp.last_match(fixnum)   => str
2282
 
 *  
 
2285
 *
2283
2286
 *  The first form returns the <code>MatchData</code> object generated by the
2284
2287
 *  last successful pattern match. Equivalent to reading the global variable
2285
2288
 *  <code>$~</code>. The second form returns the nth field in this
2286
2289
 *  <code>MatchData</code> object.
2287
 
 *     
 
2290
 *
2288
2291
 *     /c(.)t/ =~ 'cat'       #=> 0
2289
2292
 *     Regexp.last_match      #=> #<MatchData:0x401b3d30>
2290
2293
 *     Regexp.last_match(0)   #=> "cat"