summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpresuku <presuku@users.noreply.github.com>2021-11-24 15:32:57 +0000
committerBram Moolenaar <Bram@vim.org>2021-11-24 15:32:57 +0000
commit1f2453fec6f8f0f315f00ca7b562a02090cb1e37 (patch)
treed2717dd4ae150a381d0c3ee1b010dceb62e55c7b
parentcf1e0239ceec96396fa51f494e442c799ccd45fb (diff)
downloadvim-git-1f2453fec6f8f0f315f00ca7b562a02090cb1e37.tar.gz
patch 8.2.3663: using %S in printf() does not work correctlyv8.2.3663
Problem: Using %S in printf() does not work correctly. Solution: Fix the problem and add more tests. (closes #9208)
-rw-r--r--src/strings.c29
-rw-r--r--src/testdir/test_expr.vim60
-rw-r--r--src/version.c2
3 files changed, 74 insertions, 17 deletions
diff --git a/src/strings.c b/src/strings.c
index bc88ce5f6..847d74379 100644
--- a/src/strings.c
+++ b/src/strings.c
@@ -2143,27 +2143,22 @@ vim_vsnprintf_typval(
}
if (fmt_spec == 'S')
{
- size_t base_width = min_field_width;
- size_t pad_cell = 0;
+ char_u *p1;
+ size_t i;
+ int cell;
- if (precision)
- {
- char_u *p1;
- size_t i = 0;
-
- for (p1 = (char_u *)str_arg; *p1;
+ for (i = 0, p1 = (char_u *)str_arg; *p1;
p1 += mb_ptr2len(p1))
- {
- i += (size_t)mb_ptr2cells(p1);
- if (i > precision)
- break;
- }
- pad_cell = min_field_width - precision;
- base_width = str_arg_l = precision =
- p1 - (char_u *)str_arg;
+ {
+ cell = mb_ptr2cells(p1);
+ if (precision_specified && i + cell > precision)
+ break;
+ i += cell;
}
+
+ str_arg_l = p1 - (char_u *)str_arg;
if (min_field_width != 0)
- min_field_width = base_width + pad_cell;
+ min_field_width += str_arg_l - i;
}
break;
diff --git a/src/testdir/test_expr.vim b/src/testdir/test_expr.vim
index 970097427..323ce2626 100644
--- a/src/testdir/test_expr.vim
+++ b/src/testdir/test_expr.vim
@@ -302,6 +302,66 @@ function Test_printf_misc()
call assert_equal('[あいうえお]', printf('[%10.10S]', 'あいうえお'))
call assert_equal('[あいうえお]', printf('[%10.12S]', 'あいうえお'))
+ call assert_equal('あいう', printf('%S', 'あいう'))
+ call assert_equal('あいう', printf('%#S', 'あいう'))
+
+ call assert_equal('あb', printf('%2S', 'あb'))
+ call assert_equal('あb', printf('%.4S', 'あb'))
+ call assert_equal('あ', printf('%.2S', 'あb'))
+ call assert_equal(' あb', printf('%4S', 'あb'))
+ call assert_equal('0あb', printf('%04S', 'あb'))
+ call assert_equal('あb ', printf('%-4S', 'あb'))
+ call assert_equal('あ ', printf('%-4.2S', 'あb'))
+
+ call assert_equal('aい', printf('%2S', 'aい'))
+ call assert_equal('aい', printf('%.4S', 'aい'))
+ call assert_equal('a', printf('%.2S', 'aい'))
+ call assert_equal(' aい', printf('%4S', 'aい'))
+ call assert_equal('0aい', printf('%04S', 'aい'))
+ call assert_equal('aい ', printf('%-4S', 'aい'))
+ call assert_equal('a ', printf('%-4.2S', 'aい'))
+
+ call assert_equal('[あいう]', printf('[%05S]', 'あいう'))
+ call assert_equal('[あいう]', printf('[%06S]', 'あいう'))
+ call assert_equal('[0あいう]', printf('[%07S]', 'あいう'))
+
+ call assert_equal('[あiう]', printf('[%05S]', 'あiう'))
+ call assert_equal('[0あiう]', printf('[%06S]', 'あiう'))
+ call assert_equal('[00あiう]', printf('[%07S]', 'あiう'))
+
+ call assert_equal('[0あい]', printf('[%05.4S]', 'あいう'))
+ call assert_equal('[00あい]', printf('[%06.4S]', 'あいう'))
+ call assert_equal('[000あい]', printf('[%07.4S]', 'あいう'))
+
+ call assert_equal('[00あi]', printf('[%05.4S]', 'あiう'))
+ call assert_equal('[000あi]', printf('[%06.4S]', 'あiう'))
+ call assert_equal('[0000あi]', printf('[%07.4S]', 'あiう'))
+
+ call assert_equal('[0あい]', printf('[%05.5S]', 'あいう'))
+ call assert_equal('[00あい]', printf('[%06.5S]', 'あいう'))
+ call assert_equal('[000あい]', printf('[%07.5S]', 'あいう'))
+
+ call assert_equal('[あiう]', printf('[%05.5S]', 'あiう'))
+ call assert_equal('[0あiう]', printf('[%06.5S]', 'あiう'))
+ call assert_equal('[00あiう]', printf('[%07.5S]', 'あiう'))
+
+ call assert_equal('[0000000000]', printf('[%010.0S]', 'あいう'))
+ call assert_equal('[0000000000]', printf('[%010.1S]', 'あいう'))
+ call assert_equal('[00000000あ]', printf('[%010.2S]', 'あいう'))
+ call assert_equal('[00000000あ]', printf('[%010.3S]', 'あいう'))
+ call assert_equal('[000000あい]', printf('[%010.4S]', 'あいう'))
+ call assert_equal('[000000あい]', printf('[%010.5S]', 'あいう'))
+ call assert_equal('[0000あいう]', printf('[%010.6S]', 'あいう'))
+ call assert_equal('[0000あいう]', printf('[%010.7S]', 'あいう'))
+
+ call assert_equal('[0000000000]', printf('[%010.1S]', 'あiう'))
+ call assert_equal('[00000000あ]', printf('[%010.2S]', 'あiう'))
+ call assert_equal('[0000000あi]', printf('[%010.3S]', 'あiう'))
+ call assert_equal('[0000000あi]', printf('[%010.4S]', 'あiう'))
+ call assert_equal('[00000あiう]', printf('[%010.5S]', 'あiう'))
+ call assert_equal('[00000あiう]', printf('[%010.6S]', 'あiう'))
+ call assert_equal('[00000あiう]', printf('[%010.7S]', 'あiう'))
+
call assert_equal('1%', printf('%d%%', 1))
endfunc
diff --git a/src/version.c b/src/version.c
index 58a185604..b4e246445 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 */
/**/
+ 3663,
+/**/
3662,
/**/
3661,