lua-users home
lua-l archive

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


With the recent changes to string.gmatch and the whole pattern matching code, it seems like now it's much easier to allow yields through string.gsub. May Lua users have yieldable string.gsub?

local function f(s, p) return string.gsub(s, p, coroutine.yield) end
local co = coroutine.wrap(f)
local s1 = co("test", ".")
print(v1) -- t
print(co("g"), co("o")) -- e s
local s2 = co("o")
print(s2) -- t
print(co("d")) -- good 4
co = coroutine.wrap(f)

Goals of this proposal:
1. Let coroutines be used for fast dynamic substitution, something we currently can't do. 2. string.gsub uses a buffer internally, while a string.gmatch-based approach would require either table allocation or string concatenation. For large source strings and small replacement strings, string.gsub would probably be better, both in memory, by cutting down allocation, and in CPU, by cutting down hashing and interning. 3. Event system interoperability (throw and handle events from a dynamic substitution).

Another option would be to just add buffers, which would, in theory, let you reimplement most of the standard libraries in pure Lua, with performance and memory characteristics closer to the C functions.

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.