From baec005507bd6cc90bacb29be0a375902242dc62 Mon Sep 17 00:00:00 2001 From: leitner Date: Fri, 14 Mar 2014 21:32:29 +0000 Subject: [PATCH] improved code path for typical platforms where there is a wider integer type --- scan/scan_uint.c | 10 ++++++++++ scan/scan_ushort.c | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/scan/scan_uint.c b/scan/scan_uint.c index c166b41..aeff299 100644 --- a/scan/scan_uint.c +++ b/scan/scan_uint.c @@ -10,6 +10,16 @@ size_t scan_uint(const char* src,unsigned int* dest) { /* a good optimizing compiler should remove the else clause when not * needed */ return scan_ulong(src,(unsigned long*)dest); + } else if (sizeof(unsigned int) < sizeof(unsigned long)) { + const char* cur; + unsigned int l; + for (cur=src,l=0; *cur>='0' && *cur<='9'; ++cur) { + unsigned long tmp=l*10ul+*cur-'0'; + if ((unsigned int)tmp != tmp) break; + l=tmp; + } + if (cur>src) *dest=l; + return (size_t)(cur-src); } else { register const char *tmp=src; register unsigned int l=0; diff --git a/scan/scan_ushort.c b/scan/scan_ushort.c index 093e0d7..05d327d 100644 --- a/scan/scan_ushort.c +++ b/scan/scan_ushort.c @@ -10,6 +10,16 @@ size_t scan_ushort(const char* src,unsigned short* dest) { /* a good optimizing compiler should remove the else clause when not * needed */ return scan_uint(src,(unsigned int*)dest); + } if (sizeof(unsigned short) < sizeof(unsigned long)) { + const char* cur; + unsigned short l; + for (cur=src,l=0; *cur>='0' && *cur<='9'; ++cur) { + unsigned long tmp=l*10ul+*cur-'0'; + if ((unsigned short)tmp != tmp) break; + l=tmp; + } + if (cur>src) *dest=l; + return (size_t)(cur-src); } else { register const char *tmp=src; register unsigned short int l=0;