String Replace

lua-users home
wiki

Difference (from prior major revision) (minor diff, author diff)

Added: 0a1
== FAST non-regular expression string search and replace function ==

Added: 1a3,8
Half of the time when doing string search and replace we do not require the
regular expression functionality that comes with the string.gsub function.
I have done a benchmark test with this new function and found it to be
overall more than twice faster than gsub. The speed difference can
be easily seen when doing search and replace on field values of thousands
of records.

Changed: 3c10,11
{{{
I have chosen to name the function 'replace' as Python, Ruby and most languages
use 'replace' for the string search and replace function.

Changed: 5c13,19
static int str_replace(lua_State *L) {
--Sam Lie

----
Add the following function to the string lib file in Lua.

{{{
static int str_replace(lua_State *L) {

Changed: 17,34c31,47
while (1) {
s2 = lmemfind(src+init, l1-init, p, l2);
if (s2) {
luaL_addlstring(&b, src+init, s2-(src+init));
luaL_addlstring(&b, p2, l3);
init = init + (s2-(src+init)) + l2;
n++;
} else {
luaL_addlstring(&b, src+init, l1-init);
break;
}
}


luaL_pushresult(&b);
lua_pushnumber(L, (lua_Number)n); /* number of substitutions */
return 2;
}
while (1) {
s2 = lmemfind(src+init, l1-init, p, l2);
if (s2) {
luaL_addlstring(&b, src+init, s2-(src+init));
luaL_addlstring(&b, p2, l3);
init = init + (s2-(src+init)) + l2;
n++;
} else {
luaL_addlstring(&b, src+init, l1-init);
break;
}
}

luaL_pushresult(&b);
lua_pushnumber(L, (lua_Number)n); /* number of substitutions */
return 2;
}

Changed: 36,37c49

--*note add new the 'replace' function to strlib
/* note: add new the 'replace' function to strlib */

Changed: 40,56c52,67
{"len", str_len},
{"sub", str_sub},
{"lower", str_lower},
{"upper", str_upper},
{"char", str_char},
{"getchar", str_getchar},
{"rep", str_rep},
{"byte", str_byte},
{"format", str_format},
{"dump", str_dump},
{"find", str_find},
{"gfind", gfind},
{"gsub", str_gsub},
{"replace", str_replace},
{NULL, NULL}
};

{"len", str_len},
{"sub", str_sub},
{"lower", str_lower},
{"upper", str_upper},
{"char", str_char},
{"getchar", str_getchar},
{"rep", str_rep},
{"byte", str_byte},
{"format", str_format},
{"dump", str_dump},
{"find", str_find},
{"gfind", gfind},
{"gsub", str_gsub},
{"replace", str_replace},
{NULL, NULL}
};

FAST non-regular expression string search and replace function

Half of the time when doing string search and replace we do not require the regular expression functionality that comes with the string.gsub function. I have done a benchmark test with this new function and found it to be overall more than twice faster than gsub. The speed difference can be easily seen when doing search and replace on field values of thousands of records.

I have chosen to name the function 'replace' as Python, Ruby and most languages use 'replace' for the string search and replace function.

--Sam Lie


Add the following function to the string lib file in Lua.

static int str_replace(lua_State *L) {
    size_t l1, l2, l3;
    const char *src = luaL_checklstring(L, 1, &l1);
    const char *p = luaL_checklstring(L, 2, &l2);
    const char *p2 = luaL_checklstring(L, 3, &l3);
    const char *s2;
    int n = 0;
    int init = 0;

    luaL_Buffer b;
    luaL_buffinit(L, &b);

    while (1) {
        s2 = lmemfind(src+init, l1-init, p, l2);
        if (s2) {
            luaL_addlstring(&b, src+init, s2-(src+init));
            luaL_addlstring(&b, p2, l3);
            init = init + (s2-(src+init)) + l2;
            n++;
        } else {
            luaL_addlstring(&b, src+init, l1-init);
            break;
        }
    }

    luaL_pushresult(&b);
    lua_pushnumber(L, (lua_Number)n);  /* number of substitutions */
    return 2;
}

/* note: add new the 'replace' function to strlib  */

static const luaL_reg strlib[] = {
    {"len", str_len},
    {"sub", str_sub},
    {"lower", str_lower},
    {"upper", str_upper},
    {"char", str_char},
    {"getchar", str_getchar},
    {"rep", str_rep},
    {"byte", str_byte},
    {"format", str_format},
    {"dump", str_dump},
    {"find", str_find},
    {"gfind", gfind},
    {"gsub", str_gsub},
    {"replace", str_replace},
    {NULL, NULL}
};

RecentChanges · preferences
edit · history
Last edited January 6, 2007 6:27 pm GMT (diff)