diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 9fc5256a7e..a9079c5519 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -15772,8 +15772,14 @@ static void f_strcharpart(typval_T *argvars, typval_T *rettv, FunPtr fptr) { if (argvars[2].v_type != VAR_UNKNOWN) { charlen = get_tv_number(&argvars[2]); while (charlen > 0 && nbyte + len < slen) { - len += mb_char2len(p[nbyte + len]); - charlen--; + int off = nbyte + len; + + if (off < 0) { + len += 1; + } else { + len += mb_char2len(p[off]); + charlen--; + } } } else { len = slen - nbyte; // default: all bytes that are available. diff --git a/src/nvim/testdir/test_expr_utf8.vim b/src/nvim/testdir/test_expr_utf8.vim index 00d5d9a63a..7bdcb4f65f 100644 --- a/src/nvim/testdir/test_expr_utf8.vim +++ b/src/nvim/testdir/test_expr_utf8.vim @@ -4,29 +4,35 @@ if !has('multi_byte') endif scriptencoding utf-8 -func Test_strgetchar() +func Test_strgetchar_utf8() call assert_equal(char2nr('á'), strgetchar('áxb', 0)) call assert_equal(char2nr('x'), strgetchar('áxb', 1)) - call assert_equal(-1, strgetchar('axb', -1)) - call assert_equal(-1, strgetchar('axb', 3)) - call assert_equal(-1, strgetchar('', 0)) - - call assert_equal(char2nr('a'), strgetchar('àxb', 0)) + call assert_equal(char2nr('a'), strgetchar('àxb', 0)) call assert_equal(char2nr('̀'), strgetchar('àxb', 1)) - call assert_equal(char2nr('x'), strgetchar('àxb', 2)) + call assert_equal(char2nr('x'), strgetchar('àxb', 2)) call assert_equal(char2nr('あ'), strgetchar('あaい', 0)) call assert_equal(char2nr('a'), strgetchar('あaい', 1)) call assert_equal(char2nr('い'), strgetchar('あaい', 2)) endfunc -func Test_strcharpart() +func Test_strcharpart_utf8() call assert_equal('áxb', strcharpart('áxb', 0)) call assert_equal('á', strcharpart('áxb', 0, 1)) call assert_equal('x', strcharpart('áxb', 1, 1)) - call assert_equal('a', strcharpart('àxb', 0, 1)) + call assert_equal('いうeお', strcharpart('あいうeお', 1)) + call assert_equal('い', strcharpart('あいうeお', 1, 1)) + call assert_equal('いう', strcharpart('あいうeお', 1, 2)) + call assert_equal('いうe', strcharpart('あいうeお', 1, 3)) + call assert_equal('いうeお', strcharpart('あいうeお', 1, 4)) + call assert_equal('eお', strcharpart('あいうeお', 3)) + call assert_equal('e', strcharpart('あいうeお', 3, 1)) + + call assert_equal('あ', strcharpart('あいうeお', -3, 4)) + + call assert_equal('a', strcharpart('àxb', 0, 1)) call assert_equal('̀', strcharpart('àxb', 1, 1)) - call assert_equal('x', strcharpart('àxb', 2, 1)) + call assert_equal('x', strcharpart('àxb', 2, 1)) endfunc diff --git a/src/nvim/version.c b/src/nvim/version.c index 389e89b466..53435e4650 100644 --- a/src/nvim/version.c +++ b/src/nvim/version.c @@ -663,7 +663,7 @@ static int included_patches[] = { // 1782, // 1781, // 1780, - // 1779, + 1779, // 1778 NA // 1777 NA // 1776 NA