From bb7adccc31cb145b671afe75c432da3fc494ad48 Mon Sep 17 00:00:00 2001 From: solar Date: Fri, 2 Dec 2005 11:00:08 +0000 Subject: [PATCH] Added atoi, atol, atoll plus internal helpers. --- functions/_PDCLIB/atomax.c | 44 ++++++++++++++++++++++++++++++++++++++ functions/_PDCLIB/digits.c | 23 ++++++++++++++++++++ functions/stdlib/atoi.c | 28 ++++++++++++++++++++++++ functions/stdlib/atol.c | 28 ++++++++++++++++++++++++ functions/stdlib/atoll.c | 28 ++++++++++++++++++++++++ internals/_PDCLIB_int.h | 12 ++++++++++- 6 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 functions/_PDCLIB/atomax.c create mode 100644 functions/_PDCLIB/digits.c create mode 100644 functions/stdlib/atoi.c create mode 100644 functions/stdlib/atol.c create mode 100644 functions/stdlib/atoll.c diff --git a/functions/_PDCLIB/atomax.c b/functions/_PDCLIB/atomax.c new file mode 100644 index 0000000..bde1faf --- /dev/null +++ b/functions/_PDCLIB/atomax.c @@ -0,0 +1,44 @@ +/* $Id$ */ + +/* Release $Name$ */ + +/* _PDCLIB_atomax( const char * ) + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include <_PDCLIB_int.h> +#include +#include + +_PDCLIB_intmax_t _PDCLIB_atomax( const char * s ) +{ + _PDCLIB_intmax_t rc = 0; + char sign = '+'; + const char * x; + /* TODO: In other than "C" locale, additional patterns may be defined */ + while ( isspace( *s ) ) ++s; + if ( *s == '+' ) ++s; + else if ( *s == '-' ) sign = *(s++); + while ( ( x = memchr( _PDCLIB_digits, *(s++), 10 ) ) != NULL ) + { + rc = rc * 10 + ( x - _PDCLIB_digits ); + } + return ( sign == '+' ) ? rc : -rc; +} + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main() +{ + BEGIN_TESTS; + /* basic functionality */ + TESTCASE( _PDCLIB_atomax( "123" ) == 123 ); + /* testing skipping of leading whitespace and trailing garbage */ + TESTCASE( _PDCLIB_atomax( " \n\v\t\f123xyz" ) == 123 ); + return TEST_RESULTS; +} + +#endif diff --git a/functions/_PDCLIB/digits.c b/functions/_PDCLIB/digits.c new file mode 100644 index 0000000..b58886b --- /dev/null +++ b/functions/_PDCLIB/digits.c @@ -0,0 +1,23 @@ +/* $Id$ */ + +/* Release $Name$ */ + +/* _PDCLIB_digits + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +char _PDCLIB_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main() +{ + BEGIN_TESTS; + /* no tests for raw data */ + return TEST_RESULTS; +} + +#endif diff --git a/functions/stdlib/atoi.c b/functions/stdlib/atoi.c new file mode 100644 index 0000000..75987e0 --- /dev/null +++ b/functions/stdlib/atoi.c @@ -0,0 +1,28 @@ +/* $Id$ */ + +/* Release $Name$ */ + +/* atoi( const char * ) + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include <_PDCLIB_int.h> + +int atoi( const char * s ) +{ + return (int) _PDCLIB_atomax( s ); +} + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main() +{ + BEGIN_TESTS; + /* no tests for a simple wrapper */ + return TEST_RESULTS; +} + +#endif diff --git a/functions/stdlib/atol.c b/functions/stdlib/atol.c new file mode 100644 index 0000000..b779f56 --- /dev/null +++ b/functions/stdlib/atol.c @@ -0,0 +1,28 @@ +/* $Id$ */ + +/* Release $Name$ */ + +/* atol( const char * ) + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include <_PDCLIB_int.h> + +long int atol( const char * s ) +{ + return (long int) _PDCLIB_atomax( s ); +} + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main() +{ + BEGIN_TESTS; + /* no tests for a simple wrapper */ + return TEST_RESULTS; +} + +#endif diff --git a/functions/stdlib/atoll.c b/functions/stdlib/atoll.c new file mode 100644 index 0000000..62abef1 --- /dev/null +++ b/functions/stdlib/atoll.c @@ -0,0 +1,28 @@ +/* $Id$ */ + +/* Release $Name$ */ + +/* atoll( const char * ) + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include <_PDCLIB_int.h> + +long long int atoll( const char * s ) +{ + return (long long int) _PDCLIB_atomax( s ); +} + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main() +{ + BEGIN_TESTS; + /* no tests for a simple wrapper */ + return TEST_RESULTS; +} + +#endif diff --git a/internals/_PDCLIB_int.h b/internals/_PDCLIB_int.h index 24accb0..1f37c13 100644 --- a/internals/_PDCLIB_int.h +++ b/internals/_PDCLIB_int.h @@ -85,7 +85,7 @@ #if _PDCLIB_LONG_BYTES == 4 #define _PDCLIB_LONG_MAX 0x7fffffffL #define _PDCLIB_LONG_MIN (-0x7fffffffL - 1L) -#define _PDCLIB_ULONG_MAX 0xffffffffUL +#define _PDCLIB_ULONG_MAX 0xffffffffUL #elif _PDCLIB_LONG_BYTES == 8 #define _PDCLIB_LONG_MAX 0x7fffffffffffffffL #define _PDCLIB_LONG_MIN (-0x7fffffffffffffffL - 1L) @@ -251,3 +251,13 @@ typedef unsigned _PDCLIB_intmax _PDCLIB_uintmax_t; #define _PDCLIB_UINTMAX_MAX concat( concat( _PDCLIB_U, _PDCLIB_INTMAX ), _MAX ) #define _PDCLIB_INTMAX_C( value ) concat( value, _PDCLIB_INTMAX_LITERAL ) #define _PDCLIB_UINTMAX_C( value ) concat( value, concat( u, _PDCLIB_INTMAX_LITERAL ) ) + +/* -------------------------------------------------------------------------- */ +/* Declaration of helper functions (implemented in functions/_PDCLIB). */ +/* -------------------------------------------------------------------------- */ + +/* This is the main function called by atoi(), atol() and atoll(). */ +_PDCLIB_intmax_t _PDCLIB_atomax( const char * s ); + +/* Digits array used by various integer conversion functions in */ +extern char _PDCLIB_digits[]; -- 2.40.0