lua-users home
lua-l archive

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


On Fri, Dec 25, 2009 at 6:22 AM, Shmuel Zeigerman wrote:
>  function trim(s) return s:match'^%s*(.*%S)' or '' end

Yes, that is even better.

I thought this might also be solvable with the frontier pattern [1],
maybe something like this:

  s:match'^%f[%S].*%f[%s]'

but that does not work.  In fact, the behavior of %f seems anomalous to me:

  assert((' '):match'%f[%s].' == ' ') -- ok
  assert(('a'):match'%f[%S].' == nil) --> why not 'a'?
  assert(('a'):match'%f[\001-\031\033-\255].' == 'a') --> ok
  assert(('a'):match'%f[%z\001-\031\033-\255].' == nil) --> why not 'a'?
  assert((''):match'%f[%s]' == nil) --> ok?

The fourth line maybe is less surprising as p.208 of Beginning Lua
Programming mentions that string.find("\0", "%f[%z]") fails due to an
implementation quirk.

[1] http://lua-users.org/wiki/FrontierPattern