Skip to content

Commit

Permalink
Adopt _CharSpan for literals
Browse files Browse the repository at this point in the history
  • Loading branch information
ksperling-apple committed Oct 27, 2023
1 parent cdf5cf1 commit 6cd789d
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 50 deletions.
2 changes: 1 addition & 1 deletion src/app/clusters/door-lock-server/door-lock-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1827,7 +1827,7 @@ EmberAfStatus DoorLockServer::createUser(chip::EndpointId endpointId, chip::Fabr
return static_cast<EmberAfStatus>(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();
Expand Down
2 changes: 1 addition & 1 deletion src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 15 additions & 15 deletions src/credentials/tests/TestFabricTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

Expand Down
12 changes: 5 additions & 7 deletions src/crypto/tests/CHIPCryptoPALTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
8 changes: 4 additions & 4 deletions src/lib/core/tests/TestOTAImageHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/support/tests/TestJsonToTlv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand All @@ -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<float>(1.0);
structVal.h = static_cast<double>(1.0);

Expand Down
38 changes: 19 additions & 19 deletions src/lib/support/tests/TestStringSplitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);
}
Expand All @@ -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));
}

Expand All @@ -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));
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/support/tests/TestTlvToJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ void TestConverter(nlTestSuite * inSuite, void * inContext)
"}\n";
EncodeAndValidate(static_cast<double>(1.0), jsonString);

CharSpan charSpan = CharSpan::fromCharString("hello");
CharSpan charSpan = "hello"_CharSpan;
jsonString = "{\n"
" \"1:STRING\" : \"hello\"\n"
"}\n";
Expand Down

0 comments on commit 6cd789d

Please sign in to comment.