changeset 18176:dfe9db84e326 v8.1.2083

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 <Bram@vim.org> 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)
author Bram Moolenaar <Bram@vim.org>
date Fri, 27 Sep 2019 13:45:04 +0200
parents 0f3b1cafbf8c
children a98f052160ff
files src/message.c src/testdir/test_expr.vim src/version.c
diffstat 3 files changed, 15 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- 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;
 			}
 		    }
--- 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
 
--- 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,