From f3280df3bf08f37e9dd68e31573fc171d4c4b507 Mon Sep 17 00:00:00 2001 From: i7-8700 <1281261856@qq.com> Date: Fri, 18 Sep 2020 23:48:46 +0800 Subject: [PATCH] str_util, starts_with & ends_with --- jlib/net/simple_libevent_server.cpp | 7 +++--- jlib/util/str_util.h | 38 +++++++++++++++++++++++++++++ test/test_strutil/test_strutil.cpp | 30 +++++++++++++++++++++++ 3 files changed, 72 insertions(+), 3 deletions(-) diff --git a/jlib/net/simple_libevent_server.cpp b/jlib/net/simple_libevent_server.cpp index e9f2029..92be4ce 100644 --- a/jlib/net/simple_libevent_server.cpp +++ b/jlib/net/simple_libevent_server.cpp @@ -231,9 +231,10 @@ struct simple_libevent_server::PrivateImpl static void accpet_error_cb(evconnlistener* listener, void* context) { + auto server = (simple_libevent_server*)context; auto base = evconnlistener_get_base(listener); int err = EVUTIL_SOCKET_ERROR(); - JLOG_CRTC("accpet_error_cb:{}:{}", err, evutil_socket_error_to_string(err)); + JLOG_CRTC("{} accpet_error_cb:{}:{}", server->name_, err, evutil_socket_error_to_string(err)); event_base_loopexit(base, nullptr); } @@ -255,7 +256,7 @@ struct simple_libevent_server::PrivateImpl event_add((event*)((BaseClientPrivateData*)client->privateData)->timer, &server->impl->tv); } } else { - JLOG_CRTC("timercb cannot find client by fd #{}", (int)fd); + JLOG_CRTC("{} timercb cannot find client by fd #{}", server->name_, (int)fd); } } @@ -270,7 +271,7 @@ struct simple_libevent_server::PrivateImpl auto bev = bufferevent_socket_new(ctx->base, fd, BEV_OPT_CLOSE_ON_FREE); if (!bev) { - JLOG_CRTC("Error constructing bufferevent!"); + JLOG_CRTC("{} Error constructing bufferevent!", server->name_); exit(-1); } diff --git a/jlib/util/str_util.h b/jlib/util/str_util.h index db2b664..5ce8207 100644 --- a/jlib/util/str_util.h +++ b/jlib/util/str_util.h @@ -122,4 +122,42 @@ inline std::wstring to_lower_copy(std::wstring str) { return str; } + +/**************************** starts_with *************************/ + +inline bool starts_with(const std::string& str, const std::string& sub) { + if (str.size() < sub.size()) { return false; } + for (size_t i = 0; i < sub.size(); i++) { if (str[i] != sub[i]) { return false; } } + return true; +} + +inline bool starts_with(const std::wstring& str, const std::wstring& sub) { + if (str.size() < sub.size()) { return false; } + for (size_t i = 0; i < sub.size(); i++) { if (str[i] != sub[i]) { return false; } } + return true; +} + +/**************************** ends_with ***************************/ + +inline bool ends_with(const std::string& str, const std::string& sub) { + if (str.size() < sub.size()) { return false; } + for (size_t i = 0; i < sub.size(); i++) { + if (str[str.size() - i - 1] != sub[sub.size() - i - 1]) { + return false; + } + } + return true; +} + +inline bool ends_with(const std::wstring& str, const std::wstring& sub) { + if (str.size() < sub.size()) { return false; } + for (size_t i = 0; i < sub.size(); i++) { + if (str[str.size() - i - 1] != sub[sub.size() - i - 1]) { + return false; + } + } + return true; +} + + } // namespace jlib diff --git a/test/test_strutil/test_strutil.cpp b/test/test_strutil/test_strutil.cpp index 52245d3..d90233c 100644 --- a/test/test_strutil/test_strutil.cpp +++ b/test/test_strutil/test_strutil.cpp @@ -74,4 +74,34 @@ int main() to_lower(wstr); assert(wstr == L"abc"); } + + // starts_with + { + std::string str = "abc123"; + assert(starts_with(str, "abc123")); + assert(starts_with(str, "abc")); + assert(!starts_with(str, "abc1234")); + assert(!starts_with(str, "bc")); + + std::wstring wstr = L"abc123"; + assert(starts_with(wstr, L"abc123")); + assert(starts_with(wstr, L"abc")); + assert(!starts_with(wstr, L"abc1234")); + assert(!starts_with(wstr, L"bc")); + } + + // ends_with + { + std::string str = "abc123"; + assert(ends_with(str, "abc123")); + assert(ends_with(str, "123")); + assert(!ends_with(str, "0abc123")); + assert(!ends_with(str, "abc13")); + + std::wstring wstr = L"abc123"; + assert(ends_with(wstr, L"abc123")); + assert(ends_with(wstr, L"123")); + assert(!ends_with(wstr, L"0abc123")); + assert(!ends_with(wstr, L"abc13")); + } } \ No newline at end of file -- 2.18.1