From 5930585fb6f11543a87e4c5c72356ac89222237c Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Thu, 15 Oct 2020 14:18:54 +0200 Subject: [PATCH] More char* -> std::string conversions. --- src/bindings/lua/lua_utils.cpp | 39 ++++++++++++-------------------- src/bindings/lua/lua_utils.hpp | 5 ++-- src/bindings/lua/simgrid_lua.cpp | 2 +- 3 files changed, 19 insertions(+), 27 deletions(-) diff --git a/src/bindings/lua/lua_utils.cpp b/src/bindings/lua/lua_utils.cpp index 952f4de0a6..758da4083c 100644 --- a/src/bindings/lua/lua_utils.cpp +++ b/src/bindings/lua/lua_utils.cpp @@ -13,61 +13,58 @@ /* SimGrid Lua helper functions */ #include "lua_utils.hpp" #include +#include +#include /** * @brief Returns a string representation of a value in the Lua stack. * * This function is for debugging purposes. - * It always returns the same pointer. * * @param L the Lua state * @param index index in the stack * @return a string representation of the value at this index */ -const char* sglua_tostring(lua_State* L, int index) +std::string sglua_tostring(lua_State* L, int index) { - static char buff[64]; + std::string buff; switch (lua_type(L, index)) { case LUA_TNIL: - snprintf(buff, 4, "nil"); + buff = "nil"; break; case LUA_TNUMBER: - snprintf(buff, 64, "%.3f", lua_tonumber(L, index)); + buff = simgrid::xbt::string_printf("%.3f", lua_tonumber(L, index)); break; case LUA_TBOOLEAN: - snprintf(buff, 64, "%s", lua_toboolean(L, index) ? "true" : "false"); + buff = lua_toboolean(L, index) ? "true" : "false"; break; case LUA_TSTRING: - snprintf(buff, 63, "'%s'", lua_tostring(L, index)); + buff = simgrid::xbt::string_printf("'%s'", lua_tostring(L, index)); break; case LUA_TFUNCTION: - if (lua_iscfunction(L, index)) { - snprintf(buff, 11, "C-function"); - } else { - snprintf(buff, 9, "function"); - } + buff = lua_iscfunction(L, index) ? "C-function" : "function"; break; case LUA_TTABLE: - snprintf(buff, 64, "table(%p)", lua_topointer(L, index)); + buff = simgrid::xbt::string_printf("table(%p)", lua_topointer(L, index)); break; case LUA_TLIGHTUSERDATA: case LUA_TUSERDATA: - snprintf(buff, 64, "userdata(%p)", lua_touserdata(L, index)); + buff = simgrid::xbt::string_printf("userdata(%p)", lua_touserdata(L, index)); break; case LUA_TTHREAD: - snprintf(buff, 7, "thread"); + buff = "thread"; break; default: - snprintf(buff, 64, "unknown(%d)", lua_type(L, index)); + buff = simgrid::xbt::string_printf("unknown(%d)", lua_type(L, index)); break; } return buff; @@ -76,18 +73,12 @@ const char* sglua_tostring(lua_State* L, int index) /** * @brief Returns a string representation of a key-value pair. * - * It always returns the same pointer. - * * @param L the Lua state * @param key_index index of the key (in the lua stack) * @param value_index index of the value (in the lua stack) * @return a string representation of the key-value pair */ -const char* sglua_keyvalue_tostring(lua_State* L, int key_index, int value_index) +std::string sglua_keyvalue_tostring(lua_State* L, int key_index, int value_index) { - static char buff[64]; - /* value_tostring also always returns the same pointer */ - int len = snprintf(buff, 63, "[%s] -> ", sglua_tostring(L, key_index)); - snprintf(buff + len, 63 - len, "%s", sglua_tostring(L, value_index)); - return buff; + return std::string("[") + sglua_tostring(L, key_index) + "] -> " + sglua_tostring(L, value_index); } diff --git a/src/bindings/lua/lua_utils.hpp b/src/bindings/lua/lua_utils.hpp index 6114d3d27d..1e83b401ef 100644 --- a/src/bindings/lua/lua_utils.hpp +++ b/src/bindings/lua/lua_utils.hpp @@ -9,8 +9,9 @@ #define LUA_UTILS_HPP #include +#include -const char* sglua_tostring(lua_State* L, int index); -const char* sglua_keyvalue_tostring(lua_State* L, int key_index, int value_index); +std::string sglua_tostring(lua_State* L, int index); +std::string sglua_keyvalue_tostring(lua_State* L, int key_index, int value_index); #endif diff --git a/src/bindings/lua/simgrid_lua.cpp b/src/bindings/lua/simgrid_lua.cpp index 7a0f6f9be4..4fa39db576 100644 --- a/src/bindings/lua/simgrid_lua.cpp +++ b/src/bindings/lua/simgrid_lua.cpp @@ -85,7 +85,7 @@ static int dump(lua_State* L) lua_pushvalue(L, -2); /* table key val key */ - XBT_DEBUG("%s", sglua_keyvalue_tostring(L, -1, -2)); + XBT_DEBUG("%s", sglua_keyvalue_tostring(L, -1, -2).c_str()); } lua_settop(L, argc); // Remove everything except the initial arguments -- 2.20.1