Skip to content

Commit

Permalink
Revert "[core] replace strcmp() with StringMatch() (openthread#10629
Browse files Browse the repository at this point in the history
)"

This reverts commit 1c5ad34.
  • Loading branch information
lmnotran committed Aug 29, 2024
1 parent 706013f commit 2adb550
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 46 deletions.
5 changes: 2 additions & 3 deletions src/core/coap/coap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/core/common/heap_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 0 additions & 5 deletions src/core/common/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 1 addition & 13 deletions src/core/common/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
Expand Down
39 changes: 19 additions & 20 deletions src/core/diags/factory_diags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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<Radio>().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<Radio>().Receive(mChannel));
SuccessOrExit(error = Get<Radio>().SetTransmitPower(mTxPower));
Expand All @@ -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<Radio>().GetState();

Expand Down Expand Up @@ -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));
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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));
}
Expand All @@ -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<uint32_t>(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<uint32_t>(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<uint32_t>(value);
Expand All @@ -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));
}
Expand Down Expand Up @@ -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--;
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/core/net/srp_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

//---------------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions src/core/radio/trel_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/core/utils/parse_cmdline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint16_t>(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[])
{
Expand Down

0 comments on commit 2adb550

Please sign in to comment.