lua-users home
lua-l archive

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


> here's something I stumbled across. The documentation of string.gmatch()
> kinda implies that the matches are done in order from the beginning to the
> end of the string. It is not outright stated, but the documentation says
> that the iterator returned by string.gmatch() returns the next captures ...
> each time it is called.
> 
> However, for string.gsub(), there is no such implication. It talks about
> each occurrence of pattern being substituted by (whatever) in the returned
> sting. There is no mention of the order in which that happens. Of course,
> when you try it with sth like string.gsub("abc", ".", print), you will see
> the matches from beginning to end.
> 
> Sooo, as string.gsub is a mighty tool which has quite a few applications
> when working with strings, I would really like to know whether there is any
> guarantee (outside of observed behaviour) that this is always the case.

Yes. Note that a different order would affect many other things besides
functions with side effects, because matches may overlap. For instance,

  string.gsub("1111", "11", ".")    -->  "1..1"

would be correct if there were no specified order.

-- Roberto