lua-users home
lua-l archive

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


On Fri, 12 Apr 2019 at 10:08, Dirk Laurie <dirk.laurie@gmail.com> wrote:
>
> By analogy to the function utf8.codes, it would be nice to have a
> function string.bytes so that the construction
>
>      for p, c in string.bytes(s) do body end
>
> will iterate over all bytes in string s, with p being the position.
> This is surprisingly clumsy in pure Lua, unless I am missing a trick.

I usually do

for i = 1, #s do
   local c = s:byte(i)
   ..... body....
end


not as neat as a real iterator but can't complain on readability


-- 
Javier