From 5e64d92411737a8fe0ea44faebf986944bfea8ea Mon Sep 17 00:00:00 2001 From: monkoose Date: Thu, 22 May 2025 13:00:22 +0300 Subject: perf(runtime): vim.trim for long only whitespace strings --- runtime/lua/vim/shared.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'runtime/lua/vim/shared.lua') diff --git a/runtime/lua/vim/shared.lua b/runtime/lua/vim/shared.lua index fa66e18ac4..73b7408118 100644 --- a/runtime/lua/vim/shared.lua +++ b/runtime/lua/vim/shared.lua @@ -801,7 +801,9 @@ end ---@return string String with whitespace removed from its beginning and end function vim.trim(s) vim.validate('s', s, 'string') - return s:match('^%s*(.*%S)') or '' + -- `s:match('^%s*(.*%S)')` is slow for long whitespace strings, + -- so we are forced to split it into two parts to prevent this + return s:gsub('^%s+', ''):match('^.*%S') or '' end --- Escapes magic chars in |lua-pattern|s. -- cgit v1.3-3-g829e