]> granicus.if.org Git - postgresql/blobdiff - src/backend/utils/adt/numutils.c
Run pgindent on 9.2 source tree in preparation for first 9.3
[postgresql] / src / backend / utils / adt / numutils.c
index 3b0d000365a2d1ad34287f751a1fe89774f5ebb0..f98e7f1020eb0d6f797875af69ee955e125d7552 100644 (file)
@@ -3,55 +3,32 @@
  * numutils.c
  *       utility functions for I/O of built-in numeric types.
  *
- *             integer:                                pg_atoi, pg_itoa, pg_ltoa
- *
- * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
+ * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/backend/utils/adt/numutils.c,v 1.67 2004/12/31 22:01:22 pgsql Exp $
+ *       src/backend/utils/adt/numutils.c
  *
  *-------------------------------------------------------------------------
  */
 #include "postgres.h"
 
-#include <errno.h>
 #include <math.h>
 #include <limits.h>
 #include <ctype.h>
 
 #include "utils/builtins.h"
 
-#ifndef INT_MAX
-#define INT_MAX (0x7FFFFFFFL)
-#endif
-#ifndef INT_MIN
-#define INT_MIN (-INT_MAX-1)
-#endif
-#ifndef SHRT_MAX
-#define SHRT_MAX (0x7FFF)
-#endif
-#ifndef SHRT_MIN
-#define SHRT_MIN (-SHRT_MAX-1)
-#endif
-#ifndef SCHAR_MAX
-#define SCHAR_MAX (0x7F)
-#endif
-#ifndef SCHAR_MIN
-#define SCHAR_MIN (-SCHAR_MAX-1)
-#endif
-
-
 /*
  * pg_atoi: convert string to integer
  *
- * 'size' is the sizeof() the desired integral result (1, 2, or 4 bytes).
- *
  * allows any number of leading or trailing whitespace characters.
  *
- * 'c' is the character that terminates the input string (after any
- * number of whitespace characters).
+ * 'size' is the sizeof() the desired integral result (1, 2, or 4 bytes).
+ *
+ * c, if not 0, is a terminator character that may appear after the
+ * integer (plus whitespace).  If 0, the string must end after the integer.
  *
  * Unlike plain atoi(), this will throw ereport() upon bad input format or
  * overflow.
@@ -63,8 +40,8 @@ pg_atoi(char *s, int size, int c)
        char       *badp;
 
        /*
-        * Some versions of strtol treat the empty string as an error, but
-        * some seem not to.  Make an explicit test to be sure we catch it.
+        * Some versions of strtol treat the empty string as an error, but some
+        * seem not to.  Make an explicit test to be sure we catch it.
         */
        if (s == NULL)
                elog(ERROR, "NULL pointer");
@@ -84,19 +61,6 @@ pg_atoi(char *s, int size, int c)
                                 errmsg("invalid input syntax for integer: \"%s\"",
                                                s)));
 
-       /*
-        * Skip any trailing whitespace; if anything but whitespace remains
-        * before the terminating character, bail out
-        */
-       while (*badp != c && isspace((unsigned char) *badp))
-               badp++;
-
-       if (*badp != c)
-               ereport(ERROR,
-                               (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
-                                errmsg("invalid input syntax for integer: \"%s\"",
-                                               s)));
-
        switch (size)
        {
                case sizeof(int32):
@@ -108,48 +72,158 @@ pg_atoi(char *s, int size, int c)
                                )
                                ereport(ERROR,
                                                (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
-                                                errmsg("value \"%s\" is out of range for type integer", s)));
+                               errmsg("value \"%s\" is out of range for type integer", s)));
                        break;
                case sizeof(int16):
                        if (errno == ERANGE || l < SHRT_MIN || l > SHRT_MAX)
                                ereport(ERROR,
                                                (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
-                                                errmsg("value \"%s\" is out of range for type smallint", s)));
+                               errmsg("value \"%s\" is out of range for type smallint", s)));
                        break;
                case sizeof(int8):
                        if (errno == ERANGE || l < SCHAR_MIN || l > SCHAR_MAX)
                                ereport(ERROR,
                                                (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
-                                                errmsg("value \"%s\" is out of range for 8-bit integer", s)));
+                               errmsg("value \"%s\" is out of range for 8-bit integer", s)));
                        break;
                default:
                        elog(ERROR, "unsupported result size: %d", size);
        }
+
+       /*
+        * Skip any trailing whitespace; if anything but whitespace remains before
+        * the terminating character, bail out
+        */
+       while (*badp && *badp != c && isspace((unsigned char) *badp))
+               badp++;
+
+       if (*badp && *badp != c)
+               ereport(ERROR,
+                               (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
+                                errmsg("invalid input syntax for integer: \"%s\"",
+                                               s)));
+
        return (int32) l;
 }
 
 /*
- *             pg_itoa                 - converts a short int to its string represention
+ * pg_itoa: converts a signed 16-bit integer to its string representation
  *
- *             Note:
- *                             previously based on ~ingres/source/gutil/atoi.c
- *                             now uses vendor's sprintf conversion
+ * Caller must ensure that 'a' points to enough memory to hold the result
+ * (at least 7 bytes, counting a leading sign and trailing NUL).
+ *
+ * It doesn't seem worth implementing this separately.
  */
 void
 pg_itoa(int16 i, char *a)
 {
-       sprintf(a, "%hd", (short) i);
+       pg_ltoa((int32) i, a);
+}
+
+/*
+ * pg_ltoa: converts a signed 32-bit integer to its string representation
+ *
+ * Caller must ensure that 'a' points to enough memory to hold the result
+ * (at least 12 bytes, counting a leading sign and trailing NUL).
+ */
+void
+pg_ltoa(int32 value, char *a)
+{
+       char       *start = a;
+       bool            neg = false;
+
+       /*
+        * Avoid problems with the most negative integer not being representable
+        * as a positive integer.
+        */
+       if (value == (-2147483647 - 1))
+       {
+               memcpy(a, "-2147483648", 12);
+               return;
+       }
+       else if (value < 0)
+       {
+               value = -value;
+               neg = true;
+       }
+
+       /* Compute the result string backwards. */
+       do
+       {
+               int32           remainder;
+               int32           oldval = value;
+
+               value /= 10;
+               remainder = oldval - value * 10;
+               *a++ = '0' + remainder;
+       } while (value != 0);
+
+       if (neg)
+               *a++ = '-';
+
+       /* Add trailing NUL byte, and back up 'a' to the last character. */
+       *a-- = '\0';
+
+       /* Reverse string. */
+       while (start < a)
+       {
+               char            swap = *start;
+
+               *start++ = *a;
+               *a-- = swap;
+       }
 }
 
 /*
- *             pg_ltoa                 - converts a long int to its string represention
+ * pg_lltoa: convert a signed 64-bit integer to its string representation
  *
- *             Note:
- *                             previously based on ~ingres/source/gutil/atoi.c
- *                             now uses vendor's sprintf conversion
+ * Caller must ensure that 'a' points to enough memory to hold the result
+ * (at least MAXINT8LEN+1 bytes, counting a leading sign and trailing NUL).
  */
 void
-pg_ltoa(int32 l, char *a)
+pg_lltoa(int64 value, char *a)
 {
-       sprintf(a, "%d", l);
+       char       *start = a;
+       bool            neg = false;
+
+       /*
+        * Avoid problems with the most negative integer not being representable
+        * as a positive integer.
+        */
+       if (value == (-INT64CONST(0x7FFFFFFFFFFFFFFF) - 1))
+       {
+               memcpy(a, "-9223372036854775808", 21);
+               return;
+       }
+       else if (value < 0)
+       {
+               value = -value;
+               neg = true;
+       }
+
+       /* Compute the result string backwards. */
+       do
+       {
+               int64           remainder;
+               int64           oldval = value;
+
+               value /= 10;
+               remainder = oldval - value * 10;
+               *a++ = '0' + remainder;
+       } while (value != 0);
+
+       if (neg)
+               *a++ = '-';
+
+       /* Add trailing NUL byte, and back up 'a' to the last character. */
+       *a-- = '\0';
+
+       /* Reverse string. */
+       while (start < a)
+       {
+               char            swap = *start;
+
+               *start++ = *a;
+               *a-- = swap;
+       }
 }