From 4b73f702325d3d7cd90415e7db1886e3d5bf2b7f Mon Sep 17 00:00:00 2001 From: Dario Sassi Date: Fri, 10 Jan 2025 16:27:56 +0100 Subject: [PATCH] Include : - migliorie e ottimizzazioni a ToString con interi. --- EGnStringUtils.h | 50 +++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/EGnStringUtils.h b/EGnStringUtils.h index 2b20a4a..8cf93e5 100644 --- a/EGnStringUtils.h +++ b/EGnStringUtils.h @@ -184,45 +184,47 @@ EGN_EXPORT bool FromString( const std::string& sVal, DBLVECTOR& vdVal) ; EGN_EXPORT bool FromString( const std::string& sVal, STRVECTOR& vsVal) ; //---------------------------------------------------------------------------- +EGN_EXPORT const std::string ToStringAdv( int nVal, int nPrec = 1, int nRadix = 10, int* pnErr = nullptr) ; inline const std::string -ToString( int nVal, int nPrec = 1, int nRadix = 10) +ToString( int nVal, int nPrec = 1, int nRadix = 10, int* pnErr = nullptr) { + // se necessario processing avanzato + if ( nPrec > 1 || nRadix < 6) + return ToStringAdv( nVal, nPrec, nRadix, pnErr) ; // eseguo conversione - const int nBuffSize = 36 ; + const int nBuffSize = 16 ; char szBuff[nBuffSize]{} ; auto Res = std::to_chars( szBuff, szBuff + nBuffSize - 1, nVal, nRadix) ; if ( Res.ec != std::errc()) { - std::string sOut = make_error_code( Res.ec).message() ; - return "#Error (" + sOut + ")" ; + if ( pnErr != nullptr) + *pnErr = int( Res.ec) ; + return "#Error" ; } - // verifico lunghezza minima - int nLen = (int) strlen( szBuff) ; - if ( nLen >= nPrec) - return szBuff ; - // porto la stringa alla minima lunghezza - std::string sBuff( szBuff) ; - sBuff.insert( 0, ( nPrec - nLen), '0') ; - return sBuff ; + // gestione codice di errore + if ( pnErr != nullptr) + *pnErr = 0 ; + return szBuff ; } +EGN_EXPORT const std::string ToStringAdv( unsigned int nVal, int nPrec = 1, int nRadix = 10, int* pnErr = nullptr) ; inline const std::string -ToString( unsigned int nVal, int nPrec = 1, int nRadix = 10) +ToString( unsigned int nVal, int nPrec = 1, int nRadix = 10, int* pnErr = nullptr) { + // se necessario processing avanzato + if ( nPrec > 1 || nRadix < 6) + return ToStringAdv( nVal, nPrec, nRadix, pnErr) ; // eseguo conversione - const int nBuffSize = 36 ; + const int nBuffSize = 16 ; char szBuff[nBuffSize]{} ; auto Res = std::to_chars( szBuff, szBuff + nBuffSize - 1, nVal, nRadix) ; if ( Res.ec != std::errc()) { - std::string sOut = make_error_code( Res.ec).message() ; - return "#Error (" + sOut + ")" ; + if ( pnErr != nullptr) + *pnErr = int( Res.ec) ; + return "#Error" ; } - // verifico lunghezza minima - int nLen = (int) strlen( szBuff) ; - if ( nLen >= nPrec) - return szBuff ; - // porto la stringa alla minima lunghezza - std::string sBuff( szBuff) ; - sBuff.insert( 0, ( nPrec - nLen), '0') ; - return sBuff ; + // gestione codice di errore + if ( pnErr != nullptr) + *pnErr = 0 ; + return szBuff ; } template const std::string ToString( const int (&nVal)[size], int nPrec = 1)