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)
ce0fac2897
This commit is contained in:
Daniel Hahler 2019-09-28 00:32:22 +02:00 committed by GitHub
parent fe9d54f418
commit 0d9a3c86a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 4 deletions

View File

@ -953,11 +953,17 @@ int vim_vsnprintf_typval(
- mb_string2cells((char_u *)str_arg)); - mb_string2cells((char_u *)str_arg));
} }
if (precision) { if (precision) {
const char *p1 = str_arg; char_u *p1;
for (size_t i = 0; i < precision && *p1; i++) { size_t i = 0;
p1 += mb_ptr2len((const char_u *)p1);
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; break;

View File

@ -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('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)) call assert_equal('1%', printf('%d%%', 1))
endfunc endfunc