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

« back to all changes in this revision

Viewing changes to string.c

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2006-05-08 22:23:12 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060508222312-w2wqeaz030ifi59j
Tags: 1.9.0+20060423-3ubuntu1
* Resynchronized with Debian.
* Only change from Debian is the addition of
  debian/patches/903_sparc_fix_define.patch to fix illegal instructions
  at runtime on sparc. (change from 1.9.0+20050921-1ubuntu1)

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
  string.c -
4
4
 
5
 
  $Author: matz $
6
 
  $Date: 2005/09/17 14:40:05 $
 
5
  $Author: kosako $
 
6
  $Date: 2006/03/24 12:14:18 $
7
7
  created at: Mon Aug  9 17:12:58 JST 1993
8
8
 
9
 
  Copyright (C) 1993-2003 Yukihiro Matsumoto
 
9
  Copyright (C) 1993-2006 Yukihiro Matsumoto
10
10
  Copyright (C) 2000  Network Applied Communication Laboratory, Inc.
11
11
  Copyright (C) 2000  Information-technology Promotion Agency, Japan
12
12
 
1848
1848
            rb_backref_set(match);
1849
1849
        }
1850
1850
        else {
1851
 
            repl = rb_reg_regsub(repl, str, regs);
 
1851
            repl = rb_reg_regsub(repl, str, regs, pat);
1852
1852
        }
1853
1853
        if (OBJ_TAINTED(repl)) tainted = 1;
1854
1854
        plen = END(0) - BEG(0);
1887
1887
 *  If the method call specifies <i>replacement</i>, special variables such as
1888
1888
 *  <code>$&</code> will not be useful, as substitution into the string occurs
1889
1889
 *  before the pattern match starts. However, the sequences <code>\1</code>,
1890
 
 *  <code>\2</code>, etc., may be used.
 
1890
 *  <code>\2</code>, <code>\k<group_name></code>, etc., may be used.
1891
1891
 *     
1892
1892
 *  In the block form, the current match string is passed in as a parameter, and
1893
1893
 *  variables such as <code>$1</code>, <code>$2</code>, <code>$`</code>,
1897
1897
 *  The result inherits any tainting in the original string or any supplied
1898
1898
 *  replacement string.
1899
1899
 *     
1900
 
 *     "hello".sub(/[aeiou]/, '*')               #=> "h*llo"
1901
 
 *     "hello".sub(/([aeiou])/, '<\1>')          #=> "h<e>llo"
1902
 
 *     "hello".sub(/./) {|s| s[0].to_s + ' ' }   #=> "104 ello"
 
1900
 *     "hello".sub(/[aeiou]/, '*')                  #=> "h*llo"
 
1901
 *     "hello".sub(/([aeiou])/, '<\1>')             #=> "h<e>llo"
 
1902
 *     "hello".sub(/./) {|s| s[0].to_s + ' ' }      #=> "104 ello"
 
1903
 *     "hello".sub(/(?<foo>[aeiou])/, '*\k<foo>*')  #=> "h*e*llo"
1903
1904
 */
1904
1905
 
1905
1906
static VALUE
1966
1967
            rb_backref_set(match);
1967
1968
        }
1968
1969
        else {
1969
 
            val = rb_reg_regsub(repl, str, regs);
 
1970
            val = rb_reg_regsub(repl, str, regs, pat);
1970
1971
        }
1971
1972
        if (OBJ_TAINTED(val)) tainted = 1;
1972
1973
        len = (bp - buf) + (beg - offset) + RSTRING(val)->len + 3;
2066
2067
 *  If a string is used as the replacement, special variables from the match
2067
2068
 *  (such as <code>$&</code> and <code>$1</code>) cannot be substituted into it,
2068
2069
 *  as substitution into the string occurs before the pattern match
2069
 
 *  starts. However, the sequences <code>\1</code>, <code>\2</code>, and so on
2070
 
 *  may be used to interpolate successive groups in the match.
 
2070
 *  starts. However, the sequences <code>\1</code>, <code>\2</code>,
 
2071
 *  <code>\k<group_name></code>, and so on may be used to interpolate
 
2072
 *  successive groups in the match.
2071
2073
 *     
2072
2074
 *  In the block form, the current match string is passed in as a parameter, and
2073
2075
 *  variables such as <code>$1</code>, <code>$2</code>, <code>$`</code>,
2077
2079
 *  The result inherits any tainting in the original string or any supplied
2078
2080
 *  replacement string.
2079
2081
 *     
2080
 
 *     "hello".gsub(/[aeiou]/, '*')              #=> "h*ll*"
2081
 
 *     "hello".gsub(/([aeiou])/, '<\1>')         #=> "h<e>ll<o>"
2082
 
 *     "hello".gsub(/./) {|s| s[0].to_s + ' '}   #=> "104 101 108 108 111 "
 
2082
 *     "hello".gsub(/[aeiou]/, '*')                  #=> "h*ll*"
 
2083
 *     "hello".gsub(/([aeiou])/, '<\1>')             #=> "h<e>ll<o>"
 
2084
 *     "hello".gsub(/./) {|s| s[0].to_s + ' '}       #=> "104 101 108 108 111 "
 
2085
 *     "hello".gsub(/(?<foo>[aeiou])/, '{\k<foo>}')  #=> "h{e}ll{o}"
2083
2086
 */
2084
2087
 
2085
2088
static VALUE
2151
2154
    FL_UNSET(str, STR_NOCAPA);
2152
2155
    FL_SET(str, ELTS_SHARED);
2153
2156
    RSTRING(str)->ptr = null_str;
2154
 
    RARRAY(str)->len = 0;
2155
 
    return str;
2156
 
}
2157
 
 
2158
 
static VALUE
2159
 
uscore_get(void)
2160
 
{
2161
 
    VALUE line;
2162
 
 
2163
 
    line = rb_lastline_get();
2164
 
    if (TYPE(line) != T_STRING) {
2165
 
        rb_raise(rb_eTypeError, "$_ value need to be String (%s given)",
2166
 
                 NIL_P(line) ? "nil" : rb_obj_classname(line));
2167
 
    }
2168
 
    return line;
2169
 
}
2170
 
 
2171
 
/*
2172
 
 *  call-seq:
2173
 
 *     sub!(pattern, replacement)    => $_ or nil
2174
 
 *     sub!(pattern) {|...| block }  => $_ or nil
2175
 
 *  
2176
 
 *  Equivalent to <code>$_.sub!(<i>args</i>)</code>.
2177
 
 */
2178
 
 
2179
 
static VALUE
2180
 
rb_f_sub_bang(int argc, VALUE *argv)
2181
 
{
2182
 
    return rb_str_sub_bang(argc, argv, uscore_get());
2183
 
}
2184
 
 
2185
 
/*
2186
 
 *  call-seq:
2187
 
 *     sub(pattern, replacement)   => $_
2188
 
 *     sub(pattern) { block }      => $_
2189
 
 *  
2190
 
 *  Equivalent to <code>$_.sub(<i>args</i>)</code>, except that
2191
 
 *  <code>$_</code> will be updated if substitution occurs.
2192
 
 */
2193
 
 
2194
 
static VALUE
2195
 
rb_f_sub(int argc, VALUE *argv)
2196
 
{
2197
 
    VALUE str = rb_str_dup(uscore_get());
2198
 
 
2199
 
    if (NIL_P(rb_str_sub_bang(argc, argv, str)))
2200
 
        return str;
2201
 
    rb_lastline_set(str);
2202
 
    return str;
2203
 
}
2204
 
 
2205
 
/*
2206
 
 *  call-seq:
2207
 
 *     gsub!(pattern, replacement)    => string or nil
2208
 
 *     gsub!(pattern) {|...| block }  => string or nil
2209
 
 *  
2210
 
 *  Equivalent to <code>Kernel::gsub</code>, except <code>nil</code> is
2211
 
 *  returned if <code>$_</code> is not modified.
2212
 
 *     
2213
 
 *     $_ = "quick brown fox"
2214
 
 *     gsub! /cat/, '*'   #=> nil
2215
 
 *     $_                 #=> "quick brown fox"
2216
 
 */
2217
 
 
2218
 
static VALUE
2219
 
rb_f_gsub_bang(int argc, VALUE *argv)
2220
 
{
2221
 
    return rb_str_gsub_bang(argc, argv, uscore_get());
2222
 
}
2223
 
 
2224
 
/*
2225
 
 *  call-seq:
2226
 
 *     gsub(pattern, replacement)    => string
2227
 
 *     gsub(pattern) {|...| block }  => string
2228
 
 *  
2229
 
 *  Equivalent to <code>$_.gsub...</code>, except that <code>$_</code>
2230
 
 *  receives the modified result.
2231
 
 *     
2232
 
 *     $_ = "quick brown fox"
2233
 
 *     gsub /[aeiou]/, '*'   #=> "q**ck br*wn f*x"
2234
 
 *     $_                    #=> "q**ck br*wn f*x"
2235
 
 */
2236
 
 
2237
 
static VALUE
2238
 
rb_f_gsub(int argc, VALUE *argv)
2239
 
{
2240
 
    VALUE str = rb_str_dup(uscore_get());
2241
 
 
2242
 
    if (NIL_P(rb_str_gsub_bang(argc, argv, str)))
2243
 
        return str;
2244
 
    rb_lastline_set(str);
2245
 
    return str;
2246
 
}
2247
 
 
 
2157
    RSTRING(str)->len = 0;
 
2158
    return str;
 
2159
}
2248
2160
 
2249
2161
/*
2250
2162
 *  call-seq:
3439
3351
 
3440
3352
/*
3441
3353
 *  call-seq:
3442
 
 *     split([pattern [, limit]])    => array
3443
 
 *  
3444
 
 *  Equivalent to <code>$_.split(<i>pattern</i>, <i>limit</i>)</code>.
3445
 
 *  See <code>String#split</code>.
3446
 
 */
3447
 
 
3448
 
static VALUE
3449
 
rb_f_split(int argc, VALUE *argv)
3450
 
{
3451
 
    return rb_str_split_m(argc, argv, uscore_get());
3452
 
}
3453
 
 
3454
 
/*
3455
 
 *  call-seq:
3456
3354
 *     str.each(separator=$/) {|substr| block }        => str
3457
3355
 *     str.each_line(separator=$/) {|substr| block }   => str
3458
3356
 *  
3622
3520
 
3623
3521
/*
3624
3522
 *  call-seq:
3625
 
 *     chop!    => $_ or nil
3626
 
 *  
3627
 
 *  Equivalent to <code>$_.chop!</code>.
3628
 
 *     
3629
 
 *     a  = "now\r\n"
3630
 
 *     $_ = a
3631
 
 *     chop!   #=> "now"
3632
 
 *     chop!   #=> "no"
3633
 
 *     chop!   #=> "n"
3634
 
 *     chop!   #=> ""
3635
 
 *     chop!   #=> nil
3636
 
 *     $_      #=> ""
3637
 
 *     a       #=> ""
3638
 
 */
3639
 
 
3640
 
static VALUE
3641
 
rb_f_chop_bang(VALUE str)
3642
 
{
3643
 
    return rb_str_chop_bang(uscore_get());
3644
 
}
3645
 
 
3646
 
/*
3647
 
 *  call-seq:
3648
 
 *     chop   => string
3649
 
 *  
3650
 
 *  Equivalent to <code>($_.dup).chop!</code>, except <code>nil</code>
3651
 
 *  is never returned. See <code>String#chop!</code>.
3652
 
 *     
3653
 
 *     a  =  "now\r\n"
3654
 
 *     $_ = a
3655
 
 *     chop   #=> "now"
3656
 
 *     $_     #=> "now"
3657
 
 *     chop   #=> "no"
3658
 
 *     chop   #=> "n"
3659
 
 *     chop   #=> ""
3660
 
 *     chop   #=> ""
3661
 
 *     a      #=> "now\r\n"
3662
 
 */
3663
 
 
3664
 
static VALUE
3665
 
rb_f_chop(void)
3666
 
{
3667
 
    VALUE str = uscore_get();
3668
 
 
3669
 
    if (RSTRING(str)->len > 0) {
3670
 
        str = rb_str_dup(str);
3671
 
        rb_str_chop_bang(str);
3672
 
        rb_lastline_set(str);
3673
 
    }
3674
 
    return str;
3675
 
}
3676
 
 
3677
 
 
3678
 
/*
3679
 
 *  call-seq:
3680
3523
 *     str.chomp!(separator=$/)   => str or nil
3681
3524
 *  
3682
3525
 *  Modifies <i>str</i> in place as described for <code>String#chomp</code>,
3782
3625
 
3783
3626
/*
3784
3627
 *  call-seq:
3785
 
 *     chomp!             => $_ or nil
3786
 
 *     chomp!(string)     => $_ or nil
3787
 
 *  
3788
 
 *  Equivalent to <code>$_.chomp!(<em>string</em>)</code>. See
3789
 
 *  <code>String#chomp!</code>
3790
 
 *     
3791
 
 *     $_ = "now\n"
3792
 
 *     chomp!       #=> "now"
3793
 
 *     $_           #=> "now"
3794
 
 *     chomp! "x"   #=> nil
3795
 
 *     $_           #=> "now"
3796
 
 */
3797
 
 
3798
 
static VALUE
3799
 
rb_f_chomp_bang(int argc, VALUE *argv)
3800
 
{
3801
 
    return rb_str_chomp_bang(argc, argv, uscore_get());
3802
 
}
3803
 
 
3804
 
/*
3805
 
 *  call-seq:
3806
 
 *     chomp            => $_
3807
 
 *     chomp(string)    => $_
3808
 
 *  
3809
 
 *  Equivalent to <code>$_ = $_.chomp(<em>string</em>)</code>. See
3810
 
 *  <code>String#chomp</code>.
3811
 
 *     
3812
 
 *     $_ = "now\n"
3813
 
 *     chomp         #=> "now"
3814
 
 *     $_            #=> "now"
3815
 
 *     chomp "ow"    #=> "n"
3816
 
 *     $_            #=> "n"
3817
 
 *     chomp "xxx"   #=> "n"
3818
 
 *     $_            #=> "n"
3819
 
 */
3820
 
 
3821
 
static VALUE
3822
 
rb_f_chomp(int argc, VALUE *argv)
3823
 
{
3824
 
    VALUE str = uscore_get();
3825
 
    VALUE dup = rb_str_dup(str);
3826
 
 
3827
 
    if (NIL_P(rb_str_chomp_bang(argc, argv, dup)))
3828
 
        return str;
3829
 
    rb_lastline_set(dup);
3830
 
    return dup;
3831
 
}
3832
 
 
3833
 
 
3834
 
/*
3835
 
 *  call-seq:
3836
3628
 *     str.lstrip!   => self or nil
3837
3629
 *  
3838
3630
 *  Removes leading whitespace from <i>str</i>, returning <code>nil</code> if no
3993
3785
            /*
3994
3786
             * Always consume at least one character of the input string
3995
3787
             */
3996
 
            if (RSTRING(str)->len < END(0))
 
3788
            if (RSTRING(str)->len > END(0))
3997
3789
                *start = END(0)+mbclen2(RSTRING(str)->ptr[END(0)],pat);
3998
3790
            else
3999
3791
                *start = END(0)+1;
4075
3867
    return str;
4076
3868
}
4077
3869
 
4078
 
/*
4079
 
 *  call-seq:
4080
 
 *     scan(pattern)                   => array
4081
 
 *     scan(pattern) {|///| block }    => $_
4082
 
 *  
4083
 
 *  Equivalent to calling <code>$_.scan</code>. See
4084
 
 *  <code>String#scan</code>.
4085
 
 */
4086
 
 
4087
 
static VALUE
4088
 
rb_f_scan(VALUE self, VALUE pat)
4089
 
{
4090
 
    return rb_str_scan(uscore_get(), pat);
4091
 
}
4092
 
 
4093
3870
 
4094
3871
/*
4095
3872
 *  call-seq:
4510
4287
 
4511
4288
    rb_define_method(rb_cString, "sum", rb_str_sum, -1);
4512
4289
 
4513
 
    rb_define_global_function("sub", rb_f_sub, -1);
4514
 
    rb_define_global_function("gsub", rb_f_gsub, -1);
4515
 
 
4516
 
    rb_define_global_function("sub!", rb_f_sub_bang, -1);
4517
 
    rb_define_global_function("gsub!", rb_f_gsub_bang, -1);
4518
 
 
4519
 
    rb_define_global_function("chop", rb_f_chop, 0);
4520
 
    rb_define_global_function("chop!", rb_f_chop_bang, 0);
4521
 
 
4522
 
    rb_define_global_function("chomp", rb_f_chomp, -1);
4523
 
    rb_define_global_function("chomp!", rb_f_chomp_bang, -1);
4524
 
 
4525
 
    rb_define_global_function("split", rb_f_split, -1);
4526
 
    rb_define_global_function("scan", rb_f_scan, 1);
4527
 
 
4528
4290
    rb_define_method(rb_cString, "slice", rb_str_aref_m, -1);
4529
4291
    rb_define_method(rb_cString, "slice!", rb_str_slice_bang, -1);
4530
4292