Include :
- migliorie e ottimizzazioni a ToString con interi.
This commit is contained in:
+26
-24
@@ -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 <size_t size>
|
||||
const std::string ToString( const int (&nVal)[size], int nPrec = 1)
|
||||
|
||||
Reference in New Issue
Block a user