vim-patch:8.2.3630: printf() with %S does not handle multi-byte correctly

Problem:    Printf() with %S does not handle multi-byte correctly.
Solution:   Count cells instead of bytes. (closes vim/vim#9169, closes vim/vim#7486)
d85fccdfed
This commit is contained in:
zeertzjq 2022-01-16 17:37:06 +08:00
parent 7085e5b0c8
commit 9afefd32d3
2 changed files with 13 additions and 5 deletions

View File

@ -1001,10 +1001,9 @@ int vim_vsnprintf_typval(char *str, size_t str_m, const char *fmt, va_list ap, t
- str_arg);
}
if (fmt_spec == 'S') {
if (min_field_width != 0) {
min_field_width += (strlen(str_arg)
- mb_string2cells((char_u *)str_arg));
}
size_t base_width = min_field_width;
size_t pad_cell = 0;
if (precision) {
char_u *p1;
size_t i = 0;
@ -1016,7 +1015,11 @@ int vim_vsnprintf_typval(char *str, size_t str_m, const char *fmt, va_list ap, t
break;
}
}
str_arg_l = (size_t)(p1 - (char_u *)str_arg);
pad_cell = min_field_width - precision;
base_width = str_arg_l = (size_t)(p1 - (char_u *)str_arg);
}
if (min_field_width != 0) {
min_field_width = base_width + pad_cell;
}
}
break;

View File

@ -283,6 +283,11 @@ function Test_printf_misc()
call assert_equal('🐍', printf('%.2S', '🐍🐍'))
call assert_equal('', printf('%.1S', '🐍🐍'))
call assert_equal('[ あいう]', printf('[%10.6S]', 'あいうえお'))
call assert_equal('[ あいうえ]', printf('[%10.8S]', 'あいうえお'))
call assert_equal('[あいうえお]', printf('[%10.10S]', 'あいうえお'))
call assert_equal('[あいうえお]', printf('[%10.12S]', 'あいうえお'))
call assert_equal('1%', printf('%d%%', 1))
endfunc