lua-users home
lua-l archive

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


On Sun, Jul 5, 2015 at 3:34 PM, tomas <tomas@tecgraf.puc-rio.br> wrote:

I test your code but it seems it does not handle all cases at all:
print(quote"]=]==")
 [==[
]=]==]==]

Oops!  Indeed...   My bad.
This error can be fixed by appending a closing bracket to the input string before processing:

function quote (s)
   -- find maximum length of sequences of equal signs
   local n = -1
   for w in string.gmatch(s.."]", "]([]=]*)]") do
      for e in string.gmatch(w, "=*") do
         n = math.max(n, #e)
      end
   end
   -- The rest of the code is exactly the same as in the book