From 2adb5509a30bd09ac232b917ad77f25699b910d8 Mon Sep 17 00:00:00 2001 From: Mason Tran Date: Thu, 29 Aug 2024 08:52:22 -0500 Subject: [PATCH] Revert "[core] replace `strcmp()` with `StringMatch()` (#10629)" This reverts commit 1c5ad3403d624c0e57b7a6462aa82d67bf46c690. --- src/core/coap/coap.cpp | 5 ++-- src/core/common/heap_string.cpp | 2 +- src/core/common/string.cpp | 5 ---- src/core/common/string.hpp | 14 +---------- src/core/diags/factory_diags.cpp | 39 +++++++++++++++---------------- src/core/net/srp_client.cpp | 2 +- src/core/radio/trel_interface.cpp | 4 ++-- src/core/utils/parse_cmdline.cpp | 2 +- 8 files changed, 27 insertions(+), 46 deletions(-) diff --git a/src/core/coap/coap.cpp b/src/core/coap/coap.cpp index 177d2bd6e08..33369a32734 100644 --- a/src/core/coap/coap.cpp +++ b/src/core/coap/coap.cpp @@ -35,7 +35,6 @@ #include "common/locator_getters.hpp" #include "common/log.hpp" #include "common/random.hpp" -#include "common/string.hpp" #include "instance/instance.hpp" #include "net/ip6.hpp" #include "net/udp6.hpp" @@ -1361,7 +1360,7 @@ void CoapBase::ProcessReceivedRequest(Message &aMessage, const Ip6::MessageInfo for (const ResourceBlockWise &resource : mBlockWiseResources) { - if (!StringMatch(resource.GetUriPath(), uriPath)) + if (strcmp(resource.GetUriPath(), uriPath) != 0) { continue; } @@ -1428,7 +1427,7 @@ void CoapBase::ProcessReceivedRequest(Message &aMessage, const Ip6::MessageInfo for (const Resource &resource : mResources) { - if (StringMatch(resource.mUriPath, uriPath)) + if (strcmp(resource.mUriPath, uriPath) == 0) { resource.HandleRequest(aMessage, aMessageInfo); error = kErrorNone; diff --git a/src/core/common/heap_string.cpp b/src/core/common/heap_string.cpp index b286b32686c..060bfa2d23e 100644 --- a/src/core/common/heap_string.cpp +++ b/src/core/common/heap_string.cpp @@ -89,7 +89,7 @@ bool String::operator==(const char *aCString) const bool isEqual; VerifyOrExit((aCString != nullptr) && (mStringBuffer != nullptr), isEqual = (mStringBuffer == aCString)); - isEqual = StringMatch(mStringBuffer, aCString); + isEqual = (strcmp(mStringBuffer, aCString) == 0); exit: return isEqual; diff --git a/src/core/common/string.cpp b/src/core/common/string.cpp index b7f11de020b..6117f67ebf7 100644 --- a/src/core/common/string.cpp +++ b/src/core/common/string.cpp @@ -158,11 +158,6 @@ bool StringEndsWith(const char *aString, const char *aSubString, StringMatchMode return (subLen > 0) && (len >= subLen) && (Match(&aString[len - subLen], aSubString, aMode) != kNoMatch); } -bool StringMatch(const char *aFirstString, const char *aSecondString) -{ - return Match(aFirstString, aSecondString, kStringExactMatch) == kFullMatch; -} - bool StringMatch(const char *aFirstString, const char *aSecondString, StringMatchMode aMode) { return Match(aFirstString, aSecondString, aMode) == kFullMatch; diff --git a/src/core/common/string.hpp b/src/core/common/string.hpp index 360be44b515..00f903c3db1 100644 --- a/src/core/common/string.hpp +++ b/src/core/common/string.hpp @@ -153,18 +153,6 @@ bool StringEndsWith(const char *aString, char aChar); */ bool StringEndsWith(const char *aString, const char *aSubString, StringMatchMode aMode = kStringExactMatch); -/** - * Checks whether or not two null-terminated strings match exactly. - * - * @param[in] aFirstString A pointer to the first string. - * @param[in] aSecondString A pointer to the second string. - * - * @retval TRUE If @p aFirstString matches @p aSecondString. - * @retval FALSE If @p aFirstString does not match @p aSecondString. - * - */ -bool StringMatch(const char *aFirstString, const char *aSecondString); - /** * Checks whether or not two null-terminated strings match. * @@ -176,7 +164,7 @@ bool StringMatch(const char *aFirstString, const char *aSecondString); * @retval FALSE If @p aFirstString does not match @p aSecondString using match mode @p aMode. * */ -bool StringMatch(const char *aFirstString, const char *aSecondString, StringMatchMode aMode); +bool StringMatch(const char *aFirstString, const char *aSecondString, StringMatchMode aMode = kStringExactMatch); /** * Copies a string into a given target buffer with a given size if it fits. diff --git a/src/core/diags/factory_diags.cpp b/src/core/diags/factory_diags.cpp index b0691e10da9..3aa5b2f2b4b 100644 --- a/src/core/diags/factory_diags.cpp +++ b/src/core/diags/factory_diags.cpp @@ -46,7 +46,6 @@ #include "common/as_core_type.hpp" #include "common/code_utils.hpp" #include "common/locator_getters.hpp" -#include "common/string.hpp" #include "instance/instance.hpp" #include "radio/radio.hpp" #include "utils/parse_cmdline.hpp" @@ -127,7 +126,7 @@ Error Diags::ProcessEcho(uint8_t aArgsLength, char *aArgs[]) { Output("%s\r\n", aArgs[0]); } - else if ((aArgsLength == 2) && StringMatch(aArgs[0], "-n")) + else if ((aArgsLength == 2) && (strcmp(aArgs[0], "-n") == 0)) { static constexpr uint8_t kReservedLen = 1; // 1 byte '\0' static constexpr uint16_t kOutputLen = OPENTHREAD_CONFIG_DIAG_OUTPUT_BUFFER_SIZE; @@ -328,7 +327,7 @@ Error Diags::ProcessRepeat(uint8_t aArgsLength, char *aArgs[]) VerifyOrExit(otPlatDiagModeGet(), error = kErrorInvalidState); VerifyOrExit(aArgsLength > 0, error = kErrorInvalidArgs); - if (StringMatch(aArgs[0], "stop")) + if (strcmp(aArgs[0], "stop") == 0) { otPlatAlarmMilliStop(&GetInstance()); mRepeatActive = false; @@ -445,7 +444,7 @@ Error Diags::ProcessStats(uint8_t aArgsLength, char *aArgs[]) VerifyOrExit(otPlatDiagModeGet(), error = kErrorInvalidState); - if ((aArgsLength == 1) && StringMatch(aArgs[0], "clear")) + if ((aArgsLength == 1) && (strcmp(aArgs[0], "clear") == 0)) { mStats.Clear(); Output("stats cleared\r\n"); @@ -518,12 +517,12 @@ Error Diags::ProcessRadio(uint8_t aArgsLength, char *aArgs[]) VerifyOrExit(otPlatDiagModeGet(), error = kErrorInvalidState); VerifyOrExit(aArgsLength > 0, error = kErrorInvalidArgs); - if (StringMatch(aArgs[0], "sleep")) + if (strcmp(aArgs[0], "sleep") == 0) { SuccessOrExit(error = Get().Sleep()); Output("set radio from receive to sleep \r\nstatus 0x%02x\r\n", error); } - else if (StringMatch(aArgs[0], "receive")) + else if (strcmp(aArgs[0], "receive") == 0) { SuccessOrExit(error = Get().Receive(mChannel)); SuccessOrExit(error = Get().SetTransmitPower(mTxPower)); @@ -532,7 +531,7 @@ Error Diags::ProcessRadio(uint8_t aArgsLength, char *aArgs[]) Output("set radio from sleep to receive on channel %d\r\nstatus 0x%02x\r\n", mChannel, error); } - else if (StringMatch(aArgs[0], "state")) + else if (strcmp(aArgs[0], "state") == 0) { otRadioState state = Get().GetState(); @@ -639,11 +638,11 @@ Error Diags::ProcessContinuousWave(uint8_t aArgsLength, char *aArgs[]) VerifyOrExit(otPlatDiagModeGet(), error = kErrorInvalidState); VerifyOrExit(aArgsLength > 0, error = kErrorInvalidArgs); - if (StringMatch(aArgs[0], "start")) + if (strcmp(aArgs[0], "start") == 0) { SuccessOrExit(error = otPlatDiagRadioTransmitCarrier(&GetInstance(), true)); } - else if (StringMatch(aArgs[0], "stop")) + else if (strcmp(aArgs[0], "stop") == 0) { SuccessOrExit(error = otPlatDiagRadioTransmitCarrier(&GetInstance(), false)); } @@ -660,11 +659,11 @@ Error Diags::ProcessStream(uint8_t aArgsLength, char *aArgs[]) VerifyOrExit(otPlatDiagModeGet(), error = kErrorInvalidState); VerifyOrExit(aArgsLength > 0, error = kErrorInvalidArgs); - if (StringMatch(aArgs[0], "start")) + if (strcmp(aArgs[0], "start") == 0) { error = otPlatDiagRadioTransmitStream(&GetInstance(), true); } - else if (StringMatch(aArgs[0], "stop")) + else if (strcmp(aArgs[0], "stop") == 0) { error = otPlatDiagRadioTransmitStream(&GetInstance(), false); } @@ -754,11 +753,11 @@ Error Diags::ProcessRawPowerSetting(uint8_t aArgsLength, char *aArgs[]) SuccessOrExit(error = GetRawPowerSetting(setting)); Output("%s\r\n", setting.ToString().AsCString()); } - else if (StringMatch(aArgs[0], "enable")) + else if (strcmp(aArgs[0], "enable") == 0) { SuccessOrExit(error = otPlatDiagRadioRawPowerSettingEnable(&GetInstance(), true)); } - else if (StringMatch(aArgs[0], "disable")) + else if (strcmp(aArgs[0], "disable") == 0) { SuccessOrExit(error = otPlatDiagRadioRawPowerSettingEnable(&GetInstance(), false)); } @@ -782,21 +781,21 @@ Error Diags::ProcessGpio(uint8_t aArgsLength, char *aArgs[]) bool level; otGpioMode mode; - if ((aArgsLength == 2) && StringMatch(aArgs[0], "get")) + if ((aArgsLength == 2) && (strcmp(aArgs[0], "get") == 0)) { SuccessOrExit(error = ParseLong(aArgs[1], value)); gpio = static_cast(value); SuccessOrExit(error = otPlatDiagGpioGet(gpio, &level)); Output("%d\r\n", level); } - else if ((aArgsLength == 3) && StringMatch(aArgs[0], "set")) + else if ((aArgsLength == 3) && (strcmp(aArgs[0], "set") == 0)) { SuccessOrExit(error = ParseLong(aArgs[1], value)); gpio = static_cast(value); SuccessOrExit(error = ParseBool(aArgs[2], level)); SuccessOrExit(error = otPlatDiagGpioSet(gpio, level)); } - else if ((aArgsLength >= 2) && StringMatch(aArgs[0], "mode")) + else if ((aArgsLength >= 2) && (strcmp(aArgs[0], "mode") == 0)) { SuccessOrExit(error = ParseLong(aArgs[1], value)); gpio = static_cast(value); @@ -813,11 +812,11 @@ Error Diags::ProcessGpio(uint8_t aArgsLength, char *aArgs[]) Output("out\r\n"); } } - else if ((aArgsLength == 3) && StringMatch(aArgs[2], "in")) + else if ((aArgsLength == 3) && (strcmp(aArgs[2], "in") == 0)) { SuccessOrExit(error = otPlatDiagGpioSetMode(gpio, OT_GPIO_MODE_INPUT)); } - else if ((aArgsLength == 3) && StringMatch(aArgs[2], "out")) + else if ((aArgsLength == 3) && (strcmp(aArgs[2], "out") == 0)) { SuccessOrExit(error = otPlatDiagGpioSetMode(gpio, OT_GPIO_MODE_OUTPUT)); } @@ -914,7 +913,7 @@ Error Diags::ProcessCmd(uint8_t aArgsLength, char *aArgs[]) // This `rcp` command is for debugging and testing only, building only when NDEBUG is not defined // so that it will be excluded from release build. #if OPENTHREAD_RADIO && !defined(NDEBUG) - if (aArgsLength > 0 && StringMatch(aArgs[0], "rcp")) + if (aArgsLength > 0 && !strcmp(aArgs[0], "rcp")) { aArgs++; aArgsLength--; @@ -929,7 +928,7 @@ Error Diags::ProcessCmd(uint8_t aArgsLength, char *aArgs[]) for (const Command &command : sCommands) { - if (StringMatch(aArgs[0], command.mName)) + if (strcmp(aArgs[0], command.mName) == 0) { error = (this->*command.mCommand)(aArgsLength - 1, (aArgsLength > 1) ? &aArgs[1] : nullptr); ExitNow(); diff --git a/src/core/net/srp_client.cpp b/src/core/net/srp_client.cpp index c75500c9e88..0b4c66e3246 100644 --- a/src/core/net/srp_client.cpp +++ b/src/core/net/srp_client.cpp @@ -172,7 +172,7 @@ bool Client::Service::Matches(const Service &aOther) const // for use by `LinkedList::FindMatching()` to search within the // `mServices` list. - return StringMatch(GetName(), aOther.GetName()) && StringMatch(GetInstanceName(), aOther.GetInstanceName()); + return (strcmp(GetName(), aOther.GetName()) == 0) && (strcmp(GetInstanceName(), aOther.GetInstanceName()) == 0); } //--------------------------------------------------------------------- diff --git a/src/core/radio/trel_interface.cpp b/src/core/radio/trel_interface.cpp index 5441f0c37d9..dc94b45115a 100644 --- a/src/core/radio/trel_interface.cpp +++ b/src/core/radio/trel_interface.cpp @@ -271,14 +271,14 @@ Error Interface::ParsePeerInfoTxtData(const Peer::Info &aInfo, continue; } - if (StringMatch(entry.mKey, kTxtRecordExtAddressKey)) + if (strcmp(entry.mKey, kTxtRecordExtAddressKey) == 0) { VerifyOrExit(!parsedExtAddress, error = kErrorParse); VerifyOrExit(entry.mValueLength == sizeof(Mac::ExtAddress), error = kErrorParse); aExtAddress.Set(entry.mValue); parsedExtAddress = true; } - else if (StringMatch(entry.mKey, kTxtRecordExtPanIdKey)) + else if (strcmp(entry.mKey, kTxtRecordExtPanIdKey) == 0) { VerifyOrExit(!parsedExtPanId, error = kErrorParse); VerifyOrExit(entry.mValueLength == sizeof(MeshCoP::ExtendedPanId), error = kErrorParse); diff --git a/src/core/utils/parse_cmdline.cpp b/src/core/utils/parse_cmdline.cpp index c05a0d621ef..00b96047bcc 100644 --- a/src/core/utils/parse_cmdline.cpp +++ b/src/core/utils/parse_cmdline.cpp @@ -317,7 +317,7 @@ Error ParseAsHexStringSegment(const char *&aString, uint16_t &aSize, uint8_t *aB uint16_t Arg::GetLength(void) const { return IsEmpty() ? 0 : static_cast(strlen(mString)); } -bool Arg::operator==(const char *aString) const { return !IsEmpty() && StringMatch(mString, aString); } +bool Arg::operator==(const char *aString) const { return !IsEmpty() && (strcmp(mString, aString) == 0); } void Arg::CopyArgsToStringArray(Arg aArgs[], char *aStrings[]) {