# HG changeset patch # User Bram Moolenaar # Date 1569584704 -7200 # Node ID dfe9db84e326d77659b395c3eaa840f5f5213b42 # Parent 0f3b1cafbf8cc0a1b647c16e1d1a6687247046aa patch 8.1.2083: multi-byte chars do not work properly with "%.*S" in printf() Commit: https://github.com/vim/vim/commit/ce0fac28977af31f1dec411d3535b4de2c3169b3 Author: Bram Moolenaar Date: Fri Sep 27 13:32:06 2019 +0200 patch 8.1.2083: multi-byte chars do not work properly with "%.*S" in printf() Problem: Multi-byte chars do not work properly with "%.*S" in printf(). Solution: Use mb_ptr2cells(). Daniel Hahler, closes https://github.com/vim/vim/issues/4989) diff --git a/src/message.c b/src/message.c --- a/src/message.c +++ b/src/message.c @@ -4405,12 +4405,16 @@ vim_vsnprintf_typval( - mb_string2cells((char_u *)str_arg, -1); if (precision) { - char_u *p1 = (char_u *)str_arg; - size_t i; - - for (i = 0; i < precision && *p1; i++) - p1 += mb_ptr2len(p1); - + char_u *p1; + size_t i = 0; + + for (p1 = (char_u *)str_arg; *p1; + p1 += mb_ptr2len(p1)) + { + i += (size_t)mb_ptr2cells(p1); + if (i > precision) + break; + } str_arg_l = precision = p1 - (char_u *)str_arg; } } diff --git a/src/testdir/test_expr.vim b/src/testdir/test_expr.vim --- a/src/testdir/test_expr.vim +++ b/src/testdir/test_expr.vim @@ -248,6 +248,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 diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -758,6 +758,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 2083, +/**/ 2082, /**/ 2081,