Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cpp: added to_lowercase and to_uppercase to SharedString #6869

Merged
merged 14 commits into from
Nov 21, 2024
28 changes: 28 additions & 0 deletions api/cpp/include/slint_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,34 @@ struct SharedString
/// \endcode
static SharedString from_number(double n) { return SharedString(n); }

/// Returns the lowercase equivalent of this string, as a new SharedString.
///
/// For example:
/// \code
/// auto str = slint::SharedString("Hello");
/// auto str2 = str.to_lowercase(); // creates "hello"
/// \endcode
SharedString to_lowercase()
FloVanGH marked this conversation as resolved.
Show resolved Hide resolved
{
auto out = SharedString();
cbindgen_private::slint_shared_string_to_lowercase(&out, this);
return out;
}

/// Returns the uppercase equivalent of this string, as a new SharedString.
///
/// For example:
/// \code
/// auto str = slint::SharedString("Hello");
/// auto str2 = str.to_uppercase(); // creates "HELLO"
/// \endcode
SharedString to_uppercase()
{
auto out = SharedString();
cbindgen_private::slint_shared_string_to_uppercase(&out, this);
return out;
}

/// Returns true if \a a is equal to \a b; otherwise returns false.
friend bool operator==(const SharedString &a, const SharedString &b)
{
Expand Down
11 changes: 11 additions & 0 deletions api/cpp/tests/datastructures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ SCENARIO("SharedString API")
str = "Hello";
REQUIRE(str.size() == 5);
}

SECTION("to_lowercase")
{
str = "Hello";
REQUIRE(std::string_view(str.to_lowercase().data()) == "hello");
}
SECTION("to_uppercase")
{
str = "Hello";
REQUIRE(std::string_view(str.to_uppercase().data()) == "hello");
FloVanGH marked this conversation as resolved.
Show resolved Hide resolved
}
}

TEST_CASE("Basic SharedVector API", "[vector]")
Expand Down
36 changes: 36 additions & 0 deletions internal/core/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,42 @@ pub(crate) mod ffi {
append("!");
assert_eq!(s.as_str(), "Hello, world!");
}

#[no_mangle]
pub unsafe extern "C" fn slint_shared_string_to_lowercase(
out: &mut SharedString,
ss: &SharedString,
) {
*out = SharedString::from(ss.to_lowercase());
}
#[test]
fn test_slint_shared_string_to_lowercase() {
let s = SharedString::from("Hello");
let mut out = SharedString::default();

unsafe {
slint_shared_string_to_lowercase(&mut out, &s);
}
assert_eq!(out.as_str(), "hello");
}

#[no_mangle]
pub unsafe extern "C" fn slint_shared_string_to_uppercase(
out: &mut SharedString,
ss: &SharedString,
) {
*out = SharedString::from(ss.to_uppercase());
}
#[test]
fn test_slint_shared_string_to_uppercase() {
let s = SharedString::from("Hello");
let mut out = SharedString::default();

unsafe {
slint_shared_string_to_uppercase(&mut out, &s);
}
assert_eq!(out.as_str(), "HELLO");
}
}

#[cfg(feature = "serde")]
Expand Down
Loading