diff options
author | Bram Moolenaar <Bram@vim.org> | 2019-01-24 12:31:44 +0100 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2019-01-24 12:31:44 +0100 |
commit | a5be9b62480a6f338a72c01e57c9edd0bca8048b (patch) | |
tree | 175b99b7f3066c78c025858a4278c4359854a3f7 | |
parent | fb1199d934a27d338b18fe914e328346f093d3f6 (diff) | |
download | vim-git-a5be9b62480a6f338a72c01e57c9edd0bca8048b.tar.gz |
patch 8.1.0802: negative index doesn't work for Blobv8.1.0802
Problem: Negative index doesn't work for Blob.
Solution: Make it work, add a test. (closes #3856)
-rw-r--r-- | src/blob.c | 6 | ||||
-rw-r--r-- | src/eval.c | 9 | ||||
-rw-r--r-- | src/proto/blob.pro | 2 | ||||
-rw-r--r-- | src/testdir/test_blob.vim | 7 | ||||
-rw-r--r-- | src/version.c | 2 |
5 files changed, 20 insertions, 6 deletions
diff --git a/src/blob.c b/src/blob.c index a95403033..9dc7926f4 100644 --- a/src/blob.c +++ b/src/blob.c @@ -72,8 +72,12 @@ blob_copy(typval_T *from, typval_T *to) int len = from->vval.v_blob->bv_ga.ga_len; if (len > 0) + { to->vval.v_blob->bv_ga.ga_data = vim_memsave(from->vval.v_blob->bv_ga.ga_data, len); + if (to->vval.v_blob->bv_ga.ga_data == NULL) + len = 0; + } to->vval.v_blob->bv_ga.ga_len = len; } return ret; @@ -112,7 +116,7 @@ blob_len(blob_T *b) * Get byte "idx" in blob "b". * Caller must check that "idx" is valid. */ - char_u + int blob_get(blob_T *b, int idx) { return ((char_u*)b->bv_ga.ga_data)[idx]; diff --git a/src/eval.c b/src/eval.c index dac086f85..f111dc431 100644 --- a/src/eval.c +++ b/src/eval.c @@ -4723,12 +4723,13 @@ eval_index( } else { - // The resulting variable is a string of a single - // character. If the index is too big or negative the - // result is empty. + // The resulting variable is a byte value. + // If the index is too big or negative that is an error. + if (n1 < 0) + n1 = len + n1; if (n1 < len && n1 >= 0) { - int v = (int)blob_get(rettv->vval.v_blob, n1); + int v = blob_get(rettv->vval.v_blob, n1); clear_tv(rettv); rettv->v_type = VAR_NUMBER; diff --git a/src/proto/blob.pro b/src/proto/blob.pro index b8e48deee..019692dc3 100644 --- a/src/proto/blob.pro +++ b/src/proto/blob.pro @@ -6,7 +6,7 @@ int blob_copy(typval_T *from, typval_T *to); void blob_free(blob_T *b); void blob_unref(blob_T *b); long blob_len(blob_T *b); -char_u blob_get(blob_T *b, int idx); +int blob_get(blob_T *b, int idx); void blob_set(blob_T *b, int idx, char_u c); int blob_equal(blob_T *b1, blob_T *b2); int read_blob(FILE *fd, blob_T *blob); diff --git a/src/testdir/test_blob.vim b/src/testdir/test_blob.vim index 9eb205774..b01e11be2 100644 --- a/src/testdir/test_blob.vim +++ b/src/testdir/test_blob.vim @@ -95,6 +95,13 @@ func Test_blob_get() call assert_equal(999, get(b, 5, 999)) call assert_equal(-1, get(b, -8)) 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 func Test_blob_to_string() diff --git a/src/version.c b/src/version.c index d66296529..5b404ed80 100644 --- a/src/version.c +++ b/src/version.c @@ -792,6 +792,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 802, +/**/ 801, /**/ 800, |