This is the mail archive of the
libstdc++@sourceware.cygnus.com
mailing list for the libstdc++ project.
Re: strtoul etc.
Ryszard Kabatek wrote:
> Below a sample of the simplest (base == 10) conversion
> from a string to an integer.
Are you quite sure it's the simplest?
template <typename Iter, typename Int>
bool AtoN10 (Iter first, Iter last, Int& value)
{
assert (numeric_limits<Int>::is_integer);
typedef char_traits<Iter::value_type> Traits;
Int const max_val = numeric_limits<Int>::max();
Int const base = 10;
Int const cutoff = max_val / base;
Int const cutlim = max_val % base;
Traits::char_type const nought = Traits::to_char_type ('0');
value = 0;
Traits::char_type c;
int sign = 1;
// Check the sign
if (first != last && !isdigit(c = *first)) {
if (c == Traits::to_char_type ('-')) {
if (!numeric_limits<Int>::is_signed)
return false;
sign = -1;
++first;
}
else if (c == Traits::to_char_type ('+'))
++first;
else
return false;
}
if (first == last)
return false;
// Skip leading zeroes
while (first != last && isdigit (c = *first) && c == nought)
++first;
while (first != last && isdigit (c = *first)) {
Int n = c - nought;
if (value > cutoff || (value == cutoff && n > cutlim))
return false;
value *= base;
value += n;
++first;
}
value *= sign;
if (first != last)
return false;
return true;
}
> I think we should decide if we want to go this way.
> I suppose it depends on the answer to the question:
>
> Does somebody want to implement the necessary functions?
No :-)
--
Branko Čibej <branko.cibej@hermes.si>
HERMES SoftLab, Litijska 51, 1000 Ljubljana, Slovenia
voice: (+386 61) 186 53 49 fax: (+386 61) 186 52 70