added: to methods Toull(), Toll(), Toul(), Toui(), Tol(), Toi()
a new parameter: bool allow_skip_whitechars default true added: new methods: Toull_b(), Toll_b(), Toul_b(), Toui_b(), Tol_b(), Toi_b() automatically detects the base (radix): 4323 - base 10 0122 - base 8 (string starts with 0) #fff - base 16 (string starts with #) &101 - base 2 (string starts with &) added: CharType * SkipWhiteFromBack(CharType * str, bool check_additional_chars = true, bool treat_new_line_as_white = true) skipping white characters from the end of a string changed: Toll_b(), Toull_b(), Tol_b() and Toul_b() are used in Space now for methods ToInt() etc so we are able to use a different base now changed: some work in Space (const correctness) git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@1070 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
@@ -50,14 +50,16 @@ namespace PT
|
||||
|
||||
|
||||
template<typename CharType>
|
||||
unsigned long long Toull(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0)
|
||||
unsigned long long Toull(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
unsigned long long res = 0;
|
||||
bool carry = false;
|
||||
int digit;
|
||||
|
||||
SetOverflow(was_overflow, false);
|
||||
str = SkipWhite(str);
|
||||
|
||||
if( allow_skip_whitechars )
|
||||
str = SkipWhite(str);
|
||||
|
||||
while( !carry && IsDigit(*str, base, &digit) )
|
||||
{
|
||||
@@ -78,9 +80,12 @@ unsigned long long Toull(const CharType * str, int base = 10, const CharType **
|
||||
|
||||
if( carry )
|
||||
{
|
||||
while( IsDigit(*str, base, &digit) )
|
||||
if( after_str )
|
||||
{
|
||||
str += 1;
|
||||
while( IsDigit(*str, base, &digit) )
|
||||
{
|
||||
str += 1;
|
||||
}
|
||||
}
|
||||
|
||||
SetOverflow(was_overflow, true);
|
||||
@@ -95,13 +100,15 @@ unsigned long long Toull(const CharType * str, int base = 10, const CharType **
|
||||
|
||||
|
||||
template<typename CharType>
|
||||
long long Toll(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0)
|
||||
long long Toll(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
bool was_sign = false;
|
||||
bool was_overflow_u = false;
|
||||
|
||||
SetOverflow(was_overflow, false);
|
||||
str = SkipWhite(str);
|
||||
|
||||
if( allow_skip_whitechars )
|
||||
str = SkipWhite(str);
|
||||
|
||||
if( *str == '-' )
|
||||
{
|
||||
@@ -109,9 +116,7 @@ long long Toll(const CharType * str, int base = 10, const CharType ** after_str
|
||||
str += 1;
|
||||
}
|
||||
|
||||
// we do not trim spaces between a sign and a digit
|
||||
|
||||
unsigned long long uval = Toull(str, base, after_str, &was_overflow_u);
|
||||
unsigned long long uval = Toull(str, base, after_str, &was_overflow_u, false);
|
||||
unsigned long long sign_add = ( was_sign ) ? 1 : 0;
|
||||
|
||||
if( was_overflow_u )
|
||||
@@ -136,13 +141,14 @@ long long Toll(const CharType * str, int base = 10, const CharType ** after_str
|
||||
|
||||
|
||||
|
||||
|
||||
template<typename CharType, typename IntegerType>
|
||||
IntegerType ToUnsignedIntegerType(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0)
|
||||
IntegerType ToUnsignedIntegerType(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
bool was_overflow_ll = false;
|
||||
|
||||
SetOverflow(was_overflow, false);
|
||||
unsigned long long val = Toull(str, base, after_str, &was_overflow_ll);
|
||||
unsigned long long val = Toull(str, base, after_str, &was_overflow_ll, allow_skip_whitechars);
|
||||
|
||||
if( was_overflow_ll || val > static_cast<unsigned long long>(std::numeric_limits<IntegerType>::max()) )
|
||||
{
|
||||
@@ -155,15 +161,15 @@ IntegerType ToUnsignedIntegerType(const CharType * str, int base = 10, const Cha
|
||||
|
||||
|
||||
template<class CharType>
|
||||
unsigned long Toul(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0)
|
||||
unsigned long Toul(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
return ToUnsignedIntegerType<CharType, unsigned long>(str, base, after_str, was_overflow);
|
||||
return ToUnsignedIntegerType<CharType, unsigned long>(str, base, after_str, was_overflow, allow_skip_whitechars);
|
||||
}
|
||||
|
||||
template<class CharType>
|
||||
unsigned int Toui(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0)
|
||||
unsigned int Toui(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
return ToUnsignedIntegerType<CharType, unsigned int>(str, base, after_str, was_overflow);
|
||||
return ToUnsignedIntegerType<CharType, unsigned int>(str, base, after_str, was_overflow, allow_skip_whitechars);
|
||||
}
|
||||
|
||||
|
||||
@@ -171,12 +177,12 @@ unsigned int Toui(const CharType * str, int base = 10, const CharType ** after_s
|
||||
|
||||
|
||||
template<typename CharType, typename IntegerType>
|
||||
IntegerType ToIntegerType(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0)
|
||||
IntegerType ToIntegerType(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
bool was_overflow_ll = false;
|
||||
|
||||
SetOverflow(was_overflow, false);
|
||||
long long val = Toll(str, base, after_str, &was_overflow_ll);
|
||||
long long val = Toll(str, base, after_str, &was_overflow_ll, allow_skip_whitechars);
|
||||
|
||||
if( was_overflow_ll ||
|
||||
val < static_cast<long long>(std::numeric_limits<IntegerType>::min()) ||
|
||||
@@ -192,19 +198,180 @@ IntegerType ToIntegerType(const CharType * str, int base = 10, const CharType **
|
||||
|
||||
|
||||
template<class CharType>
|
||||
long Tol(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0)
|
||||
long Tol(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
return ToIntegerType<CharType, long>(str, base, after_str, was_overflow);
|
||||
return ToIntegerType<CharType, long>(str, base, after_str, was_overflow, allow_skip_whitechars);
|
||||
}
|
||||
|
||||
template<class CharType>
|
||||
int Toi(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0)
|
||||
int Toi(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
return ToIntegerType<CharType, int>(str, base, after_str, was_overflow);
|
||||
return ToIntegerType<CharType, int>(str, base, after_str, was_overflow, allow_skip_whitechars);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
* the base will be automatically detected
|
||||
*
|
||||
* (first digit is [1-9]) - base 10
|
||||
* 0 - 8
|
||||
* # - 16
|
||||
* & - 2
|
||||
*
|
||||
*/
|
||||
template<typename CharType>
|
||||
unsigned long long Toull_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
if( allow_skip_whitechars )
|
||||
str = SkipWhite(str);
|
||||
|
||||
int base = 10;
|
||||
|
||||
if( *str == '0' )
|
||||
{
|
||||
base = 8;
|
||||
str += 1;
|
||||
}
|
||||
else
|
||||
if( *str == '#' )
|
||||
{
|
||||
base = 16;
|
||||
str += 1;
|
||||
}
|
||||
else
|
||||
if( *str == '&' )
|
||||
{
|
||||
base = 2;
|
||||
str += 1;
|
||||
}
|
||||
|
||||
return Toull(str, base, after_str, was_overflow, false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<typename CharType>
|
||||
long long Toll_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
bool was_sign = false;
|
||||
bool was_overflow_u = false;
|
||||
|
||||
SetOverflow(was_overflow, false);
|
||||
|
||||
if( allow_skip_whitechars )
|
||||
str = SkipWhite(str);
|
||||
|
||||
if( *str == '-' )
|
||||
{
|
||||
was_sign = true;
|
||||
str += 1;
|
||||
}
|
||||
|
||||
unsigned long long uval = Toull_b(str, after_str, &was_overflow_u, false);
|
||||
unsigned long long sign_add = ( was_sign ) ? 1 : 0;
|
||||
|
||||
if( was_overflow_u )
|
||||
{
|
||||
SetOverflow(was_overflow, true);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if( uval > static_cast<unsigned long long>(std::numeric_limits<long long>::max()) + sign_add )
|
||||
{
|
||||
SetOverflow(was_overflow, true);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if( was_sign )
|
||||
{
|
||||
return static_cast<long long>(0) - static_cast<long long>(uval);
|
||||
}
|
||||
|
||||
return static_cast<long long>(uval);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template<typename CharType, typename IntegerType>
|
||||
IntegerType ToUnsignedIntegerType_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
bool was_overflow_ll = false;
|
||||
|
||||
SetOverflow(was_overflow, false);
|
||||
unsigned long long val = Toull_b(str, after_str, &was_overflow_ll, allow_skip_whitechars);
|
||||
|
||||
if( was_overflow_ll || val > static_cast<unsigned long long>(std::numeric_limits<IntegerType>::max()) )
|
||||
{
|
||||
SetOverflow(was_overflow, true);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return static_cast<IntegerType>(val);
|
||||
}
|
||||
|
||||
|
||||
template<class CharType>
|
||||
unsigned long Toul_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
return ToUnsignedIntegerType_b<CharType, unsigned long>(str, after_str, was_overflow, allow_skip_whitechars);
|
||||
}
|
||||
|
||||
template<class CharType>
|
||||
unsigned int Toui_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
return ToUnsignedIntegerType_b<CharType, unsigned int>(str, after_str, was_overflow, allow_skip_whitechars);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template<typename CharType, typename IntegerType>
|
||||
IntegerType ToIntegerType_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
bool was_overflow_ll = false;
|
||||
|
||||
SetOverflow(was_overflow, false);
|
||||
long long val = Toll_b(str, after_str, &was_overflow_ll, allow_skip_whitechars);
|
||||
|
||||
if( was_overflow_ll ||
|
||||
val < static_cast<long long>(std::numeric_limits<IntegerType>::min()) ||
|
||||
val > static_cast<long long>(std::numeric_limits<IntegerType>::max()) )
|
||||
{
|
||||
SetOverflow(was_overflow, true);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return static_cast<IntegerType>(val);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class CharType>
|
||||
long Tol_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
return ToIntegerType_b<CharType, long>(str, after_str, was_overflow, allow_skip_whitechars);
|
||||
}
|
||||
|
||||
template<class CharType>
|
||||
int Toi_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
return ToIntegerType_b<CharType, int>(str, after_str, was_overflow, allow_skip_whitechars);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user