[string] introduce StringWriter class (#6564)

This commit moves methods of String out of the header file to save code
size.

Since most use cases of String writers ignores errors, this commit
changes the return of writers to the Writer object itself for chained
writes.
This commit is contained in:
Yakun Xu
2021-05-05 21:52:59 -07:00
committed by GitHub
parent 26cdf63231
commit b39f5b6dd2
18 changed files with 284 additions and 278 deletions
+46 -52
View File
@@ -43,76 +43,70 @@ enum
template <uint16_t kSize> void PrintString(const char *aName, const String<kSize> aString)
{
printf("\t%s = [%d] \"%s\"\n", aName, aString.GetLength(), aString.AsCString());
printf("\t%s = [%zu] \"%s\"\n", aName, strlen(aString.AsCString()), aString.AsCString());
}
void TestString(void)
void TestStringWriter(void)
{
otError error;
String<kStringSize> str1;
String<kStringSize> str2("abc");
String<kStringSize> str3("%d", 12);
String<kStringSize> str;
constexpr char kLongString[] = "abcdefghijklmnopqratuvwxyzabcdefghijklmnopqratuvwxyz";
printf("\nTest 1: String constructor\n");
printf("\nTest 1: StringWriter constructor\n");
VerifyOrQuit(str1.GetSize() == kStringSize, "GetSize() failed");
VerifyOrQuit(StringWriter(str).GetSize() == kStringSize, "GetSize() failed");
VerifyOrQuit(StringWriter(str).GetLength() == 0, "GetLength() failed for empty string");
VerifyOrQuit(str1.GetLength() == 0, "GetLength() failed for empty string");
VerifyOrQuit(str2.GetLength() == 3, "GetLength() failed");
VerifyOrQuit(str3.GetLength() == 2, "GetLength() failed");
VerifyOrQuit(strcmp(str.AsCString(), "") == 0, "String content is incorrect");
VerifyOrQuit(strcmp(str1.AsCString(), "") == 0, "String content is incorrect");
VerifyOrQuit(strcmp(str2.AsCString(), "abc") == 0, "String content is incorrect");
VerifyOrQuit(strcmp(str3.AsCString(), "12") == 0, "String content is incorrect");
PrintString("str1", str1);
PrintString("str2", str2);
PrintString("str3", str3);
PrintString("str", str);
printf(" -- PASS\n");
printf("\nTest 2: String::Set() and String::Clear() method\n");
printf("\nTest 2: String::Append() method\n");
error = str1.Set("Hello");
SuccessOrQuit(error, "String::Set() failed unexpectedly");
VerifyOrQuit(str1.GetLength() == 5, "GetLength() failed for empty string");
VerifyOrQuit(strcmp(str1.AsCString(), "Hello") == 0, "String content is incorrect");
PrintString("str1", str1);
{
StringWriter writer(str);
str1.Clear();
VerifyOrQuit(str1.GetLength() == 0, "GetLength() failed for empty string");
VerifyOrQuit(strcmp(str1.AsCString(), "") == 0, "String content is incorrect");
writer.Append("Hi");
VerifyOrQuit(writer.GetLength() == 2, "GetLength() failed");
VerifyOrQuit(strcmp(str.AsCString(), "Hi") == 0, "String content is incorrect");
PrintString("str", str);
IgnoreError(str1.Set("%d", 12));
VerifyOrQuit(str1.GetLength() == 2, "GetLength() failed");
VerifyOrQuit(strcmp(str1.AsCString(), "12") == 0, "String content is incorrect");
PrintString("str1", str1);
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);
error = str1.Set("abcdefghijklmnopqratuvwxyzabcdefghijklmnopqratuvwxyz");
VerifyOrQuit(error == OT_ERROR_NO_BUFS, "String::Set() did not handle overflow buffer correctly");
PrintString("str1", str1);
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: String::Append() method\n");
printf("\nTest 3: String::Clear() method\n");
str2.Clear();
VerifyOrQuit(str2.GetLength() == 0, "GetLength() failed for empty string");
VerifyOrQuit(strcmp(str2.AsCString(), "") == 0, "String content is incorrect");
{
StringWriter writer(str);
error = str2.Append("Hi");
SuccessOrQuit(error, "String::Append() failed unexpectedly");
VerifyOrQuit(str2.GetLength() == 2, "GetLength() failed");
VerifyOrQuit(strcmp(str2.AsCString(), "Hi") == 0, "String content is incorrect");
PrintString("str2", str2);
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);
error = str2.Append("%s%d", "!", 12);
SuccessOrQuit(error, "String::Append() failed unexpectedly");
VerifyOrQuit(str2.GetLength() == 5, "GetLength() failed");
VerifyOrQuit(strcmp(str2.AsCString(), "Hi!12") == 0, "String content is incorrect");
PrintString("str2", str2);
writer.Clear();
VerifyOrQuit(writer.GetLength() == 0, "GetLength() failed for empty string");
VerifyOrQuit(strcmp(str.AsCString(), "") == 0, "String content is incorrect");
error = str2.Append("abcdefghijklmnopqratuvwxyzabcdefghijklmnopqratuvwxyz");
VerifyOrQuit(error == OT_ERROR_NO_BUFS, "String::Append() did not handle overflow buffer correctly");
PrintString("str2", str2);
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);
}
printf(" -- PASS\n");
}
@@ -179,7 +173,7 @@ void TestStringFind(void)
int main(void)
{
ot::TestString();
ot::TestStringWriter();
ot::TestStringLength();
ot::TestUtf8();
ot::TestStringFind();