Class FString: improve integer exception handling

This commit is contained in:
Markus Gans 2015-06-30 09:29:49 +02:00
parent 97cefa927f
commit 65626b98a2
3 changed files with 53 additions and 21 deletions

View File

@ -927,6 +927,54 @@ FString FString::toUpper() const
return s;
}
//----------------------------------------------------------------------
sInt16 FString::toShort() const
{
register long num;
num = this->toLong();
if ( num > SHRT_MAX || num < SHRT_MIN )
throw std::overflow_error ("overflow");
return sInt16(num);
}
//----------------------------------------------------------------------
uInt16 FString::toUShort() const
{
register uLong num;
num = this->toLong();
if ( num > USHRT_MAX )
throw std::overflow_error ("overflow");
return uInt16(num);
}
//----------------------------------------------------------------------
int FString::toInt() const
{
register long num;
num = this->toLong();
if ( num > INT_MAX || num < INT_MIN )
throw std::overflow_error ("overflow");
return int(num);
}
//----------------------------------------------------------------------
uInt FString::toUInt() const
{
register uLong num;
num = this->toLong();
if ( num > UINT_MAX )
throw std::overflow_error ("overflow");
return uInt(num);
}
//----------------------------------------------------------------------
long FString::toLong() const
{
@ -1024,13 +1072,13 @@ uLong FString::toULong() const
//----------------------------------------------------------------------
float FString::toFloat() const
{
double d;
d = this->toDouble();
register double num;
num = this->toDouble();
if ( d > FLT_MAX || d < FLT_MIN )
if ( num > FLT_MAX || num < FLT_MIN )
throw std::overflow_error ("overflow");
return float(d);
return float(num);
}
//----------------------------------------------------------------------

View File

@ -320,22 +320,6 @@ inline wchar_t FString::back() const
return string[length-1];
}
//----------------------------------------------------------------------
inline sInt16 FString::toShort() const
{ return sInt16( toLong() ); }
//----------------------------------------------------------------------
inline uInt16 FString::toUShort() const
{ return uInt16( toULong() ); }
//----------------------------------------------------------------------
inline int FString::toInt() const
{ return int( toLong() ); }
//----------------------------------------------------------------------
inline uInt FString::toUInt() const
{ return uInt( toULong() ); }
//----------------------------------------------------------------------
inline std::vector<FString> FString::split (std::wstring& s)
{ return split(FString(s)); }

View File

@ -134,7 +134,7 @@ int main (int, char**)
try
{
long long_num = FString("-9876543210").toLong();
std::cout << " toLong: " << long_num << std::endl;
std::cout << " toLong: " << long_num << std::endl;
}
catch (const std::invalid_argument& ex)
{