From e972ee3b65bc85dbee8e1e7f74594490037dcb67 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Tue, 21 May 2024 08:57:03 +0200 Subject: More string_view --- src/cancel.cpp | 2 +- src/lane.cpp | 38 +++++++++++++++++++------------------- src/lane.h | 9 +++++---- src/tracker.cpp | 2 +- 4 files changed, 26 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/cancel.cpp b/src/cancel.cpp index f093905..2fdd1ef 100644 --- a/src/cancel.cpp +++ b/src/cancel.cpp @@ -249,7 +249,7 @@ LUAG_FUNC(thread_cancel) case CancelResult::Cancelled: lua_pushboolean(L_, 1); // true - _lane->pushThreadStatus(L_); // true status + std::ignore = _lane->pushThreadStatus(L_); // true status break; } STACK_CHECK(L_, 2); diff --git a/src/lane.cpp b/src/lane.cpp index e838b7d..d68da76 100644 --- a/src/lane.cpp +++ b/src/lane.cpp @@ -216,7 +216,7 @@ static int thread_index_number(lua_State* L_) // this is an internal error, we probably never get here lua_settop(L_, 0); // L_: lua_pushliteral(L_, "Unexpected status: "); // L_: "Unexpected status: " - _lane->pushThreadStatus(L_); // L_: "Unexpected status: " "" + std::ignore = _lane->pushThreadStatus(L_); // L_: "Unexpected status: " "" lua_concat(L_, 2); // L_: "Unexpected status: " raise_lua_error(L_); @@ -300,14 +300,14 @@ static int thread_index_string(lua_State* L_) Lane* const _lane{ ToLane(L_, kSelf) }; LUA_ASSERT(L_, lua_gettop(L_) == 2); // L_: lane "key" - char const* const _keystr{ lua_tostring(L_, kKey) }; + std::string_view const _keystr{ lua_tostringview(L_, kKey) }; lua_settop(L_, 2); // keep only our original arguments on the stack - if (strcmp(_keystr, "status") == 0) { - _lane->pushThreadStatus(L_); // L_: lane "key" "" + if (_keystr == "status") { + std::ignore = _lane->pushThreadStatus(L_); // L_: lane "key" "" return 1; } - if (strcmp(_keystr, "error_trace_level") == 0) { - _lane->pushErrorTraceLevel(L_); // L_: lane "key" "" + if (_keystr == "error_trace_level") { + std::ignore = _lane->pushErrorTraceLevel(L_); // L_: lane "key" "" return 1; } // return self.metatable[key] @@ -316,7 +316,7 @@ static int thread_index_string(lua_State* L_) lua_rawget(L_, -2); // L_: mt value // only "cancel" and "join" are registered as functions, any other string will raise an error if (!lua_iscfunction(L_, -1)) { - raise_luaL_error(L_, "can't index a lane with '%s'", _keystr); + raise_luaL_error(L_, "can't index a lane with '%s'", _keystr.data()); } return 1; } @@ -833,9 +833,9 @@ void Lane::changeDebugName(int nameIdx_) // / "error" finished at an error, error value is there // / "cancelled" execution cancelled by M (state gone) // -[[nodiscard]] char const* Lane::errorTraceLevelString() const +[[nodiscard]] std::string_view Lane::errorTraceLevelString() const { - char const* const _str{ + std::string_view const _str{ (errorTraceLevel == ErrorTraceLevel::Minimal) ? "minimal" : (errorTraceLevel == ErrorTraceLevel::Basic) ? "basic" : (errorTraceLevel == ErrorTraceLevel::Extended) ? "extended" : @@ -890,22 +890,22 @@ void Lane::PushMetatable(lua_State* L_) // ################################################################################################# -void Lane::pushThreadStatus(lua_State* L_) const +[[nodiscard]] std::string_view Lane::pushThreadStatus(lua_State* L_) const { - char const* const _str{ threadStatusString() }; - LUA_ASSERT(L_, _str); + std::string_view const _str{ threadStatusString() }; + LUA_ASSERT(L_, !_str.empty()); - lua_pushstring(L_, _str); + return lua_pushstringview(L_, _str); } // ################################################################################################# -void Lane::pushErrorTraceLevel(lua_State* L_) const +[[nodiscard]] std::string_view Lane::pushErrorTraceLevel(lua_State* L_) const { - char const* const _str{ errorTraceLevelString() }; - LUA_ASSERT(L_, _str); + std::string_view const _str{ errorTraceLevelString() }; + LUA_ASSERT(L_, !_str.empty()); - lua_pushstring(L_, _str); + return lua_pushstringview(L_, _str); } // ################################################################################################# @@ -948,9 +948,9 @@ void Lane::startThread(int priority_) // / "error" finished at an error, error value is there // / "cancelled" execution cancelled by M (state gone) // -[[nodiscard]] char const* Lane::threadStatusString() const +[[nodiscard]] std::string_view Lane::threadStatusString() const { - char const* const _str{ + std::string_view const _str{ (status == Lane::Pending) ? "pending" : (status == Lane::Running) ? "running" : // like in 'co.status()' (status == Lane::Waiting) ? "waiting" : diff --git a/src/lane.h b/src/lane.h index 10045c8..3c1ab58 100644 --- a/src/lane.h +++ b/src/lane.h @@ -8,6 +8,7 @@ #include #include #include +#include #include // ################################################################################################# @@ -127,14 +128,14 @@ class Lane void changeDebugName(int nameIdx_); void close() { lua_State* _L{ L }; L = nullptr; lua_close(_L); } - [[nodiscard]] char const* errorTraceLevelString() const; + [[nodiscard]] std::string_view errorTraceLevelString() const; [[nodiscard]] int pushErrorHandler() const; - void pushErrorTraceLevel(lua_State* L_) const; + [[nodiscard]] std::string_view pushErrorTraceLevel(lua_State* L_) const; static void PushMetatable(lua_State* L_); - void pushThreadStatus(lua_State* L_) const; + [[nodiscard]] std::string_view pushThreadStatus(lua_State* L_) const; void securizeDebugName(lua_State* L_); void startThread(int priority_); - [[nodiscard]] char const* threadStatusString() const; + [[nodiscard]] std::string_view threadStatusString() const; [[nodiscard]] bool waitForCompletion(std::chrono::time_point until_); }; diff --git a/src/tracker.cpp b/src/tracker.cpp index 3040154..69cd90c 100644 --- a/src/tracker.cpp +++ b/src/tracker.cpp @@ -96,7 +96,7 @@ void LaneTracker::tracking_add(Lane* lane_) lua_createtable(L_, 0, 2); // L_: {} {} std::ignore = lua_pushstringview(L_, _lane->debugName); // L_: {} {} "name" lua_setfield(L_, -2, "name"); // L_: {} {} - _lane->pushThreadStatus(L_); // L_: {} {} "status" + std::ignore = _lane->pushThreadStatus(L_); // L_: {} {} "status" lua_setfield(L_, -2, "status"); // L_: {} {} lua_rawseti(L_, -2, ++_index); // L_: {} _lane = _lane->tracking_next; -- cgit v1.2.3-55-g6feb