changeset 12221:a12bc0c11a21

inttostr: aesthetics and improved (compile-time) safety Define inttype_is_signed rather than inttype_is_unsigned, since the sole use is via "#if inttype_is_signed". * lib/imaxtostr.c (inttype_is_signed): Define this, rather than inttype_is_unsigned. * lib/offtostr.c (inttype_is_signed): Likewise. * lib/uinttostr.c (inttype_is_signed): Likewise. * lib/umaxtostr.c (inttype_is_signed): Likewise. * lib/inttostr.c (inttostr): Use verify to cross-check the inttype_is_signed value and the signedness of the actual type. * modules/inttostr (Depends-on): Add verify.
author Jim Meyering <meyering@redhat.com>
date Sat, 31 Oct 2009 09:42:37 +0100
parents 1785ef4dfcdf
children a968e87200d1
files ChangeLog lib/imaxtostr.c lib/inttostr.c lib/offtostr.c lib/uinttostr.c lib/umaxtostr.c modules/inttostr
diffstat 7 files changed, 22 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri Oct 30 09:47:12 2009 -0600
+++ b/ChangeLog	Sat Oct 31 09:42:37 2009 +0100
@@ -1,3 +1,17 @@
+2009-10-31  Jim Meyering  <meyering@redhat.com>
+
+	inttostr: aesthetics and improved (compile-time) safety
+	Define inttype_is_signed rather than inttype_is_unsigned,
+	since the sole use is via "#if inttype_is_signed".
+	* lib/imaxtostr.c (inttype_is_signed): Define this, rather than
+	inttype_is_unsigned.
+	* lib/offtostr.c (inttype_is_signed): Likewise.
+	* lib/uinttostr.c (inttype_is_signed): Likewise.
+	* lib/umaxtostr.c (inttype_is_signed): Likewise.
+	* lib/inttostr.c (inttostr): Use verify to cross-check the
+	inttype_is_signed value and the signedness of the actual type.
+	* modules/inttostr (Depends-on): Add verify.
+
 2009-10-30  Eric Blake  <ebb9@byu.net>
 
 	build: avoid compiler warnings
--- a/lib/imaxtostr.c	Fri Oct 30 09:47:12 2009 -0600
+++ b/lib/imaxtostr.c	Sat Oct 31 09:42:37 2009 +0100
@@ -1,3 +1,4 @@
 #define inttostr imaxtostr
 #define inttype intmax_t
+#define inttype_is_signed 1
 #include "inttostr.c"
--- a/lib/inttostr.c	Fri Oct 30 09:47:12 2009 -0600
+++ b/lib/inttostr.c	Sat Oct 31 09:42:37 2009 +0100
@@ -20,6 +20,7 @@
 #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
@@ -31,7 +32,8 @@
   char *p = buf + INT_STRLEN_BOUND (inttype);
   *p = 0;
 
-#ifndef inttype_is_unsigned
+  verify (TYPE_SIGNED (inttype) == inttype_is_signed);
+#if inttype_is_signed
   if (i < 0)
     {
       do
--- a/lib/offtostr.c	Fri Oct 30 09:47:12 2009 -0600
+++ b/lib/offtostr.c	Sat Oct 31 09:42:37 2009 +0100
@@ -1,3 +1,4 @@
 #define inttostr offtostr
 #define inttype off_t
+#define inttype_is_signed 1
 #include "inttostr.c"
--- a/lib/uinttostr.c	Fri Oct 30 09:47:12 2009 -0600
+++ b/lib/uinttostr.c	Sat Oct 31 09:42:37 2009 +0100
@@ -1,4 +1,4 @@
 #define inttostr uinttostr
 #define inttype unsigned int
-#define inttype_is_unsigned
+#define inttype_is_signed 0
 #include "inttostr.c"
--- a/lib/umaxtostr.c	Fri Oct 30 09:47:12 2009 -0600
+++ b/lib/umaxtostr.c	Sat Oct 31 09:42:37 2009 +0100
@@ -1,4 +1,4 @@
 #define inttostr umaxtostr
 #define inttype uintmax_t
-#define inttype_is_unsigned
+#define inttype_is_signed 0
 #include "inttostr.c"
--- a/modules/inttostr	Fri Oct 30 09:47:12 2009 -0600
+++ b/modules/inttostr	Sat Oct 31 09:42:37 2009 +0100
@@ -13,6 +13,7 @@
 Depends-on:
 intprops
 stdint
+verify
 
 configure.ac:
 gl_INTTOSTR