Skip to content

Commit

Permalink
➕ changed scalar parser to fast_float
Browse files Browse the repository at this point in the history
  • Loading branch information
lamarrr committed Nov 28, 2024
1 parent eb1fdd5 commit 5bf498b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 54 deletions.
19 changes: 16 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
46 changes: 45 additions & 1 deletion ashura/engine/views.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,51 @@
/// SPDX-License-Identifier: MIT
#include "ashura/engine/views.h"
#include "fast_float/fast_float.h"

namespace ash
{

}
void ScalarDragBox::scalar_parse(Span<u32 const> text, ScalarState &styling)
{
if (text.is_empty())
{
return;
}

Vec<u8> 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
46 changes: 1 addition & 45 deletions ashura/engine/views.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "ashura/std/dyn.h"
#include "ashura/std/text.h"
#include "ashura/std/types.h"
#include <charconv>

namespace ash
{
Expand Down Expand Up @@ -2048,50 +2047,7 @@ struct ScalarDragBox : View
fmt::format(ctx, v);
}

static void scalar_parse(Span<u32 const> text, ScalarState &styling)
{
if (text.is_empty())
{
return;
}

Vec<u8> 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<u32 const> text, ScalarState &styling);

virtual ViewState tick(ViewContext const &ctx, CRect const &region, f32,
ViewEvents events, Fn<void(View &)> build) override
Expand Down
10 changes: 5 additions & 5 deletions ashura/std/format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 <typename FloatT>
Expand Down

0 comments on commit 5bf498b

Please sign in to comment.