From db74d417293e937ab171addd937b7b6c00d44444 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Wed, 9 Jun 2010 18:22:25 +0200 Subject: inttostr: add a new function, inttostr, and tests The namesake function was not available. The existence of the template file, inttostr.c makes its addition nontrivial. * lib/anytostr.c: Rename from inttostr.c. (anytostr): Rename from inttostr. * lib/inttostr.c: New file. * modules/inttostr (Files): Add anytostr.c. (Makefile.am): Set lib_SOURCES instead of ... * m4/inttostr.m4: Remove uses of AC_LIBOBJ. * lib/imaxtostr.c: Update use. s/inttostr/anytostr/ * lib/offtostr.c: Likewise. * lib/uinttostr.c: Likewise. * lib/umaxtostr.c: Likewise. * modules/inttostr-tests: New file. * tests/test-inttostr.c: New file. Test these functions. --- lib/anytostr.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/imaxtostr.c | 4 ++-- lib/inttostr.c | 58 ++++----------------------------------------------------- lib/inttostr.h | 5 +++-- lib/offtostr.c | 4 ++-- lib/uinttostr.c | 4 ++-- lib/umaxtostr.c | 4 ++-- 7 files changed, 69 insertions(+), 64 deletions(-) create mode 100644 lib/anytostr.c (limited to 'lib') diff --git a/lib/anytostr.c b/lib/anytostr.c new file mode 100644 index 0000000000..27ad985024 --- /dev/null +++ b/lib/anytostr.c @@ -0,0 +1,54 @@ +/* anytostr.c -- convert integers to printable strings + + Copyright (C) 2001, 2006, 2008, 2009, 2010 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +/* Written by Paul Eggert */ + +#include + +#include "inttostr.h" +#include "verify.h" + +/* Convert I to a printable string in BUF, which must be at least + INT_BUFSIZE_BOUND (INTTYPE) bytes long. Return the address of the + printable string, which need not start at BUF. */ + +char * __attribute_warn_unused_result__ +anytostr (inttype i, char *buf) +{ + verify (TYPE_SIGNED (inttype) == inttype_is_signed); + char *p = buf + INT_STRLEN_BOUND (inttype); + *p = 0; + +#if inttype_is_signed + if (i < 0) + { + do + *--p = '0' - i % 10; + while ((i /= 10) != 0); + + *--p = '-'; + } + else +#endif + { + do + *--p = '0' + i % 10; + while ((i /= 10) != 0); + } + + return p; +} diff --git a/lib/imaxtostr.c b/lib/imaxtostr.c index 34ef96c450..d2a0429b0d 100644 --- a/lib/imaxtostr.c +++ b/lib/imaxtostr.c @@ -1,4 +1,4 @@ -#define inttostr imaxtostr +#define anytostr imaxtostr #define inttype intmax_t #define inttype_is_signed 1 -#include "inttostr.c" +#include "anytostr.c" diff --git a/lib/inttostr.c b/lib/inttostr.c index 7a4a47f5f6..14db9b8c11 100644 --- a/lib/inttostr.c +++ b/lib/inttostr.c @@ -1,54 +1,4 @@ -/* inttostr.c -- convert integers to printable strings - - Copyright (C) 2001, 2006, 2008, 2009, 2010 Free Software Foundation, Inc. - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . */ - -/* Written by Paul Eggert */ - -#include - -#include "inttostr.h" -#include "verify.h" - -/* Convert I to a printable string in BUF, which must be at least - INT_BUFSIZE_BOUND (INTTYPE) bytes long. Return the address of the - printable string, which need not start at BUF. */ - -char * -inttostr (inttype i, char *buf) -{ - verify (TYPE_SIGNED (inttype) == inttype_is_signed); - char *p = buf + INT_STRLEN_BOUND (inttype); - *p = 0; - -#if inttype_is_signed - if (i < 0) - { - do - *--p = '0' - i % 10; - while ((i /= 10) != 0); - - *--p = '-'; - } - else -#endif - { - do - *--p = '0' + i % 10; - while ((i /= 10) != 0); - } - - return p; -} +#define anytostr inttostr +#define inttype int +#define inttype_is_signed 1 +#include "anytostr.c" diff --git a/lib/inttostr.h b/lib/inttostr.h index f11a5ff672..4f74968703 100644 --- a/lib/inttostr.h +++ b/lib/inttostr.h @@ -39,7 +39,8 @@ # define __attribute_warn_unused_result__ /* empty */ #endif -char *offtostr (off_t, char *) __attribute_warn_unused_result__; char *imaxtostr (intmax_t, char *) __attribute_warn_unused_result__; -char *umaxtostr (uintmax_t, char *) __attribute_warn_unused_result__; +char *inttostr (int, char *) __attribute_warn_unused_result__; +char *offtostr (off_t, char *) __attribute_warn_unused_result__; char *uinttostr (unsigned int, char *) __attribute_warn_unused_result__; +char *umaxtostr (uintmax_t, char *) __attribute_warn_unused_result__; diff --git a/lib/offtostr.c b/lib/offtostr.c index 3a60c6e744..68f2b695be 100644 --- a/lib/offtostr.c +++ b/lib/offtostr.c @@ -1,4 +1,4 @@ -#define inttostr offtostr +#define anytostr offtostr #define inttype off_t #define inttype_is_signed 1 -#include "inttostr.c" +#include "anytostr.c" diff --git a/lib/uinttostr.c b/lib/uinttostr.c index 1662985f40..21fa3761a9 100644 --- a/lib/uinttostr.c +++ b/lib/uinttostr.c @@ -1,4 +1,4 @@ -#define inttostr uinttostr +#define anytostr uinttostr #define inttype unsigned int #define inttype_is_signed 0 -#include "inttostr.c" +#include "anytostr.c" diff --git a/lib/umaxtostr.c b/lib/umaxtostr.c index 914f388d1b..7fd995084e 100644 --- a/lib/umaxtostr.c +++ b/lib/umaxtostr.c @@ -1,4 +1,4 @@ -#define inttostr umaxtostr +#define anytostr umaxtostr #define inttype uintmax_t #define inttype_is_signed 0 -#include "inttostr.c" +#include "anytostr.c" -- cgit v1.2.1