是否有一个Lua string replace()函数可以比gsub()更快地进行替换?

是否有一个Lua string replace()函数可以比gsub()更快地进行替换?,string,replace,lua,String,Replace,Lua,我看到了Lua字符串函数的列表,以及用于全局搜索和替换的.gsub(): 所有lua字符串函数: static const luaL_Reg strlib[] = { {"byte", str_byte}, {"char", str_char}, {"dump", str_dump}, {"find", str_find}, {"format", str_format}, {"gfind", gfind_nodef}, {"gmatch", gmatch}, {"

我看到了Lua字符串函数的列表,以及用于全局搜索和替换的
.gsub()

所有lua字符串函数:

static const luaL_Reg strlib[] = {
  {"byte", str_byte},
  {"char", str_char},
  {"dump", str_dump},
  {"find", str_find},
  {"format", str_format},
  {"gfind", gfind_nodef},
  {"gmatch", gmatch},
  {"gsub", str_gsub},
  {"len", str_len},
  {"lower", str_lower},
  {"match", str_match},
  {"rep", str_rep},
  {"reverse", str_reverse},
  {"sub", str_sub},
  {"upper", str_upper},
  {NULL, NULL}
};
为什么没有简单、快速、通用(非正则表达式)的字符串替换函数?
.gsub()
是否高效到没有任何好处

我发现这是在2006年写的,但它似乎没有包括在内:

这可能是因为它能够做一个
替换
函数所能做的事情,而Lua包含了一个小的、通常不复杂的标准库。没有必要将这样的冗余直接烘焙到语言中

作为一个外部示例,Ruby编程语言在其标准库中提供了
String 35; gsub
String#replace
。Ruby是一种非常非常大的开箱即用语言,因为这样的决策

然而,Lua为自己是一种非常容易扩展的语言而感到自豪。您展示的链接显示了在编译整个Lua时如何将函数烘焙到标准库中。您还可以将其拼合起来创建一个模块

将需要的部件快速拼接在一起会产生以下结果(注意,我们需要来自的
lmemfind
功能):


这个答案是对上述注释的形式化重构。

在gsub中使用表可能比使用函数更快。“为什么没有简单、快速、通用(非正则表达式)的替换函数?”用什么替换什么?请举例说明您想用什么替换。您是否遇到了性能问题?如果
gsub
已经可以相对高效地执行此操作,那么纯文本搜索和替换还有什么其他用途呢。即使是Penlight对的解释也只是对gsub的几次调用。无论如何,您可以相当容易地将找到的代码片段打包到一个模块中。@MindaugasBernatavičius可能是因为
gsub
能够完成这个
replace
函数将要做的事情,而Lua的设计目标包括一个小型的、通常不复杂的标准库。不需要像这样的冗余。再说一次,Lua是一种可扩展语言——如果您需要,可以非常非常容易地添加此功能。不过,大多数人并不需要它。
#include <lua.h>
#include <lauxlib.h>
#include <string.h>

static const char *lmemfind
(const char *s1, size_t l1, const char *s2, size_t l2) {
    if (l2 == 0)
        return s1;  /* empty strings are everywhere */
    else if (l2 > l1)
        return NULL;  /* avoids a negative 'l1' */

    const char *init;  /* to search for a '*s2' inside 's1' */
    l2--;  /* 1st char will be checked by 'memchr' */
    l1 = l1-l2;  /* 's2' cannot be found after that */

    while (l1 > 0 && (init = (const char *) memchr(s1, *s2, l1)) != NULL) {
        init++;   /* 1st char is already checked */

        if (memcmp(init, s2+1, l2) == 0)
            return init-1;
        else {  /* correct 'l1' and 's1' to try again */
            l1 -= init-s1;
            s1 = init;
        }
    }

    return NULL;  /* not found */
}

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;
}

int luaopen_strrep (lua_State *L) {
    lua_pushcfunction(L, str_replace);
    return 1;
}
local replace = require 'strrep'

print(replace('hello world', 'hello', 'yellow')) -- yellow world, 1.0