From 00a205011be5ff0f629f3048fec22d14a5a6ed8d Mon Sep 17 00:00:00 2001 From: Jan Orend <56254096+3dJan@users.noreply.github.com> Date: Fri, 3 Nov 2023 14:00:14 +0100 Subject: [PATCH 1/2] Adding fast_float as submodule --- .gitmodules | 3 +++ submodules/fast_float | 1 + 2 files changed, 4 insertions(+) create mode 160000 submodules/fast_float diff --git a/.gitmodules b/.gitmodules index b230a93cf..ce92af3a7 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,6 @@ [submodule "submodules/AutomaticComponentToolkit"] path = submodules/AutomaticComponentToolkit url = https://github.com/Autodesk/AutomaticComponentToolkit.git +[submodule "submodules/fast_float"] + path = submodules/fast_float + url = https://github.com/fastfloat/fast_float.git diff --git a/submodules/fast_float b/submodules/fast_float new file mode 160000 index 000000000..1dfad243e --- /dev/null +++ b/submodules/fast_float @@ -0,0 +1 @@ +Subproject commit 1dfad243ec42a0459a786a621be0b1f8c8a8287e From f33ac2a4912a7a8948f30151c156cda6b803aabe 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 2/2] Using fast_float for string parsing --- CMakeLists.txt | 3 +++ Include/Common/NMR_StringUtils.h | 3 +++ Source/Common/NMR_StringUtils.cpp | 13 +++++-------- .../v093/NMR_ModelReaderNode093_TextureVertex.cpp | 4 ++-- .../Reader/v093/NMR_ModelReaderNode093_Vertex.cpp | 6 +++--- .../Reader/v100/NMR_ModelReaderNode100_Vertex.cpp | 6 +++--- 6 files changed, 19 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a20f84eb8..dd19e0a60 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -144,6 +144,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 a6d37ddca..63166d6e8 100644 --- a/Source/Common/NMR_StringUtils.cpp +++ b/Source/Common/NMR_StringUtils.cpp @@ -154,17 +154,14 @@ namespace NMR { nfDouble dResult = 0.0; // 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); diff --git a/Source/Model/Reader/v093/NMR_ModelReaderNode093_TextureVertex.cpp b/Source/Model/Reader/v093/NMR_ModelReaderNode093_TextureVertex.cpp index 3f79b9729..96c4e32d0 100644 --- a/Source/Model/Reader/v093/NMR_ModelReaderNode093_TextureVertex.cpp +++ b/Source/Model/Reader/v093/NMR_ModelReaderNode093_TextureVertex.cpp @@ -86,7 +86,7 @@ namespace NMR { __NMRASSERT(pAttributeValue); if (strcmp(pAttributeName, XML_3MF_ATTRIBUTE_TEXTURE_U) == 0) { - m_fU = strtof(pAttributeValue, nullptr); + m_fU = fnStringToFloat(pAttributeValue); if (std::isnan (m_fU)) throw CNMRException(NMR_ERROR_INVALIDMODELTEXTURECOORDINATES); if (fabs (m_fU) > XML_3MF_MAXIMUMCOORDINATEVALUE) @@ -95,7 +95,7 @@ namespace NMR { } if (strcmp(pAttributeName, XML_3MF_ATTRIBUTE_TEXTURE_V) == 0) { - m_fV = strtof(pAttributeValue, nullptr); + m_fV = fnStringToFloat(pAttributeValue); if (std::isnan (m_fV)) throw CNMRException(NMR_ERROR_INVALIDMODELTEXTURECOORDINATES); if (fabs(m_fV) > XML_3MF_MAXIMUMCOORDINATEVALUE) diff --git a/Source/Model/Reader/v093/NMR_ModelReaderNode093_Vertex.cpp b/Source/Model/Reader/v093/NMR_ModelReaderNode093_Vertex.cpp index a7502b5c2..5c8d7aeee 100644 --- a/Source/Model/Reader/v093/NMR_ModelReaderNode093_Vertex.cpp +++ b/Source/Model/Reader/v093/NMR_ModelReaderNode093_Vertex.cpp @@ -82,7 +82,7 @@ namespace NMR { __NMRASSERT(pAttributeValue); if (strcmp(pAttributeName, XML_3MF_ATTRIBUTE_VERTEX_X) == 0) { - m_fX = strtof(pAttributeValue, nullptr); + m_fX = fnStringToFloat(pAttributeValue); if (std::isnan (m_fX)) throw CNMRException(NMR_ERROR_INVALIDMODELCOORDINATES); if (fabs (m_fX) > XML_3MF_MAXIMUMCOORDINATEVALUE) @@ -91,7 +91,7 @@ namespace NMR { } if (strcmp(pAttributeName, XML_3MF_ATTRIBUTE_VERTEX_Y) == 0) { - m_fY = strtof(pAttributeValue, nullptr); + m_fY = fnStringToFloat(pAttributeValue); if (std::isnan (m_fY)) throw CNMRException(NMR_ERROR_INVALIDMODELCOORDINATES); if (fabs(m_fY) > XML_3MF_MAXIMUMCOORDINATEVALUE) @@ -100,7 +100,7 @@ namespace NMR { } if (strcmp(pAttributeName, XML_3MF_ATTRIBUTE_VERTEX_Z) == 0) { - m_fZ = strtof(pAttributeValue, nullptr); + m_fZ = fnStringToFloat(pAttributeValue); if (std::isnan (m_fZ)) throw CNMRException(NMR_ERROR_INVALIDMODELCOORDINATES); if (fabs(m_fZ) > XML_3MF_MAXIMUMCOORDINATEVALUE) diff --git a/Source/Model/Reader/v100/NMR_ModelReaderNode100_Vertex.cpp b/Source/Model/Reader/v100/NMR_ModelReaderNode100_Vertex.cpp index 797e782e6..a2cc72984 100644 --- a/Source/Model/Reader/v100/NMR_ModelReaderNode100_Vertex.cpp +++ b/Source/Model/Reader/v100/NMR_ModelReaderNode100_Vertex.cpp @@ -82,7 +82,7 @@ namespace NMR { __NMRASSERT(pAttributeValue); if (strcmp(pAttributeName, XML_3MF_ATTRIBUTE_VERTEX_X) == 0) { - m_fX = strtof(pAttributeValue, nullptr); + m_fX = fnStringToFloat(pAttributeValue); if (std::isnan (m_fX)) throw CNMRException(NMR_ERROR_INVALIDMODELCOORDINATES); if (fabs (m_fX) > XML_3MF_MAXIMUMCOORDINATEVALUE) @@ -90,7 +90,7 @@ namespace NMR { m_bHasX = true; } else if (strcmp(pAttributeName, XML_3MF_ATTRIBUTE_VERTEX_Y) == 0) { - m_fY = strtof(pAttributeValue, nullptr); + m_fY = fnStringToFloat(pAttributeValue); if (std::isnan (m_fY)) throw CNMRException(NMR_ERROR_INVALIDMODELCOORDINATES); if (fabs(m_fY) > XML_3MF_MAXIMUMCOORDINATEVALUE) @@ -98,7 +98,7 @@ namespace NMR { m_bHasY = true; } else if (strcmp(pAttributeName, XML_3MF_ATTRIBUTE_VERTEX_Z) == 0) { - m_fZ = strtof(pAttributeValue, nullptr); + m_fZ = fnStringToFloat(pAttributeValue); if (std::isnan (m_fZ)) throw CNMRException(NMR_ERROR_INVALIDMODELCOORDINATES); if (fabs(m_fZ) > XML_3MF_MAXIMUMCOORDINATEVALUE)