lua-users home
lua-l archive

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


On Tue, Dec 29, 2009 at 3:04 AM, steve donovan wrote:
> Lua is unique in that all string searching/substitution functions use
> patterns, not literals. So we consider it part of the learning curve
> that people say '%.' if they are looking for a literal period.

Probably.  Also, I think its rare that we need to split on a delimiter
consisting of arbitrary user input, which is good since such a case
would require a function to escape magic characters [1].

[1] http://snippets.luacode.org/?p=snippets/Escape_magic_characters_in_a_string_4

>> Having sep = "" can provide an idiomatic way of converting a character
>> array to and from a string (as in Perl).
> Is this such a common operation?

Probably not very common, and not the only way to achieve it.  I found
a few occasions of it in grepping sources, mainly for splitting apart
a sequence of character flags, and it may come up in some byte-wise
encoding things.

A related case is to split on a pattern that evaluates to an empty
string.  For example, using Rici's implementation, and assuming we
have our limited version of lookahead via the frontier pattern, we can
break apart text into lines while preserving the new-line characters
in the lines:

  for k,v in ("\nasdf\r\nsdfg\n"):split('%f[^\r\n]') do
print(string.format("%q", k)) end
  "\
  "
  "asdf\r\
  "
  "sdfg\
  "
  ""

The limitations of the lookahead in Lua patterns might limit the
usefulness of this.  Perhaps you'd like to support lpeg and lrexlib
patterns in the split function though.  There's one on the lpeg manual
page.