Feature #3388 ยป 0001-string.c-rb_str_start_with-rb_str_end_with-allow-Reg.patch
| string.c | ||
|---|---|---|
|
int i;
|
||
|
for (i=0; i<argc; i++) {
|
||
|
VALUE tmp = argv[i];
|
||
|
StringValue(tmp);
|
||
|
rb_enc_check(str, tmp);
|
||
|
if (RSTRING_LEN(str) < RSTRING_LEN(tmp)) continue;
|
||
|
if (memcmp(RSTRING_PTR(str), RSTRING_PTR(tmp), RSTRING_LEN(tmp)) == 0)
|
||
|
return Qtrue;
|
||
|
VALUE prefix = argv[i];
|
||
|
if (RB_TYPE_P(prefix, T_REGEXP)) {
|
||
|
VALUE src = rb_str_dup(RREGEXP_SRC(prefix));
|
||
|
rb_str_prepend(src, rb_str_new2("\\A"));
|
||
|
prefix = rb_reg_new_str(src, rb_reg_options(prefix));
|
||
|
if(RTEST(rb_reg_match(prefix, str)))
|
||
|
return Qtrue;
|
||
|
}
|
||
|
else {
|
||
|
StringValue(prefix);
|
||
|
rb_enc_check(str, prefix);
|
||
|
if (RSTRING_LEN(str) < RSTRING_LEN(prefix)) continue;
|
||
|
if (memcmp(RSTRING_PTR(str), RSTRING_PTR(prefix), RSTRING_LEN(prefix)) == 0)
|
||
|
return Qtrue;
|
||
|
}
|
||
|
}
|
||
|
return Qfalse;
|
||
|
}
|
||
| ... | ... | |
|
rb_encoding *enc;
|
||
|
for (i=0; i<argc; i++) {
|
||
|
VALUE tmp = argv[i];
|
||
|
StringValue(tmp);
|
||
|
enc = rb_enc_check(str, tmp);
|
||
|
if (RSTRING_LEN(str) < RSTRING_LEN(tmp)) continue;
|
||
|
p = RSTRING_PTR(str);
|
||
|
e = p + RSTRING_LEN(str);
|
||
|
s = e - RSTRING_LEN(tmp);
|
||
|
if (rb_enc_left_char_head(p, s, e, enc) != s)
|
||
|
continue;
|
||
|
if (memcmp(s, RSTRING_PTR(tmp), RSTRING_LEN(tmp)) == 0)
|
||
|
return Qtrue;
|
||
|
VALUE suffix = argv[i];
|
||
|
if (RB_TYPE_P(suffix, T_REGEXP)) {
|
||
|
VALUE src = rb_str_dup(RREGEXP_SRC(suffix));
|
||
|
rb_str_append(src, rb_str_new2("\\z"));
|
||
|
suffix = rb_reg_new_str(src, rb_reg_options(suffix));
|
||
|
if(RTEST(rb_reg_match(suffix, str)))
|
||
|
return Qtrue;
|
||
|
}
|
||
|
else {
|
||
|
StringValue(suffix);
|
||
|
enc = rb_enc_check(str, suffix);
|
||
|
if (RSTRING_LEN(str) < RSTRING_LEN(suffix)) continue;
|
||
|
p = RSTRING_PTR(str);
|
||
|
e = p + RSTRING_LEN(str);
|
||
|
s = e - RSTRING_LEN(suffix);
|
||
|
if (rb_enc_left_char_head(p, s, e, enc) != s)
|
||
|
continue;
|
||
|
if (memcmp(s, RSTRING_PTR(suffix), RSTRING_LEN(suffix)) == 0)
|
||
|
return Qtrue;
|
||
|
}
|
||
|
}
|
||
|
return Qfalse;
|
||
|
}
|
||
| test/ruby/test_string.rb | ||
|---|---|---|
|
assert_not_send([S("hello"), :end_with?, S("ll")])
|
||
|
assert_send([S("hello"), :end_with?, S("el"), S("lo")])
|
||
|
assert_send([S("hello"), :end_with?, /lo/])
|
||
|
assert_not_send([S("hello"), :end_with?, /ll/])
|
||
|
assert_send([S("hello"), :end_with?, /l{2}o/])
|
||
|
assert_not_send([S("hello"), :end_with?, /l{3}o/])
|
||
|
assert_send([S("helle"), :end_with?, /e/])
|
||
|
assert_send([S("hello"), :end_with?, /ll/, /lo/])
|
||
|
assert_not_send([S("heLLo"), :end_with?, /lo/])
|
||
|
assert_send([S("heLLo"), :end_with?, /lo/i])
|
||
|
bug5536 = '[ruby-core:40623]'
|
||
|
assert_raise(TypeError, bug5536) {S("str").end_with? :not_convertible_to_string}
|
||
|
end
|
||
| ... | ... | |
|
assert_not_send([S("hello"), :start_with?, S("el")])
|
||
|
assert_send([S("hello"), :start_with?, S("el"), S("he")])
|
||
|
assert_send([S("hello"), :start_with?, /he/])
|
||
|
assert_not_send([S("hello"), :start_with?, /el/])
|
||
|
assert_send([S("hello"), :start_with?, /hel{2}/])
|
||
|
assert_not_send([S("hello"), :start_with?, /hel{3}/])
|
||
|
assert_send([S("hello"), :start_with?, /el/, /he/])
|
||
|
assert_not_send([S("Hello"), :start_with?, /he/])
|
||
|
assert_send([S("Hello"), :start_with?, /he/i])
|
||
|
bug5536 = '[ruby-core:40623]'
|
||
|
assert_raise(TypeError, bug5536) {S("str").start_with? :not_convertible_to_string}
|
||
|
end
|
||