6
$Date: 2005/09/17 14:40:05 $
6
$Date: 2006/03/24 12:14:18 $
7
7
created at: Mon Aug 9 17:12:58 JST 1993
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
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.
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.
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"
1966
1967
rb_backref_set(match);
1969
val = rb_reg_regsub(repl, str, regs);
1970
val = rb_reg_regsub(repl, str, regs, pat);
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.
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.
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}"
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;
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));
2173
* sub!(pattern, replacement) => $_ or nil
2174
* sub!(pattern) {|...| block } => $_ or nil
2176
* Equivalent to <code>$_.sub!(<i>args</i>)</code>.
2180
rb_f_sub_bang(int argc, VALUE *argv)
2182
return rb_str_sub_bang(argc, argv, uscore_get());
2187
* sub(pattern, replacement) => $_
2188
* sub(pattern) { block } => $_
2190
* Equivalent to <code>$_.sub(<i>args</i>)</code>, except that
2191
* <code>$_</code> will be updated if substitution occurs.
2195
rb_f_sub(int argc, VALUE *argv)
2197
VALUE str = rb_str_dup(uscore_get());
2199
if (NIL_P(rb_str_sub_bang(argc, argv, str)))
2201
rb_lastline_set(str);
2207
* gsub!(pattern, replacement) => string or nil
2208
* gsub!(pattern) {|...| block } => string or nil
2210
* Equivalent to <code>Kernel::gsub</code>, except <code>nil</code> is
2211
* returned if <code>$_</code> is not modified.
2213
* $_ = "quick brown fox"
2214
* gsub! /cat/, '*' #=> nil
2215
* $_ #=> "quick brown fox"
2219
rb_f_gsub_bang(int argc, VALUE *argv)
2221
return rb_str_gsub_bang(argc, argv, uscore_get());
2226
* gsub(pattern, replacement) => string
2227
* gsub(pattern) {|...| block } => string
2229
* Equivalent to <code>$_.gsub...</code>, except that <code>$_</code>
2230
* receives the modified result.
2232
* $_ = "quick brown fox"
2233
* gsub /[aeiou]/, '*' #=> "q**ck br*wn f*x"
2234
* $_ #=> "q**ck br*wn f*x"
2238
rb_f_gsub(int argc, VALUE *argv)
2240
VALUE str = rb_str_dup(uscore_get());
2242
if (NIL_P(rb_str_gsub_bang(argc, argv, str)))
2244
rb_lastline_set(str);
2157
RSTRING(str)->len = 0;
3442
* split([pattern [, limit]]) => array
3444
* Equivalent to <code>$_.split(<i>pattern</i>, <i>limit</i>)</code>.
3445
* See <code>String#split</code>.
3449
rb_f_split(int argc, VALUE *argv)
3451
return rb_str_split_m(argc, argv, uscore_get());
3456
3354
* str.each(separator=$/) {|substr| block } => str
3457
3355
* str.each_line(separator=$/) {|substr| block } => str
3625
* chop! => $_ or nil
3627
* Equivalent to <code>$_.chop!</code>.
3641
rb_f_chop_bang(VALUE str)
3643
return rb_str_chop_bang(uscore_get());
3650
* Equivalent to <code>($_.dup).chop!</code>, except <code>nil</code>
3651
* is never returned. See <code>String#chop!</code>.
3667
VALUE str = uscore_get();
3669
if (RSTRING(str)->len > 0) {
3670
str = rb_str_dup(str);
3671
rb_str_chop_bang(str);
3672
rb_lastline_set(str);
3680
3523
* str.chomp!(separator=$/) => str or nil
3682
3525
* Modifies <i>str</i> in place as described for <code>String#chomp</code>,
3785
* chomp! => $_ or nil
3786
* chomp!(string) => $_ or nil
3788
* Equivalent to <code>$_.chomp!(<em>string</em>)</code>. See
3789
* <code>String#chomp!</code>
3794
* chomp! "x" #=> nil
3799
rb_f_chomp_bang(int argc, VALUE *argv)
3801
return rb_str_chomp_bang(argc, argv, uscore_get());
3807
* chomp(string) => $_
3809
* Equivalent to <code>$_ = $_.chomp(<em>string</em>)</code>. See
3810
* <code>String#chomp</code>.
3815
* chomp "ow" #=> "n"
3817
* chomp "xxx" #=> "n"
3822
rb_f_chomp(int argc, VALUE *argv)
3824
VALUE str = uscore_get();
3825
VALUE dup = rb_str_dup(str);
3827
if (NIL_P(rb_str_chomp_bang(argc, argv, dup)))
3829
rb_lastline_set(dup);
3836
3628
* str.lstrip! => self or nil
3838
3630
* Removes leading whitespace from <i>str</i>, returning <code>nil</code> if no
4511
4288
rb_define_method(rb_cString, "sum", rb_str_sum, -1);
4513
rb_define_global_function("sub", rb_f_sub, -1);
4514
rb_define_global_function("gsub", rb_f_gsub, -1);
4516
rb_define_global_function("sub!", rb_f_sub_bang, -1);
4517
rb_define_global_function("gsub!", rb_f_gsub_bang, -1);
4519
rb_define_global_function("chop", rb_f_chop, 0);
4520
rb_define_global_function("chop!", rb_f_chop_bang, 0);
4522
rb_define_global_function("chomp", rb_f_chomp, -1);
4523
rb_define_global_function("chomp!", rb_f_chomp_bang, -1);
4525
rb_define_global_function("split", rb_f_split, -1);
4526
rb_define_global_function("scan", rb_f_scan, 1);
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);