lua-users home
lua-l archive

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




2011/1/10 Josh Haberman <jhaberman@gmail.com>
There doesn't seem to be a way to construct a Lua string by writing
directly into Lua's internal buffer.  For example, read_chars() in
iolib uses the luaL_Buffer abstraction which uses an intermediate
buffer.

Is there any reason Lua couldn't support a function like:

 /* Pushes a string of length "len" whose contents are uninitialized.
  * The caller must initialize the string by writing to the returned
  * buffer and call lua_finalizewstring() before calling any other Lua
  * function on "L"; after this call, the client may no longer write
  * into the returned buffer. */
 char *lua_pushwstring(lua_State *L, size_t len);
 void lua_finalizewstring(lua_State *L);

(lua_finalizewstring() is there in case Lua wants to internally
precompute a hash of the string or anything like that.)

In some cases (like read()/fread()) this could save a memory copy for
string data that is flowing from C->Lua, which seems like a big
benefit.  Though I haven't implemented this idea to perform benchmarks,
avoiding memory copies can improve efficiency significantly in my
experience.

Josh


I think Lua need some mutable string like mechanism. LuaL_Buffer is To simple. maybe lua std-lib will support something like e.g. stringbuffer.