1 /*-------------------------------------------------------------------------
4 * utility functions for I/O of built-in numeric types.
6 * integer: pg_atoi, pg_itoa, pg_ltoa
8 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
9 * Portions Copyright (c) 1994, Regents of the University of California
13 * $PostgreSQL: pgsql/src/backend/utils/adt/numutils.c,v 1.71 2005/12/01 21:16:13 momjian Exp $
15 *-------------------------------------------------------------------------
23 #include "utils/builtins.h"
26 #define INT_MAX (0x7FFFFFFFL)
29 #define INT_MIN (-INT_MAX-1)
32 #define SHRT_MAX (0x7FFF)
35 #define SHRT_MIN (-SHRT_MAX-1)
38 #define SCHAR_MAX (0x7F)
41 #define SCHAR_MIN (-SCHAR_MAX-1)
46 * pg_atoi: convert string to integer
48 * allows any number of leading or trailing whitespace characters.
50 * 'size' is the sizeof() the desired integral result (1, 2, or 4 bytes).
52 * c, if not 0, is a terminator character that may appear after the
53 * integer (plus whitespace). If 0, the string must end after the integer.
55 * Unlike plain atoi(), this will throw ereport() upon bad input format or
59 pg_atoi(char *s, int size, int c)
65 * Some versions of strtol treat the empty string as an error, but some
66 * seem not to. Make an explicit test to be sure we catch it.
69 elog(ERROR, "NULL pointer");
72 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
73 errmsg("invalid input syntax for integer: \"%s\"",
76 errno = 0; /* avoid having to check the result for failure */
77 l = strtol(s, &badp, 10);
79 /* We made no progress parsing the string, so bail out */
82 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
83 errmsg("invalid input syntax for integer: \"%s\"",
90 #if defined(HAVE_LONG_INT_64)
91 /* won't get ERANGE on these with 64-bit longs... */
92 || l < INT_MIN || l > INT_MAX
96 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
97 errmsg("value \"%s\" is out of range for type integer", s)));
100 if (errno == ERANGE || l < SHRT_MIN || l > SHRT_MAX)
102 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
103 errmsg("value \"%s\" is out of range for type smallint", s)));
106 if (errno == ERANGE || l < SCHAR_MIN || l > SCHAR_MAX)
108 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
109 errmsg("value \"%s\" is out of range for 8-bit integer", s)));
112 elog(ERROR, "unsupported result size: %d", size);
116 * Skip any trailing whitespace; if anything but whitespace remains before
117 * the terminating character, bail out
119 while (*badp && *badp != c && isspace((unsigned char) *badp))
122 if (*badp && *badp != c)
124 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
125 errmsg("invalid input syntax for integer: \"%s\"",
132 * pg_itoa - converts a short int to its string represention
135 * previously based on ~ingres/source/gutil/atoi.c
136 * now uses vendor's sprintf conversion
139 pg_itoa(int16 i, char *a)
141 sprintf(a, "%hd", (short) i);
145 * pg_ltoa - converts a long int to its string represention
148 * previously based on ~ingres/source/gutil/atoi.c
149 * now uses vendor's sprintf conversion
152 pg_ltoa(int32 l, char *a)