lua-users home
lua-l archive

Re: the most optimal string "trim" implementation

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Sat, Dec 26, 2009 at 6:31 PM, Shmuel Zeigerman wrote:
> [this] slightly outperformed your variant, except for the all-space strings,
> where it performed worse (which I don't understand why).
> function trim(s)
>  local from = s:find("%S")
>  return from and s:match(".*%S", from) or ""
> end
This variant of yours performs much better in the worst case and has
about the same performance overall as my last one:
function trim(s)
 local from = s:match"^%s*()"
 return from > #s and "" or s:match(".*%S", from)
end
For some reason, s:find"^%s*$" is about twice as fast as s:find"%S"
given s = (" "):rep(100).

AltStyle によって変換されたページ (->オリジナル) /