我使用unsigned int处理INT_MIN绝对值比INT_MAX大的情况,
对于overflow, 我分两步, 一是判断unsigned int有没有overflow,
一个是判断unsigned int没有overflow的情况下int有没有overflow
class CException
{
public:
CException(const char* str) : m_strErr(str) {}
string GetErrInfo() { return m_strErr; }
private:
std::string m_strErr;
};
int myatoi(const char* p)
{
assert(p);
//Judge signal
bool bNeg = false;
const char* pCur = p;
if(*pCur == '-' || *pCur == '+')
{
if (*pCur++ == '-')
bNeg = true;
}
//treat the rest as positive numeric number, so for negative number
//it is 2^31, and for positive number it is 2^31
unsigned int uLimit = bNeg ? 0x7FFFFFFF + 1 : 0x7FFFFFFF;
// The reson to use unsigned int rather than int is to deal with 2^31
unsigned int uRes = 0;
unsigned int uDummy = UINT_MAX/10; // used to judge overflow of unsigned
int
do
{
//Deals with illegal character
if (*pCur < '0' || *pCur > '9')
throw CException("Illegal character found");
//Get current digit
int nDigit = *pCur - '0';
//Detect the unsigned integer overflow
if (uRes > uDummy || (uRes == uDummy && nDigit > (UINT_MAX - (UINT_
MAX/10)*10)))
throw CException("Over flow detected");
//update uRes
uRes = uRes*10 + nDigit;
//Overflow situation if uRes isn't overflow
if (uRes > uLimit)
throw CException("Over flow detected");
pCur++;
}while (*pCur != 0); // WTF stupid error again, do while not while you
mother fucker!!!
return bNeg ? (int)(-uRes) : (int)(uRes);
}