From 28f5e1052c7ee7fb15585621bd7c32ce72c02a82 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sat, 6 Jan 2018 22:12:46 +1100 Subject: src/lauxlib.js: Use luaL_Buffer to implement luaL_gsub --- src/lauxlib.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/lauxlib.js b/src/lauxlib.js index f7988c4..7f491e1 100644 --- a/src/lauxlib.js +++ b/src/lauxlib.js @@ -570,14 +570,15 @@ const find_subarray = function(arr, subarr, from_index) { const luaL_gsub = function(L, s, p, r) { let wild; - let b = []; + let b = new luaL_Buffer(); + luaL_buffinit(L, b); while ((wild = find_subarray(s, p)) >= 0) { - b.push(...s.slice(0, wild)); /* push prefix */ - b.push(...r); /* push replacement in place of pattern */ - s = s.slice(wild + p.length); /* continue after 'p' */ + luaL_addlstring(b, s, wild); /* push prefix */ + luaL_addstring(b, r); /* push replacement in place of pattern */ + s = s.subarray(wild + p.length); /* continue after 'p' */ } - b.push(...s); /* push last suffix */ - lua.lua_pushstring(L, Uint8Array.from(b)); + luaL_addstring(b, s); /* push last suffix */ + luaL_pushresult(b); return lua.lua_tostring(L, -1); }; -- cgit v1.2.3-54-g00ecf