m4-patches
[Top][All Lists]
Advanced

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

06-bye-bye-ecvt.patch


From: Akim Demaille
Subject: 06-bye-bye-ecvt.patch
Date: Fri, 17 Aug 2001 13:47:38 +0200

This is very debatable, I don't apply it, waiting for some feedback.
I like simplifying the code, but we might lose some performances here.
Still, when aiming at portability, I think we should stick to extreme
simplicity.  And IMHO, if we actually want to keep ecvt, then it is
the converse that should be done: implement a simple ecvt based on
sprintf, not two implementation of format.

FYI, the GNU libc's doc snippet:

   Old-fashioned System V number-to-string functions
   =================================================

      The old System V C library provided three functions to convert
   numbers to strings, with unusual and hard-to-use semantics.  The GNU C
   library also provides these functions and some natural extensions.

      These functions are only available in glibc and on systems descended
   from AT&T Unix.  Therefore, unless these functions do precisely what you
   need, it is better to use `sprintf', which is standard.


Index: ChangeLog
from  Akim Demaille  <address@hidden>

        Use sprintf, not ecft and friends since it is standard, portable,
        simplifies the code, and since the latter is even deprecated
        according to the GNU libc documentation.

        * modules/format.c, configure.in: Drop evct support.

Index: config-h.in
--- config-h.in Wed, 15 Aug 2001 18:31:50 +0200 akim
+++ config-h.in Thu, 16 Aug 2001 14:29:16 +0200 akim
@@ -81,13 +81,6 @@
 /* Define if you have the `doprnt' function. */
 #undef HAVE_DOPRNT

-/* Define if you have the `ecvt' function. */
-#undef HAVE_ECVT
-
-/* Define to 1 if you have ecvt(3), fcvt(3) and gcvt(3), define to 2 if these
-   are declared in <stdlib.h>. */
-#undef HAVE_EFGCVT
-
 /* Define if you have the <errno.h> header file. */
 #undef HAVE_ERRNO_H

Index: configure.in
--- configure.in Thu, 16 Aug 2001 14:25:10 +0200 akim
+++ configure.in Thu, 16 Aug 2001 14:27:20 +0200 akim
@@ -91,13 +91,6 @@

 jm_PREREQ_ERROR

-AC_MSG_CHECKING(ecvt declaration)
-AC_EGREP_HEADER(ecvt, stdlib.h,
-  [AC_MSG_RESULT(yes); AC_DEFINE(HAVE_EFGCVT, 2,
-[Define to 1 if you have ecvt(3), fcvt(3) and gcvt(3), define to 2 if
-   these are declared in <stdlib.h>.])],
-  [AC_MSG_RESULT(no); AC_CHECK_FUNCS(ecvt)])
-
 M4_AC_SYS_STACKOVF

 AC_MSG_CHECKING([[if changeword is wanted]])
Index: modules/format.c
--- modules/format.c Fri, 10 Aug 2001 14:01:13 +0200 akim
+++ modules/format.c Thu, 16 Aug 2001 14:27:06 +0200 akim
@@ -1,5 +1,6 @@
 /* GNU m4 -- A simple macro processor
-   Copyright 1989, 90, 91, 92, 93, 94 Free Software Foundation, Inc.
+   Copyright 1989, 1990, 1991, 1992, 1993, 1994, 2001
+   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
@@ -19,67 +20,6 @@

 /* printf like formatting for m4.  */

-#ifdef HAVE_EFGCVT
-
-/* Various constants for floating point formatting.  */
-#define MAXFIELD       128     /* size of buffer for formatted text */
-/* The following two are hardware dependant.  */
-#define ECVTMAX                18      /* max number of significant digits for 
%e */
-#define FCVTMAX                (18+38+4) /* max number of significant digits 
for %f */
-
-/* Externs used herein.  */
-#if HAVE_EFGCVT <= 1
-extern char *ecvt (), *fcvt (), *gcvt ();
-#endif
-
-#ifndef STDC_HEADERS
-extern int atoi ();
-extern long atol ();
-extern double atof ();
-#endif /* STDC_HEADERS */
-
-#ifndef min
-#define min(a, b)      ((a) < (b) ? (a) : (b))
-#endif
-
-static char const digits[] = "0123456789abcdef";
-static char const Digits[] = "0123456789ABCDEF";
-
-/* STR has dimension MAXFIELD (?).  */
-
-static char *
-ulong_to_str (register unsigned long val, char *str, int base,
-             const char *map)
-{
-  register char *s = &str[MAXFIELD];
-
-  *--s = '\0';
-  do
-    {
-      *--s = map[val % base];
-      val /= base;
-    }
-  while (val > 0);
-
-  return s;
-}
-
-/*-----------------------------------------.
-| Clear trailing zeroes, return argument.  |
-`-----------------------------------------*/
-
-static char *
-clr0 (char *s)
-{
-  register char *t;
-
-  for (t = s + strlen (s); *--t == '0' && t > s;)
-    *t = '\0';
-  return s;
-}
-
-#endif /* HAVE_EFGCVT */
-
 /* Simple varargs substitute.  */

 #define ARG_INT(argc, argv) \
@@ -121,409 +61,6 @@
      int argc;
      m4_token_data **argv;
 {
-#ifdef HAVE_EFGCVT
-
-  const unsigned char *fmt;    /* format control string */
-  int c;                       /* a simple character */
-  char fc;                     /* format code */
-
-  /* Flags.  */
-  char flags;                  /* 1 iff treating flags */
-  char ljust;                  /* left justification */
-  char mandsign;               /* mandatory sign */
-  char noplus;                 /* use space if no sign */
-  char alternate;              /* use alternate form */
-  char zeropad;                        /* do zero padding */
-  char plus;                   /* plus-sign, according to mandatory and noplus 
*/
-
-  /* Precision specifiers.  */
-  int width;                   /* minimum field width */
-  int prec;                    /* precision */
-  int maxch;                   /* maximum no. of chars to print */
-  char lflag;                  /* long flag */
-  char hflag;                  /* short flag */
-
-  /* Different parts of each specification.  */
-  char sign;                   /* wanted sign, iff any */
-  int ppad;                    /* pre-prefix zero padding */
-  const char *prefix;          /* value prefix */
-  int lpad;                    /* zero padding on the left */
-  register char *s;            /* ptr to formatted text */
-  int rpad;                    /* zero padding on the rigth*/
-  const char *suffix;          /* value suffix */
-
-  /* Buffer and stuff.  */
-  char str[MAXFIELD];          /* buffer for formatted text */
-  int length;                  /* length of str */
-  int padding;                 /* padding at the left or rigth */
-  register int i;              /* an index */
-
-/* Length of trailing string in str.  */
-#define LENGTH(s)      (&str[MAXFIELD-1] - (s))
-#define HAS_SIGN       (sign != '\0')
-
-  fmt = ARG_STR (argc, argv);
-  for (;;)
-    {
-      while ((c = *fmt++) != '%')
-       {
-         if (c == 0)
-           return;
-         obstack_1grow (obs, c);
-       }
-      if (*fmt == '%')
-       {
-         obstack_1grow (obs, '%');
-         fmt++;
-         continue;
-       }
-
-      /* Parse flags.  */
-      flags = 1;
-      ljust = mandsign = noplus = alternate = zeropad = 0;
-      do
-       {
-         switch (*fmt)
-           {
-           case '-':           /* left justification */
-             ljust = 1;
-             break;
-
-           case '+':           /* mandatory sign */
-             mandsign = 1;
-             break;
-
-           case ' ':           /* space instead of positive sign */
-             noplus = 1;
-             break;
-
-           case '0':           /* zero padding */
-             zeropad = 1;
-             break;
-
-           case '#':           /* alternate output */
-             alternate = 1;
-             break;
-
-           default:
-             flags = 0;
-             break;
-           }
-       }
-      while (flags && fmt++);
-
-      plus = '\0';             /* what to use as a plus ??? */
-      if (mandsign)
-       plus = '+';
-      else if (noplus)
-       plus = ' ';
-
-      if (ljust)
-       zeropad = 0;
-
-      /* Minimum field width.  */
-      width = -1;
-      if (*fmt == '*')
-       {
-         width = ARG_INT (argc, argv);
-         fmt++;
-       }
-      else if (isdigit (*fmt))
-       {
-         width = 0;
-         do
-           {
-             width = width * 10 + *fmt++ - '0';
-           }
-         while (isdigit (*fmt));
-       }
-
-      /* Maximum precision.  */
-      prec = -1;
-      if (*fmt == '.')
-       {
-         if (*(++fmt) == '*')
-           {
-             prec = ARG_INT (argc, argv);
-             ++fmt;
-           }
-         else if (isdigit (*fmt))
-           {
-             prec = 0;
-             do
-               {
-                 prec = prec * 10 + *fmt++ - '0';
-               }
-             while (isdigit (*fmt))
-               ;
-           }
-       }
-
-      /* Length modifiers.  */
-      lflag = (*fmt == 'l');
-      hflag = (*fmt == 'h');
-      if (lflag || hflag)
-       fmt++;
-
-      sign = '\0';
-      ppad = lpad = rpad = 0;
-      maxch = -1;
-      prefix = suffix = "";
-
-      switch (fc = *fmt++)
-       {
-
-       case '\0':
-         return;
-
-       case 'c':
-         c = ARG_INT (argc, argv);
-         str[0] = (unsigned char) c;
-         str[1] = '\0';
-         s = str;
-         break;
-
-       case 's':
-         s = ARG_STR (argc, argv);
-         maxch = prec;
-         break;
-
-       case 'd':
-       case 'i':
-         if (lflag)
-           {
-             long val = ARG_LONG (argc, argv);
-             if (val < 0)
-               {
-                 val = -val;   /* does not work for MINLONG */
-                 sign = '-';
-               }
-             else
-               sign = plus;
-             s = ulong_to_str ((unsigned long) val, str, 10, digits);
-           }
-         else
-           {
-             int val = ARG_INT (argc, argv);
-             if (hflag)
-               val = (short) val;
-             if (val < 0)
-               {
-                 val = -val;   /* does not work for MININT */
-                 sign = '-';
-               }
-             else
-               sign = plus;
-             s = ulong_to_str ((unsigned long) val, str, 10, digits);
-           }
-         if (zeropad)
-           lpad = width - LENGTH (s) - HAS_SIGN;
-         break;
-
-       case 'o':
-         if (lflag)
-           {
-             unsigned long val = ARG_ULONG (argc, argv);
-             s = ulong_to_str ((unsigned long) val, str, 8, digits);
-           }
-         else
-           {
-             unsigned int val = ARG_UINT (argc, argv);
-             if (hflag)
-               val = (unsigned short) val;
-             s = ulong_to_str ((unsigned long) val, str, 8, digits);
-           }
-         if (alternate)
-           prefix = "0";
-         if (zeropad)
-           lpad = width - LENGTH (s) - alternate;
-         break;
-
-       case 'x':
-       case 'X':
-         if (lflag)
-           {
-             unsigned long val = ARG_ULONG (argc, argv);
-             s = ulong_to_str ((unsigned long) val, str, 16,
-                              (fc == 'x') ? digits : Digits);
-           }
-         else
-           {
-             unsigned int val = ARG_UINT (argc, argv);
-             if (hflag)
-               val = (unsigned short) val;
-             s = ulong_to_str ((unsigned long) val, str, 16,
-                              (fc == 'x') ? digits : Digits);
-           }
-         if (alternate)
-           prefix = (fc == 'X') ? "0X" : "0x";
-         if (zeropad)
-           lpad = width - LENGTH (s) - 2*alternate;
-         break;
-
-       case 'u':
-         if (lflag)
-           {
-             unsigned long val = ARG_ULONG (argc, argv);
-             s = ulong_to_str ((unsigned long) val, str, 10, digits);
-           }
-         else
-           {
-             unsigned int val = ARG_UINT (argc, argv);
-             if (hflag)
-               val = (unsigned short) val;
-             s = ulong_to_str ((unsigned long) val, str, 10, digits);
-           }
-         if (zeropad)
-           lpad = width - LENGTH (s);
-         break;
-
-       case 'e':
-       case 'E':
-         {
-           char *t;
-           int sgn, decpt, exp, n;
-           double val = ARG_DOUBLE (argc, argv);
-
-           if (prec < 0)
-             prec = 6;
-           t = clr0 (ecvt (val, min (prec + 1, ECVTMAX), &decpt, &sgn));
-           sign = sgn ? '-' : plus;
-
-           n = prec;
-           s = str;
-           exp = (t[0] == '0' && t[1] == '\0') ? 0 : decpt - 1;
-
-           *s++ = *t++;
-           if (n > 0 || alternate)
-             *s++ = '.';
-           while (*t != '\0' && --n >= 0)
-             *s++ = *t++;
-           *s = '\0';
-           rpad = n;
-
-           sgn = 0;
-           if (exp < 0)
-             {
-               exp = -exp;
-               sgn = 1;
-             }
-           t = ulong_to_str ((unsigned long) exp, str, 10, digits);
-           if (exp < 10)
-             *--t = '0';       /* always at least two digits */
-           *--t = sgn ? '-' : '+';
-           *--t = fc;
-
-           if (zeropad)
-             {
-               lpad = width - HAS_SIGN - (s - str) - LENGTH (t);
-               if (rpad > 0)
-                 lpad -= rpad;
-             }
-
-           suffix = t;
-           s = str;
-         }
-         break;
-
-       case 'f':
-         {
-           const char *t;
-           int sgn, decpt, n;
-           double val = ARG_DOUBLE (argc, argv);
-
-           if (prec < 0)
-             prec = 6;
-
-           /* FIXME: For the following line, Dave Anglin reports
-              ``warning: passing arg 1 of `clr0' discards `const' from
-              pointer target type''.  I suspect fcvt might be declared
-              as returning const on some systems.  Pouah!  I should
-              revise this whole module, one of these days...  */
-
-           t = clr0 (fcvt (val, min (prec, FCVTMAX), &decpt, &sgn));
-
-           sign = sgn ? '-' : plus;
-
-           n = prec;
-           s = str;
-
-           if (decpt <= 0)
-             {
-               prefix = (n > 0 || alternate) ? "0." : "0";
-               lpad = min (-decpt, prec);
-               n -= lpad;
-             }
-           else
-             {
-               while (*t && --decpt >= 0)
-                 *s++ = *t++;
-               while (--decpt>=0)
-                 *s++ = '0';
-               if (n > 0 || alternate)
-                 *s++ = '.';
-             }
-           while (*t && --n >= 0)
-             *s++ = *t++;
-
-           *s = '\0';
-           rpad = n;
-
-           if (zeropad)
-             ppad = width - HAS_SIGN - (prefix[1] ? 2 : 1) - lpad -
-               (s - str) - rpad;
-
-           s = str;
-         }
-         break;
-
-       default:
-         continue;
-       }
-
-      if (lpad < 0)
-       lpad = 0;
-      if (rpad < 0)
-       rpad = 0;
-      if (width < 0)
-       width = 0;
-
-      i = strlen (s);
-      if (maxch <= 0 || maxch > i)
-       maxch = i;
-
-      length = (HAS_SIGN + ppad + strlen (prefix) + lpad + maxch
-               + rpad + strlen (suffix));
-      padding = 0;
-      if (width != 0)
-       {
-         padding = width - length;
-       }
-
-      if (ljust == 0)          /* left padding */
-       for (i = padding; --i >= 0;)
-         obstack_1grow (obs, ' ');
-      if (HAS_SIGN)            /* sign */
-       obstack_1grow (obs, sign);
-      for (i = ppad; --i >= 0;)        /* pre-prefix zero padding */
-       obstack_1grow (obs, '0');
-      for (; *prefix; ++prefix)        /* prefix */
-       obstack_1grow (obs, *prefix);
-      for (i = lpad; --i >= 0;)        /* left zero padding */
-       obstack_1grow (obs, '0');
-      for (i = maxch; --i >= 0; ++s) /* actual text */
-       obstack_1grow (obs, *s);
-      for (i = rpad; --i >= 0;)        /* right zero padding */
-       obstack_1grow (obs, '0');
-      for (; *suffix; ++suffix)        /* suffix */
-       obstack_1grow (obs, *suffix);
-      if (ljust != 0)          /* right padding */
-       for (i = padding; --i >= 0;)
-         obstack_1grow (obs, ' ');
-    }
-
-#else /* not HAVE_EFGCVT */
-
   char *fmt;                   /* format control string */
   const char *fstart;          /* beginning of current format spec */
   int c;                       /* a simple character */
@@ -747,6 +284,4 @@

       obstack_grow (obs, str, strlen (str));
     }
-
-#endif /* not HAVE_EFGCVT */
 }



reply via email to

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