Add tests for the edge caces of String#end_with?
Also, check if a suffix is empty, to guarantee the assumption of `onigenc_get_left_adjust_char_head` that `*s` is always accessible, even in the case of `SHARABLE_MIDDLE_SUBSTRING`.
This commit is contained in:
parent
d0268c5ec2
commit
78ff9b719c
8
string.c
8
string.c
@ -10237,12 +10237,14 @@ rb_str_end_with(int argc, VALUE *argv, VALUE str)
|
|||||||
|
|
||||||
for (i=0; i<argc; i++) {
|
for (i=0; i<argc; i++) {
|
||||||
VALUE tmp = argv[i];
|
VALUE tmp = argv[i];
|
||||||
|
long slen, tlen;
|
||||||
StringValue(tmp);
|
StringValue(tmp);
|
||||||
enc = rb_enc_check(str, tmp);
|
enc = rb_enc_check(str, tmp);
|
||||||
if (RSTRING_LEN(str) < RSTRING_LEN(tmp)) continue;
|
if ((tlen = RSTRING_LEN(tmp)) == 0) return Qtrue;
|
||||||
|
if ((slen = RSTRING_LEN(str)) < tlen) continue;
|
||||||
p = RSTRING_PTR(str);
|
p = RSTRING_PTR(str);
|
||||||
e = p + RSTRING_LEN(str);
|
e = p + slen;
|
||||||
s = e - RSTRING_LEN(tmp);
|
s = e - tlen;
|
||||||
if (rb_enc_left_char_head(p, s, e, enc) != s)
|
if (rb_enc_left_char_head(p, s, e, enc) != s)
|
||||||
continue;
|
continue;
|
||||||
if (memcmp(s, RSTRING_PTR(tmp), RSTRING_LEN(tmp)) == 0)
|
if (memcmp(s, RSTRING_PTR(tmp), RSTRING_LEN(tmp)) == 0)
|
||||||
|
@ -1180,6 +1180,8 @@ CODE
|
|||||||
assert_send([S("hello"), :end_with?, S("llo")])
|
assert_send([S("hello"), :end_with?, S("llo")])
|
||||||
assert_not_send([S("hello"), :end_with?, S("ll")])
|
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?, S("el"), S("lo")])
|
||||||
|
assert_send([S("hello"), :end_with?, S("")])
|
||||||
|
assert_not_send([S("hello"), :end_with?])
|
||||||
|
|
||||||
bug5536 = '[ruby-core:40623]'
|
bug5536 = '[ruby-core:40623]'
|
||||||
assert_raise(TypeError, bug5536) {S("str").end_with? :not_convertible_to_string}
|
assert_raise(TypeError, bug5536) {S("str").end_with? :not_convertible_to_string}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user