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.
 
 
 
 

33 lines
766 B

  1. #include "scan.h"
  2. #ifdef UNITTEST
  3. #undef UNITTEST
  4. #include "scan_fromhex.c"
  5. #define UNITTEST
  6. #endif
  7. size_t scan_xshort(const char* src,unsigned short* dest) {
  8. register const char *tmp=src;
  9. register unsigned short l=0;
  10. register unsigned char c;
  11. while ((l>>(sizeof(l)*8-4))==0 && (c=(unsigned char)scan_fromhex((unsigned char)*tmp))<16) {
  12. l=(unsigned short)((l<<4)+c);
  13. ++tmp;
  14. }
  15. *dest=l;
  16. return (size_t)(tmp-src);
  17. }
  18. #ifdef UNITTEST
  19. #include <assert.h>
  20. int main() {
  21. unsigned short i;
  22. assert(scan_xshort("fefe",&i)==4 && i==0xfefe);
  23. assert(scan_xshort("0xfefe",&i)==1); // 0x not supported, will scan the 0
  24. assert(scan_xshort("+fefe",&i)==0);
  25. assert(scan_xshort("fefec0de",&i)==4 && i==0xfefe); // test truncation
  26. return 0;
  27. }
  28. #endif