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.
 
 
 
 

39 lines
1.1 KiB

  1. #include "scan.h"
  2. static const unsigned short maxshort = ((unsigned short)-1)>>1;
  3. size_t scan_short(const char* src,short* dest) {
  4. register const char *tmp;
  5. register short l;
  6. register unsigned char c;
  7. int neg;
  8. int ok;
  9. tmp=src; l=0; ok=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 short int n;
  16. /* we want to do: l=l*10+c
  17. * but we need to check for integer overflow.
  18. * to check whether l*10 overflows, we could do
  19. * if ((l*10)/10 != l)
  20. * however, multiplication and division are expensive.
  21. * so instead of *10 we do (l<<3) (i.e. *8) + (l<<1) (i.e. *2)
  22. * and check for overflow on all the intermediate steps */
  23. n=(unsigned short)(l<<3); if ((n>>3)!=(unsigned short)l) break;
  24. if (n+(l<<1) < n) break;
  25. n=(unsigned short)(n+(l<<1));
  26. if (n+c < n) break;
  27. n=(unsigned short)(n+c);
  28. if (n > maxshort+neg) break;
  29. l=(short)n;
  30. ++tmp;
  31. ok=1;
  32. }
  33. if (!ok) return 0;
  34. *dest=(short)(neg?-l:l);
  35. return (size_t)(tmp-src);
  36. }