perf(runtime): vim.trim for long only whitespace strings

This commit is contained in:
monkoose
2025-05-22 13:00:22 +03:00
committed by Lewis Russell
parent ba1cc9e10c
commit 5e64d92411
3 changed files with 76 additions and 2 deletions

View File

@@ -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.