vim-patch:8.1.0802: negative index doesn't work for Blob

Problem:    Negative index doesn't work for Blob.
Solution:   Make it work, add a test. (closes vim/vim#3856)
a5be9b6248

Leave tv_blob_get()'s return type untouched.
This commit is contained in:
Sean Dewar
2021-07-29 22:54:47 +01:00
parent 23f5999d28
commit bd9c787b4f
3 changed files with 13 additions and 4 deletions

View File

@@ -4647,9 +4647,11 @@ eval_index(
tv_blob_set_ret(rettv, blob); tv_blob_set_ret(rettv, blob);
} }
} else { } else {
// The resulting variable is a string of a single // The resulting variable is a byte value.
// character. If the index is too big or negative the // If the index is too big or negative that is an error.
// result is empty. if (n1 < 0) {
n1 = len + n1;
}
if (n1 < len && n1 >= 0) { if (n1 < len && n1 >= 0) {
const int v = (int)tv_blob_get(rettv->vval.v_blob, n1); const int v = (int)tv_blob_get(rettv->vval.v_blob, n1);
tv_clear(rettv); tv_clear(rettv);

View File

@@ -2266,7 +2266,7 @@ void tv_blob_copy(typval_T *const from, typval_T *const to)
to->vval.v_blob = NULL; to->vval.v_blob = NULL;
} else { } else {
tv_blob_alloc_ret(to); tv_blob_alloc_ret(to);
const int len = from->vval.v_blob->bv_ga.ga_len; int len = from->vval.v_blob->bv_ga.ga_len;
if (len > 0) { if (len > 0) {
to->vval.v_blob->bv_ga.ga_data to->vval.v_blob->bv_ga.ga_data

View File

@@ -95,6 +95,13 @@ func Test_blob_get()
call assert_equal(999, get(b, 5, 999)) call assert_equal(999, get(b, 5, 999))
call assert_equal(-1, get(b, -8)) call assert_equal(-1, get(b, -8))
call assert_equal(999, get(b, -8, 999)) call assert_equal(999, get(b, -8, 999))
call assert_equal(0x00, b[0])
call assert_equal(0x22, b[2])
call assert_equal(0x44, b[4])
call assert_equal(0x44, b[-1])
call assert_fails('echo b[5]', 'E979:')
call assert_fails('echo b[-8]', 'E979:')
endfunc endfunc
func Test_blob_to_string() func Test_blob_to_string()