diff --git a/src/core/common/string.hpp b/src/core/common/string.hpp index 4b355038b..623804416 100644 --- a/src/core/common/string.hpp +++ b/src/core/common/string.hpp @@ -103,6 +103,12 @@ template class String static_assert(kSize > 0, "String buffer cannot be empty."); public: + /** + * This method clears the string (sets it to empty). + * + */ + void Clear(void) { mBuffer[0] = '\0'; } + /** * This method returns the string as a null-terminated C string. * diff --git a/src/core/mac/mac_frame.cpp b/src/core/mac/mac_frame.cpp index 8c674e04d..0e58cab25 100644 --- a/src/core/mac/mac_frame.cpp +++ b/src/core/mac/mac_frame.cpp @@ -1447,14 +1447,15 @@ Frame::InfoString Frame::ToInfoString(void) const BeaconPayload::InfoString BeaconPayload::ToInfoString(void) const { - NetworkName name; - InfoString string; + NetworkName name; + InfoString string; + StringWriter writer(string); IgnoreError(name.Set(GetNetworkName())); - StringWriter(string).Append("name:%s, xpanid:%s, id:%d, ver:%d, joinable:%s, native:%s", name.GetAsCString(), - mExtendedPanId.ToString().AsCString(), GetProtocolId(), GetProtocolVersion(), - IsJoiningPermitted() ? "yes" : "no", IsNative() ? "yes" : "no"); + writer.Append("name:%s, xpanid:%s, id:%d, ver:%d, joinable:%s, native:%s", name.GetAsCString(), + mExtendedPanId.ToString().AsCString(), GetProtocolId(), GetProtocolVersion(), + IsJoiningPermitted() ? "yes" : "no", IsNative() ? "yes" : "no"); return string; } diff --git a/src/core/mac/mac_types.cpp b/src/core/mac/mac_types.cpp index 8349bba72..df9fb1355 100644 --- a/src/core/mac/mac_types.cpp +++ b/src/core/mac/mac_types.cpp @@ -65,9 +65,10 @@ void ExtAddress::GenerateRandom(void) ExtAddress::InfoString ExtAddress::ToString(void) const { - InfoString string; + InfoString string; + StringWriter writer(string); - StringWriter(string).AppendHexBytes(m8, sizeof(ExtAddress)); + writer.AppendHexBytes(m8, sizeof(ExtAddress)); return string; } @@ -92,19 +93,20 @@ void ExtAddress::CopyAddress(uint8_t *aDst, const uint8_t *aSrc, CopyByteOrder a Address::InfoString Address::ToString(void) const { - InfoString string; + InfoString string; + StringWriter writer(string); if (mType == kTypeExtended) { - string = GetExtended().ToString(); + writer.AppendHexBytes(GetExtended().m8, sizeof(ExtAddress)); } else if (mType == kTypeNone) { - StringWriter(string).Append("None"); + writer.Append("None"); } else { - StringWriter(string).Append("0x%04x", GetShort()); + writer.Append("0x%04x", GetShort()); } return string; @@ -112,9 +114,10 @@ Address::InfoString Address::ToString(void) const ExtendedPanId::InfoString ExtendedPanId::ToString(void) const { - InfoString string; + InfoString string; + StringWriter writer(string); - StringWriter(string).AppendHexBytes(m8, sizeof(ExtendedPanId)); + writer.AppendHexBytes(m8, sizeof(ExtendedPanId)); return string; } diff --git a/src/core/net/ip4_address.cpp b/src/core/net/ip4_address.cpp index 3e2ceda5a..5ff557e94 100644 --- a/src/core/net/ip4_address.cpp +++ b/src/core/net/ip4_address.cpp @@ -88,9 +88,11 @@ exit: Address::InfoString Address::ToString(void) const { - InfoString string; + InfoString string; + StringWriter writer(string); + + writer.Append("%d.%d.%d.%d", mBytes[0], mBytes[1], mBytes[2], mBytes[3]); - StringWriter(string).Append("%d.%d.%d.%d", mBytes[0], mBytes[1], mBytes[2], mBytes[3]); return string; } diff --git a/src/core/net/ip6_address.cpp b/src/core/net/ip6_address.cpp index e5b2b0471..57c7c3095 100644 --- a/src/core/net/ip6_address.cpp +++ b/src/core/net/ip6_address.cpp @@ -247,9 +247,10 @@ bool InterfaceIdentifier::IsAnycastServiceLocator(void) const InterfaceIdentifier::InfoString InterfaceIdentifier::ToString(void) const { - InfoString string; + InfoString string; + StringWriter writer(string); - StringWriter(string).AppendHexBytes(mFields.m8, kSize); + writer.AppendHexBytes(mFields.m8, kSize); return string; } @@ -630,11 +631,12 @@ exit: Address::InfoString Address::ToString(void) const { - InfoString string; + InfoString string; + StringWriter writer(string); - StringWriter(string).Append("%x:%x:%x:%x:%x:%x:%x:%x", HostSwap16(mFields.m16[0]), HostSwap16(mFields.m16[1]), - HostSwap16(mFields.m16[2]), HostSwap16(mFields.m16[3]), HostSwap16(mFields.m16[4]), - HostSwap16(mFields.m16[5]), HostSwap16(mFields.m16[6]), HostSwap16(mFields.m16[7])); + writer.Append("%x:%x:%x:%x:%x:%x:%x:%x", HostSwap16(mFields.m16[0]), HostSwap16(mFields.m16[1]), + HostSwap16(mFields.m16[2]), HostSwap16(mFields.m16[3]), HostSwap16(mFields.m16[4]), + HostSwap16(mFields.m16[5]), HostSwap16(mFields.m16[6]), HostSwap16(mFields.m16[7])); return string; } diff --git a/src/core/net/socket.cpp b/src/core/net/socket.cpp index 5631c0a06..31064c0e7 100644 --- a/src/core/net/socket.cpp +++ b/src/core/net/socket.cpp @@ -38,9 +38,11 @@ namespace Ip6 { SockAddr::InfoString SockAddr::ToString(void) const { - InfoString string; + InfoString string; + StringWriter writer(string); + + writer.Append("[%s]:%u", GetAddress().ToString().AsCString(), GetPort()); - StringWriter(string).Append("[%s]:%u", GetAddress().ToString().AsCString(), GetPort()); return string; } diff --git a/src/core/thread/link_quality.cpp b/src/core/thread/link_quality.cpp index 64df6b8ef..5d7ed9312 100644 --- a/src/core/thread/link_quality.cpp +++ b/src/core/thread/link_quality.cpp @@ -106,11 +106,11 @@ exit: RssAverager::InfoString RssAverager::ToString(void) const { - InfoString string; + InfoString string; + StringWriter writer(string); VerifyOrExit(mCount != 0); - StringWriter(string).Append("%d.%s", -(mAverage >> kPrecisionBitShift), - kDigitsString[mAverage & kPrecisionBitMask]); + writer.Append("%d.%s", -(mAverage >> kPrecisionBitShift), kDigitsString[mAverage & kPrecisionBitMask]); exit: return string; @@ -167,10 +167,12 @@ uint8_t LinkQualityInfo::GetLinkMargin(void) const LinkQualityInfo::InfoString LinkQualityInfo::ToInfoString(void) const { - InfoString string; + InfoString string; + StringWriter writer(string); + + writer.Append("aveRss:%s, lastRss:%d, linkQuality:%d", mRssAverager.ToString().AsCString(), GetLastRss(), + GetLinkQuality()); - StringWriter(string).Append("aveRss:%s, lastRss:%d, linkQuality:%d", mRssAverager.ToString().AsCString(), - GetLastRss(), GetLinkQuality()); return string; } diff --git a/src/core/thread/mle.cpp b/src/core/thread/mle.cpp index 00bd582a6..c32069872 100644 --- a/src/core/thread/mle.cpp +++ b/src/core/thread/mle.cpp @@ -4114,10 +4114,11 @@ void Mle::Log(MessageAction aAction, MessageType aType, const Ip6::Address &aAdd }; String rlocString; + StringWriter writer(rlocString); if (aRloc != Mac::kShortAddrInvalid) { - StringWriter(rlocString).Append(",0x%04x", aRloc); + writer.Append(",0x%04x", aRloc); } otLogInfoMle("%s %s%s (%s%s)", MessageActionToString(aAction), MessageTypeToString(aType), diff --git a/src/core/thread/mle_types.cpp b/src/core/thread/mle_types.cpp index 32d55e2d8..7ffee2261 100644 --- a/src/core/thread/mle_types.cpp +++ b/src/core/thread/mle_types.cpp @@ -55,10 +55,12 @@ void DeviceMode::Set(const ModeConfig &aModeConfig) DeviceMode::InfoString DeviceMode::ToString(void) const { - InfoString string; + InfoString string; + StringWriter writer(string); + + writer.Append("rx-on:%s ftd:%s full-net:%s", IsRxOnWhenIdle() ? "yes" : "no", IsFullThreadDevice() ? "yes" : "no", + IsFullNetworkData() ? "yes" : "no"); - StringWriter(string).Append("rx-on:%s ftd:%s full-net:%s", IsRxOnWhenIdle() ? "yes" : "no", - IsFullThreadDevice() ? "yes" : "no", IsFullNetworkData() ? "yes" : "no"); return string; } diff --git a/src/core/thread/radio_selector.cpp b/src/core/thread/radio_selector.cpp index 32086b592..e8ac62f53 100644 --- a/src/core/thread/radio_selector.cpp +++ b/src/core/thread/radio_selector.cpp @@ -361,6 +361,7 @@ void RadioSelector::Log(otLogLevel aLogLevel, const Neighbor &aNeighbor) { String preferenceString; + StringWriter writer(preferenceString); bool isFirstEntry = true; VerifyOrExit(otLoggingGetLevel() >= aLogLevel); @@ -369,9 +370,8 @@ void RadioSelector::Log(otLogLevel aLogLevel, { if (aNeighbor.GetSupportedRadioTypes().Contains(radio)) { - StringWriter(preferenceString) - .Append("%s%s:%d", isFirstEntry ? "" : " ", RadioTypeToString(radio), - aNeighbor.GetRadioPreference(radio)); + writer.Append("%s%s:%d", isFirstEntry ? "" : " ", RadioTypeToString(radio), + aNeighbor.GetRadioPreference(radio)); isFirstEntry = false; } } diff --git a/src/core/utils/channel_monitor.cpp b/src/core/utils/channel_monitor.cpp index ca7bb4987..a5787fb9c 100644 --- a/src/core/utils/channel_monitor.cpp +++ b/src/core/utils/channel_monitor.cpp @@ -192,10 +192,11 @@ void ChannelMonitor::LogResults(void) #if (OPENTHREAD_CONFIG_LOG_LEVEL >= OT_LOG_LEVEL_INFO) && (OPENTHREAD_CONFIG_LOG_UTIL == 1) const size_t kStringSize = 128; String logString; + StringWriter writer(logString); for (uint16_t channel : mChannelOccupancy) { - StringWriter(logString).Append("%02x ", channel >> 8); + writer.Append("%02x ", channel >> 8); } otLogInfoUtil("ChannelMonitor: %u [%s]", mSampleCount, logString.AsCString()); diff --git a/tests/unit/test_string.cpp b/tests/unit/test_string.cpp index c1e8489d1..b97f9e23d 100644 --- a/tests/unit/test_string.cpp +++ b/tests/unit/test_string.cpp @@ -49,12 +49,13 @@ template void PrintString(const char *aName, const String str; + StringWriter writer(str); constexpr char kLongString[] = "abcdefghijklmnopqratuvwxyzabcdefghijklmnopqratuvwxyz"; printf("\nTest 1: StringWriter constructor\n"); - VerifyOrQuit(StringWriter(str).GetSize() == kStringSize, "GetSize() failed"); - VerifyOrQuit(StringWriter(str).GetLength() == 0, "GetLength() failed for empty string"); + VerifyOrQuit(writer.GetSize() == kStringSize, "GetSize() failed"); + VerifyOrQuit(writer.GetLength() == 0, "GetLength() failed for empty string"); VerifyOrQuit(strcmp(str.AsCString(), "") == 0, "String content is incorrect"); @@ -62,51 +63,44 @@ void TestStringWriter(void) printf(" -- PASS\n"); - printf("\nTest 2: String::Append() method\n"); + printf("\nTest 2: StringWriter::Append() method\n"); - { - StringWriter writer(str); + writer.Append("Hi"); + VerifyOrQuit(writer.GetLength() == 2, "GetLength() failed"); + VerifyOrQuit(strcmp(str.AsCString(), "Hi") == 0, "String content is incorrect"); + PrintString("str", str); - writer.Append("Hi"); - VerifyOrQuit(writer.GetLength() == 2, "GetLength() failed"); - VerifyOrQuit(strcmp(str.AsCString(), "Hi") == 0, "String content is incorrect"); - PrintString("str", str); + writer.Append("%s%d", "!", 12); + VerifyOrQuit(writer.GetLength() == 5, "GetLength() failed"); + VerifyOrQuit(strcmp(str.AsCString(), "Hi!12") == 0, "String content is incorrect"); + PrintString("str", str); - writer.Append("%s%d", "!", 12); - VerifyOrQuit(writer.GetLength() == 5, "GetLength() failed"); - VerifyOrQuit(strcmp(str.AsCString(), "Hi!12") == 0, "String content is incorrect"); - PrintString("str", str); + writer.Append(kLongString); + VerifyOrQuit(writer.IsTruncated() && writer.GetLength() == 5 + sizeof(kLongString) - 1, + "String::Append() did not handle overflow buffer correctly"); + PrintString("str", str); - writer.Append(kLongString); - VerifyOrQuit(writer.IsTruncated() && writer.GetLength() == 5 + sizeof(kLongString) - 1, - "String::Append() did not handle overflow buffer correctly"); - PrintString("str", str); - } + printf("\nTest 3: StringWriter::Clear() method\n"); - printf("\nTest 3: String::Clear() method\n"); + writer.Clear(); + writer.Append("Hello"); + VerifyOrQuit(writer.GetLength() == 5, "GetLength() failed for empty string"); + VerifyOrQuit(strcmp(str.AsCString(), "Hello") == 0, "String content is incorrect"); + PrintString("str", str); - { - StringWriter writer(str); + writer.Clear(); + VerifyOrQuit(writer.GetLength() == 0, "GetLength() failed for empty string"); + VerifyOrQuit(strcmp(str.AsCString(), "") == 0, "String content is incorrect"); - writer.Append("Hello"); - VerifyOrQuit(writer.GetLength() == 5, "GetLength() failed for empty string"); - VerifyOrQuit(strcmp(str.AsCString(), "Hello") == 0, "String content is incorrect"); - PrintString("str", str); + writer.Append("%d", 12); + VerifyOrQuit(writer.GetLength() == 2, "GetLength() failed"); + VerifyOrQuit(strcmp(str.AsCString(), "12") == 0, "String content is incorrect"); + PrintString("str", str); - writer.Clear(); - VerifyOrQuit(writer.GetLength() == 0, "GetLength() failed for empty string"); - VerifyOrQuit(strcmp(str.AsCString(), "") == 0, "String content is incorrect"); - - writer.Append("%d", 12); - VerifyOrQuit(writer.GetLength() == 2, "GetLength() failed"); - VerifyOrQuit(strcmp(str.AsCString(), "12") == 0, "String content is incorrect"); - PrintString("str", str); - - writer.Clear().Append(kLongString); - VerifyOrQuit(writer.IsTruncated() && writer.GetLength() == sizeof(kLongString) - 1, - "String::Clear() + String::Append() did not handle overflow buffer correctly"); - PrintString("str", str); - } + writer.Clear().Append(kLongString); + VerifyOrQuit(writer.IsTruncated() && writer.GetLength() == sizeof(kLongString) - 1, + "String::Clear() + String::Append() did not handle overflow buffer correctly"); + PrintString("str", str); printf(" -- PASS\n"); }