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.
 
 
 
 

44 lines
1.6 KiB

  1. #include "scan.h"
  2. size_t scan_8longn(const char *src,size_t n,unsigned long *dest) {
  3. /* make a copy of src so we can return the number of bytes we progressed */
  4. register const char *tmp=src;
  5. register unsigned long l=0;
  6. register unsigned char c;
  7. /* *tmp - '0' can be negative, but casting to unsigned char makes
  8. * those cases positive and large; that means we only need one
  9. * comparison. This trick is no longer needed on modern compilers,
  10. * but we also want to produce good code on old compilers :) */
  11. while (n-->0 && (c=(unsigned char)(*tmp-'0'))<8) {
  12. /* overflow check; for each digit we multiply by 8 and then add the
  13. * digit; 0-7 needs 3 bits of storage, so we need to check if the
  14. * uppermost 3 bits of l are empty. Do it by shifting to the right */
  15. if (l>>(sizeof(l)*8-3)) break;
  16. l=l*8+c;
  17. ++tmp;
  18. }
  19. *dest=l;
  20. return (size_t)(tmp-src);
  21. }
  22. #ifdef UNITTEST
  23. #include <assert.h>
  24. int main() {
  25. unsigned long i;
  26. assert(scan_8longn("1234",100,&i)==4 && i==01234);
  27. assert(scan_8longn("12345678",100,&i)==7 && i==01234567);
  28. if (sizeof(long)==sizeof(int)) {
  29. assert(scan_8longn("37777777777",100,&i)==11 && i==0xffffffff);
  30. assert(scan_8longn("40000000000",100,&i)==10 && i==04000000000);
  31. } else if (sizeof(long)==sizeof(long long)) {
  32. assert(scan_8longn("1777777777777777777777",100,&i)==22 && i==0xffffffffffffffffull);
  33. assert(scan_8longn("2000000000000000000000",100,&i)==21 && i==0200000000000000000000ull);
  34. }
  35. assert(scan_8longn("-4",100,&i)==0 && i==0);
  36. assert(scan_8longn("01234",100,&i)==5 && i==01234);
  37. assert(scan_8longn("1234",2,&i)==2 && i==012);
  38. return 0;
  39. }
  40. #endif