lua-users home
lua-l archive

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


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