[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua string library partially reimplemented in Lua
- From: "David Manura" <dm.lua@...>
- Date: Thu, 18 Sep 2008 21:44:41 -0400
On Thu, Sep 18, 2008 at 12:52 PM, Roberto Ierusalimschy wrote:
>> Isn't str:match("^__") already doing that ?
> Not when the second string has magic characters.
Plus, I understand the original motivation was to avoid temporary
string creation. On success, str:match("^__") returns the string "__"
(which is different from "^__"). str:match("^()__") is not much of an
improvement.
~~~
Something related that bothered me was the overloading of the
string.find function with the "plain" Boolean parameter:
string.find (s, pattern [, init [, plain]])
This is stuffing two functions into one:
(1) searching for a "pattern", returning start and end positions and captures
(2) searching for a "simple string", returning start and end
positions (where the end position is redundant since the string length
is known).
The second is conceptually simpler yet requires more command arguments
to disable the default pattern behavior.
Moreover, string.match and string.find are essentially one function
(lstrlib.c:str_find_aux) split into two.
(Note also the related proposal to add a "plain" argument to the
string.gsub function[1].)
~~~
So, how about these string functions for Lua 5.2?
(1) string.match (s, pattern [, init ])
This is unchanged from Lua 5.1. It searches for a pattern in the
string. Positions can be returned via "()" in the pattern,
eliminating the need for string.find:
string.match (s, "pattern", init)
string.match (s, "()pattern()", init)
I know of one potential limitation[2] of this compared to string.find,
but that could be addressed by some extension to the () pattern
matching syntax if desired.
(2) string.smatch (s, s2, [, init, [, final ] ])
This searches for the string s2 in the string s, returning the
position. The search is optionally bounded in the range [init,
final]. Set init == final to match at a specific position (it's
sort-of like string.sub). Perhaps it would be useful to add the
optional "final" parameter to the string.match function too.
(3) Deprecate string.find.
[1] http://lua-users.org/lists/lua-l/2005-09/msg00122.html
[2] http://lua-users.org/lists/lua-l/2005-09/msg00350.html