mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-02 14:51:15 +00:00
Fix number to string conversion in C++
This commit is contained in:
parent
486b2ab8f8
commit
5580b5112f
5 changed files with 90 additions and 59 deletions
|
@ -228,40 +228,6 @@ where
|
|||
|
||||
impl Eq for SharedString {}
|
||||
|
||||
/// for cbingen.
|
||||
#[allow(non_camel_case_types)]
|
||||
type c_char = u8;
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn sixtyfps_shared_string_bytes(ss: &SharedString) -> *const c_char {
|
||||
ss.as_ptr()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
/// Destroy the shared string
|
||||
pub unsafe extern "C" fn sixtyfps_shared_string_drop(ss: *const SharedString) {
|
||||
core::ptr::read(ss);
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
/// Increment the reference count of the string.
|
||||
/// the resulting structure must be passed to sixtyfps_shared_string_drop
|
||||
pub unsafe extern "C" fn sixtyfps_shared_string_clone(out: *mut SharedString, ss: &SharedString) {
|
||||
core::ptr::write(out, ss.clone())
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
/// Safety: bytes must be a valid utf-8 string of size len wihout null inside.
|
||||
/// the resulting structure must be passed to sixtyfps_shared_string_drop
|
||||
pub unsafe extern "C" fn sixtyfps_shared_string_from_bytes(
|
||||
out: *mut SharedString,
|
||||
bytes: *const c_char,
|
||||
len: usize,
|
||||
) {
|
||||
let str = core::str::from_utf8_unchecked(core::slice::from_raw_parts(bytes, len));
|
||||
core::ptr::write(out, SharedString::from(str));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn simple_test() {
|
||||
let x = SharedString::from("hello world!");
|
||||
|
@ -279,3 +245,67 @@ fn simple_test() {
|
|||
&*std::ffi::CString::new("hello world!").unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
/// for cbingen.
|
||||
#[allow(non_camel_case_types)]
|
||||
type c_char = u8;
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn sixtyfps_shared_string_bytes(ss: &SharedString) -> *const c_char {
|
||||
ss.as_ptr()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
/// Destroy the shared string
|
||||
pub unsafe extern "C" fn sixtyfps_shared_string_drop(ss: *const SharedString) {
|
||||
core::ptr::read(ss);
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
/// Increment the reference count of the string.
|
||||
/// The resulting structure must be passed to sixtyfps_shared_string_drop
|
||||
pub unsafe extern "C" fn sixtyfps_shared_string_clone(out: *mut SharedString, ss: &SharedString) {
|
||||
core::ptr::write(out, ss.clone())
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
/// Safety: bytes must be a valid utf-8 string of size len wihout null inside.
|
||||
/// The resulting structure must be passed to sixtyfps_shared_string_drop
|
||||
pub unsafe extern "C" fn sixtyfps_shared_string_from_bytes(
|
||||
out: *mut SharedString,
|
||||
bytes: *const c_char,
|
||||
len: usize,
|
||||
) {
|
||||
let str = core::str::from_utf8_unchecked(core::slice::from_raw_parts(bytes, len));
|
||||
core::ptr::write(out, SharedString::from(str));
|
||||
}
|
||||
|
||||
/// Create a string from a number.
|
||||
/// The resulting structure must be passed to sixtyfps_shared_string_drop
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn sixtyfps_shared_string_from_number(out: *mut SharedString, n: f64) {
|
||||
// TODO: implement Write for SharedString so this can be done without alocation
|
||||
let str = format!("{}", n);
|
||||
core::ptr::write(out, SharedString::from(str.as_str()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_sixtyfps_shared_string_from_number() {
|
||||
unsafe {
|
||||
let mut s = core::mem::MaybeUninit::uninit();
|
||||
sixtyfps_shared_string_from_number(s.as_mut_ptr(), 45.);
|
||||
assert_eq!(s.assume_init(), "45");
|
||||
|
||||
let mut s = core::mem::MaybeUninit::uninit();
|
||||
sixtyfps_shared_string_from_number(s.as_mut_ptr(), 45.12);
|
||||
assert_eq!(s.assume_init(), "45.12");
|
||||
|
||||
let mut s = core::mem::MaybeUninit::uninit();
|
||||
sixtyfps_shared_string_from_number(s.as_mut_ptr(), -1325466.);
|
||||
assert_eq!(s.assume_init(), "-1325466");
|
||||
|
||||
let mut s = core::mem::MaybeUninit::uninit();
|
||||
sixtyfps_shared_string_from_number(s.as_mut_ptr(), -0.);
|
||||
assert_eq!(s.assume_init(), "0");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue