2001-02-02 17:54:47 +00:00
|
|
|
#include "scan.h"
|
|
|
|
|
2006-11-07 17:56:05 +00:00
|
|
|
size_t scan_8short(const char* src,unsigned short* dest) {
|
2014-06-30 11:40:52 +00:00
|
|
|
/* make a copy of src so we can return the number of bytes we progressed */
|
2014-03-14 00:24:02 +00:00
|
|
|
register const char *tmp=src;
|
2014-06-30 11:40:52 +00:00
|
|
|
/* this is called l because we copy and pasted from 8long, where l
|
|
|
|
* stood for "long" */
|
2014-03-14 00:24:02 +00:00
|
|
|
register unsigned short l=0;
|
|
|
|
register unsigned char c;
|
2014-06-30 11:40:52 +00:00
|
|
|
/* *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 :) */
|
2014-03-14 01:53:08 +00:00
|
|
|
while ((c=(unsigned char)(*tmp-'0'))<8) {
|
2014-06-30 11:40:52 +00:00
|
|
|
/* 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 */
|
2014-03-14 00:24:02 +00:00
|
|
|
if (l>>(sizeof(l)*8-3)) break;
|
2014-03-14 01:53:08 +00:00
|
|
|
l=(unsigned short)(l*8+c);
|
2014-03-14 00:24:02 +00:00
|
|
|
++tmp;
|
|
|
|
}
|
2001-02-02 17:54:47 +00:00
|
|
|
*dest=l;
|
2014-03-14 01:53:08 +00:00
|
|
|
return (size_t)(tmp-src);
|
2001-02-02 17:54:47 +00:00
|
|
|
}
|