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.
 
 
 
 

34 lines
796 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_xint(const char* src,unsigned int* dest) {
  8. register const char *tmp=src;
  9. register unsigned int 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=(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 int i;
  22. assert(scan_xint("fefe",&i)==4 && i==0xfefe);
  23. assert(scan_xint("0xfefe",&i)==1); // 0x not supported, will scan the 0
  24. assert(scan_xint("+fefe",&i)==0);
  25. assert(scan_xint("fefec0de",&i)==8 && i==0xfefec0de);
  26. assert(scan_xint("fefec0debaad",&i)==8 && i==0xfefec0de); // test truncation
  27. return 0;
  28. }
  29. #endif