lua-users home
lua-l archive

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


On 01/12/2012 14:40, Luiz Henrique de Figueiredo wrote:
we constantly need to check whether a given substring is present at a
given index in a string. I don't see any other way to do that in Lua
than:
    if (str:sub(i, i-1+sub:len()) == sub) then ... end

Try
	str:find(sub,i,true)==i

This would be perfect if we could limit the search performed by find with a second index j (which in this case, probably the common case, would be equal to i). What do you think?

I also had the following thought: it seems to me a rather common need to check the presence of a substring in a string. What about a dedicated string func? On the implementation side, it would just be a call to memcmp (since Lua TString's know their length):
   str:contains_at(sub, i)
(Choose your preferred name for the func.) A kind of find, but at a given index only. This would be a feature of common and general usage, I guess [1]. Would also allow implementing custom, but still efficient, variants of find, replace, count, and the like. (Slightly low-level, providing a tool, maybe fitting the lua way?)

Things would be different if substrings were not actual strings but pointers to a range in the original string (as in D: strings and substrings both are {p,len} structs)). A very interesting idea, imo.

I personly have a usage of such a func for a parsing lib: the simplest, and ultimately terminal pattern matching func, for literals or possibly multibyte chars, is just that: source:contains_at(sub, current_idx).

For the record, I was also thinking at comparing bytes in Lua itself using string.byte, but this involves either repeted calls to string.byte on both strings, or making 2 tables of bytes (eg using table.pack) and then comparing them byte after byte. Probably lighter than creating and hashing a new string a runtime, but seems still costly and unduly complicated for the task.

Denis

[1] I just realise this is a generalisation of python's startswith and endswith string methods, for any index.