Repository navigation
feat: decide whether two C strings are equal and support CHECK_STREQ* and CHECK_STRNE* - #1300
Conversation
… and CHECK_STRNE*
… and CHECK_STRNE*
… and CHECK_STRNE*
… and CHECK_STRNE*
… and CHECK_STRNE*
… and CHECK_STRNE*
… and CHECK_STRNE*
|
How about to replace |
OK, I think I can replace with a new function |
| std::string &err_info) | ||
| { | ||
| if (!strcasecmp(hotkey_type.c_str(), "read")) { | ||
| if (dsn::utils::iequals(hotkey_type.c_str(), "read")) { |
There was a problem hiding this comment.
how about define the parameter as string_view? so you it's not needed to convet to C string.
There was a problem hiding this comment.
Actually string_view is used not only for the string of the printable characters, but also binary bytes. Once string_view is used as the type of the parameters, we have to reimplement comparison ignoring cases based on string_view where binary bytes such as '\0' should be skipped. Therefore here we just declare other functions one parameter of which is declared as std::string. Comparing two std::string-typed strings we can use boost::iequals.
| }; | ||
|
|
||
| for (const auto &test : tests) { | ||
| EXPECT_EQ(test.is_equal, dsn::utils::equals(test.lhs, test.rhs)); |
There was a problem hiding this comment.
This style of unit test is wildly used, but it has a shortcoming, when some case failed, it's hard to find out which one failed, right?
There was a problem hiding this comment.
Yes, once a case has failed it is really hard to look out it. I'll use TEST_P to refactor this.
… and CHECK_STRNE*
This PR is to resolve #1299.
There are several types of comparisons for equality between two strings:
std::string, to compare case-sensitive equality, we can useoperator==std::string, to compare case-insensitive equality, we can useboost::iequalsstd::stringand another is of C string, to compare case-sensitive equality we can also useoperator==std::stringand another is of C string, to compare case-insensitive equality we can usestrcasecmpstrcmpstrcasecmpstrncmp,strncasecmp,memcmpHowever, the problem of
strcmp,strcmp,strcasecmpstrncmp,strncasecmpandmemcmpis that once the compared strings is NULL pointer, these functions may lead to undefined behavior. Thus in this PR we will encapsulate the following functions to solve this problem:This PR will also implement the following macros to help check the equality between C strings and true or false: