C++: Add size() method to SharedString (#6417)

The length is an important property of a string and should be available
as a getter. The new size() method is consistent with std::string.

ChangeLog: [C++] Added `SharedString::size()`
This commit is contained in:
U. Bruhin 2024-10-01 14:51:56 +02:00 committed by GitHub
parent 9c04c62cc6
commit 40811193aa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 0 deletions

View file

@ -83,6 +83,8 @@ struct SharedString
/// Provides a raw pointer to the string data. The returned pointer is only valid as long as at
/// least this SharedString exists.
auto data() const -> const char * { return cbindgen_private::slint_shared_string_bytes(this); }
/// Size of the string, in bytes. This excludes the terminating null character.
std::size_t size() const { return std::string_view(*this).size(); }
/// Returns a pointer to the first character. It is only safe to dereference the pointer if the
/// string contains at least one character.

View file

@ -13,6 +13,7 @@ SCENARIO("SharedString API")
slint::SharedString str;
REQUIRE(str.empty());
REQUIRE(str.size() == 0);
REQUIRE(str == "");
REQUIRE(std::string_view(str.data()) == ""); // this test null termination of data()
@ -45,6 +46,12 @@ SCENARIO("SharedString API")
str = "Hello";
REQUIRE(str.begin() + std::string_view(str).size() == str.end());
}
SECTION("size")
{
str = "Hello";
REQUIRE(str.size() == 5);
}
}
TEST_CASE("Basic SharedVector API", "[vector]")