Do string validation before parsing.

...this is NOT the right place for this.

At some point, we need to either use std::stof directly
and/or just accept that we need to use exceptions.
This commit is contained in:
Jason Felds
2013-05-04 13:43:54 -04:00
parent fc2b198ce2
commit a8fe2634ca
2 changed files with 18 additions and 8 deletions
+15 -7
View File
@@ -1753,19 +1753,27 @@ void MakeLower( wchar_t *p, size_t iLen )
float StringToFloat( const RString &sString )
{
float ret = strtof( sString, nullptr );
if( !isfinite(ret) )
ret = 0.0f;
return ret;
RString toTrim = sString;
Trim(toTrim);
if (toTrim.size() == 0)
{
return 0;
}
return std::stof(toTrim);
}
bool StringToFloat( const RString &sString, float &fOut )
{
RString toTrim = sString;
Trim(toTrim);
if (toTrim.size() == 0)
{
return false;
}
char *endPtr;
fOut = strtof( sString, &endPtr );
return sString.size() && *endPtr == '\0' && isfinite( fOut );
fOut = strtof( toTrim, &endPtr );
return *endPtr == '\0' && isfinite( fOut );
}
RString FloatToString( const float &num )
+3 -1
View File
@@ -406,7 +406,10 @@ void MakeLower( char *p, size_t iLen );
void MakeUpper( wchar_t *p, size_t iLen );
void MakeLower( wchar_t *p, size_t iLen );
// TODO: Have the three functions below be moved to better locations.
float StringToFloat( const RString &sString );
bool StringToFloat( const RString &sString, float &fOut );
/**
* @brief Have a standard way of converting floats to strings.
*
@@ -414,7 +417,6 @@ float StringToFloat( const RString &sString );
* @param num the number to convert.
* @return the string as trimmed as it can be. */
RString FloatToString( const float &num );
bool StringToFloat( const RString &sString, float &fOut );
RString WStringToRString( const wstring &sString );
RString WcharToUTF8( wchar_t c );