nightmaremail

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

scan_ulong.c (1166B)


      1 #include "scan.h"
      2 
      3 unsigned int scan_ulong(char *s, unsigned long *u)
      4 {
      5   unsigned int pos; unsigned long result;
      6   unsigned long c;
      7   pos = 0; result = 0;
      8   while ((c = (unsigned long) (unsigned char) (s[pos] - '0')) < 10)
      9     { result = result * 10 + c; ++pos; }
     10   *u = result; return pos;
     11 }
     12 
     13 /* no quick LUTtery; specific functions could use a static LUT but that's out scope here */
     14 static inline long first(char x, char *iny, unsigned long ylen)
     15 {
     16 	int i;
     17 	/* iny can only be ascii characters unless you change char somehow */
     18 	/* unsigned long y[256]; * oof, a whole kb */
     19 	for (i = 0; i < ylen; ++i)
     20 	{
     21 		/* y[(iny[i])] = i; */
     22 		if (x == iny[i]) return i;
     23 	}
     24 	return -1;
     25 }
     26 
     27 /* kinda unavoidably inefficient ~Hildie */
     28 unsigned int scan_ulongalphabet(char *s, unsigned long *u, char *alphabet, unsigned long alphabetlen)
     29 {
     30   unsigned int pos;
     31   unsigned long result;
     32   long c;
     33   pos = 0; result = 0;
     34   c = (long) (unsigned char) (first(s[pos], alphabet, alphabetlen));
     35   while (c < alphabetlen && c >= 0)
     36   {
     37     result = result * alphabetlen + c; ++pos;
     38     c = (long) (unsigned char) (first(s[pos], alphabet, alphabetlen));
     39   }
     40   *u = result; return pos;
     41 }