mirror of
https://github.com/slint-ui/slint.git
synced 2025-08-04 10:50:00 +00:00
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:
parent
9c04c62cc6
commit
40811193aa
2 changed files with 9 additions and 0 deletions
|
@ -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.
|
||||
|
|
|
@ -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]")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue