quicker %

This commit is contained in:
Enno Rehling 2007-04-04 19:27:20 +00:00
parent 85e9803acf
commit 81a89b8d47

View file

@ -29,82 +29,82 @@
int int
atoi36(const char * str) atoi36(const char * str)
{ {
/* cannot use strtol, becuase invalid strings will cause crash */ /* cannot use strtol, becuase invalid strings will cause crash */
const unsigned char * s = (const unsigned char *)str; const unsigned char * s = (const unsigned char *)str;
int i = 0; int i = 0;
assert(s); assert(s);
if(!(*s)) return 0; if(!(*s)) return 0;
while(isspace((int)*s)) ++s; while(isspace((int)*s)) ++s;
while(isalnum((int)*s)) { while(isalnum((int)*s)) {
if (isupper((int)*s)) i = i*36 + (*s)-'A' + 10; if (isupper((int)*s)) i = i*36 + (*s)-'A' + 10;
else if (islower((int)*s)) i=i*36 + (*s)-'a' + 10; else if (islower((int)*s)) i=i*36 + (*s)-'a' + 10;
else if (isdigit((int)*s)) i=i*36 + (*s)-'0'; else if (isdigit((int)*s)) i=i*36 + (*s)-'0';
else else
break; break;
++s; ++s;
} }
if (i<0) return 0; if (i<0) return 0;
return i; return i;
} }
const char* const char*
itoab(int i, int base) itoab(int i, int base)
{ {
static char **as = NULL; static char **as = NULL;
char * s, * dst; char * s, * dst;
static int index = 0; static int index = 0;
int neg = 0; int neg = 0;
if (!as) { if (!as) {
int j; int j;
char * x = (char*)calloc(sizeof(char), 8*4); char * x = (char*)calloc(sizeof(char), 8*4);
as = (char **)calloc(sizeof(char*), 4); as = (char **)calloc(sizeof(char*), 4);
for (j=0;j!=4;++j) as[j] = x+j*8; for (j=0;j!=4;++j) as[j] = x+j*8;
} }
s = as[index]; s = as[index];
index = (index+1) % 4; index = (index+1) & 3; /* quick for % 4 */
dst = s+7; dst = s+7;
(*dst--)=0; (*dst--)=0;
if (i!=0) { if (i!=0) {
if (i<0) { if (i<0) {
i=-i; i=-i;
neg = 1; neg = 1;
} }
while (i) { while (i) {
int x = i % base; int x = i % base;
i = i / base; i = i / base;
if (x<10) *(dst--) = (char)('0' + x); if (x<10) *(dst--) = (char)('0' + x);
else if ('a' + x - 10 == 'l') *(dst--) = 'L'; else if ('a' + x - 10 == 'l') *(dst--) = 'L';
else *(dst--) = (char)('a' + (x-10)); else *(dst--) = (char)('a' + (x-10));
} }
if (neg) *(dst) = '-'; if (neg) *(dst) = '-';
else ++dst; else ++dst;
} }
else *dst = '0'; else *dst = '0';
return dst; return dst;
} }
const char* const char*
itoa36(int i) itoa36(int i)
{ {
return itoab(i, 36); return itoab(i, 36);
} }
const char* const char*
itoa10(int i) itoa10(int i)
{ {
return itoab(i, 10); return itoab(i, 10);
} }
int int
i10toi36(int i) i10toi36(int i)
{ {
int r = 0; int r = 0;
while(i) { while(i) {
r = r*36 + i % 10; r = r*36 + i % 10;
i = i / 10; i = i / 10;
} }
return r; return r;
} }