diff options
author | Danny Smith <dannysmith@users.sourceforge.net> | 2002-06-13 10:20:48 +0000 |
---|---|---|
committer | Danny Smith <dannysmith@users.sourceforge.net> | 2002-06-13 10:20:48 +0000 |
commit | dc8971488e7c174ab4084cbdb0a8f12c39b738d7 (patch) | |
tree | d679387fa379b6d5feb9756bdd7237347614453b /winsup/mingw/mingwex/strtoumax.c | |
parent | 5f74ae83e5fba1ceb73a8d822cbe2b48c619127f (diff) | |
download | cygnal-dc8971488e7c174ab4084cbdb0a8f12c39b738d7.tar.gz cygnal-dc8971488e7c174ab4084cbdb0a8f12c39b738d7.tar.bz2 cygnal-dc8971488e7c174ab4084cbdb0a8f12c39b738d7.zip |
* include/_mingw.h: Increment version to 2.0.
* Makefile.in: Ditto.
Merge in mingwex branch.
Diffstat (limited to 'winsup/mingw/mingwex/strtoumax.c')
-rw-r--r-- | winsup/mingw/mingwex/strtoumax.c | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/winsup/mingw/mingwex/strtoumax.c b/winsup/mingw/mingwex/strtoumax.c new file mode 100644 index 000000000..2c052ac06 --- /dev/null +++ b/winsup/mingw/mingwex/strtoumax.c @@ -0,0 +1,110 @@ +/* + This source code was extracted from the Q8 package created and + placed in the PUBLIC DOMAIN by Doug Gwyn <gwyn@arl.mil> + last edit: 1999/11/05 gwyn@arl.mil + + Implements subclause 7.8.2 of ISO/IEC 9899:1999 (E). + + This particular implementation requires the matching <inttypes.h>. + It also assumes that character codes for A..Z and a..z are in + contiguous ascending order; this is true for ASCII but not EBCDIC. +*/ +#include <stdlib.h> +#include <errno.h> +#include <ctype.h> +#include <inttypes.h> + +/* Helper macros */ + +/* convert digit character to number, in any base */ +#define ToNumber(c) (isdigit(c) ? (c) - '0' : \ + isupper(c) ? (c) - 'A' + 10 : \ + islower(c) ? (c) - 'a' + 10 : \ + -1 /* "invalid" flag */ \ + ) +/* validate converted digit character for specific base */ +#define valid(n, b) ((n) >= 0 && (n) < (b)) + +uintmax_t +strtoumax(nptr, endptr, base) + register const char * __restrict__ nptr; + char ** __restrict__ endptr; + register int base; + { + register uintmax_t accum; /* accumulates converted value */ + register uintmax_t next; /* for computing next value of accum */ + register int n; /* numeral from digit character */ + int minus; /* set iff minus sign seen (yes!) */ + int toobig; /* set iff value overflows */ + + if ( endptr != NULL ) + *endptr = (char *)nptr; /* in case no conversion's performed */ + + if ( base < 0 || base == 1 || base > 36 ) + { + errno = EDOM; + return 0; /* unspecified behavior */ + } + + /* skip initial, possibly empty sequence of white-space characters */ + + while ( isspace(*nptr) ) + ++nptr; + + /* process subject sequence: */ + + /* optional sign (yes!) */ + + if ( (minus = *nptr == '-') || *nptr == '+' ) + ++nptr; + + if ( base == 0 ) + { + if ( *nptr == '0' ) + { + if ( nptr[1] == 'X' || nptr[1] == 'x' ) + base = 16; + else + base = 8; + } + else + base = 10; + } + + /* optional "0x" or "0X" for base 16 */ + + if ( base == 16 && *nptr == '0' && (nptr[1] == 'X' || nptr[1] == 'x') ) + nptr += 2; /* skip past this prefix */ + + /* check whether there is at least one valid digit */ + + n = ToNumber(*nptr); + ++nptr; + + if ( !valid(n, base) ) + return 0; /* subject seq. not of expected form */ + + accum = n; + + for ( toobig = 0; n = ToNumber(*nptr), valid(n, base); ++nptr ) + if ( accum > UINTMAX_MAX / base + 1 /* major wrap-around */ + || (next = base * accum + n) < accum /* minor wrap-around */ + ) + toobig = 1; /* but keep scanning */ + else + accum = next; + + if ( endptr != NULL ) + *endptr = (char *)nptr; /* points to first not-valid-digit */ + + if ( toobig ) + { + errno = ERANGE; + return UINTMAX_MAX; + } + else + return minus ? -accum : accum; /* (yes!) */ + } + +unsigned long long __attribute__ ((alias ("strtoumax"))) +strtoull (const char* __restrict__ nptr, char ** __restrict__ endptr, int base); |