Add operator == for acl::string for comparing "ok" == acl::string.

This commit is contained in:
zhengshuxin 2023-12-08 21:37:42 +08:00
parent 8d175b2f02
commit cad5d4c8d7
3 changed files with 31 additions and 0 deletions

View File

@ -1319,6 +1319,13 @@ private:
void init(size_t len); 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; * string s1, s2;

View File

@ -271,6 +271,17 @@ static void test7(void)
ACL_METER_TIME(">> end ACL_VSTRING: acl_vstring_sprintf"); 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) static void test_main(void)
@ -429,6 +440,9 @@ int main(void)
{ {
printf("%s\r\n", (*cit).c_str()); printf("%s\r\n", (*cit).c_str());
} }
test8();
#ifdef WIN32 #ifdef WIN32
printf("enter any key to exit\r\n"); printf("enter any key to exit\r\n");
getchar(); getchar();

View File

@ -1952,4 +1952,14 @@ string& string::parse_int64(acl_uint64 n)
return s; 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 } // namespace acl