]> pd.if.org Git - pdclib/commitdiff
Added atoi, atol, atoll plus internal helpers.
authorsolar <unknown>
Fri, 2 Dec 2005 11:00:08 +0000 (11:00 +0000)
committersolar <unknown>
Fri, 2 Dec 2005 11:00:08 +0000 (11:00 +0000)
functions/_PDCLIB/atomax.c [new file with mode: 0644]
functions/_PDCLIB/digits.c [new file with mode: 0644]
functions/stdlib/atoi.c [new file with mode: 0644]
functions/stdlib/atol.c [new file with mode: 0644]
functions/stdlib/atoll.c [new file with mode: 0644]
internals/_PDCLIB_int.h

diff --git a/functions/_PDCLIB/atomax.c b/functions/_PDCLIB/atomax.c
new file mode 100644 (file)
index 0000000..bde1faf
--- /dev/null
@@ -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 <string.h>
+#include <ctype.h>
+
+_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 (file)
index 0000000..b58886b
--- /dev/null
@@ -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 (file)
index 0000000..75987e0
--- /dev/null
@@ -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 (file)
index 0000000..b779f56
--- /dev/null
@@ -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 (file)
index 0000000..62abef1
--- /dev/null
@@ -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
index 24accb0da68e221d6f34821649f9cbb79a658257..1f37c13bedd915fe8d96f1b2318f5f8d40658609 100644 (file)
@@ -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 <stdlib.h>    */
+extern char _PDCLIB_digits[];