Skip to content

Commit

Permalink
Add operator == for acl::string for comparing "ok" == acl::string.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengshuxin committed Dec 8, 2023
1 parent 8d175b2 commit cad5d4c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib_acl_cpp/include/acl_cpp/stdlib/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,13 @@ class ACL_CPP_API string {
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;
Expand Down
14 changes: 14 additions & 0 deletions lib_acl_cpp/samples/string/string1/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down
10 changes: 10 additions & 0 deletions lib_acl_cpp/src/stdlib/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit cad5d4c

Please sign in to comment.