File: scan_8short.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 (39 lines) | stat: -rw-r--r-- 1,378 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
33
34
35
36
37
38
39
#include "scan.h"

size_t scan_8short(const char* src,unsigned short* dest) {
  /* make a copy of src so we can return the number of bytes we progressed */
  register const char *tmp=src;
  /* this is called l because we copy and pasted from 8long, where l
   * stood for "long" */
  register unsigned short l=0;
  register unsigned char c;
  /* *tmp - '0' can be negative, but casting to unsigned char makes
   * those cases positive and large; that means we only need one
   * comparison.  This trick is no longer needed on modern compilers,
   * but we also want to produce good code on old compilers :) */
  while ((c=(unsigned char)(*tmp-'0'))<8) {
    /* overflow check; for each digit we multiply by 8 and then add the
     * digit; 0-7 needs 3 bits of storage, so we need to check if the
     * uppermost 3 bits of l are empty. Do it by shifting to the right */
    if (l>>(sizeof(l)*8-3)) break;
    l=(unsigned short)(l*8+c);
    ++tmp;
  }
  *dest=l;
  return (size_t)(tmp-src);
}

#ifdef UNITTEST
#include <assert.h>

int main() {
  unsigned short i;
  assert(scan_8short("1234",&i)==4 && i==01234);
  assert(scan_8short("5678",&i)==3 && i==0567);
  assert(scan_8short("177777",&i)==6 && i==0xffff);
  assert(scan_8short("200000",&i)==5 && i==020000);
  assert(scan_8short("-4",&i)==0 && i==0);
  assert(scan_8short("01234",&i)==5 && i==01234);
  return 0;
}
#endif