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 3, 2023
1 parent 00a2050 commit f33ac2a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
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 @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions Source/Model/Reader/v093/NMR_ModelReaderNode093_Vertex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions Source/Model/Reader/v100/NMR_ModelReaderNode100_Vertex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,23 @@ 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)
throw CNMRException(NMR_ERROR_INVALIDMODELCOORDINATES);
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)
throw CNMRException(NMR_ERROR_INVALIDMODELCOORDINATES);
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)
Expand Down

0 comments on commit f33ac2a

Please sign in to comment.