To anyone offering atoi:
- My atoi () does not install errno.
- The implementation of my atoi () does not return INT_MIN or INT_MAX on overflow.
- We cannot rely on a sign reversal. Consider 0x4000 ... 0.
- * 2 and the negative bit is set.
- * 4, and the value is zero.
- With base-10 numbers, our next digit will multiply this by 10.
These are all nuts. If your lexer does not read the numeric data, stop the premature optimization . It only leads to grief.
This approach may not be effective, but it is suitable for your needs:
const char * p = "1234567890123"; int i = atoi( p ); ostringstream o; o << i; return o.str() == p;
Or using the stack:
const char * p = "1234567890123"; int i = atoi( p ); char buffer [ 12 ]; snprintf( buffer, 12, "%d", i ); return strcmp(buffer,p) == 0;
source share