Skip to content

Commit ce0fac2

Browse files
committed
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 #4989)
1 parent 30e8e73 commit ce0fac2

3 files changed

Lines changed: 14 additions & 5 deletions

File tree

src/message.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4405,12 +4405,16 @@ vim_vsnprintf_typval(
44054405
- mb_string2cells((char_u *)str_arg, -1);
44064406
if (precision)
44074407
{
4408-
char_u *p1 = (char_u *)str_arg;
4409-
size_t i;
4410-
4411-
for (i = 0; i < precision && *p1; i++)
4412-
p1 += mb_ptr2len(p1);
4408+
char_u *p1;
4409+
size_t i = 0;
44134410

4411+
for (p1 = (char_u *)str_arg; *p1;
4412+
p1 += mb_ptr2len(p1))
4413+
{
4414+
i += (size_t)mb_ptr2cells(p1);
4415+
if (i > precision)
4416+
break;
4417+
}
44144418
str_arg_l = precision = p1 - (char_u *)str_arg;
44154419
}
44164420
}

src/testdir/test_expr.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ function Test_printf_misc()
248248
call assert_equal('abc ', printf('%-4s', 'abc'))
249249
call assert_equal('abc ', printf('%-4S', 'abc'))
250250

251+
call assert_equal('🐍', printf('%.2S', '🐍🐍'))
252+
call assert_equal('', printf('%.1S', '🐍🐍'))
253+
251254
call assert_equal('1%', printf('%d%%', 1))
252255
endfunc
253256

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,8 @@ static char *(features[]) =
757757

758758
static int included_patches[] =
759759
{ /* Add new patch number below this line */
760+
/**/
761+
2083,
760762
/**/
761763
2082,
762764
/**/

0 commit comments

Comments
 (0)