PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
numutils.c File Reference
#include "postgres.h"
#include <math.h>
#include <limits.h>
#include <ctype.h>
#include "utils/builtins.h"
Include dependency graph for numutils.c:

Go to the source code of this file.

Functions

int32 pg_atoi (const char *s, int size, int c)
 
void pg_itoa (int16 i, char *a)
 
void pg_ltoa (int32 value, char *a)
 
void pg_lltoa (int64 value, char *a)
 
char * pg_ltostr_zeropad (char *str, int32 value, int32 minwidth)
 
char * pg_ltostr (char *str, int32 value)
 
uint64 pg_strtouint64 (const char *str, char **endptr, int base)
 

Function Documentation

int32 pg_atoi ( const char *  s,
int  size,
int  c 
)

Definition at line 37 of file numutils.c.

References elog, ereport, errcode(), errmsg(), ERROR, and NULL.

Referenced by ArrayGetIntegerTypmods(), check_foreign_key(), int2in(), int2vectorin(), int4in(), libpqrcv_endstreaming(), libpqrcv_identify_system(), pq_parse_errornotice(), prsd_headline(), and text_format().

38 {
39  long l;
40  char *badp;
41 
42  /*
43  * Some versions of strtol treat the empty string as an error, but some
44  * seem not to. Make an explicit test to be sure we catch it.
45  */
46  if (s == NULL)
47  elog(ERROR, "NULL pointer");
48  if (*s == 0)
49  ereport(ERROR,
50  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
51  errmsg("invalid input syntax for %s: \"%s\"",
52  "integer", s)));
53 
54  errno = 0;
55  l = strtol(s, &badp, 10);
56 
57  /* We made no progress parsing the string, so bail out */
58  if (s == badp)
59  ereport(ERROR,
60  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
61  errmsg("invalid input syntax for %s: \"%s\"",
62  "integer", s)));
63 
64  switch (size)
65  {
66  case sizeof(int32):
67  if (errno == ERANGE
68 #if defined(HAVE_LONG_INT_64)
69  /* won't get ERANGE on these with 64-bit longs... */
70  || l < INT_MIN || l > INT_MAX
71 #endif
72  )
73  ereport(ERROR,
74  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
75  errmsg("value \"%s\" is out of range for type %s", s,
76  "integer")));
77  break;
78  case sizeof(int16):
79  if (errno == ERANGE || l < SHRT_MIN || l > SHRT_MAX)
80  ereport(ERROR,
81  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
82  errmsg("value \"%s\" is out of range for type %s", s,
83  "smallint")));
84  break;
85  case sizeof(int8):
86  if (errno == ERANGE || l < SCHAR_MIN || l > SCHAR_MAX)
87  ereport(ERROR,
88  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
89  errmsg("value \"%s\" is out of range for 8-bit integer", s)));
90  break;
91  default:
92  elog(ERROR, "unsupported result size: %d", size);
93  }
94 
95  /*
96  * Skip any trailing whitespace; if anything but whitespace remains before
97  * the terminating character, bail out
98  */
99  while (*badp && *badp != c && isspace((unsigned char) *badp))
100  badp++;
101 
102  if (*badp && *badp != c)
103  ereport(ERROR,
104  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
105  errmsg("invalid input syntax for %s: \"%s\"",
106  "integer", s)));
107 
108  return (int32) l;
109 }
signed short int16
Definition: c.h:255
int errcode(int sqlerrcode)
Definition: elog.c:575
signed int int32
Definition: c.h:256
#define ERROR
Definition: elog.h:43
char * c
#define ereport(elevel, rest)
Definition: elog.h:122
signed char int8
Definition: c.h:254
#define NULL
Definition: c.h:229
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define elog
Definition: elog.h:219
void pg_itoa ( int16  i,
char *  a 
)

Definition at line 120 of file numutils.c.

References pg_ltoa().

Referenced by int2out(), and int2vectorout().

121 {
122  pg_ltoa((int32) i, a);
123 }
signed int int32
Definition: c.h:256
int i
void pg_ltoa(int32 value, char *a)
Definition: numutils.c:132
void pg_lltoa ( int64  value,
char *  a 
)

Definition at line 186 of file numutils.c.

References PG_INT64_MIN, swap, and value.

Referenced by int8out(), and printsimple().

187 {
188  char *start = a;
189  bool neg = false;
190 
191  /*
192  * Avoid problems with the most negative integer not being representable
193  * as a positive integer.
194  */
195  if (value == PG_INT64_MIN)
196  {
197  memcpy(a, "-9223372036854775808", 21);
198  return;
199  }
200  else if (value < 0)
201  {
202  value = -value;
203  neg = true;
204  }
205 
206  /* Compute the result string backwards. */
207  do
208  {
209  int64 remainder;
210  int64 oldval = value;
211 
212  value /= 10;
213  remainder = oldval - value * 10;
214  *a++ = '0' + remainder;
215  } while (value != 0);
216 
217  if (neg)
218  *a++ = '-';
219 
220  /* Add trailing NUL byte, and back up 'a' to the last character. */
221  *a-- = '\0';
222 
223  /* Reverse string. */
224  while (start < a)
225  {
226  char swap = *start;
227 
228  *start++ = *a;
229  *a-- = swap;
230  }
231 }
#define swap(a, b)
Definition: qsort.c:94
static struct @121 value
#define PG_INT64_MIN
Definition: c.h:342
void pg_ltoa ( int32  value,
char *  a 
)

Definition at line 132 of file numutils.c.

References PG_INT32_MIN, swap, and value.

Referenced by int44out(), int4out(), pg_itoa(), and printsimple().

133 {
134  char *start = a;
135  bool neg = false;
136 
137  /*
138  * Avoid problems with the most negative integer not being representable
139  * as a positive integer.
140  */
141  if (value == PG_INT32_MIN)
142  {
143  memcpy(a, "-2147483648", 12);
144  return;
145  }
146  else if (value < 0)
147  {
148  value = -value;
149  neg = true;
150  }
151 
152  /* Compute the result string backwards. */
153  do
154  {
155  int32 remainder;
156  int32 oldval = value;
157 
158  value /= 10;
159  remainder = oldval - value * 10;
160  *a++ = '0' + remainder;
161  } while (value != 0);
162 
163  if (neg)
164  *a++ = '-';
165 
166  /* Add trailing NUL byte, and back up 'a' to the last character. */
167  *a-- = '\0';
168 
169  /* Reverse string. */
170  while (start < a)
171  {
172  char swap = *start;
173 
174  *start++ = *a;
175  *a-- = swap;
176  }
177 }
#define swap(a, b)
Definition: qsort.c:94
signed int int32
Definition: c.h:256
static struct @121 value
#define PG_INT32_MIN
Definition: c.h:339
char* pg_ltostr ( char *  str,
int32  value 
)

Definition at line 334 of file numutils.c.

References swap, and value.

Referenced by AppendSeconds(), and pg_ltostr_zeropad().

335 {
336  char *start;
337  char *end;
338 
339  /*
340  * Handle negative numbers in a special way. We can't just write a '-'
341  * prefix and reverse the sign as that would overflow for INT32_MIN.
342  */
343  if (value < 0)
344  {
345  *str++ = '-';
346 
347  /* Mark the position we must reverse the string from. */
348  start = str;
349 
350  /* Compute the result string backwards. */
351  do
352  {
353  int32 oldval = value;
354  int32 remainder;
355 
356  value /= 10;
357  remainder = oldval - value * 10;
358  /* As above, we expect remainder to be negative. */
359  *str++ = '0' - remainder;
360  } while (value != 0);
361  }
362  else
363  {
364  /* Mark the position we must reverse the string from. */
365  start = str;
366 
367  /* Compute the result string backwards. */
368  do
369  {
370  int32 oldval = value;
371  int32 remainder;
372 
373  value /= 10;
374  remainder = oldval - value * 10;
375  *str++ = '0' + remainder;
376  } while (value != 0);
377  }
378 
379  /* Remember the end+1 and back up 'str' to the last character. */
380  end = str--;
381 
382  /* Reverse string. */
383  while (start < str)
384  {
385  char swap = *start;
386 
387  *start++ = *str;
388  *str-- = swap;
389  }
390 
391  return end;
392 }
#define swap(a, b)
Definition: qsort.c:94
signed int int32
Definition: c.h:256
static struct @121 value
char* pg_ltostr_zeropad ( char *  str,
int32  value,
int32  minwidth 
)

Definition at line 257 of file numutils.c.

References Assert, pg_ltostr(), and value.

Referenced by AppendSeconds(), EncodeDateOnly(), EncodeDateTime(), EncodeTimeOnly(), and EncodeTimezone().

258 {
259  char *start = str;
260  char *end = &str[minwidth];
261  int32 num = value;
262 
263  Assert(minwidth > 0);
264 
265  /*
266  * Handle negative numbers in a special way. We can't just write a '-'
267  * prefix and reverse the sign as that would overflow for INT32_MIN.
268  */
269  if (num < 0)
270  {
271  *start++ = '-';
272  minwidth--;
273 
274  /*
275  * Build the number starting at the last digit. Here remainder will
276  * be a negative number, so we must reverse the sign before adding '0'
277  * in order to get the correct ASCII digit.
278  */
279  while (minwidth--)
280  {
281  int32 oldval = num;
282  int32 remainder;
283 
284  num /= 10;
285  remainder = oldval - num * 10;
286  start[minwidth] = '0' - remainder;
287  }
288  }
289  else
290  {
291  /* Build the number starting at the last digit */
292  while (minwidth--)
293  {
294  int32 oldval = num;
295  int32 remainder;
296 
297  num /= 10;
298  remainder = oldval - num * 10;
299  start[minwidth] = '0' + remainder;
300  }
301  }
302 
303  /*
304  * If minwidth was not high enough to fit the number then num won't have
305  * been divided down to zero. We punt the problem to pg_ltostr(), which
306  * will generate a correct answer in the minimum valid width.
307  */
308  if (num != 0)
309  return pg_ltostr(str, value);
310 
311  /* Otherwise, return last output character + 1 */
312  return end;
313 }
signed int int32
Definition: c.h:256
static struct @121 value
char * pg_ltostr(char *str, int32 value)
Definition: numutils.c:334
#define Assert(condition)
Definition: c.h:675
uint64 pg_strtouint64 ( const char *  str,
char **  endptr,
int  base 
)

Definition at line 405 of file numutils.c.

Referenced by _SPI_execute_plan(), and pgss_ProcessUtility().

406 {
407 #ifdef _MSC_VER /* MSVC only */
408  return _strtoui64(str, endptr, base);
409 #elif defined(HAVE_STRTOULL) && SIZEOF_LONG < 8
410  return strtoull(str, endptr, base);
411 #else
412  return strtoul(str, endptr, base);
413 #endif
414 }