mirror of
https://github.com/tuxbox-fork-migrations/recycled-ni-neutrino.git
synced 2025-08-30 17:01:08 +02:00
CLuaInstance: Add strFind() and strSub() functions
- These functions are possible substitutes for the
Lua functions string.find() and string.sub()
- This Lua functions have problems e.g. with the contents
of some websites.
Origin commit data
------------------
Branch: ni/coolstream
Commit: 51fb2fb9e7
Author: Michael Liebmann <tuxcode.bbg@gmail.com>
Date: 2014-09-10 (Wed, 10 Sep 2014)
Origin message was:
------------------
CLuaInstance: Add strFind() and strSub() functions
- These functions are possible substitutes for the
Lua functions string.find() and string.sub()
- This Lua functions have problems e.g. with the contents
of some websites.
------------------
This commit was generated by Migit
This commit is contained in:
@@ -447,6 +447,8 @@ const luaL_Reg CLuaInstance::methods[] =
|
||||
{ "GetLanguage", CLuaInstance::GetLanguage },
|
||||
{ "runScript", CLuaInstance::runScriptExt },
|
||||
{ "PlayFile", CLuaInstance::PlayFile },
|
||||
{ "strFind", CLuaInstance::strFind },
|
||||
{ "strSub", CLuaInstance::strSub },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
@@ -632,6 +634,62 @@ int CLuaInstance::PlayFile(lua_State *L)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CLuaInstance::strFind(lua_State *L)
|
||||
{
|
||||
int numargs = lua_gettop(L);
|
||||
if (numargs < 3) {
|
||||
printf("CLuaInstance::%s: not enough arguments (%d, expected 2 (or 3 or 4))\n", __func__, numargs);
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
const char *s1;
|
||||
const char *s2;
|
||||
int pos=0, n=0, ret=0;
|
||||
s1 = luaL_checkstring(L, 2);
|
||||
s2 = luaL_checkstring(L, 3);
|
||||
if (numargs > 3)
|
||||
pos = luaL_checkint(L, 4);
|
||||
if (numargs > 4)
|
||||
n = luaL_checkint(L, 5);
|
||||
|
||||
std::string str(s1);
|
||||
if (numargs > 4)
|
||||
ret = str.find(s2, pos, n);
|
||||
else
|
||||
ret = str.find(s2, pos);
|
||||
|
||||
// printf("####[%s:%d] str_len: %d, s2: %s, pos: %d, n: %d, ret: %d\n", __func__, __LINE__, str.length(), s2, pos, n, ret);
|
||||
if (ret == (int)std::string::npos)
|
||||
lua_pushnil(L);
|
||||
else
|
||||
lua_pushinteger(L, ret);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int CLuaInstance::strSub(lua_State *L)
|
||||
{
|
||||
int numargs = lua_gettop(L);
|
||||
if (numargs < 3) {
|
||||
printf("CLuaInstance::%s: not enough arguments (%d, expected 2 (or 3))\n", __func__, numargs);
|
||||
lua_pushstring(L, "");
|
||||
return 1;
|
||||
}
|
||||
const char *s1;
|
||||
int pos=0, len=std::string::npos;
|
||||
std::string ret="";
|
||||
s1 = luaL_checkstring(L, 2);
|
||||
pos = luaL_checkint(L, 3);
|
||||
if (numargs > 3)
|
||||
len = luaL_checkint(L, 4);
|
||||
|
||||
std::string str(s1);
|
||||
ret = str.substr(pos, len);
|
||||
|
||||
// printf("####[%s:%d] str_len: %d, pos: %d, len: %d, ret_len: %d\n", __func__, __LINE__, str.length(), pos, len, ret.length());
|
||||
lua_pushstring(L, ret.c_str());
|
||||
return 1;
|
||||
}
|
||||
|
||||
int CLuaInstance::GetSize(lua_State *L)
|
||||
{
|
||||
DBG("CLuaInstance::%s %d\n", __func__, lua_gettop(L));
|
||||
|
Reference in New Issue
Block a user