aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--runtime/doc/builtin.txt9
-rw-r--r--runtime/lua/vim/_meta/vimfn.lua9
-rw-r--r--src/nvim/eval.lua9
-rw-r--r--src/nvim/strings.c4
-rw-r--r--test/old/testdir/test_functions.vim4
5 files changed, 26 insertions, 9 deletions
diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt
index ceecc61b97..8b472523f1 100644
--- a/runtime/doc/builtin.txt
+++ b/runtime/doc/builtin.txt
@@ -8291,15 +8291,18 @@ tr({src}, {fromstr}, {tostr}) *tr()*
trim({text} [, {mask} [, {dir}]]) *trim()*
Return {text} as a String where any character in {mask} is
removed from the beginning and/or end of {text}.
- If {mask} is not given, {mask} is all characters up to 0x20,
- which includes Tab, space, NL and CR, plus the non-breaking
- space character 0xa0.
+
+ If {mask} is not given, or is an empty string, {mask} is all
+ characters up to 0x20, which includes Tab, space, NL and CR,
+ plus the non-breaking space character 0xa0.
+
The optional {dir} argument specifies where to remove the
characters:
0 remove from the beginning and end of {text}
1 remove only at the beginning of {text}
2 remove only at the end of {text}
When omitted both ends are trimmed.
+
This function deals with multibyte characters properly.
Returns an empty string on error.
diff --git a/runtime/lua/vim/_meta/vimfn.lua b/runtime/lua/vim/_meta/vimfn.lua
index 2b79feec8e..2c594e049f 100644
--- a/runtime/lua/vim/_meta/vimfn.lua
+++ b/runtime/lua/vim/_meta/vimfn.lua
@@ -9845,15 +9845,18 @@ function vim.fn.tr(src, fromstr, tostr) end
--- Return {text} as a String where any character in {mask} is
--- removed from the beginning and/or end of {text}.
---- If {mask} is not given, {mask} is all characters up to 0x20,
---- which includes Tab, space, NL and CR, plus the non-breaking
---- space character 0xa0.
+---
+--- If {mask} is not given, or is an empty string, {mask} is all
+--- characters up to 0x20, which includes Tab, space, NL and CR,
+--- plus the non-breaking space character 0xa0.
+---
--- The optional {dir} argument specifies where to remove the
--- characters:
--- 0 remove from the beginning and end of {text}
--- 1 remove only at the beginning of {text}
--- 2 remove only at the end of {text}
--- When omitted both ends are trimmed.
+---
--- This function deals with multibyte characters properly.
--- Returns an empty string on error.
---
diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua
index 46d17d8c03..eec3c68e0f 100644
--- a/src/nvim/eval.lua
+++ b/src/nvim/eval.lua
@@ -11805,15 +11805,18 @@ M.funcs = {
desc = [=[
Return {text} as a String where any character in {mask} is
removed from the beginning and/or end of {text}.
- If {mask} is not given, {mask} is all characters up to 0x20,
- which includes Tab, space, NL and CR, plus the non-breaking
- space character 0xa0.
+
+ If {mask} is not given, or is an empty string, {mask} is all
+ characters up to 0x20, which includes Tab, space, NL and CR,
+ plus the non-breaking space character 0xa0.
+
The optional {dir} argument specifies where to remove the
characters:
0 remove from the beginning and end of {text}
1 remove only at the beginning of {text}
2 remove only at the end of {text}
When omitted both ends are trimmed.
+
This function deals with multibyte characters properly.
Returns an empty string on error.
diff --git a/src/nvim/strings.c b/src/nvim/strings.c
index cc66f917f8..af82f5e578 100644
--- a/src/nvim/strings.c
+++ b/src/nvim/strings.c
@@ -2926,6 +2926,10 @@ void f_trim(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
if (argvars[1].v_type == VAR_STRING) {
mask = tv_get_string_buf_chk(&argvars[1], buf2);
+ if (*mask == NUL) {
+ mask = NULL;
+ }
+
if (argvars[2].v_type != VAR_UNKNOWN) {
bool error = false;
// leading or trailing characters to trim
diff --git a/test/old/testdir/test_functions.vim b/test/old/testdir/test_functions.vim
index 9448ff21aa..eae9c03335 100644
--- a/test/old/testdir/test_functions.vim
+++ b/test/old/testdir/test_functions.vim
@@ -2099,6 +2099,10 @@ func Test_trim()
let chars = join(map(range(1, 0x20) + [0xa0], {n -> n->nr2char()}), '')
call assert_equal("x", trim(chars . "x" . chars))
+ call assert_equal("x", trim(chars . "x" . chars, '', 0))
+ call assert_equal("x" . chars, trim(chars . "x" . chars, '', 1))
+ call assert_equal(chars . "x", trim(chars . "x" . chars, '', 2))
+
call assert_fails('let c=trim([])', 'E730:')
endfunc