File: scan_xshort.c

package info (click to toggle)
libowfat 0.34-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,288 kB
  • sloc: ansic: 20,181; makefile: 16
file content (32 lines) | stat: -rw-r--r-- 766 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "scan.h"

#ifdef UNITTEST
#undef UNITTEST
#include "scan_fromhex.c"
#define UNITTEST
#endif

size_t scan_xshort(const char* src,unsigned short* dest) {
  register const char *tmp=src;
  register unsigned short l=0;
  register unsigned char c;
  while ((l>>(sizeof(l)*8-4))==0 && (c=(unsigned char)scan_fromhex((unsigned char)*tmp))<16) {
    l=(unsigned short)((l<<4)+c);
    ++tmp;
  }
  *dest=l;
  return (size_t)(tmp-src);
}

#ifdef UNITTEST
#include <assert.h>

int main() {
  unsigned short i;
  assert(scan_xshort("fefe",&i)==4 && i==0xfefe);
  assert(scan_xshort("0xfefe",&i)==1);	// 0x not supported, will scan the 0
  assert(scan_xshort("+fefe",&i)==0);
  assert(scan_xshort("fefec0de",&i)==4 && i==0xfefe);	// test truncation
  return 0;
}
#endif