Project

General

Profile

Feature #8853 ยป string-sub-enumerator.patch

jeremyevans0 (Jeremy Evans), 07/13/2019 12:30 AM

View differences:

string.c
4987 4987
 *  call-seq:
4988 4988
 *     str.sub!(pattern, replacement)          -> str or nil
4989 4989
 *     str.sub!(pattern) {|match| block }      -> str or nil
4990
 *     str.sub!(pattern)                       -> enumerator
4990 4991
 *
4991 4992
 *  Performs the same substitution as String#sub in-place.
4992 4993
 *
4993 4994
 *  Returns +str+ if a substitution was performed or +nil+ if no substitution
4994
 *  was performed.
4995
 *  was performed.  If no block and no <i>replacement</i> is given, an
4996
 *  enumerator is returned instead.
4995 4997
 */
4996 4998

  
4997 4999
static VALUE
......
5001 5003
    int iter = 0;
5002 5004
    int tainted = 0;
5003 5005
    long plen;
5004
    int min_arity = rb_block_given_p() ? 1 : 2;
5005 5006
    long beg;
5006 5007

  
5007
    rb_check_arity(argc, min_arity, 2);
5008
    rb_check_arity(argc, 1, 2);
5008 5009
    if (argc == 1) {
5010
        RETURN_ENUMERATOR(str, argc, argv);
5009 5011
	iter = 1;
5010 5012
    }
5011 5013
    else {
......
5111 5113
 *     str.sub(pattern, replacement)         -> new_str
5112 5114
 *     str.sub(pattern, hash)                -> new_str
5113 5115
 *     str.sub(pattern) {|match| block }     -> new_str
5116
 *     str.sub(pattern)                      -> enumerator
5114 5117
 *
5115 5118
 *  Returns a copy of +str+ with the _first_ occurrence of +pattern+
5116 5119
 *  replaced by the second argument. The +pattern+ is typically a Regexp; if
......
5137 5140
 *  <code>$&</code>, and <code>$'</code> will be set appropriately. The value
5138 5141
 *  returned by the block will be substituted for the match on each call.
5139 5142
 *
5143
 *  When neither a block nor a second argument is supplied, an
5144
 *  Enumerator is returned.
5145
 *
5140 5146
 *  The result inherits any tainting in the original string or any supplied
5141 5147
 *  replacement string.
5142 5148
 *
......
5151 5157
static VALUE
5152 5158
rb_str_sub(int argc, VALUE *argv, VALUE str)
5153 5159
{
5160
    VALUE ret;
5154 5161
    str = rb_str_dup(str);
5155
    rb_str_sub_bang(argc, argv, str);
5162
    ret = rb_str_sub_bang(argc, argv, str);
5163
    if (argc == 1 && !rb_block_given_p()) {
5164
        return ret;
5165
    }
5156 5166
    return str;
5157 5167
}
5158 5168

  
test/ruby/test_string.rb
1270 1270
    assert_nil(a.sub!(S('X'), S('Y')))
1271 1271
  end
1272 1272

  
1273
  def test_sub_enumerator
1274
    enum = 'abc'.sub("b")
1275
    assert_equal("b", enum.next)
1276
    assert_raise(StopIteration) { enum.next }
1277

  
1278
    str = 'abc'
1279
    enum = str.sub!("b")
1280
    enum.feed 'z'
1281
    assert_equal("b", enum.next)
1282
    assert_raise(StopIteration) { enum.next }
1283
    assert_equal("azc", str)
1284
  end
1285

  
1273 1286
  def test_sub_hash
1274 1287
    assert_equal('azc', 'abc'.sub(/b/, "b" => "z"))
1275 1288
    assert_equal('ac', 'abc'.sub(/b/, {}))