diff --git a/src/fstring.cpp b/src/fstring.cpp index 43b2a5e2..4aa91503 100644 --- a/src/fstring.cpp +++ b/src/fstring.cpp @@ -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); } //---------------------------------------------------------------------- diff --git a/src/fstring.h b/src/fstring.h index 0f53591e..13ef0523 100644 --- a/src/fstring.h +++ b/src/fstring.h @@ -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::split (std::wstring& s) { return split(FString(s)); } diff --git a/test/fstring.cpp b/test/fstring.cpp index 3b7bae88..2b93a266 100644 --- a/test/fstring.cpp +++ b/test/fstring.cpp @@ -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) {