nightmaremail

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

scan_ulong.c (1229B)


      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  * this can also only work up to... base 127? 255? mi ne scias */
     15 static inline long first(char x, char *iny, unsigned long ylen)
     16 {
     17 	int i;
     18 	/* iny can only be ascii characters unless you change char somehow */
     19 	/* unsigned long y[256]; * oof, a whole kb */
     20 	for (i = 0; i < ylen; ++i)
     21 	{
     22 		/* y[(iny[i])] = i; */
     23 		if (x == iny[i]) return i;
     24 	}
     25 	return -1;
     26 }
     27 
     28 /* kinda unavoidably inefficient ~Hildie */
     29 unsigned int scan_ulongalphabet(char *s, unsigned long *u, char *alphabet, unsigned long alphabetlen)
     30 {
     31   unsigned int pos;
     32   unsigned long result;
     33   long c;
     34   pos = 0; result = 0;
     35   c = (long) (unsigned char) (first(s[pos], alphabet, alphabetlen));
     36   while (c < alphabetlen && c >= 0)
     37   {
     38     result = result * alphabetlen + c; ++pos;
     39     c = (long) (unsigned char) (first(s[pos], alphabet, alphabetlen));
     40   }
     41   *u = result; return pos;
     42 }