Skip to content

Instantly share code, notes, and snippets.

@BenDol
Created November 25, 2014 13:23
Show Gist options
  • Save BenDol/db6e545e7f4f8c2033f7 to your computer and use it in GitHub Desktop.
Save BenDol/db6e545e7f4f8c2033f7 to your computer and use it in GitHub Desktop.
isInArray
int32 LuaInterface::luaIsInArray(lua_State* L) {
//isInArray(array, value[, caseSensitive = false])
bool caseSensitive = false;
if(lua_gettop(L) > 2)
caseSensitive = popBoolean(L);
boost::any value;
if(lua_isnumber(L, -1))
value = popFloatNumber(L);
else if(lua_isboolean(L, -1))
value = popBoolean(L);
else if(lua_isstring(L, -1))
value = popString(L);
else {
lua_pop(L, 1);
lua_pushboolean(L, false);
return 1;
}
const std::type_info& type = value.type();
if(!caseSensitive && type == typeid(std::string))
value = Tools::asLowerCaseString(boost::any_cast<std::string>(value));
if(!lua_istable(L, -1)) {
boost::any data;
if(lua_isnumber(L, -1))
data = popFloatNumber(L);
else if(lua_isboolean(L, -1))
data = popBoolean(L);
else if(lua_isstring(L, -1))
data = popString(L);
else {
lua_pop(L, 1);
lua_pushboolean(L, false);
return 1;
}
if(type != data.type()) // check is it even same data type before searching deeper
lua_pushboolean(L, false);
else if(type == typeid(bool))
lua_pushboolean(L, boost::any_cast<bool>(value) == boost::any_cast<bool>(data));
else if(type == typeid(double))
lua_pushboolean(L, boost::any_cast<double>(value) == boost::any_cast<double>(data));
else if(caseSensitive)
lua_pushboolean(L, boost::any_cast<std::string>(value) == boost::any_cast<std::string>(data));
else
lua_pushboolean(L, boost::any_cast<std::string>(value) == Tools::asLowerCaseString(boost::any_cast<std::string>(data)));
return 1;
}
lua_pushnil(L);
while(lua_next(L, -2)) {
boost::any data;
if(lua_isnumber(L, -1))
data = popFloatNumber(L);
else if(lua_isboolean(L, -1))
data = popBoolean(L);
else if(lua_isstring(L, -1))
data = popString(L);
else {
lua_pop(L, 1);
break;
}
if(type != data.type()) // check is it same data type before searching deeper
continue;
if(type == typeid(bool)) {
if(boost::any_cast<bool>(value) != boost::any_cast<bool>(data))
continue;
lua_pushboolean(L, true);
return 1;
} else if(type == typeid(double)) {
if(boost::any_cast<double>(value) != boost::any_cast<double>(data))
continue;
lua_pushboolean(L, true);
return 1;
} else if(caseSensitive) {
if(boost::any_cast<std::string>(value) != boost::any_cast<std::string>(data))
continue;
lua_pushboolean(L, true);
return 1;
} else if(boost::any_cast<std::string>(value) == Tools::asLowerCaseString(boost::any_cast<std::string>(data))) {
lua_pushboolean(L, true);
return 1;
}
}
lua_pop(L, 2);
lua_pushboolean(L, false);
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment