-
Notifications
You must be signed in to change notification settings - Fork 312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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 |
src/shell/commands/detect_hotkey.cpp
Outdated
@@ -32,21 +33,21 @@ bool generate_hotkey_request(dsn::replication::detect_hotkey_request &req, | |||
int partition_index, | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
.
src/utils/test/utils.cpp
Outdated
}; | ||
|
||
for (const auto &test : tests) { | ||
EXPECT_EQ(test.is_equal, dsn::utils::equals(test.lhs, test.rhs)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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::iequals
std::string
and another is of C string, to compare case-sensitive equality we can also useoperator==
std::string
and another is of C string, to compare case-insensitive equality we can usestrcasecmp
strcmp
strcasecmp
strncmp
,strncasecmp
,memcmp
However, the problem of
strcmp
,strcmp
,strcasecmp
strncmp
,strncasecmp
andmemcmp
is 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: