From cad5d4c8d7ad73a8820aa9e7b5fb82732431abe6 Mon Sep 17 00:00:00 2001 From: zhengshuxin Date: Fri, 8 Dec 2023 21:37:42 +0800 Subject: [PATCH] Add operator == for acl::string for comparing "ok" == acl::string. --- lib_acl_cpp/include/acl_cpp/stdlib/string.hpp | 7 +++++++ lib_acl_cpp/samples/string/string1/main.cpp | 14 ++++++++++++++ lib_acl_cpp/src/stdlib/string.cpp | 10 ++++++++++ 3 files changed, 31 insertions(+) diff --git a/lib_acl_cpp/include/acl_cpp/stdlib/string.hpp b/lib_acl_cpp/include/acl_cpp/stdlib/string.hpp index e837ca8a2..5432f88b7 100644 --- a/lib_acl_cpp/include/acl_cpp/stdlib/string.hpp +++ b/lib_acl_cpp/include/acl_cpp/stdlib/string.hpp @@ -1319,6 +1319,13 @@ private: void init(size_t len); }; +/** + * string s = "ok"; + * printf("first: %s\r\n", "ok" == s ? "true" : "false"); + */ +bool operator==(const string* s,const string& str); +bool operator==(const char* s,const string& str); + /** * 模板函数,可用在以下场景: * string s1, s2; diff --git a/lib_acl_cpp/samples/string/string1/main.cpp b/lib_acl_cpp/samples/string/string1/main.cpp index 53e8b3c7a..622dfb8d2 100644 --- a/lib_acl_cpp/samples/string/string1/main.cpp +++ b/lib_acl_cpp/samples/string/string1/main.cpp @@ -271,6 +271,17 @@ static void test7(void) ACL_METER_TIME(">> end ACL_VSTRING: acl_vstring_sprintf"); } +static void test8(void) +{ + acl::string s = "ok"; + acl::string s1 = "ok", *sp = &s1; + + + printf("s==\"ok\" : %s, \"ok\"==s : %s\r\n", + s == "ok" ? "yes" : "no", "ok" == s ? "yes" : "no"); + printf("sp=s : %s\r\n", sp == s ? "yes" : "no"); +} + ////////////////////////////////////////////////////////////////////////// static void test_main(void) @@ -429,6 +440,9 @@ int main(void) { printf("%s\r\n", (*cit).c_str()); } + + test8(); + #ifdef WIN32 printf("enter any key to exit\r\n"); getchar(); diff --git a/lib_acl_cpp/src/stdlib/string.cpp b/lib_acl_cpp/src/stdlib/string.cpp index b136ef80c..70e0b6648 100644 --- a/lib_acl_cpp/src/stdlib/string.cpp +++ b/lib_acl_cpp/src/stdlib/string.cpp @@ -1952,4 +1952,14 @@ string& string::parse_int64(acl_uint64 n) return s; } +bool operator==(const string* s,const string& str) +{ + return (str == s); +} + +bool operator==(const char* s,const string& str) +{ + return (str == s); +} + } // namespace acl