add scan_ulongn, scan_8longn, scan_xlongn and scan_longn
parent
ee4a2c0cdb
commit
69eb8c01e3
@ -0,0 +1,13 @@
|
||||
#include "scan.h"
|
||||
|
||||
size_t scan_8longn(const char *src,size_t n,unsigned long *dest) {
|
||||
register const char *tmp=src;
|
||||
register unsigned long l=0;
|
||||
register unsigned char c;
|
||||
while (n-->0 && (c=*tmp-'0')<8) {
|
||||
l=l*8+c;
|
||||
++tmp;
|
||||
}
|
||||
*dest=l;
|
||||
return tmp-src;
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
#include "scan.h"
|
||||
|
||||
size_t scan_longn(const char *src,size_t n,long *dest) {
|
||||
register const char *tmp;
|
||||
register long int l;
|
||||
register unsigned char c;
|
||||
int neg;
|
||||
int ok;
|
||||
if (!n--) return 0;
|
||||
tmp=src; l=0; ok=neg=0;
|
||||
switch (*tmp) {
|
||||
case '-': neg=1;
|
||||
case '+': ++tmp;
|
||||
}
|
||||
while (n-->0 && (c=*tmp-'0')<10) {
|
||||
l=l*10+c;
|
||||
++tmp;
|
||||
ok=1;
|
||||
}
|
||||
if (!ok) return 0;
|
||||
*dest=(neg?-l:l);
|
||||
return tmp-src;
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
#include "scan.h"
|
||||
|
||||
size_t scan_ulongn(const char* src,size_t n,unsigned long int* dest) {
|
||||
register const char *tmp=src;
|
||||
register unsigned long int l=0;
|
||||
register unsigned char c;
|
||||
while (n-->0 && (c=*tmp-'0')<10) {
|
||||
unsigned long int n;
|
||||
/* we want to do: l=l*10+c
|
||||
* but we need to check for integer overflow.
|
||||
* to check whether l*10 overflows, we could do
|
||||
* if ((l*10)/10 != l)
|
||||
* however, multiplication and division are expensive.
|
||||
* so instead of *10 we do (l<<3) (i.e. *8) + (l<<1) (i.e. *2)
|
||||
* and check for overflow on all the intermediate steps */
|
||||
n=l<<3; if ((n>>3)!=l) break;
|
||||
if (n+(l<<1) < n) break;
|
||||
n+=l<<1;
|
||||
if (n+c < n) break;
|
||||
l=n+c;
|
||||
++tmp;
|
||||
}
|
||||
if (tmp-src) *dest=l;
|
||||
return tmp-src;
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
#include "scan.h"
|
||||
|
||||
size_t scan_xlongn(const char *src,size_t n,unsigned long *dest) {
|
||||
register const char *tmp=src;
|
||||
register unsigned long l=0;
|
||||
register unsigned char c;
|
||||
while (n-->0 && (c=scan_fromhex(*tmp))<16) {
|
||||
l=(l<<4)+c;
|
||||
++tmp;
|
||||
}
|
||||
*dest=l;
|
||||
return tmp-src;
|
||||
}
|
Loading…
Reference in New Issue