VERSIÓ 1.4.5
- [NEW] Afegida funció "sub" al modul estandar "utf8"
This commit is contained in:
47
lua.cpp
47
lua.cpp
@@ -993,8 +993,50 @@ extern "C" {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int cpp_utf8_sub(lua_State *L) {
|
||||
size_t slen;
|
||||
const char *s = luaL_checklstring(L, 1, &slen);
|
||||
int i = luaL_checkinteger(L, 2);
|
||||
int j = luaL_optinteger(L, 3, -1);
|
||||
|
||||
// Get utf8.offset
|
||||
lua_getglobal(L, "utf8");
|
||||
lua_getfield(L, -1, "offset");
|
||||
|
||||
// Compute start byte index
|
||||
lua_pushvalue(L, 1); // string
|
||||
lua_pushinteger(L, i); // start index
|
||||
lua_call(L, 2, 1); // utf8.offset(s, i)
|
||||
lua_Integer start = lua_tointeger(L, -1);
|
||||
lua_pop(L, 1);
|
||||
|
||||
if (start == 0) {
|
||||
lua_pushliteral(L, "");
|
||||
lua_pop(L, 1); // pop utf8 table
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Compute end byte index (j+1)
|
||||
lua_getfield(L, -1, "offset");
|
||||
lua_pushvalue(L, 1);
|
||||
lua_pushinteger(L, j + 1);
|
||||
lua_call(L, 2, 1);
|
||||
lua_Integer end = lua_tointeger(L, -1);
|
||||
lua_pop(L, 2); // pop result + utf8 table
|
||||
|
||||
if (end == 0) {
|
||||
// until end of string
|
||||
lua_pushlstring(L, s + start - 1, slen - (start - 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
lua_pushlstring(L, s + start - 1, (end - 1) - (start - 1));
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
lua_State *L;
|
||||
bool is_playing = false;
|
||||
bool init_exists = false;
|
||||
@@ -1293,6 +1335,11 @@ void push_lua_funcs() {
|
||||
|
||||
lua_setglobal(L, "pad");
|
||||
|
||||
|
||||
lua_getglobal(L, "utf8"); // push utf8 table
|
||||
lua_pushcfunction(L, cpp_utf8_sub); // push C function
|
||||
lua_setfield(L, -2, "sub"); // utf8.sub = cpp_utf8_sub
|
||||
lua_pop(L, 1); // pop utf8 table
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user