From 1c258cbe6078c4ab9db3d02be644f239055328c6 Mon Sep 17 00:00:00 2001 From: Jan Orend <56254096+3dJan@users.noreply.github.com> Date: Fri, 3 Nov 2023 14:00:55 +0100 Subject: [PATCH] Using fast_float for string parsing --- CMakeLists.txt | 3 +++ Include/Common/NMR_StringUtils.h | 3 +++ Source/Common/NMR_StringUtils.cpp | 13 +++++-------- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 35ff1948a..eb2d10b25 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -158,6 +158,9 @@ else() endif() +target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/submodules/fast_float/include) + + set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" IMPORT_PREFIX "" ) # This makes sure symbols are exported target_compile_options(${PROJECT_NAME} PRIVATE "-D__LIB3MF_EXPORTS") diff --git a/Include/Common/NMR_StringUtils.h b/Include/Common/NMR_StringUtils.h index 99eb31644..b32bc76a1 100644 --- a/Include/Common/NMR_StringUtils.h +++ b/Include/Common/NMR_StringUtils.h @@ -36,6 +36,9 @@ and Exception-safe #include "Common/NMR_Types.h" #include "Common/NMR_Local.h" + +#include + #include #include #include diff --git a/Source/Common/NMR_StringUtils.cpp b/Source/Common/NMR_StringUtils.cpp index 022491aad..e0882f2d7 100644 --- a/Source/Common/NMR_StringUtils.cpp +++ b/Source/Common/NMR_StringUtils.cpp @@ -168,17 +168,14 @@ namespace NMR { #ifdef __NO_FROM_CHARS_FP__ // Convert to double and make a input and range check! - nfChar * pEndPtr; - - dResult = strtod(pszValue, &pEndPtr); + auto answer = fast_float::from_chars(pszValue, pszValue + strlen(pszValue), dResult); // Check if any conversion happened - if ((pEndPtr == pszValue) || (!pEndPtr)) + if (answer.ec != std::errc()) + { throw CNMRException(NMR_ERROR_EMPTYSTRINGTODOUBLECONVERSION); - - if ((*pEndPtr != '\0') && (*pEndPtr != ' ')) - throw CNMRException(NMR_ERROR_INVALIDSTRINGTODOUBLECONVERSION); - + } + if ((dResult == HUGE_VAL) || (dResult == -HUGE_VAL)) throw CNMRException(NMR_ERROR_STRINGTODOUBLECONVERSIONOUTOFRANGE); #else