summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorpresuku <presuku@users.noreply.github.com>2021-11-20 19:38:31 +0000
committerBram Moolenaar <Bram@vim.org>2021-11-20 19:38:31 +0000
commitd85fccdfed58108c4e0958d0b17c64690b5f073f (patch)
tree1b0d2973f2c49e6f8bb0925c57740c193fb7cf4b /src
parenta1198124370a366ff02811a43845a631b5c6e7f0 (diff)
downloadvim-git-d85fccdfed58108c4e0958d0b17c64690b5f073f.tar.gz
patch 8.2.3630: printf() with %S does not handle multi-byte correctlyv8.2.3630
Problem: Printf() with %S does not handle multi-byte correctly. Solution: Count cells instead of bytes. (closes #9169, closes #7486)
Diffstat (limited to 'src')
-rw-r--r--src/strings.c13
-rw-r--r--src/testdir/test_expr.vim5
-rw-r--r--src/version.c2
3 files changed, 16 insertions, 4 deletions
diff --git a/src/strings.c b/src/strings.c
index d2d37d80f..bc88ce5f6 100644
--- a/src/strings.c
+++ b/src/strings.c
@@ -2137,14 +2137,15 @@ vim_vsnprintf_typval(
char *q = memchr(str_arg, '\0',
precision <= (size_t)0x7fffffffL ? precision
: (size_t)0x7fffffffL);
+
str_arg_l = (q == NULL) ? precision
: (size_t)(q - str_arg);
}
if (fmt_spec == 'S')
{
- if (min_field_width != 0)
- min_field_width += STRLEN(str_arg)
- - mb_string2cells((char_u *)str_arg, -1);
+ size_t base_width = min_field_width;
+ size_t pad_cell = 0;
+
if (precision)
{
char_u *p1;
@@ -2157,8 +2158,12 @@ vim_vsnprintf_typval(
if (i > precision)
break;
}
- str_arg_l = precision = p1 - (char_u *)str_arg;
+ pad_cell = min_field_width - precision;
+ base_width = str_arg_l = precision =
+ p1 - (char_u *)str_arg;
}
+ if (min_field_width != 0)
+ min_field_width = base_width + pad_cell;
}
break;
diff --git a/src/testdir/test_expr.vim b/src/testdir/test_expr.vim
index 080eee7e1..970097427 100644
--- a/src/testdir/test_expr.vim
+++ b/src/testdir/test_expr.vim
@@ -297,6 +297,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
diff --git a/src/version.c b/src/version.c
index b30979a0b..4fb0e9eb7 100644
--- 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 */
/**/
+ 3630,
+/**/
3629,
/**/
3628,