From 5bf498b7d61fda659e5910876d71f541a3fb1c1d Mon Sep 17 00:00:00 2001 From: Basit Ayantunde Date: Thu, 28 Nov 2024 08:31:11 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9E=95=20changed=20scalar=20parser=20to=20fa?= =?UTF-8?q?st=5Ffloat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 19 ++++++++++++++--- ashura/engine/views.cc | 46 +++++++++++++++++++++++++++++++++++++++++- ashura/engine/views.h | 46 +----------------------------------------- ashura/std/format.cc | 10 ++++----- 4 files changed, 67 insertions(+), 54 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b73b21af9..b0cc402fe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -96,6 +96,12 @@ FetchContent_Declare( GIT_REPOSITORY https://github.com/curl/curl.git GIT_TAG curl-8_8_0) +FetchContent_Declare( + fast_float + GIT_REPOSITORY https://github.com/fastfloat/fast_float.git + GIT_TAG v7.0.0 + CONFIGURE_COMMAND "" BUILD_COMMAND "") + find_package(simdjson CONFIG REQUIRED) find_package(GTest REQUIRED) find_package(libjpeg-turbo CONFIG REQUIRED) @@ -117,9 +123,12 @@ FetchContent_MakeAvailable(SpirvReflect) FetchContent_MakeAvailable(SDL3) FetchContent_MakeAvailable(WebP) FetchContent_MakeAvailable(CURL) +FetchContent_MakeAvailable(fast_float) file(GLOB SheenBidi_SOURCES ${sheenbidi_SOURCE_DIR}/Source/*.c) +set(fast_float_INCLUDE_DIR ${fast_float_SOURCE_DIR}/include) + add_library(SheenBidi ${SheenBidi_SOURCES}) target_include_directories( SheenBidi @@ -151,7 +160,9 @@ add_library( ashura/std/hash.cc ashura/std/log.cc ashura/std/panic.cc) -target_include_directories(ashura_std PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_include_directories( + ashura_std + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) target_link_libraries(ashura_std PUBLIC xxHash::xxhash Threads::Threads) add_executable( @@ -218,8 +229,10 @@ target_link_libraries( SheenBidi::SheenBidi) target_include_directories( - ashura_engine PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${JPEG_INCLUDE_DIR} - ${FFMPEG_INCLUDE_DIRS} ${FFMPEG_LIBRARY_DIRS}) + ashura_engine + PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${JPEG_INCLUDE_DIR} ${FFMPEG_INCLUDE_DIRS} + ${FFMPEG_LIBRARY_DIRS} + PRIVATE ${fast_float_INCLUDE_DIR}) add_executable( ashura_engine_tests diff --git a/ashura/engine/views.cc b/ashura/engine/views.cc index d5fa88376..a27aa72f8 100644 --- a/ashura/engine/views.cc +++ b/ashura/engine/views.cc @@ -1,7 +1,51 @@ /// SPDX-License-Identifier: MIT #include "ashura/engine/views.h" +#include "fast_float/fast_float.h" namespace ash { -} \ No newline at end of file +void ScalarDragBox::scalar_parse(Span text, ScalarState &styling) +{ + if (text.is_empty()) + { + return; + } + + Vec utf8{default_allocator}; + utf8_encode(text, utf8).unwrap(); + + char const *const first = (char const *) utf8.begin(); + char const *const last = (char const *) utf8.end(); + + switch (styling.base.type) + { + case ScalarInputType::i32: + { + i32 value = 0; + auto [ptr, ec] = fast_float::from_chars(first, last, value); + if (ec != std::errc{} || value < styling.min.i32 || + value > styling.max.i32) + { + return; + } + styling.current = ScalarInput{.i32 = value, .type = ScalarInputType::i32}; + } + break; + + case ScalarInputType::f32: + { + f32 value = 0; + auto [ptr, ec] = fast_float::from_chars(first, last, value); + if (ec != std::errc{} || value < styling.min.f32 || + value > styling.max.f32) + { + return; + } + styling.current = ScalarInput{.f32 = value, .type = ScalarInputType::f32}; + } + break; + } +} + +} // namespace ash \ No newline at end of file diff --git a/ashura/engine/views.h b/ashura/engine/views.h index 461c04e86..421f848d8 100644 --- a/ashura/engine/views.h +++ b/ashura/engine/views.h @@ -9,7 +9,6 @@ #include "ashura/std/dyn.h" #include "ashura/std/text.h" #include "ashura/std/types.h" -#include namespace ash { @@ -2048,50 +2047,7 @@ struct ScalarDragBox : View fmt::format(ctx, v); } - static void scalar_parse(Span text, ScalarState &styling) - { - if (text.is_empty()) - { - return; - } - - Vec utf8{default_allocator}; - utf8_encode(text, utf8).unwrap(); - - char const *const first = (char const *) utf8.begin(); - char const *const last = (char const *) utf8.end(); - - switch (styling.base.type) - { - case ScalarInputType::i32: - { - i32 value = 0; - auto [ptr, ec] = std::from_chars(first, last, value); - if (ec != std::errc{} || value < styling.min.i32 || - value > styling.max.i32) - { - return; - } - styling.current = - ScalarInput{.i32 = value, .type = ScalarInputType::i32}; - } - break; - - case ScalarInputType::f32: - { - f32 value = 0; - auto [ptr, ec] = std::from_chars(first, last, value); - if (ec != std::errc{} || value < styling.min.f32 || - value > styling.max.f32) - { - return; - } - styling.current = - ScalarInput{.f32 = value, .type = ScalarInputType::f32}; - } - break; - } - } + static void scalar_parse(Span text, ScalarState &styling); virtual ViewState tick(ViewContext const &ctx, CRect const ®ion, f32, ViewEvents events, Fn build) override diff --git a/ashura/std/format.cc b/ashura/std/format.cc index 2b8f46aa2..d7897ee9b 100644 --- a/ashura/std/format.cc +++ b/ashura/std/format.cc @@ -40,15 +40,15 @@ bool push_int(fmt::Context const &ctx, fmt::Spec const &spec, IntT value) break; } - std::to_chars_result result = + auto [ptr, ec] = std::to_chars(ctx.scratch.begin(), ctx.scratch.end(), value, base); - if (result.ec == std::errc{}) + if (ec != std::errc{}) { - return ctx.push( - Span{ctx.scratch.begin(), (usize) (result.ptr - ctx.scratch.begin())}); + return false; } - return false; + return ctx.push( + Span{ctx.scratch.begin(), (usize) (ptr - ctx.scratch.begin())}); } template