repositories / dotfiles
dotfiles
bugabingas dorkfiles
owned by bugabinga
neovim/lua/patches/string.lua
RawFile outline
--- Strips leading and trailing whitespace from a string.
---
--- Uses pattern matching to remove ASCII whitespace (spaces, tabs, newlines, etc.).
--- Returns the trimmed string, or an empty string if the input is empty or all whitespace.
---
--- @param str string The input string to strip.
--- @return string The stripped string.
--- @usage
--- local str = " hello world \n"
--- local result = string.strip(str) -- Returns "hello world"
--- @note
--- - Only handles ASCII whitespace (`%s` in Lua patterns).
--- - Unicode whitespace requires custom handling, which Lua 5.1 sucks at.
--- - Edge cases: empty string returns "", all whitespace returns "".
function string.strip(str)
return string.match(str, "^%s*(.-)%s*$") or str
end