Skip to content

Commit fd8ca21

Browse files
committed
patch 8.1.1835: cannot use printf() as a method
Problem: Cannot use printf() as a method. Solution: Pass the base as the second argument to printf().
1 parent 22a0c0c commit fd8ca21

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

runtime/doc/eval.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6739,7 +6739,11 @@ printf({fmt}, {expr1} ...) *printf()*
67396739
< May result in:
67406740
" 99: E42 asdfasdfasdfasdfasdfasdfasdfas" ~
67416741

6742-
Often used items are:
6742+
When used as a |method| the base is passed as the second
6743+
argument: >
6744+
Compute()->printf("result: %d")
6745+
6746+
< Often used items are:
67436747
%s string
67446748
%6S string right-aligned in 6 display cells
67456749
%6s string right-aligned in 6 bytes

src/evalfunc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ static funcentry_T global_functions[] =
733733
{"pow", 2, 2, 0, f_pow},
734734
#endif
735735
{"prevnonblank", 1, 1, 0, f_prevnonblank},
736-
{"printf", 1, 19, 0, f_printf},
736+
{"printf", 1, 19, FEARG_2, f_printf},
737737
#ifdef FEAT_JOB_CHANNEL
738738
{"prompt_setcallback", 2, 2, 0, f_prompt_setcallback},
739739
{"prompt_setinterrupt", 2, 2, 0, f_prompt_setinterrupt},

src/testdir/test_method.vim

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,15 @@ func Test_dict_method()
6767
endfunc
6868

6969
func Test_string_method()
70-
call assert_equal(['1', '2', '3'], '1 2 3'->split())
71-
call assert_equal([1, 2, 3], '1 2 3'->split()->map({i, v -> str2nr(v)}))
72-
call assert_equal([65, 66, 67], 'ABC'->str2list())
73-
call assert_equal(3, 'ABC'->strlen())
74-
call assert_equal('a^Mb^[c', "a\rb\ec"->strtrans())
75-
call assert_equal(4, "aあb"->strwidth())
76-
call assert_equal('axc', 'abc'->substitute('b', 'x', ''))
70+
eval '1 2 3'->split()->assert_equal(['1', '2', '3'])
71+
eval '1 2 3'->split()->map({i, v -> str2nr(v)})->assert_equal([1, 2, 3])
72+
eval 'ABC'->str2list()->assert_equal([65, 66, 67])
73+
eval 'ABC'->strlen()->assert_equal(3)
74+
eval "a\rb\ec"->strtrans()->assert_equal('a^Mb^[c')
75+
eval "aあb"->strwidth()->assert_equal(4)
76+
eval 'abc'->substitute('b', 'x', '')->assert_equal('axc')
77+
78+
eval 'abc'->printf('the %s arg')->assert_equal('the abc arg')
7779
endfunc
7880

7981
func Test_method_append()

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,8 @@ static char *(features[]) =
769769

770770
static int included_patches[] =
771771
{ /* Add new patch number below this line */
772+
/**/
773+
1835,
772774
/**/
773775
1834,
774776
/**/

0 commit comments

Comments
 (0)