diff options
-rw-r--r-- | ChangeLog | 4 | ||||
-rw-r--r-- | string.c | 56 | ||||
-rw-r--r-- | test/ruby/test_string.rb | 1 |
3 files changed, 34 insertions, 27 deletions
@@ -1,3 +1,7 @@ +Wed Mar 2 23:19:56 2011 Nobuyoshi Nakada <nobu@ruby-lang.org> + + * string.c (str_byte_substr): return nil for negative length. + Wed Mar 2 21:15:00 2011 Tanaka Akira <akr@fsij.org> * ext/openssl/ossl_pkey_dh.c: parenthesize macro arguments. @@ -3990,35 +3990,37 @@ rb_str_setbyte(VALUE str, VALUE index, VALUE value) static VALUE str_byte_substr(VALUE str, long beg, long len) { - char *p, *s = RSTRING_PTR(str), *e = s + RSTRING_LEN(str); - VALUE str2; - if (beg > RSTRING_LEN(str)) return Qnil; - if (beg < 0) { - beg += RSTRING_LEN(str); - if (beg < 0) return Qnil; - } - if (beg + len > RSTRING_LEN(str)) - len = RSTRING_LEN(str) - beg; - if (len <= 0) { - len = 0; - p = 0; - } - else - p = s + beg; + char *p, *s = RSTRING_PTR(str); + long n = RSTRING_LEN(str); + VALUE str2; - if (len > RSTRING_EMBED_LEN_MAX && beg + len == RSTRING_LEN(str)) { - str2 = rb_str_new4(str); - str2 = str_new3(rb_obj_class(str2), str2); - RSTRING(str2)->as.heap.ptr += RSTRING(str2)->as.heap.len - len; - RSTRING(str2)->as.heap.len = len; - } - else { - str2 = rb_str_new5(str, p, len); - rb_enc_cr_str_copy_for_substr(str2, str); - OBJ_INFECT(str2, str); - } + if (beg > n || len < 0) return Qnil; + if (beg < 0) { + beg += n; + if (beg < 0) return Qnil; + } + if (beg + len > n) + len = n - beg; + if (len <= 0) { + len = 0; + p = 0; + } + else + p = s + beg; - return str2; + if (len > RSTRING_EMBED_LEN_MAX && beg + len == n) { + str2 = rb_str_new4(str); + str2 = str_new3(rb_obj_class(str2), str2); + RSTRING(str2)->as.heap.ptr += RSTRING(str2)->as.heap.len - len; + RSTRING(str2)->as.heap.len = len; + } + else { + str2 = rb_str_new5(str, p, len); + rb_enc_cr_str_copy_for_substr(str2, str); + OBJ_INFECT(str2, str); + } + + return str2; } static VALUE diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index 0b632ab1a8..faa7bd670d 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -1961,6 +1961,7 @@ class TestString < Test::Unit::TestCase assert_equal("", "hello".byteslice(5, 1)) assert_equal("o", "hello".byteslice(-1, 6)) assert_equal(nil, "hello".byteslice(-6, 1)) + assert_equal(nil, "hello".byteslice(0, -1)) assert_equal("h", "hello".byteslice(0..0)) assert_equal("", "hello".byteslice(5..0)) |