diff options
author | Daniel Hahler <git@thequod.de> | 2019-09-28 00:32:22 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-28 00:32:22 +0200 |
commit | 0d9a3c86a1c7143187398e6cb6005ed06a5e2fde (patch) | |
tree | c11a233f496ec926630f63e074c4e94a9e39306e /src | |
parent | fe9d54f418973f59ec88ca05489da6df3609811f (diff) | |
download | rneovim-0d9a3c86a1c7143187398e6cb6005ed06a5e2fde.tar.gz rneovim-0d9a3c86a1c7143187398e6cb6005ed06a5e2fde.tar.bz2 rneovim-0d9a3c86a1c7143187398e6cb6005ed06a5e2fde.zip |
vim-patch:8.1.2083: multi-byte chars do not work properly with "%.*S" in printf() (#11106)
Problem: Multi-byte chars do not work properly with "%.*S" in printf().
Solution: Use mb_ptr2cells(). Daniel Hahler, closes vim/vim#4989)
https://github.com/vim/vim/commit/ce0fac28977af31f1dec411d3535b4de2c3169b3
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/strings.c | 14 | ||||
-rw-r--r-- | src/nvim/testdir/test_expr.vim | 3 |
2 files changed, 13 insertions, 4 deletions
diff --git a/src/nvim/strings.c b/src/nvim/strings.c index 3ba9354c67..2f5491fda5 100644 --- a/src/nvim/strings.c +++ b/src/nvim/strings.c @@ -953,11 +953,17 @@ int vim_vsnprintf_typval( - mb_string2cells((char_u *)str_arg)); } if (precision) { - const char *p1 = str_arg; - for (size_t i = 0; i < precision && *p1; i++) { - p1 += mb_ptr2len((const char_u *)p1); + char_u *p1; + size_t i = 0; + + for (p1 = (char_u *)str_arg; *p1; + p1 += mb_ptr2len(p1)) { + i += (size_t)utf_ptr2cells(p1); + if (i > precision) { + break; + } } - str_arg_l = precision = (size_t)(p1 - str_arg); + str_arg_l = precision = (size_t)(p1 - (char_u *)str_arg); } } break; diff --git a/src/nvim/testdir/test_expr.vim b/src/nvim/testdir/test_expr.vim index 4f99625e73..dd546dbf71 100644 --- a/src/nvim/testdir/test_expr.vim +++ b/src/nvim/testdir/test_expr.vim @@ -279,6 +279,9 @@ function Test_printf_misc() call assert_equal('abc ', printf('%-4s', 'abc')) call assert_equal('abc ', printf('%-4S', 'abc')) + call assert_equal('🐍', printf('%.2S', '🐍🐍')) + call assert_equal('', printf('%.1S', '🐍🐍')) + call assert_equal('1%', printf('%d%%', 1)) endfunc |