aboutsummaryrefslogtreecommitdiff
path: root/src/compat.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/compat.h')
-rw-r--r--src/compat.h13
1 files changed, 12 insertions, 1 deletions
diff --git a/src/compat.h b/src/compat.h
index 3fb4e2d..b5afe17 100644
--- a/src/compat.h
+++ b/src/compat.h
@@ -254,9 +254,20 @@ LuaType luaG_getmodule(lua_State* L_, char const* name_);
254// ################################################################################################# 254// #################################################################################################
255 255
256// a replacement of lua_tolstring 256// a replacement of lua_tolstring
257inline std::string_view lua_tostringview(lua_State* L_, int idx_) 257[[nodiscard]] inline std::string_view lua_tostringview(lua_State* L_, int idx_)
258{ 258{
259 size_t _len{ 0 }; 259 size_t _len{ 0 };
260 char const* _str{ lua_tolstring(L_, idx_, &_len) }; 260 char const* _str{ lua_tolstring(L_, idx_, &_len) };
261 return std::string_view{ _str, _len }; 261 return std::string_view{ _str, _len };
262} 262}
263
264[[nodiscard]] inline std::string_view lua_pushstringview(lua_State* L_, std::string_view const& str_)
265{
266#if LUA_VERSION_NUM == 501
267 // lua_pushlstring doesn't return a value in Lua 5.1
268 lua_pushlstring(L_, str_.data(), str_.size());
269 return lua_tostringview(L_, -1);
270#else
271 return std::string_view{ lua_pushlstring(L_, str_.data(), str_.size()), str_.size() };
272#endif // LUA_VERSION_NUM > 501
273} \ No newline at end of file