From 81a89b8d47d87b8624f92a0a7220cc6c7a2b2c24 Mon Sep 17 00:00:00 2001 From: Enno Rehling Date: Wed, 4 Apr 2007 19:27:20 +0000 Subject: [PATCH] quicker % --- src/common/util/base36.c | 110 +++++++++++++++++++-------------------- 1 file changed, 55 insertions(+), 55 deletions(-) diff --git a/src/common/util/base36.c b/src/common/util/base36.c index 81e7aef32..51777c3da 100644 --- a/src/common/util/base36.c +++ b/src/common/util/base36.c @@ -29,82 +29,82 @@ int atoi36(const char * str) { - /* cannot use strtol, becuase invalid strings will cause crash */ - const unsigned char * s = (const unsigned char *)str; - int i = 0; - assert(s); - if(!(*s)) return 0; + /* cannot use strtol, becuase invalid strings will cause crash */ + const unsigned char * s = (const unsigned char *)str; + int i = 0; + assert(s); + if(!(*s)) return 0; - while(isspace((int)*s)) ++s; - while(isalnum((int)*s)) { - if (isupper((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 - break; - ++s; - } - if (i<0) return 0; - return i; + while(isspace((int)*s)) ++s; + while(isalnum((int)*s)) { + if (isupper((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 + break; + ++s; + } + if (i<0) return 0; + return i; } const char* itoab(int i, int base) { - static char **as = NULL; - char * s, * dst; - static int index = 0; - int neg = 0; + static char **as = NULL; + char * s, * dst; + static int index = 0; + int neg = 0; - if (!as) { - int j; - char * x = (char*)calloc(sizeof(char), 8*4); - as = (char **)calloc(sizeof(char*), 4); - for (j=0;j!=4;++j) as[j] = x+j*8; - } - s = as[index]; - index = (index+1) % 4; - dst = s+7; - (*dst--)=0; - if (i!=0) { - if (i<0) { - i=-i; - neg = 1; - } - while (i) { - int x = i % base; - i = i / base; - if (x<10) *(dst--) = (char)('0' + x); - else if ('a' + x - 10 == 'l') *(dst--) = 'L'; - else *(dst--) = (char)('a' + (x-10)); - } - if (neg) *(dst) = '-'; - else ++dst; - } - else *dst = '0'; + if (!as) { + int j; + char * x = (char*)calloc(sizeof(char), 8*4); + as = (char **)calloc(sizeof(char*), 4); + for (j=0;j!=4;++j) as[j] = x+j*8; + } + s = as[index]; + index = (index+1) & 3; /* quick for % 4 */ + dst = s+7; + (*dst--)=0; + if (i!=0) { + if (i<0) { + i=-i; + neg = 1; + } + while (i) { + int x = i % base; + i = i / base; + if (x<10) *(dst--) = (char)('0' + x); + else if ('a' + x - 10 == 'l') *(dst--) = 'L'; + else *(dst--) = (char)('a' + (x-10)); + } + if (neg) *(dst) = '-'; + else ++dst; + } + else *dst = '0'; - return dst; + return dst; } const char* itoa36(int i) { - return itoab(i, 36); + return itoab(i, 36); } const char* itoa10(int i) { - return itoab(i, 10); + return itoab(i, 10); } int i10toi36(int i) { - int r = 0; - while(i) { - r = r*36 + i % 10; - i = i / 10; - } - return r; + int r = 0; + while(i) { + r = r*36 + i % 10; + i = i / 10; + } + return r; }