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.
 
 
 
 

40 lines
1.3 KiB

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