Skip to content

Commit 8079917

Browse files
ilya-bobyrchrisbra
authored andcommitted
patch 9.0.2041: trim(): hard to use default mask
Problem: trim(): hard to use default mask (partly revert v9.0.2040) Solution: use default mask when it is empty 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. Currently, an empty 'mask' will make 'trim()' call return 'text' value that is passed in unmodified. It is unlikely that someone is using it, so the chances of scripts being broken by this change are low. Also, this reverts commit 9.0.2040 (which uses v:none for the default and requires to use an empty string instead). closes: #13358 Signed-off-by: Christian Brabandt <[email protected]> Co-authored-by: Illia Bobyr <[email protected]>
1 parent 5a33ce2 commit 8079917

File tree

8 files changed

+27
-56
lines changed

8 files changed

+27
-56
lines changed

runtime/doc/builtin.txt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10119,10 +10119,9 @@ 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, 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.
10122+
If {mask} is not given, or is an empty string, {mask} is all
10123+
characters up to 0x20, which includes Tab, space, NL and CR,
10124+
plus the non-breaking space character 0xa0.
1012610125

1012710126
The optional {dir} argument specifies where to remove the
1012810127
characters:

src/errors.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3539,8 +3539,6 @@ 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"));
35443542
// E1393 - E1499 unused (reserved for Vim9 class support)
35453543
EXTERN char e_cannot_mix_positional_and_non_positional_str[]
35463544
INIT(= N_("E1500: Cannot mix positional and non-positional arguments: %s"));

src/proto/typval.pro

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ 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);
1817
int check_for_number_arg(typval_T *args, int idx);
1918
int check_for_opt_number_arg(typval_T *args, int idx);
2019
int check_for_float_or_nr_arg(typval_T *args, int idx);

src/strings.c

Lines changed: 17 additions & 13 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_or_none_arg(argvars, 1, NULL) == FAIL
1965+
|| check_for_opt_string_arg(argvars, 1) == FAIL
19661966
|| (argvars[1].v_type != VAR_UNKNOWN
19671967
&& check_for_opt_number_arg(argvars, 2) == FAIL)))
19681968
return;
@@ -1971,24 +1971,28 @@ f_trim(typval_T *argvars, typval_T *rettv)
19711971
if (head == NULL)
19721972
return;
19731973

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

19771977
if (argvars[1].v_type == VAR_STRING)
1978-
mask = tv_get_string_buf_chk(&argvars[1], buf2);
1979-
1980-
if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
19811978
{
1982-
int error = 0;
1979+
mask = tv_get_string_buf_chk(&argvars[1], buf2);
1980+
if (*mask == NUL)
1981+
mask = NULL;
19831982

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)
1983+
if (argvars[2].v_type != VAR_UNKNOWN)
19891984
{
1990-
semsg(_(e_invalid_argument_str), tv_get_string(&argvars[2]));
1991-
return;
1985+
int error = 0;
1986+
1987+
// leading or trailing characters to trim
1988+
dir = (int)tv_get_number_chk(&argvars[2], &error);
1989+
if (error)
1990+
return;
1991+
if (dir < 0 || dir > 2)
1992+
{
1993+
semsg(_(e_invalid_argument_str), tv_get_string(&argvars[2]));
1994+
return;
1995+
}
19921996
}
19931997
}
19941998

src/testdir/test_functions.vim

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2231,14 +2231,14 @@ 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)', 'E1393:')
2234+
call assert_fails('eval trim(" vim ", 0)', 'E1174:')
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))
2239+
call assert_equal("x", trim(chars . "x" . chars, '', 0))
2240+
call assert_equal("x" . chars, trim(chars . "x" . chars, '', 1))
2241+
call assert_equal(chars . "x", trim(chars . "x" . chars, '', 2))
22422242

22432243
call assert_fails('let c=trim([])', 'E730:')
22442244
endfunc

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>', 'E1393: String or v:none required for argument 2'])
4789+
v9.CheckDefAndScriptFailure(['trim("a", ["b"])'], ['E1013: Argument 2: type mismatch, expected string but got list<string>', 'E1174: String 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: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -450,37 +450,6 @@ 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-
484453
/*
485454
* Give an error and return FAIL unless "args[idx]" is a number.
486455
*/

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+
2041,
707709
/**/
708710
2040,
709711
/**/

0 commit comments

Comments
 (0)