parent
ba0ce02134
commit
ea980e5ec0
110
integer.cpp
110
integer.cpp
|
|
@ -3021,7 +3021,7 @@ Integer::Integer(word value, size_t length)
|
||||||
template <class T>
|
template <class T>
|
||||||
static Integer StringToInteger(const T *str)
|
static Integer StringToInteger(const T *str)
|
||||||
{
|
{
|
||||||
int radix;
|
int radix, sign = 1;
|
||||||
// GCC workaround
|
// GCC workaround
|
||||||
// std::char_traits<wchar_t>::length() not defined in GCC 3.2 and STLport 4.5.3
|
// std::char_traits<wchar_t>::length() not defined in GCC 3.2 and STLport 4.5.3
|
||||||
unsigned int length;
|
unsigned int length;
|
||||||
|
|
@ -3030,7 +3030,7 @@ static Integer StringToInteger(const T *str)
|
||||||
Integer v;
|
Integer v;
|
||||||
|
|
||||||
if (length == 0)
|
if (length == 0)
|
||||||
return v;
|
return Integer::Zero();
|
||||||
|
|
||||||
switch (str[length-1])
|
switch (str[length-1])
|
||||||
{
|
{
|
||||||
|
|
@ -3050,30 +3050,102 @@ static Integer StringToInteger(const T *str)
|
||||||
radix=10;
|
radix=10;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (length > 2 && str[0] == '0' && str[1] == 'x')
|
// 'str' is of length 1 or more
|
||||||
radix = 16;
|
if (str[0] == '-')
|
||||||
|
|
||||||
for (unsigned i=0; i<length; i++)
|
|
||||||
{
|
{
|
||||||
int digit;
|
sign = -1;
|
||||||
|
str += 1, length -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
if (str[i] >= '0' && str[i] <= '9')
|
if (length > 2 && str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
|
||||||
digit = str[i] - '0';
|
{
|
||||||
else if (str[i] >= 'A' && str[i] <= 'F')
|
radix = 16;
|
||||||
digit = str[i] - 'A' + 10;
|
str += 2, length -= 2;
|
||||||
else if (str[i] >= 'a' && str[i] <= 'f')
|
}
|
||||||
digit = str[i] - 'a' + 10;
|
|
||||||
else
|
|
||||||
digit = radix;
|
|
||||||
|
|
||||||
if (digit < radix)
|
if(radix == 16 && order == LITTLE_ENDIAN_ORDER)
|
||||||
|
{
|
||||||
|
// Nibble high, low and count
|
||||||
|
unsigned int nh, nl, nc = 0;
|
||||||
|
Integer position(Integer::One());
|
||||||
|
|
||||||
|
for (unsigned int i=0; i<length; i++)
|
||||||
{
|
{
|
||||||
v *= radix;
|
int digit, ch = static_cast<int>(str[i]);
|
||||||
v += digit;
|
|
||||||
|
if (ch >= '0' && ch <= '9')
|
||||||
|
digit = ch - '0';
|
||||||
|
else if (ch >= 'A' && ch <= 'F')
|
||||||
|
digit = ch - 'A' + 10;
|
||||||
|
else if (ch >= 'a' && ch <= 'f')
|
||||||
|
digit = ch - 'a' + 10;
|
||||||
|
else
|
||||||
|
digit = radix;
|
||||||
|
|
||||||
|
if (digit < radix)
|
||||||
|
{
|
||||||
|
if(nc++ == 0)
|
||||||
|
nh = digit;
|
||||||
|
else
|
||||||
|
nl = digit;
|
||||||
|
|
||||||
|
if(nc == 2)
|
||||||
|
{
|
||||||
|
v += position * (nh << 4 | nl);
|
||||||
|
nc = 0, position <<= 8;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(nc == 1)
|
||||||
|
v += nh * position;
|
||||||
|
}
|
||||||
|
else if(order == LITTLE_ENDIAN_ORDER)
|
||||||
|
{
|
||||||
|
for (int i=static_cast<int>(length)-1; i>=0; i--)
|
||||||
|
{
|
||||||
|
int digit, ch = static_cast<int>(str[i]);
|
||||||
|
|
||||||
|
if (ch >= '0' && ch <= '9')
|
||||||
|
digit = ch - '0';
|
||||||
|
else if (ch >= 'A' && ch <= 'F')
|
||||||
|
digit = ch - 'A' + 10;
|
||||||
|
else if (ch >= 'a' && ch <= 'f')
|
||||||
|
digit = ch - 'a' + 10;
|
||||||
|
else
|
||||||
|
digit = radix;
|
||||||
|
|
||||||
|
if (digit < radix)
|
||||||
|
{
|
||||||
|
v *= radix;
|
||||||
|
v += digit;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (unsigned int i=0; i<length; i++)
|
||||||
|
{
|
||||||
|
int digit, ch = static_cast<int>(str[i]);
|
||||||
|
|
||||||
|
if (ch >= '0' && ch <= '9')
|
||||||
|
digit = ch - '0';
|
||||||
|
else if (ch >= 'A' && ch <= 'F')
|
||||||
|
digit = ch - 'A' + 10;
|
||||||
|
else if (ch >= 'a' && ch <= 'f')
|
||||||
|
digit = ch - 'a' + 10;
|
||||||
|
else
|
||||||
|
digit = radix;
|
||||||
|
|
||||||
|
if (digit < radix)
|
||||||
|
{
|
||||||
|
v *= radix;
|
||||||
|
v += digit;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (str[0] == '-')
|
if (sign == -1)
|
||||||
v.Negate();
|
v.Negate();
|
||||||
|
|
||||||
return v;
|
return v;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue