aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2022-01-16 17:37:06 +0800
committerzeertzjq <zeertzjq@outlook.com>2022-01-16 17:37:06 +0800
commit9afefd32d3938a50a023355af86ec3293a047130 (patch)
tree9d3bad389ef267de8a2b38fd2e4a33dc0f466f27 /src
parent7085e5b0c8588618e643c87802afc515f67812d9 (diff)
downloadrneovim-9afefd32d3938a50a023355af86ec3293a047130.tar.gz
rneovim-9afefd32d3938a50a023355af86ec3293a047130.tar.bz2
rneovim-9afefd32d3938a50a023355af86ec3293a047130.zip
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) https://github.com/vim/vim/commit/d85fccdfed58108c4e0958d0b17c64690b5f073f
Diffstat (limited to 'src')
-rw-r--r--src/nvim/strings.c13
-rw-r--r--src/nvim/testdir/test_expr.vim5
2 files changed, 13 insertions, 5 deletions
diff --git a/src/nvim/strings.c b/src/nvim/strings.c
index e2a8108c45..efcd40bb75 100644
--- a/src/nvim/strings.c
+++ b/src/nvim/strings.c
@@ -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;
diff --git a/src/nvim/testdir/test_expr.vim b/src/nvim/testdir/test_expr.vim
index 1d7fd3e385..447a519f2c 100644
--- a/src/nvim/testdir/test_expr.vim
+++ b/src/nvim/testdir/test_expr.vim
@@ -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