Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
More char* -> std::string conversions.
authorArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Thu, 15 Oct 2020 12:18:54 +0000 (14:18 +0200)
committerArnaud Giersch <arnaud.giersch@univ-fcomte.fr>
Thu, 15 Oct 2020 12:18:54 +0000 (14:18 +0200)
src/bindings/lua/lua_utils.cpp
src/bindings/lua/lua_utils.hpp
src/bindings/lua/simgrid_lua.cpp

index 952f4de..758da40 100644 (file)
 /* SimGrid Lua helper functions                                             */
 #include "lua_utils.hpp"
 #include <lauxlib.h>
+#include <string>
+#include <xbt/string.hpp>
 
 /**
  * @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);
 }
index 6114d3d..1e83b40 100644 (file)
@@ -9,8 +9,9 @@
 #define LUA_UTILS_HPP
 
 #include <lua.h>
+#include <string>
 
-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
index 7a0f6f9..4fa39db 100644 (file)
@@ -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