From fd4b84807f184f5259afb89b18cdc288f48176b2 Mon Sep 17 00:00:00 2001 From: Dario Sassi Date: Fri, 3 Jan 2025 12:16:52 +0100 Subject: [PATCH] Include : - modifiche a FromString con double per uso libreria fast_float. --- EGnStringUtils.h | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/EGnStringUtils.h b/EGnStringUtils.h index 34002ac..6b828e1 100644 --- a/EGnStringUtils.h +++ b/EGnStringUtils.h @@ -15,6 +15,7 @@ #include "/EgtDev/Include/EgtStringBase.h" #include "/EgtDev/Include/EgtNumCollection.h" +#include "/EgtDev/Extern/fast_float/fast_float.h" #include #include @@ -123,6 +124,7 @@ ValidateDxfName( std::string& sName, bool bAdvanced) } //---------------------------------------------------------------------------- +#define FAST_FLOAT_FMT fast_float::chars_format::general | fast_float::chars_format::allow_leading_plus | fast_float::chars_format::skip_white_space inline bool FromString( const std::string& sVal, int& nVal) { const char* pStart = sVal.c_str() ; @@ -146,11 +148,8 @@ FromString( const std::string& sVal, bool& bVal) return true ; } inline bool FromString( const std::string& sVal, double& dVal) - { const char* pStart = sVal.c_str() ; - char* pStop ; - errno = 0 ; - dVal = strtod( pStart, &pStop) ; - return ( pStop != pStart && errno == 0) ; } + { auto answer = fast_float::from_chars( sVal.data(), sVal.data() + sVal.size(), dVal, FAST_FLOAT_FMT) ; + return ( answer.ec == std::errc() && answer.ptr != sVal.data()) ; } template bool FromString( const std::string& sVal, int (&nVal)[size]) { const char* pStart = sVal.c_str() ; @@ -166,29 +165,25 @@ bool FromString( const std::string& sVal, int (&nVal)[size]) } template bool FromString( const std::string& sVal, double (&dVal)[size]) - { const char* pStart = sVal.c_str() ; - char* pStop ; - errno = 0 ; + { const char* pFirst = sVal.data() ; for ( int i = 0 ; i < size ; ++ i) { - dVal[i] = strtod( pStart, &pStop) ; - if ( ( i < size - 1 && *pStop != ',') || errno != 0) + auto answer = fast_float::from_chars( pFirst, sVal.data() + sVal.size(), dVal[i], FAST_FLOAT_FMT) ; + if ( answer.ec != std::errc() || ( i < size - 1 && answer.ptr[0] != ',')) return false ; - pStart = pStop + 1 ; + pFirst = answer.ptr + 1 ; } - return ( errno == 0) ; + return true ; } template bool FromString( const std::string& sVal, std::array& dVal) - { const char* pStart = sVal.c_str() ; - char* pStop ; - errno = 0 ; + { const char* pFirst = sVal.data() ; for ( int i = 0 ; i < size ; ++ i) { - dVal[i] = strtod( pStart, &pStop) ; - if ( ( i < size - 1 && *pStop != ',') || errno != 0) + auto answer = fast_float::from_chars( pFirst, sVal.data() + sVal.size(), dVal[i], FAST_FLOAT_FMT) ; + if ( answer.ec != std::errc() || ( i < size - 1 && answer.ptr[0] != ',')) return false ; - pStart = pStop + 1 ; + pFirst = answer.ptr + 1 ; } - return ( errno == 0) ; + return true ; } EGN_EXPORT bool FromString( const std::string& sVal, INTVECTOR& vnVal) ; EGN_EXPORT bool FromString( const std::string& sVal, DBLVECTOR& vdVal) ;