lua-users home
lua-l archive

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


Tome Spilman wrote:
>  Before I do that I'll have to fix at least the strcat test to do the local
> optimization instead.  There are some rules that some of these tests must be
> done the "same way" (
> and strcat is one of them.  They state that the default implementation must
> have "N distinct string append statements done in a loop" and "The program
> should not construct a list of strings and join it".  The also allow
> submission of second entries with a more efficient implementation of
> concatenation which is what your script is.

Umm, please have a look at the original version:

local buff = {}
for i=1,n do
  table.insert(buff, "hello\n")
local s = table.concat(buff)

This *does* construct a list of strings and then joins it. And this version
was accepted ... I have just replaced the function call with equivalent
inlined code. And I should mention that table.concat() does the real work
of growing a string buffer piecewise (as demanded by the rules).

Well, I guess you'd have to discuss this with them ... but another general
rule is to use whatever idiom is common in a language.

And I certainly have used this style in my code, e.g.:

  local rsp = {}
  local i = 2
  rsp[1] = "HTTP/1.1 200 OK"
  for k, v in pairs(self.rspheaders) do rsp[i] = v; i = i +1 end
  rsp[i] = ""
  local rspstr = table.concat(rsp, "\013\010", 1, i+1)

>From that perspective my replacement code could be considered using a
common language idiom.

BTW: Another case in point why a mutable buffer type should be part of
     the Lua core or the Lua core library.