From 6cd789d0ba018ac04234a2b4b1a572aac36f61b7 Mon Sep 17 00:00:00 2001 From: Karsten Sperling Date: Fri, 27 Oct 2023 17:57:21 +1300 Subject: [PATCH] Adopt _CharSpan for literals --- .../door-lock-server/door-lock-server.cpp | 2 +- src/controller/CHIPDeviceController.cpp | 2 +- src/credentials/tests/TestFabricTable.cpp | 30 +++++++-------- src/crypto/tests/CHIPCryptoPALTest.cpp | 12 +++--- src/lib/core/tests/TestOTAImageHeader.cpp | 8 ++-- src/lib/support/tests/TestJsonToTlv.cpp | 4 +- src/lib/support/tests/TestStringSplitter.cpp | 38 +++++++++---------- src/lib/support/tests/TestTlvToJson.cpp | 2 +- 8 files changed, 48 insertions(+), 50 deletions(-) diff --git a/src/app/clusters/door-lock-server/door-lock-server.cpp b/src/app/clusters/door-lock-server/door-lock-server.cpp index 7552841edfb5e6..617f4d611d1862 100644 --- a/src/app/clusters/door-lock-server/door-lock-server.cpp +++ b/src/app/clusters/door-lock-server/door-lock-server.cpp @@ -1827,7 +1827,7 @@ EmberAfStatus DoorLockServer::createUser(chip::EndpointId endpointId, chip::Fabr return static_cast(DlStatus::kOccupied); } - const auto & newUserName = !userName.IsNull() ? userName.Value() : chip::CharSpan::fromCharString(""); + const auto & newUserName = !userName.IsNull() ? userName.Value() : chip::""_CharSpan; auto newUserUniqueId = userUniqueId.IsNull() ? 0xFFFFFFFF : userUniqueId.Value(); auto newUserStatus = userStatus.IsNull() ? UserStatusEnum::kOccupiedEnabled : userStatus.Value(); auto newUserType = userType.IsNull() ? UserTypeEnum::kUnrestrictedUser : userType.Value(); diff --git a/src/controller/CHIPDeviceController.cpp b/src/controller/CHIPDeviceController.cpp index 9a59e89837329b..55496f65a00bba 100644 --- a/src/controller/CHIPDeviceController.cpp +++ b/src/controller/CHIPDeviceController.cpp @@ -2543,7 +2543,7 @@ void DeviceCommissioner::PerformCommissioningStep(DeviceProxy * proxy, Commissio else { // Default to "XX", for lack of anything better. - countryCode = CharSpan::fromCharString("XX"); + countryCode = "XX"_CharSpan; } GeneralCommissioning::Commands::SetRegulatoryConfig::Type request; diff --git a/src/credentials/tests/TestFabricTable.cpp b/src/credentials/tests/TestFabricTable.cpp index 3bcb5411560e7a..5dba72a6ccbc34 100644 --- a/src/credentials/tests/TestFabricTable.cpp +++ b/src/credentials/tests/TestFabricTable.cpp @@ -2261,32 +2261,32 @@ void TestFabricLabelChange(nlTestSuite * inSuite, void * inContext) // Second scope: set FabricLabel to "acme fabric", make sure it cannot be reverted { // Fabric label starts unset from prior scope - CharSpan fabricLabel = CharSpan::fromCharString("placeholder"); + CharSpan fabricLabel = "placeholder"_CharSpan; NL_TEST_ASSERT_SUCCESS(inSuite, fabricTable.GetFabricLabel(1, fabricLabel)); NL_TEST_ASSERT(inSuite, fabricLabel.size() == 0); // Set a valid name - NL_TEST_ASSERT_SUCCESS(inSuite, fabricTable.SetFabricLabel(1, CharSpan::fromCharString("acme fabric"))); + NL_TEST_ASSERT_SUCCESS(inSuite, fabricTable.SetFabricLabel(1, "acme fabric"_CharSpan)); NL_TEST_ASSERT_SUCCESS(inSuite, fabricTable.GetFabricLabel(1, fabricLabel)); - NL_TEST_ASSERT(inSuite, fabricLabel.data_equal(CharSpan::fromCharString("acme fabric")) == true); + NL_TEST_ASSERT(inSuite, fabricLabel.data_equal("acme fabric"_CharSpan) == true); // Revert pending fabric data. Should not revert name since nothing pending. fabricTable.RevertPendingFabricData(); - fabricLabel = CharSpan::fromCharString("placeholder"); + fabricLabel = "placeholder"_CharSpan; NL_TEST_ASSERT_SUCCESS(inSuite, fabricTable.GetFabricLabel(1, fabricLabel)); - NL_TEST_ASSERT(inSuite, fabricLabel.data_equal(CharSpan::fromCharString("acme fabric")) == true); + NL_TEST_ASSERT(inSuite, fabricLabel.data_equal("acme fabric"_CharSpan) == true); // Verify we fail to set too large a label (> kFabricLabelMaxLengthInBytes) - CharSpan fabricLabelTooBig = CharSpan::fromCharString("012345678901234567890123456789123456"); + CharSpan fabricLabelTooBig = "012345678901234567890123456789123456"_CharSpan; NL_TEST_ASSERT(inSuite, fabricLabelTooBig.size() > chip::kFabricLabelMaxLengthInBytes); NL_TEST_ASSERT(inSuite, fabricTable.SetFabricLabel(1, fabricLabelTooBig) == CHIP_ERROR_INVALID_ARGUMENT); - fabricLabel = CharSpan::fromCharString("placeholder"); + fabricLabel = "placeholder"_CharSpan; NL_TEST_ASSERT_SUCCESS(inSuite, fabricTable.GetFabricLabel(1, fabricLabel)); - NL_TEST_ASSERT(inSuite, fabricLabel.data_equal(CharSpan::fromCharString("acme fabric")) == true); + NL_TEST_ASSERT(inSuite, fabricLabel.data_equal("acme fabric"_CharSpan) == true); } // Third scope: set fabric label after an update, it sticks, but then goes back after revert @@ -2320,23 +2320,23 @@ void TestFabricLabelChange(nlTestSuite * inSuite, void * inContext) NL_TEST_ASSERT(inSuite, fabricInfo->GetVendorId() == kVendorId); CharSpan fabricLabel = fabricInfo->GetFabricLabel(); - NL_TEST_ASSERT(inSuite, fabricLabel.data_equal(CharSpan::fromCharString("acme fabric")) == true); + NL_TEST_ASSERT(inSuite, fabricLabel.data_equal("acme fabric"_CharSpan) == true); } // Update fabric label - CharSpan fabricLabel = CharSpan::fromCharString("placeholder"); - NL_TEST_ASSERT_SUCCESS(inSuite, fabricTable.SetFabricLabel(1, CharSpan::fromCharString("roboto fabric"))); + CharSpan fabricLabel = "placeholder"_CharSpan; + NL_TEST_ASSERT_SUCCESS(inSuite, fabricTable.SetFabricLabel(1, "roboto fabric"_CharSpan)); - fabricLabel = CharSpan::fromCharString("placeholder"); + fabricLabel = "placeholder"_CharSpan; NL_TEST_ASSERT_SUCCESS(inSuite, fabricTable.GetFabricLabel(1, fabricLabel)); - NL_TEST_ASSERT(inSuite, fabricLabel.data_equal(CharSpan::fromCharString("roboto fabric")) == true); + NL_TEST_ASSERT(inSuite, fabricLabel.data_equal("roboto fabric"_CharSpan) == true); // Revert pending fabric data. Should revert name to "acme fabric" fabricTable.RevertPendingFabricData(); - fabricLabel = CharSpan::fromCharString("placeholder"); + fabricLabel = "placeholder"_CharSpan; NL_TEST_ASSERT_SUCCESS(inSuite, fabricTable.GetFabricLabel(1, fabricLabel)); - NL_TEST_ASSERT(inSuite, fabricLabel.data_equal(CharSpan::fromCharString("acme fabric")) == true); + NL_TEST_ASSERT(inSuite, fabricLabel.data_equal("acme fabric"_CharSpan) == true); } } diff --git a/src/crypto/tests/CHIPCryptoPALTest.cpp b/src/crypto/tests/CHIPCryptoPALTest.cpp index 39d9998fa95b80..3a889ccdd4dd1f 100644 --- a/src/crypto/tests/CHIPCryptoPALTest.cpp +++ b/src/crypto/tests/CHIPCryptoPALTest.cpp @@ -2412,15 +2412,13 @@ static void TestSubject_x509Extraction(nlTestSuite * inSuite, void * inContext) ChipDN subjectDN_Node02_02; NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == subjectDN_Node02_02.AddAttribute_MatterNodeId(0xDEDEDEDE00020002)); NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == subjectDN_Node02_02.AddAttribute_MatterFabricId(0xFAB000000000001D)); - NL_TEST_ASSERT(inSuite, - CHIP_NO_ERROR == - subjectDN_Node02_02.AddAttribute_CommonName( - chip::CharSpan::fromCharString("TEST CERT COMMON NAME Attr for Node02_02"), false)); + NL_TEST_ASSERT( + inSuite, + CHIP_NO_ERROR == + subjectDN_Node02_02.AddAttribute_CommonName(chip::"TEST CERT COMMON NAME Attr for Node02_02"_CharSpan, false)); ChipDN subjectDN_Node02_04; NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == subjectDN_Node02_04.AddAttribute_MatterCASEAuthTag(0xABCE1002)); - NL_TEST_ASSERT(inSuite, - CHIP_NO_ERROR == - subjectDN_Node02_04.AddAttribute_CommonName(chip::CharSpan::fromCharString("TestCert02_04"), false)); + NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == subjectDN_Node02_04.AddAttribute_CommonName(chip::"TestCert02_04"_CharSpan, false)); NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == subjectDN_Node02_04.AddAttribute_MatterFabricId(0xFAB000000000001D)); NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == subjectDN_Node02_04.AddAttribute_MatterCASEAuthTag(0xABCD0003)); NL_TEST_ASSERT(inSuite, CHIP_NO_ERROR == subjectDN_Node02_04.AddAttribute_MatterNodeId(0xDEDEDEDE00020004)); diff --git a/src/lib/core/tests/TestOTAImageHeader.cpp b/src/lib/core/tests/TestOTAImageHeader.cpp index 928dc2618100b9..57a5d13ccd6832 100644 --- a/src/lib/core/tests/TestOTAImageHeader.cpp +++ b/src/lib/core/tests/TestOTAImageHeader.cpp @@ -83,13 +83,13 @@ void TestHappyPath(nlTestSuite * inSuite, void * inContext) NL_TEST_ASSERT(inSuite, header.mVendorId == 0xDEAD); NL_TEST_ASSERT(inSuite, header.mProductId == 0xBEEF); NL_TEST_ASSERT(inSuite, header.mSoftwareVersion == 0xFFFFFFFF); - NL_TEST_ASSERT(inSuite, header.mSoftwareVersionString.data_equal(CharSpan::fromCharString("1.0"))); + NL_TEST_ASSERT(inSuite, header.mSoftwareVersionString.data_equal("1.0"_CharSpan)); NL_TEST_ASSERT(inSuite, header.mPayloadSize == strlen("test payload")); NL_TEST_ASSERT(inSuite, header.mMinApplicableVersion.HasValue()); NL_TEST_ASSERT(inSuite, header.mMinApplicableVersion.Value() == 1); NL_TEST_ASSERT(inSuite, header.mMaxApplicableVersion.HasValue()); NL_TEST_ASSERT(inSuite, header.mMaxApplicableVersion.Value() == 2); - NL_TEST_ASSERT(inSuite, header.mReleaseNotesURL.data_equal(CharSpan::fromCharString("https://rn"))); + NL_TEST_ASSERT(inSuite, header.mReleaseNotesURL.data_equal("https://rn"_CharSpan)); NL_TEST_ASSERT(inSuite, header.mImageDigestType == OTAImageDigestType::kSha256); NL_TEST_ASSERT(inSuite, header.mImageDigest.size() == 256 / 8); } @@ -169,13 +169,13 @@ void TestSmallBlocks(nlTestSuite * inSuite, void * inContext) NL_TEST_ASSERT(inSuite, header.mVendorId == 0xDEAD); NL_TEST_ASSERT(inSuite, header.mProductId == 0xBEEF); NL_TEST_ASSERT(inSuite, header.mSoftwareVersion == 0xFFFFFFFF); - NL_TEST_ASSERT(inSuite, header.mSoftwareVersionString.data_equal(CharSpan::fromCharString("1.0"))); + NL_TEST_ASSERT(inSuite, header.mSoftwareVersionString.data_equal("1.0"_CharSpan)); NL_TEST_ASSERT(inSuite, header.mPayloadSize == strlen("test payload")); NL_TEST_ASSERT(inSuite, header.mMinApplicableVersion.HasValue()); NL_TEST_ASSERT(inSuite, header.mMinApplicableVersion.Value() == 1); NL_TEST_ASSERT(inSuite, header.mMaxApplicableVersion.HasValue()); NL_TEST_ASSERT(inSuite, header.mMaxApplicableVersion.Value() == 2); - NL_TEST_ASSERT(inSuite, header.mReleaseNotesURL.data_equal(CharSpan::fromCharString("https://rn"))); + NL_TEST_ASSERT(inSuite, header.mReleaseNotesURL.data_equal("https://rn"_CharSpan)); NL_TEST_ASSERT(inSuite, header.mImageDigestType == OTAImageDigestType::kSha256); NL_TEST_ASSERT(inSuite, header.mImageDigest.size() == 256 / 8); } diff --git a/src/lib/support/tests/TestJsonToTlv.cpp b/src/lib/support/tests/TestJsonToTlv.cpp index 87f37ab4e052b1..a28632ae103251 100644 --- a/src/lib/support/tests/TestJsonToTlv.cpp +++ b/src/lib/support/tests/TestJsonToTlv.cpp @@ -141,7 +141,7 @@ void TestConverter(nlTestSuite * inSuite, void * inContext) jsonString = "{\n" " \"1:STRING\" : \"hello\"\n" "}\n"; - ConvertJsonToTlvAndValidate(CharSpan::fromCharString("hello"), jsonString); + ConvertJsonToTlvAndValidate("hello"_CharSpan, jsonString); // Validated using https://base64.guru/converter/encode/hex const uint8_t byteBuf[] = { 0x01, 0x02, 0x03, 0x04, 0xff, 0xfe, 0x99, 0x88, 0xdd, 0xcd }; @@ -161,7 +161,7 @@ void TestConverter(nlTestSuite * inSuite, void * inContext) structVal.a = 20; structVal.b = true; structVal.d = byteBuf; - structVal.e = CharSpan::fromCharString("hello"); + structVal.e = "hello"_CharSpan; structVal.g = static_cast(1.0); structVal.h = static_cast(1.0); diff --git a/src/lib/support/tests/TestStringSplitter.cpp b/src/lib/support/tests/TestStringSplitter.cpp index a8c41750211e1a..8202d9f8117689 100644 --- a/src/lib/support/tests/TestStringSplitter.cpp +++ b/src/lib/support/tests/TestStringSplitter.cpp @@ -45,7 +45,7 @@ void TestStrdupSplitter(nlTestSuite * inSuite, void * inContext) StringSplitter splitter("single", ','); NL_TEST_ASSERT(inSuite, splitter.Next(out)); - NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString("single"))); + NL_TEST_ASSERT(inSuite, out.data_equal("single"_CharSpan)); // next stays at nullptr also after valid data NL_TEST_ASSERT(inSuite, !splitter.Next(out)); @@ -59,11 +59,11 @@ void TestStrdupSplitter(nlTestSuite * inSuite, void * inContext) StringSplitter splitter("one,two,three", ','); NL_TEST_ASSERT(inSuite, splitter.Next(out)); - NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString("one"))); + NL_TEST_ASSERT(inSuite, out.data_equal("one"_CharSpan)); NL_TEST_ASSERT(inSuite, splitter.Next(out)); - NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString("two"))); + NL_TEST_ASSERT(inSuite, out.data_equal("two"_CharSpan)); NL_TEST_ASSERT(inSuite, splitter.Next(out)); - NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString("three"))); + NL_TEST_ASSERT(inSuite, out.data_equal("three"_CharSpan)); NL_TEST_ASSERT(inSuite, !splitter.Next(out)); NL_TEST_ASSERT(inSuite, out.data() == nullptr); } @@ -73,15 +73,15 @@ void TestStrdupSplitter(nlTestSuite * inSuite, void * inContext) StringSplitter splitter("a**bc*d,e*f", '*'); NL_TEST_ASSERT(inSuite, splitter.Next(out)); - NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString("a"))); + NL_TEST_ASSERT(inSuite, out.data_equal("a"_CharSpan)); NL_TEST_ASSERT(inSuite, splitter.Next(out)); - NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString(""))); + NL_TEST_ASSERT(inSuite, out.data_equal(""_CharSpan)); NL_TEST_ASSERT(inSuite, splitter.Next(out)); - NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString("bc"))); + NL_TEST_ASSERT(inSuite, out.data_equal("bc"_CharSpan)); NL_TEST_ASSERT(inSuite, splitter.Next(out)); - NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString("d,e"))); + NL_TEST_ASSERT(inSuite, out.data_equal("d,e"_CharSpan)); NL_TEST_ASSERT(inSuite, splitter.Next(out)); - NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString("f"))); + NL_TEST_ASSERT(inSuite, out.data_equal("f"_CharSpan)); NL_TEST_ASSERT(inSuite, !splitter.Next(out)); } @@ -90,37 +90,37 @@ void TestStrdupSplitter(nlTestSuite * inSuite, void * inContext) StringSplitter splitter(",", ','); // Note that even though "" is nullptr right away, "," becomes two empty strings NL_TEST_ASSERT(inSuite, splitter.Next(out)); - NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString(""))); + NL_TEST_ASSERT(inSuite, out.data_equal(""_CharSpan)); NL_TEST_ASSERT(inSuite, splitter.Next(out)); - NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString(""))); + NL_TEST_ASSERT(inSuite, out.data_equal(""_CharSpan)); NL_TEST_ASSERT(inSuite, !splitter.Next(out)); } { StringSplitter splitter("log,", ','); NL_TEST_ASSERT(inSuite, splitter.Next(out)); - NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString("log"))); + NL_TEST_ASSERT(inSuite, out.data_equal("log"_CharSpan)); NL_TEST_ASSERT(inSuite, splitter.Next(out)); - NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString(""))); + NL_TEST_ASSERT(inSuite, out.data_equal(""_CharSpan)); NL_TEST_ASSERT(inSuite, !splitter.Next(out)); } { StringSplitter splitter(",log", ','); NL_TEST_ASSERT(inSuite, splitter.Next(out)); - NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString(""))); + NL_TEST_ASSERT(inSuite, out.data_equal(""_CharSpan)); NL_TEST_ASSERT(inSuite, splitter.Next(out)); - NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString("log"))); + NL_TEST_ASSERT(inSuite, out.data_equal("log"_CharSpan)); NL_TEST_ASSERT(inSuite, !splitter.Next(out)); } { StringSplitter splitter(",,,", ','); NL_TEST_ASSERT(inSuite, splitter.Next(out)); - NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString(""))); + NL_TEST_ASSERT(inSuite, out.data_equal(""_CharSpan)); NL_TEST_ASSERT(inSuite, splitter.Next(out)); - NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString(""))); + NL_TEST_ASSERT(inSuite, out.data_equal(""_CharSpan)); NL_TEST_ASSERT(inSuite, splitter.Next(out)); - NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString(""))); + NL_TEST_ASSERT(inSuite, out.data_equal(""_CharSpan)); NL_TEST_ASSERT(inSuite, splitter.Next(out)); - NL_TEST_ASSERT(inSuite, out.data_equal(CharSpan::fromCharString(""))); + NL_TEST_ASSERT(inSuite, out.data_equal(""_CharSpan)); NL_TEST_ASSERT(inSuite, !splitter.Next(out)); } } diff --git a/src/lib/support/tests/TestTlvToJson.cpp b/src/lib/support/tests/TestTlvToJson.cpp index 63dcf48c9fde16..8d6f99dc53b6cd 100644 --- a/src/lib/support/tests/TestTlvToJson.cpp +++ b/src/lib/support/tests/TestTlvToJson.cpp @@ -140,7 +140,7 @@ void TestConverter(nlTestSuite * inSuite, void * inContext) "}\n"; EncodeAndValidate(static_cast(1.0), jsonString); - CharSpan charSpan = CharSpan::fromCharString("hello"); + CharSpan charSpan = "hello"_CharSpan; jsonString = "{\n" " \"1:STRING\" : \"hello\"\n" "}\n";