C++: fix string to float so that it doesn't depends on the current locale

This commit is contained in:
Olivier Goffart 2024-05-07 14:16:25 +02:00
parent 8996948fd1
commit 5c3732c74e
2 changed files with 19 additions and 6 deletions

View file

@ -9,6 +9,7 @@ extern crate alloc;
use alloc::rc::Rc;
use core::ffi::c_void;
use i_slint_core::window::{ffi::WindowAdapterRcOpaque, WindowAdapter};
use i_slint_core::SharedString;
pub mod platform;
@ -100,8 +101,8 @@ pub unsafe extern "C" fn slint_quit_event_loop() {
#[no_mangle]
pub unsafe extern "C" fn slint_register_font_from_path(
win: *const WindowAdapterRcOpaque,
path: &i_slint_core::SharedString,
error_str: *mut i_slint_core::SharedString,
path: &SharedString,
error_str: *mut SharedString,
) {
let window_adapter = &*(win as *const Rc<dyn WindowAdapter>);
core::ptr::write(
@ -119,7 +120,7 @@ pub unsafe extern "C" fn slint_register_font_from_path(
pub unsafe extern "C" fn slint_register_font_from_data(
win: *const WindowAdapterRcOpaque,
data: i_slint_core::slice::Slice<'static, u8>,
error_str: *mut i_slint_core::SharedString,
error_str: *mut SharedString,
) {
let window_adapter = &*(win as *const Rc<dyn WindowAdapter>);
core::ptr::write(
@ -140,6 +141,17 @@ pub unsafe extern "C" fn slint_register_bitmap_font(
window_adapter.renderer().register_bitmap_font(font_data);
}
#[no_mangle]
pub extern "C" fn slint_string_to_float(string: &SharedString, value: &mut f32) -> bool {
match string.as_str().parse::<f32>() {
Ok(v) => {
*value = v;
true
}
Err(_) => false,
}
}
#[cfg(not(feature = "std"))]
mod allocator {
use core::alloc::Layout;

View file

@ -3131,7 +3131,8 @@ fn compile_builtin_function_call(
panic!("internal error: invalid args to ClearFocusItem {:?}", arguments)
}
}
/* std::from_chars is unfortunately not yet implemented in gcc
/* std::from_chars is unfortunately not yet implemented in all stdlib compiler we support.
* And std::strtod depends on the locale. Use slint_string_to_float implemented in Rust
BuiltinFunction::StringIsFloat => {
"[](const auto &a){ double v; auto r = std::from_chars(std::begin(a), std::end(a), v); return r.ptr == std::end(a); }"
.into()
@ -3142,11 +3143,11 @@ fn compile_builtin_function_call(
}*/
BuiltinFunction::StringIsFloat => {
ctx.generator_state.conditional_includes.cstdlib.set(true);
format!("[](const auto &a){{ auto e1 = std::end(a); auto e2 = const_cast<char*>(e1); std::strtod(std::begin(a), &e2); return e1 == e2; }}({})", a.next().unwrap())
format!("[](const auto &a){{ float res = 0; return slint::cbindgen_private::slint_string_to_float(&a, &res); }}({})", a.next().unwrap())
}
BuiltinFunction::StringToFloat => {
ctx.generator_state.conditional_includes.cstdlib.set(true);
format!("[](const auto &a){{ auto e1 = std::end(a); auto e2 = const_cast<char*>(e1); auto r = std::strtod(std::begin(a), &e2); return e1 == e2 ? r : 0; }}({})", a.next().unwrap())
format!("[](const auto &a){{ float res = 0; slint::cbindgen_private::slint_string_to_float(&a, &res); return res; }}({})", a.next().unwrap())
}
BuiltinFunction::ColorRgbaStruct => {
format!("{}.to_argb_uint()", a.next().unwrap())