[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: the most optimal string "trim" implementation
- From: David Manura <dm.lua@...>
- Date: Sat, 26 Dec 2009 17:50:47 -0500
On Sat, Dec 26, 2009 at 4:37 PM, Hans Hagen wrote:
> how about return find(s,'^%s*$') and '' or match(s,'^%s*(.*%S)')
It looks reasonable. For similar effect, we may alternately insert a
"()" into match to prevent default string capture. Neither seems to
give a noticeable speed improvement in the given benchmarks, but upon
inlining the trim function calls and measuring more accurately,
observed speed can improve by ~ 10% for whitespace strings. It
doesn't really hurt to make this change. Here's the latest proposed
version:
local match = string.match
function trim(s)
return match(s,'^()%s*$') and '' or match(s,'^%s*(.*%S)')
end