lua-users home
lua-l archive

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


> For some reason, s:find"^%s*$" is about twice as fast as s:find"%S"
> given s = (" "):rep(100).

|s:find"%S"| does the 100-time repetition at the outer level, calling
the whole pattern-matching machinery for "%S" at every string position.
|s:find"%s*$"| calls the machinery only once, and it does the repetition
at the inner level, while matching %s*.

BTW, the following implementation with LPEG seems to outperform the
gsub implementations:

do
  local space = lpeg.S' \t\v\n'
  local nospace = 1 - space
  local ptrim = space^0 * m.C((space^0 * nospace^1)^0)
  local match = lpeg.match
  function trim (s)
    return match(ptrim, s)
  end
end

or, with re:

do
  local ptrim = re.compile("%s* {(%s* %S+)*}")
  local match = lpeg.match
  function trim2 (s)
    return match(ptrim, s)
  end
end

Its worst case is a sequence like "a ", where it has the same
performance of gsub.

-- Roberto