aboutsummaryrefslogtreecommitdiff
path: root/src/compat.h
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-06-06 17:20:12 +0200
committerBenoit Germain <benoit.germain@ubisoft.com>2024-06-06 17:20:12 +0200
commitd9a149a230f9b320113020789beb78a883da3b40 (patch)
tree64925d938067e97b76d142cf915a5e1ce840246f /src/compat.h
parent5fcbc757f62cdc9698a8f783864141f40fdff34e (diff)
downloadlanes-d9a149a230f9b320113020789beb78a883da3b40.tar.gz
lanes-d9a149a230f9b320113020789beb78a883da3b40.tar.bz2
lanes-d9a149a230f9b320113020789beb78a883da3b40.zip
Converted a few more raw string pointers to std::string_view
Diffstat (limited to 'src/compat.h')
-rw-r--r--src/compat.h30
1 files changed, 18 insertions, 12 deletions
diff --git a/src/compat.h b/src/compat.h
index 58af985..0ae7759 100644
--- a/src/compat.h
+++ b/src/compat.h
@@ -323,21 +323,21 @@ inline void luaG_setmetatable(lua_State* const L_, std::string_view const& tname
323#define STRINGVIEW_FMT "%.*s" 323#define STRINGVIEW_FMT "%.*s"
324 324
325// a replacement of lua_tolstring 325// a replacement of lua_tolstring
326[[nodiscard]] inline std::string_view luaG_tostringview(lua_State* L_, int idx_) 326[[nodiscard]] inline std::string_view luaG_tostringview(lua_State* const L_, int const idx_)
327{ 327{
328 size_t _len{ 0 }; 328 size_t _len{ 0 };
329 char const* _str{ lua_tolstring(L_, idx_, &_len) }; 329 char const* _str{ lua_tolstring(L_, idx_, &_len) };
330 return std::string_view{ _str, _len }; 330 return std::string_view{ _str, _len };
331} 331}
332 332
333[[nodiscard]] inline std::string_view luaG_checkstringview(lua_State* L_, int idx_) 333[[nodiscard]] inline std::string_view luaG_checkstringview(lua_State* const L_, int const idx_)
334{ 334{
335 size_t _len{ 0 }; 335 size_t _len{ 0 };
336 char const* _str{ luaL_checklstring(L_, idx_, &_len) }; 336 char const* _str{ luaL_checklstring(L_, idx_, &_len) };
337 return std::string_view{ _str, _len }; 337 return std::string_view{ _str, _len };
338} 338}
339 339
340[[nodiscard]] inline std::string_view luaG_optstringview(lua_State* L_, int idx_, std::string_view const& default_) 340[[nodiscard]] inline std::string_view luaG_optstringview(lua_State* const L_, int const idx_, std::string_view const& default_)
341{ 341{
342 if (lua_isnoneornil(L_, idx_)) { 342 if (lua_isnoneornil(L_, idx_)) {
343 return default_; 343 return default_;
@@ -347,13 +347,19 @@ inline void luaG_setmetatable(lua_State* const L_, std::string_view const& tname
347 return std::string_view{ _str, _len }; 347 return std::string_view{ _str, _len };
348} 348}
349 349
350[[nodiscard]] inline std::string_view luaG_pushstringview(lua_State* L_, std::string_view const& str_) 350template<typename ...EXTRA>
351[[nodiscard]] inline std::string_view luaG_pushstringview(lua_State* const L_, std::string_view const& str_, EXTRA&&... extra_)
351{ 352{
352#if LUA_VERSION_NUM == 501 353 if constexpr (sizeof...(EXTRA) == 0) {
353 // lua_pushlstring doesn't return a value in Lua 5.1 354 if constexpr (LUA_VERSION_NUM == 501) {
354 lua_pushlstring(L_, str_.data(), str_.size()); 355 // lua_pushlstring doesn't return a value in Lua 5.1
355 return luaG_tostringview(L_, -1); 356 lua_pushlstring(L_, str_.data(), str_.size());
356#else 357 return luaG_tostringview(L_, -1);
357 return std::string_view{ lua_pushlstring(L_, str_.data(), str_.size()), str_.size() }; 358 } else {
358#endif // LUA_VERSION_NUM > 501 359 return std::string_view{ lua_pushlstring(L_, str_.data(), str_.size()), str_.size() };
359} \ No newline at end of file 360 }
361 } else {
362 static_assert((... && std::is_trivial_v<std::decay_t<EXTRA>>));
363 return std::string_view{ lua_pushfstring(L_, str_.data(), std::forward<EXTRA>(extra_)...) };
364 }
365}