Skip to content

Commit

Permalink
Using fast_float for string parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
3dJan committed Nov 8, 2023
1 parent 8beaf20 commit 1c258cb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
3 changes: 3 additions & 0 deletions Include/Common/NMR_StringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ and Exception-safe

#include "Common/NMR_Types.h"
#include "Common/NMR_Local.h"

#include <fast_float/fast_float.h>

#include <string>
#include <string.h>
#include <vector>
Expand Down
13 changes: 5 additions & 8 deletions Source/Common/NMR_StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1c258cb

Please sign in to comment.