bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] inttostr: add a new function, inttostr


From: Jim Meyering
Subject: [PATCH] inttostr: add a new function, inttostr
Date: Wed, 09 Jun 2010 18:32:46 +0200

Hi Paul,

To my surprise, I found that the inttostr module could not convert an
"int" value (tightly) to a string, while it can handle the likes of
uintmax_t, intmax_t, off_t, and even "unsigned int".  It does not provide
an "inttostr" function.  Of course, I could simply use intmax_t instead,
but I could also use sprintf ;-)  The whole point of this module is to
perform the conversion using minimal resources.

However, since inttostr.c is already in use, I couldn't just fill in
the definitions and #include as is done in the other *tostr.c files.
Instead, I've renamed the inttostr.c template to "anytostr.c", and
then added the new, trivial inttostr.c.

Ok to commit?

>From 86551ba9ebbcf389e7b72b06a34d36a5c0a44cd3 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Wed, 9 Jun 2010 18:22:25 +0200
Subject: [PATCH] inttostr: add a new function, inttostr

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.
* lib/inttostr.c: New file.
* modules/inttostr (Files): Add anytostr.c.
* lib/imaxtostr.c: Update use.
* lib/offtostr.c: Likewise.
* lib/uinttostr.c: Likewise.
* lib/umaxtostr.c: Likewise.
---
 ChangeLog        |   13 ++++++++++++
 lib/anytostr.c   |   54 +++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/imaxtostr.c  |    2 +-
 lib/inttostr.c   |   57 ++---------------------------------------------------
 lib/offtostr.c   |    2 +-
 lib/uinttostr.c  |    2 +-
 lib/umaxtostr.c  |    2 +-
 modules/inttostr |    1 +
 8 files changed, 75 insertions(+), 58 deletions(-)
 create mode 100644 lib/anytostr.c

diff --git a/ChangeLog b/ChangeLog
index edb9122..ead5306 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2010-06-09  Jim Meyering  <address@hidden>
+
+       inttostr: add a new function, inttostr
+       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.
+       * lib/inttostr.c: New file.
+       * modules/inttostr (Files): Add anytostr.c.
+       * lib/imaxtostr.c: Update use.
+       * lib/offtostr.c: Likewise.
+       * lib/uinttostr.c: Likewise.
+       * lib/umaxtostr.c: Likewise.
+
 2010-06-08  Peter Simons  <address@hidden>

        maint.mk: make the news-check rule more configurable
diff --git a/lib/anytostr.c b/lib/anytostr.c
new file mode 100644
index 0000000..07e5966
--- /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 <http://www.gnu.org/licenses/>.  */
+
+/* Written by Paul Eggert */
+
+#include <config.h>
+
+#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;
+}
diff --git a/lib/imaxtostr.c b/lib/imaxtostr.c
index 34ef96c..33f361c 100644
--- a/lib/imaxtostr.c
+++ b/lib/imaxtostr.c
@@ -1,4 +1,4 @@
 #define inttostr 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 7a4a47f..bb403e5 100644
--- a/lib/inttostr.c
+++ b/lib/inttostr.c
@@ -1,54 +1,3 @@
-/* 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 <http://www.gnu.org/licenses/>.  */
-
-/* Written by Paul Eggert */
-
-#include <config.h>
-
-#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 inttype int
+#define inttype_is_signed 1
+#include "anytostr.c"
diff --git a/lib/offtostr.c b/lib/offtostr.c
index 3a60c6e..c5143dc 100644
--- a/lib/offtostr.c
+++ b/lib/offtostr.c
@@ -1,4 +1,4 @@
 #define inttostr 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 1662985..0ec328a 100644
--- a/lib/uinttostr.c
+++ b/lib/uinttostr.c
@@ -1,4 +1,4 @@
 #define inttostr 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 914f388..19908f3 100644
--- a/lib/umaxtostr.c
+++ b/lib/umaxtostr.c
@@ -1,4 +1,4 @@
 #define inttostr umaxtostr
 #define inttype uintmax_t
 #define inttype_is_signed 0
-#include "inttostr.c"
+#include "anytostr.c"
diff --git a/modules/inttostr b/modules/inttostr
index 2c2b76e..c0b721d 100644
--- a/modules/inttostr
+++ b/modules/inttostr
@@ -2,6 +2,7 @@ Description:
 Convert integers to printable strings.

 Files:
+lib/anytostr.c
 lib/imaxtostr.c
 lib/inttostr.c
 lib/inttostr.h
--
1.7.1.501.g23b46



reply via email to

[Prev in Thread] Current Thread [Next in Thread]