Skip to content

Commit 6e63867

Browse files
ilya-bobyrchrisbra
authored andcommitted
patch 9.0.2040: trim(): hard to use default mask
Problem: trim(): hard to use default mask Solution: Use default 'mask' when it is v:none The default 'mask' value is pretty complex, as it includes many characters. Yet, if one needs to specify the trimming direction, the third argument, 'trim()' currently requires the 'mask' value to be provided explicitly. 'v:none' is already used to mean "use the default argument value" in user defined functions. See |none-function_argument| in help. closes: #13363 Signed-off-by: Christian Brabandt <[email protected]> Co-authored-by: Illia Bobyr <[email protected]>
1 parent 2e3cd52 commit 6e63867

File tree

8 files changed

+59
-20
lines changed

8 files changed

+59
-20
lines changed

runtime/doc/builtin.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10119,9 +10119,10 @@ trim({text} [, {mask} [, {dir}]]) *trim()*
1011910119
Return {text} as a String where any character in {mask} is
1012010120
removed from the beginning and/or end of {text}.
1012110121

10122-
If {mask} is not given, {mask} is all characters up to 0x20,
10123-
which includes Tab, space, NL and CR, plus the non-breaking
10124-
space character 0xa0.
10122+
If {mask} is not given, or is |v:none| (see
10123+
|none-function_argument|), {mask} is all characters up to
10124+
0x20, which includes Tab, space, NL and CR, plus the
10125+
non-breaking space character 0xa0.
1012510126

1012610127
The optional {dir} argument specifies where to remove the
1012710128
characters:

src/errors.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3539,6 +3539,8 @@ EXTERN char e_cannot_lock_object_variable_str[]
35393539
EXTERN char e_cannot_lock_class_variable_str[]
35403540
INIT(= N_("E1392: Cannot (un)lock class variable \"%s\" in class \"%s\""));
35413541
#endif
3542+
EXTERN char e_string_or_none_required_for_argument_nr[]
3543+
INIT(= N_("E1393: String or v:none required for argument %d"));
35423544
// E1393 - E1499 unused (reserved for Vim9 class support)
35433545
EXTERN char e_cannot_mix_positional_and_non_positional_str[]
35443546
INIT(= N_("E1500: Cannot mix positional and non-positional arguments: %s"));

src/proto/typval.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ int check_for_unknown_arg(typval_T *args, int idx);
1414
int check_for_string_arg(typval_T *args, int idx);
1515
int check_for_nonempty_string_arg(typval_T *args, int idx);
1616
int check_for_opt_string_arg(typval_T *args, int idx);
17+
int check_for_opt_string_or_none_arg(typval_T *args, int idx, int *is_none);
1718
int check_for_number_arg(typval_T *args, int idx);
1819
int check_for_opt_number_arg(typval_T *args, int idx);
1920
int check_for_float_or_nr_arg(typval_T *args, int idx);

src/strings.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1962,7 +1962,7 @@ f_trim(typval_T *argvars, typval_T *rettv)
19621962

19631963
if (in_vim9script()
19641964
&& (check_for_string_arg(argvars, 0) == FAIL
1965-
|| check_for_opt_string_arg(argvars, 1) == FAIL
1965+
|| check_for_opt_string_or_none_arg(argvars, 1, NULL) == FAIL
19661966
|| (argvars[1].v_type != VAR_UNKNOWN
19671967
&& check_for_opt_number_arg(argvars, 2) == FAIL)))
19681968
return;
@@ -1971,26 +1971,24 @@ f_trim(typval_T *argvars, typval_T *rettv)
19711971
if (head == NULL)
19721972
return;
19731973

1974-
if (check_for_opt_string_arg(argvars, 1) == FAIL)
1974+
if (check_for_opt_string_or_none_arg(argvars, 1, NULL) == FAIL)
19751975
return;
19761976

19771977
if (argvars[1].v_type == VAR_STRING)
1978-
{
19791978
mask = tv_get_string_buf_chk(&argvars[1], buf2);
19801979

1981-
if (argvars[2].v_type != VAR_UNKNOWN)
1982-
{
1983-
int error = 0;
1980+
if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
1981+
{
1982+
int error = 0;
19841983

1985-
// leading or trailing characters to trim
1986-
dir = (int)tv_get_number_chk(&argvars[2], &error);
1987-
if (error)
1988-
return;
1989-
if (dir < 0 || dir > 2)
1990-
{
1991-
semsg(_(e_invalid_argument_str), tv_get_string(&argvars[2]));
1992-
return;
1993-
}
1984+
// leading or trailing characters to trim
1985+
dir = (int)tv_get_number_chk(&argvars[2], &error);
1986+
if (error)
1987+
return;
1988+
if (dir < 0 || dir > 2)
1989+
{
1990+
semsg(_(e_invalid_argument_str), tv_get_string(&argvars[2]));
1991+
return;
19941992
}
19951993
}
19961994

src/testdir/test_functions.vim

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2231,11 +2231,15 @@ func Test_trim()
22312231
call assert_fails('eval trim(" vim ", " ", [])', 'E745:')
22322232
call assert_fails('eval trim(" vim ", " ", -1)', 'E475:')
22332233
call assert_fails('eval trim(" vim ", " ", 3)', 'E475:')
2234-
call assert_fails('eval trim(" vim ", 0)', 'E1174:')
2234+
call assert_fails('eval trim(" vim ", 0)', 'E1393:')
22352235

22362236
let chars = join(map(range(1, 0x20) + [0xa0], {n -> n->nr2char()}), '')
22372237
call assert_equal("x", trim(chars . "x" . chars))
22382238

2239+
call assert_equal("x", trim(chars . "x" . chars, v:none, 0))
2240+
call assert_equal("x" . chars, trim(chars . "x" . chars, v:none, 1))
2241+
call assert_equal(chars . "x", trim(chars . "x" . chars, v:none, 2))
2242+
22392243
call assert_fails('let c=trim([])', 'E730:')
22402244
endfunc
22412245

src/testdir/test_vim9_builtin.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4786,7 +4786,7 @@ enddef
47864786

47874787
def Test_trim()
47884788
v9.CheckDefAndScriptFailure(['trim(["a"])'], ['E1013: Argument 1: type mismatch, expected string but got list<string>', 'E1174: String required for argument 1'])
4789-
v9.CheckDefAndScriptFailure(['trim("a", ["b"])'], ['E1013: Argument 2: type mismatch, expected string but got list<string>', 'E1174: String required for argument 2'])
4789+
v9.CheckDefAndScriptFailure(['trim("a", ["b"])'], ['E1013: Argument 2: type mismatch, expected string but got list<string>', 'E1393: String or v:none required for argument 2'])
47904790
v9.CheckDefAndScriptFailure(['trim("a", "b", "c")'], ['E1013: Argument 3: type mismatch, expected number but got string', 'E1210: Number required for argument 3'])
47914791
trim('')->assert_equal('')
47924792
trim('', '')->assert_equal('')

src/typval.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,37 @@ check_for_opt_string_arg(typval_T *args, int idx)
450450
|| check_for_string_arg(args, idx) != FAIL) ? OK : FAIL;
451451
}
452452

453+
/*
454+
* Check for an optional string argument at 'idx', that can also be 'v:none' to
455+
* use the default value.
456+
*
457+
* If 'is_none' is non-NULL it is set to 0 and updated to 1 when "args[idx]" is
458+
* 'v:none'.
459+
*/
460+
int
461+
check_for_opt_string_or_none_arg(typval_T *args, int idx, int *is_none)
462+
{
463+
if (is_none != NULL)
464+
*is_none = 0;
465+
466+
if (args[idx].v_type == VAR_UNKNOWN)
467+
return OK;
468+
469+
if (args[idx].v_type == VAR_SPECIAL
470+
&& args[idx].vval.v_number == VVAL_NONE)
471+
{
472+
if (is_none != NULL)
473+
*is_none = 1;
474+
return OK;
475+
}
476+
477+
if (args[idx].v_type == VAR_STRING)
478+
return OK;
479+
480+
semsg(_(e_string_or_none_required_for_argument_nr), idx + 1);
481+
return FAIL;
482+
}
483+
453484
/*
454485
* Give an error and return FAIL unless "args[idx]" is a number.
455486
*/

src/version.c

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

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
2040,
707709
/**/
708710
2039,
709711
/**/

0 commit comments

Comments
 (0)