Skip to content

Commit

Permalink
Add CStringArray::release method
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 authored and mikee47 committed Jun 17, 2024
1 parent 1b5ebe5 commit e50f112
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Sming/Core/Data/CStringArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ class CStringArray : private String
return *this;
}

/**
* @brief Give up underlying String object to caller so it can be manipulated
*/
String release()
{
return std::move(*this);
}

/** @brief Append a new string (or array of strings) to the end of the array
* @param str
* @param length Length of new string in array (default is length of str)
Expand Down
26 changes: 26 additions & 0 deletions tests/HostTests/modules/CStringArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class CStringArrayTest : public TestGroup
"b\0"
"c\0"
"d\0");
DEFINE_FSTR_LOCAL(FS_BasicJoined, "a,b,c,d")

TEST_CASE("Empty construction")
{
Expand Down Expand Up @@ -231,6 +232,31 @@ class CStringArrayTest : public TestGroup
csa.add(F("test\0again"));
REQUIRE_EQ(csa.join("}+{"), "a}+{}+{test}+{again");
REQUIRE_EQ(csa.join(nullptr), "atestagain");

csa = FS_Basic;
REQUIRE_EQ(csa.join(), FS_BasicJoined);
}

TEST_CASE("release")
{
CStringArray csa(FS_Basic);
csa += FS_Basic; // Allocate > SSO
Serial << csa.join() << endl;
auto cstrWant = csa.c_str();
String s = csa.release();
REQUIRE(!csa);
REQUIRE(s.c_str() == cstrWant);

REQUIRE(s == String(FS_Basic) + FS_Basic);

csa = std::move(s);
REQUIRE(csa == String(FS_Basic) + FS_Basic);

String js;
js += FS_BasicJoined;
js += ',';
js += FS_BasicJoined;
REQUIRE(csa.join() == js);
}
}
};
Expand Down

0 comments on commit e50f112

Please sign in to comment.