From d379137f7010b24a8ef39259076773f2e810faa4 Mon Sep 17 00:00:00 2001 From: Dario Sassi Date: Fri, 9 Jan 2026 15:39:40 +0100 Subject: [PATCH] Include : - tutte le funzioni ToStringAdv sono diventate inline nell'header. --- EGnStringUtils.h | 77 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 74 insertions(+), 3 deletions(-) diff --git a/EGnStringUtils.h b/EGnStringUtils.h index 5cb5de0..e15724a 100644 --- a/EGnStringUtils.h +++ b/EGnStringUtils.h @@ -189,7 +189,30 @@ 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 +ToStringAdv( int nVal, int nPrec = 1, int nRadix = 10, int* pnErr = nullptr) + { + // eseguo conversione + const int nBuffSize = 36 ; + char szBuff[nBuffSize]{} ; + auto Res = std::to_chars( szBuff, szBuff + nBuffSize - 1, nVal, nRadix) ; + if ( Res.ec != std::errc()) { + if ( pnErr != nullptr) + *pnErr = int( Res.ec) ; + return "#Error" ; + } + // gestione codice di errore + if ( pnErr != nullptr) + *pnErr = 0 ; + // 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 ; + } inline const std::string ToString( int nVal, int nPrec = 1, int nRadix = 10, int* pnErr = nullptr) { @@ -210,7 +233,31 @@ ToString( int nVal, int nPrec = 1, int nRadix = 10, int* 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 +ToStringAdv( unsigned int nVal, int nPrec = 1, int nRadix = 10, int* pnErr = nullptr) + { + // eseguo conversione + const int nBuffSize = 36 ; + char szBuff[nBuffSize]{} ; + auto Res = std::to_chars( szBuff, szBuff + nBuffSize - 1, nVal, nRadix) ; + if ( Res.ec != std::errc()) { + if ( pnErr != nullptr) + *pnErr = int( Res.ec) ; + return "#Error" ; + } + // gestione codice di errore + if ( pnErr != nullptr) + *pnErr = 0 ; + // 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 ; + } inline const std::string ToString( unsigned int nVal, int nPrec = 1, int nRadix = 10, int* pnErr = nullptr) { @@ -231,7 +278,31 @@ ToString( unsigned int nVal, int nPrec = 1, int nRadix = 10, int* pnErr = nullpt *pnErr = 0 ; return szBuff ; } -EGN_EXPORT const std::string ToStringAdv( long long nVal, int nPrec = 1, int nRadix = 10, int* pnErr = nullptr) ; + +inline const std::string +ToStringAdv( long long nVal, int nPrec = 1, int nRadix = 10, int* pnErr = nullptr) + { + // eseguo conversione + const int nBuffSize = 68 ; + char szBuff[nBuffSize]{} ; + auto Res = std::to_chars( szBuff, szBuff + nBuffSize - 1, nVal, nRadix) ; + if ( Res.ec != std::errc()) { + if ( pnErr != nullptr) + *pnErr = int( Res.ec) ; + return "#Error" ; + } + // gestione codice di errore + if ( pnErr != nullptr) + *pnErr = 0 ; + // 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 ; + } inline const std::string ToString( long long nVal, int nPrec = 1, int nRadix = 10, int* pnErr = nullptr) {