lua-users home
lua-l archive

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


Function quote() gives incorrect result for some input strings, for example "]]=]"
BTW, PiL2 contains correct (but not optimal) code for this function.
 
A correct code may look like the following:
 
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
 
Probably, it is more important for code to be simple and beginner-friendly than correct.  

Instead of modifying code of Listing 12.1 this issue may be solved by introducing an exercise: "Find an input string on which function quote() fails to get correct result. Rewrite the function to fix this error."

-- Egor