Skip to content

Commit d85fccd

Browse files
presukubrammool
authored andcommitted
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 #9169, closes #7486)
1 parent a119812 commit d85fccd

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

src/strings.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2137,14 +2137,15 @@ vim_vsnprintf_typval(
21372137
char *q = memchr(str_arg, '\0',
21382138
precision <= (size_t)0x7fffffffL ? precision
21392139
: (size_t)0x7fffffffL);
2140+
21402141
str_arg_l = (q == NULL) ? precision
21412142
: (size_t)(q - str_arg);
21422143
}
21432144
if (fmt_spec == 'S')
21442145
{
2145-
if (min_field_width != 0)
2146-
min_field_width += STRLEN(str_arg)
2147-
- mb_string2cells((char_u *)str_arg, -1);
2146+
size_t base_width = min_field_width;
2147+
size_t pad_cell = 0;
2148+
21482149
if (precision)
21492150
{
21502151
char_u *p1;
@@ -2157,8 +2158,12 @@ vim_vsnprintf_typval(
21572158
if (i > precision)
21582159
break;
21592160
}
2160-
str_arg_l = precision = p1 - (char_u *)str_arg;
2161+
pad_cell = min_field_width - precision;
2162+
base_width = str_arg_l = precision =
2163+
p1 - (char_u *)str_arg;
21612164
}
2165+
if (min_field_width != 0)
2166+
min_field_width = base_width + pad_cell;
21622167
}
21632168
break;
21642169

src/testdir/test_expr.vim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,11 @@ function Test_printf_misc()
297297
call assert_equal('🐍', printf('%.2S', '🐍🐍'))
298298
call assert_equal('', printf('%.1S', '🐍🐍'))
299299

300+
call assert_equal('[ あいう]', printf('[%10.6S]', 'あいうえお'))
301+
call assert_equal('[ あいうえ]', printf('[%10.8S]', 'あいうえお'))
302+
call assert_equal('[あいうえお]', printf('[%10.10S]', 'あいうえお'))
303+
call assert_equal('[あいうえお]', printf('[%10.12S]', 'あいうえお'))
304+
300305
call assert_equal('1%', printf('%d%%', 1))
301306
endfunc
302307

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+
3630,
760762
/**/
761763
3629,
762764
/**/

0 commit comments

Comments
 (0)