sl@0: /* sl@0: * strtoull.c -- sl@0: * sl@0: * Source code for the "strtoull" library procedure. sl@0: * sl@0: * Copyright (c) 1988 The Regents of the University of California. sl@0: * Copyright (c) 1994 Sun Microsystems, Inc. sl@0: * sl@0: * See the file "license.terms" for information on usage and redistribution sl@0: * of this file, and for a DISCLAIMER OF ALL WARRANTIES. sl@0: * sl@0: * RCS: @(#) $Id: strtoull.c,v 1.5 2002/02/24 02:53:25 dgp Exp $ sl@0: */ sl@0: sl@0: #include "tcl.h" sl@0: #include "tclPort.h" sl@0: #include sl@0: sl@0: /* sl@0: * The table below is used to convert from ASCII digits to a sl@0: * numerical equivalent. It maps from '0' through 'z' to integers sl@0: * (100 for non-digit characters). sl@0: */ sl@0: sl@0: static char cvtIn[] = { sl@0: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /* '0' - '9' */ sl@0: 100, 100, 100, 100, 100, 100, 100, /* punctuation */ sl@0: 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, /* 'A' - 'Z' */ sl@0: 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, sl@0: 30, 31, 32, 33, 34, 35, sl@0: 100, 100, 100, 100, 100, 100, /* punctuation */ sl@0: 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, /* 'a' - 'z' */ sl@0: 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, sl@0: 30, 31, 32, 33, 34, 35}; sl@0: sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * strtoull -- sl@0: * sl@0: * Convert an ASCII string into an integer. sl@0: * sl@0: * Results: sl@0: * The return value is the integer equivalent of string. If endPtr sl@0: * is non-NULL, then *endPtr is filled in with the character sl@0: * after the last one that was part of the integer. If string sl@0: * doesn't contain a valid integer value, then zero is returned sl@0: * and *endPtr is set to string. sl@0: * sl@0: * Side effects: sl@0: * None. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: #if TCL_WIDE_INT_IS_LONG sl@0: unsigned long long sl@0: #else sl@0: Tcl_WideUInt sl@0: #endif sl@0: strtoull(string, endPtr, base) sl@0: CONST char *string; /* String of ASCII digits, possibly sl@0: * preceded by white space. For bases sl@0: * greater than 10, either lower- or sl@0: * upper-case digits may be used. sl@0: */ sl@0: char **endPtr; /* Where to store address of terminating sl@0: * character, or NULL. */ sl@0: int base; /* Base for conversion. Must be less sl@0: * than 37. If 0, then the base is chosen sl@0: * from the leading characters of string: sl@0: * "0x" means hex, "0" means octal, anything sl@0: * else means decimal. sl@0: */ sl@0: { sl@0: register CONST char *p; sl@0: register Tcl_WideUInt result = 0; sl@0: register unsigned digit; sl@0: register Tcl_WideUInt shifted; sl@0: int anyDigits = 0, negative = 0; sl@0: sl@0: /* sl@0: * Skip any leading blanks. sl@0: */ sl@0: sl@0: p = string; sl@0: while (isspace(UCHAR(*p))) { /* INTL: locale-dependent */ sl@0: p += 1; sl@0: } sl@0: sl@0: /* sl@0: * Check for a sign. sl@0: */ sl@0: sl@0: if (*p == '-') { sl@0: p += 1; sl@0: negative = 1; sl@0: } else { sl@0: if (*p == '+') { sl@0: p += 1; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * If no base was provided, pick one from the leading characters sl@0: * of the string. sl@0: */ sl@0: sl@0: if (base == 0) { sl@0: if (*p == '0') { sl@0: p += 1; sl@0: if (*p == 'x' || *p == 'X') { sl@0: p += 1; sl@0: base = 16; sl@0: } else { sl@0: sl@0: /* sl@0: * Must set anyDigits here, otherwise "0" produces a sl@0: * "no digits" error. sl@0: */ sl@0: sl@0: anyDigits = 1; sl@0: base = 8; sl@0: } sl@0: } else { sl@0: base = 10; sl@0: } sl@0: } else if (base == 16) { sl@0: sl@0: /* sl@0: * Skip a leading "0x" from hex numbers. sl@0: */ sl@0: sl@0: if ((p[0] == '0') && (p[1] == 'x' || *p == 'X')) { sl@0: p += 2; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * Sorry this code is so messy, but speed seems important. Do sl@0: * different things for base 8, 10, 16, and other. sl@0: */ sl@0: sl@0: if (base == 8) { sl@0: for ( ; ; p += 1) { sl@0: digit = *p - '0'; sl@0: if (digit > 7) { sl@0: break; sl@0: } sl@0: shifted = result << 3; sl@0: if ((shifted >> 3) != result) { sl@0: goto overflow; sl@0: } sl@0: result = shifted + digit; sl@0: if ( result < shifted ) { sl@0: goto overflow; sl@0: } sl@0: anyDigits = 1; sl@0: } sl@0: } else if (base == 10) { sl@0: for ( ; ; p += 1) { sl@0: digit = *p - '0'; sl@0: if (digit > 9) { sl@0: break; sl@0: } sl@0: shifted = 10 * result; sl@0: if ((shifted / 10) != result) { sl@0: goto overflow; sl@0: } sl@0: result = shifted + digit; sl@0: if ( result < shifted ) { sl@0: goto overflow; sl@0: } sl@0: anyDigits = 1; sl@0: } sl@0: } else if (base == 16) { sl@0: for ( ; ; p += 1) { sl@0: digit = *p - '0'; sl@0: if (digit > ('z' - '0')) { sl@0: break; sl@0: } sl@0: digit = cvtIn[digit]; sl@0: if (digit > 15) { sl@0: break; sl@0: } sl@0: shifted = result << 4; sl@0: if ((shifted >> 4) != result) { sl@0: goto overflow; sl@0: } sl@0: result = shifted + digit; sl@0: if ( result < shifted ) { sl@0: goto overflow; sl@0: } sl@0: anyDigits = 1; sl@0: } sl@0: } else if ( base >= 2 && base <= 36 ) { sl@0: for ( ; ; p += 1) { sl@0: digit = *p - '0'; sl@0: if (digit > ('z' - '0')) { sl@0: break; sl@0: } sl@0: digit = cvtIn[digit]; sl@0: if (digit >= (unsigned) base) { sl@0: break; sl@0: } sl@0: shifted = result * base; sl@0: if ((shifted/base) != result) { sl@0: goto overflow; sl@0: } sl@0: result = shifted + digit; sl@0: if ( result < shifted ) { sl@0: goto overflow; sl@0: } sl@0: anyDigits = 1; sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * Negate if we found a '-' earlier. sl@0: */ sl@0: sl@0: if (negative) { sl@0: result = (Tcl_WideUInt)(-((Tcl_WideInt)result)); sl@0: } sl@0: sl@0: /* sl@0: * See if there were any digits at all. sl@0: */ sl@0: sl@0: if (!anyDigits) { sl@0: p = string; sl@0: } sl@0: sl@0: if (endPtr != 0) { sl@0: *endPtr = (char *) p; sl@0: } sl@0: sl@0: return result; sl@0: sl@0: /* sl@0: * On overflow generate the right output sl@0: */ sl@0: sl@0: overflow: sl@0: errno = ERANGE; sl@0: if (endPtr != 0) { sl@0: for ( ; ; p += 1) { sl@0: digit = *p - '0'; sl@0: if (digit > ('z' - '0')) { sl@0: break; sl@0: } sl@0: digit = cvtIn[digit]; sl@0: if (digit >= (unsigned) base) { sl@0: break; sl@0: } sl@0: } sl@0: *endPtr = (char *) p; sl@0: } sl@0: return (Tcl_WideUInt)Tcl_LongAsWide(-1); sl@0: }