Project

General

Profile

Feature #3388 ยป 0001-string.c-rb_str_start_with-rb_str_end_with-allow-Reg.patch

Eregon (Benoit Daloze), 08/26/2012 11:20 PM

View differences:

string.c
7284 7284
    int i;
7285 7285

  
7286 7286
    for (i=0; i<argc; i++) {
7287
	VALUE tmp = argv[i];
7288
	StringValue(tmp);
7289
	rb_enc_check(str, tmp);
7290
	if (RSTRING_LEN(str) < RSTRING_LEN(tmp)) continue;
7291
	if (memcmp(RSTRING_PTR(str), RSTRING_PTR(tmp), RSTRING_LEN(tmp)) == 0)
7292
	    return Qtrue;
7287
	VALUE prefix = argv[i];
7288
	if (RB_TYPE_P(prefix, T_REGEXP)) {
7289
	    VALUE src = rb_str_dup(RREGEXP_SRC(prefix));
7290
	    rb_str_prepend(src, rb_str_new2("\\A"));
7291
	    prefix = rb_reg_new_str(src, rb_reg_options(prefix));
7292
	    if(RTEST(rb_reg_match(prefix, str)))
7293
		return Qtrue;
7294
	}
7295
	else {
7296
	    StringValue(prefix);
7297
	    rb_enc_check(str, prefix);
7298
	    if (RSTRING_LEN(str) < RSTRING_LEN(prefix)) continue;
7299
	    if (memcmp(RSTRING_PTR(str), RSTRING_PTR(prefix), RSTRING_LEN(prefix)) == 0)
7300
		return Qtrue;
7301
	}
7293 7302
    }
7294 7303
    return Qfalse;
7295 7304
}
......
7309 7318
    rb_encoding *enc;
7310 7319

  
7311 7320
    for (i=0; i<argc; i++) {
7312
	VALUE tmp = argv[i];
7313
	StringValue(tmp);
7314
	enc = rb_enc_check(str, tmp);
7315
	if (RSTRING_LEN(str) < RSTRING_LEN(tmp)) continue;
7316
	p = RSTRING_PTR(str);
7317
        e = p + RSTRING_LEN(str);
7318
	s = e - RSTRING_LEN(tmp);
7319
	if (rb_enc_left_char_head(p, s, e, enc) != s)
7320
	    continue;
7321
	if (memcmp(s, RSTRING_PTR(tmp), RSTRING_LEN(tmp)) == 0)
7322
	    return Qtrue;
7321
	VALUE suffix = argv[i];
7322
	if (RB_TYPE_P(suffix, T_REGEXP)) {
7323
	    VALUE src = rb_str_dup(RREGEXP_SRC(suffix));
7324
	    rb_str_append(src, rb_str_new2("\\z"));
7325
	    suffix = rb_reg_new_str(src, rb_reg_options(suffix));
7326
	    if(RTEST(rb_reg_match(suffix, str)))
7327
		return Qtrue;
7328
	}
7329
	else {
7330
	    StringValue(suffix);
7331
	    enc = rb_enc_check(str, suffix);
7332
	    if (RSTRING_LEN(str) < RSTRING_LEN(suffix)) continue;
7333
	    p = RSTRING_PTR(str);
7334
	    e = p + RSTRING_LEN(str);
7335
	    s = e - RSTRING_LEN(suffix);
7336
	    if (rb_enc_left_char_head(p, s, e, enc) != s)
7337
		continue;
7338
	    if (memcmp(s, RSTRING_PTR(suffix), RSTRING_LEN(suffix)) == 0)
7339
		return Qtrue;
7340
	}
7323 7341
    }
7324 7342
    return Qfalse;
7325 7343
}
test/ruby/test_string.rb
671 671
    assert_not_send([S("hello"), :end_with?, S("ll")])
672 672
    assert_send([S("hello"), :end_with?, S("el"), S("lo")])
673 673

  
674
    assert_send([S("hello"), :end_with?, /lo/])
675
    assert_not_send([S("hello"), :end_with?, /ll/])
676
    assert_send([S("hello"), :end_with?, /l{2}o/])
677
    assert_not_send([S("hello"), :end_with?, /l{3}o/])
678
    assert_send([S("helle"), :end_with?, /e/])
679
    assert_send([S("hello"), :end_with?, /ll/, /lo/])
680
    assert_not_send([S("heLLo"), :end_with?, /lo/])
681
    assert_send([S("heLLo"), :end_with?, /lo/i])
682

  
674 683
    bug5536 = '[ruby-core:40623]'
675 684
    assert_raise(TypeError, bug5536) {S("str").end_with? :not_convertible_to_string}
676 685
  end
......
1228 1237
    assert_not_send([S("hello"), :start_with?, S("el")])
1229 1238
    assert_send([S("hello"), :start_with?, S("el"), S("he")])
1230 1239

  
1240
    assert_send([S("hello"), :start_with?, /he/])
1241
    assert_not_send([S("hello"), :start_with?, /el/])
1242
    assert_send([S("hello"), :start_with?, /hel{2}/])
1243
    assert_not_send([S("hello"), :start_with?, /hel{3}/])
1244
    assert_send([S("hello"), :start_with?, /el/, /he/])
1245
    assert_not_send([S("Hello"), :start_with?, /he/])
1246
    assert_send([S("Hello"), :start_with?, /he/i])
1247

  
1231 1248
    bug5536 = '[ruby-core:40623]'
1232 1249
    assert_raise(TypeError, bug5536) {S("str").start_with? :not_convertible_to_string}
1233 1250
  end
1234
-