Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

61 lines
1.7 KiB

  1. #include "scan.h"
  2. static const unsigned int maxint = ((unsigned int)-1)>>1;
  3. size_t scan_int(const char* src,int* dest) {
  4. register const char *tmp;
  5. register int l;
  6. register unsigned char c;
  7. unsigned int neg;
  8. int ok;
  9. tmp=src; l=0; ok=0; neg=0;
  10. switch (*tmp) {
  11. case '-': neg=1; /* fall through */
  12. case '+': ++tmp;
  13. }
  14. while ((c=(unsigned char)(*tmp-'0'))<10) {
  15. unsigned int n;
  16. #if defined(__GNUC__) && (__GNUC__ >= 5)
  17. if (__builtin_mul_overflow(l,10,&n) || __builtin_add_overflow(n,c,&n))
  18. break;
  19. #else
  20. /* we want to do: l=l*10+c
  21. * but we need to check for integer overflow.
  22. * to check whether l*10 overflows, we could do
  23. * if ((l*10)/10 != l)
  24. * however, multiplication and division are expensive.
  25. * so instead of *10 we do (l<<3) (i.e. *8) + (l<<1) (i.e. *2)
  26. * and check for overflow on all the intermediate steps */
  27. n=(unsigned int)l<<3; if ((n>>3)!=(unsigned int)l) break;
  28. if (n+((unsigned int)l<<1) < n) break;
  29. n+=(unsigned int)l<<1;
  30. if (n+c < n) break;
  31. n+=c;
  32. #endif
  33. if (n > maxint+neg) break;
  34. l=(int)n;
  35. ++tmp;
  36. ok=1;
  37. }
  38. if (!ok) return 0;
  39. *dest=(neg?-l:l);
  40. return (size_t)(tmp-src);
  41. }
  42. #ifdef UNITTEST
  43. #include <assert.h>
  44. int main() {
  45. int i;
  46. assert(scan_int("1234",&i)==4 && i==1234);
  47. assert(scan_int("-1234",&i)==5 && i==-1234);
  48. assert(scan_int("+1234",&i)==5 && i==1234);
  49. assert(scan_int("4294967295",&i)==9 && i==429496729); // overflow
  50. assert(scan_int("2147483647",&i)==10 && i==2147483647); // MAX_INT
  51. assert(scan_int("2147483648",&i)==9 && i==214748364); // overflow
  52. assert(scan_int("-2147483648",&i)==11 && i==-2147483648); // MIN_INT
  53. assert(scan_int("-2147483649",&i)==10 && i==-214748364); // underflow
  54. return 0;
  55. }
  56. #endif