PostgreSQL Source Code  git master
numutils.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * numutils.c
4  * utility functions for I/O of built-in numeric types.
5  *
6  * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/utils/adt/numutils.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16 
17 #include <math.h>
18 #include <limits.h>
19 #include <ctype.h>
20 
21 #include "common/int.h"
22 #include "utils/builtins.h"
23 #include "port/pg_bitutils.h"
24 
25 /*
26  * A table of all two-digit numbers. This is used to speed up decimal digit
27  * generation by copying pairs of digits into the final output.
28  */
29 static const char DIGIT_TABLE[200] =
30 "00" "01" "02" "03" "04" "05" "06" "07" "08" "09"
31 "10" "11" "12" "13" "14" "15" "16" "17" "18" "19"
32 "20" "21" "22" "23" "24" "25" "26" "27" "28" "29"
33 "30" "31" "32" "33" "34" "35" "36" "37" "38" "39"
34 "40" "41" "42" "43" "44" "45" "46" "47" "48" "49"
35 "50" "51" "52" "53" "54" "55" "56" "57" "58" "59"
36 "60" "61" "62" "63" "64" "65" "66" "67" "68" "69"
37 "70" "71" "72" "73" "74" "75" "76" "77" "78" "79"
38 "80" "81" "82" "83" "84" "85" "86" "87" "88" "89"
39 "90" "91" "92" "93" "94" "95" "96" "97" "98" "99";
40 
41 /*
42  * Adapted from http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10
43  */
44 static inline int
46 {
47  int t;
48  static uint32 PowersOfTen[] =
49  {1, 10, 100,
50  1000, 10000, 100000,
51  1000000, 10000000, 100000000,
52  1000000000};
53  /*
54  * Compute base-10 logarithm by dividing the base-2 logarithm by a
55  * good-enough approximation of the base-2 logarithm of 10
56  */
57  t = (pg_leftmost_one_pos32(v) + 1) * 1233 / 4096;
58  return t + (v >= PowersOfTen[t]);
59 }
60 
61 static inline int
62 decimalLength64(const uint64 v)
63 {
64  int t;
65  static uint64 PowersOfTen[] = {
66  UINT64CONST(1), UINT64CONST(10),
67  UINT64CONST(100), UINT64CONST(1000),
68  UINT64CONST(10000), UINT64CONST(100000),
69  UINT64CONST(1000000), UINT64CONST(10000000),
70  UINT64CONST(100000000), UINT64CONST(1000000000),
71  UINT64CONST(10000000000), UINT64CONST(100000000000),
72  UINT64CONST(1000000000000), UINT64CONST(10000000000000),
73  UINT64CONST(100000000000000), UINT64CONST(1000000000000000),
74  UINT64CONST(10000000000000000), UINT64CONST(100000000000000000),
75  UINT64CONST(1000000000000000000), UINT64CONST(10000000000000000000)
76  };
77 
78  /*
79  * Compute base-10 logarithm by dividing the base-2 logarithm by a
80  * good-enough approximation of the base-2 logarithm of 10
81  */
82  t = (pg_leftmost_one_pos64(v) + 1) * 1233 / 4096;
83  return t + (v >= PowersOfTen[t]);
84 }
85 
86 /*
87  * pg_atoi: convert string to integer
88  *
89  * allows any number of leading or trailing whitespace characters.
90  *
91  * 'size' is the sizeof() the desired integral result (1, 2, or 4 bytes).
92  *
93  * c, if not 0, is a terminator character that may appear after the
94  * integer (plus whitespace). If 0, the string must end after the integer.
95  *
96  * Unlike plain atoi(), this will throw ereport() upon bad input format or
97  * overflow.
98  */
99 int32
100 pg_atoi(const char *s, int size, int c)
101 {
102  long l;
103  char *badp;
104 
105  /*
106  * Some versions of strtol treat the empty string as an error, but some
107  * seem not to. Make an explicit test to be sure we catch it.
108  */
109  if (s == NULL)
110  elog(ERROR, "NULL pointer");
111  if (*s == 0)
112  ereport(ERROR,
113  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
114  errmsg("invalid input syntax for type %s: \"%s\"",
115  "integer", s)));
116 
117  errno = 0;
118  l = strtol(s, &badp, 10);
119 
120  /* We made no progress parsing the string, so bail out */
121  if (s == badp)
122  ereport(ERROR,
123  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
124  errmsg("invalid input syntax for type %s: \"%s\"",
125  "integer", s)));
126 
127  switch (size)
128  {
129  case sizeof(int32):
130  if (errno == ERANGE
131 #if defined(HAVE_LONG_INT_64)
132  /* won't get ERANGE on these with 64-bit longs... */
133  || l < INT_MIN || l > INT_MAX
134 #endif
135  )
136  ereport(ERROR,
137  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
138  errmsg("value \"%s\" is out of range for type %s", s,
139  "integer")));
140  break;
141  case sizeof(int16):
142  if (errno == ERANGE || l < SHRT_MIN || l > SHRT_MAX)
143  ereport(ERROR,
144  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
145  errmsg("value \"%s\" is out of range for type %s", s,
146  "smallint")));
147  break;
148  case sizeof(int8):
149  if (errno == ERANGE || l < SCHAR_MIN || l > SCHAR_MAX)
150  ereport(ERROR,
151  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
152  errmsg("value \"%s\" is out of range for 8-bit integer", s)));
153  break;
154  default:
155  elog(ERROR, "unsupported result size: %d", size);
156  }
157 
158  /*
159  * Skip any trailing whitespace; if anything but whitespace remains before
160  * the terminating character, bail out
161  */
162  while (*badp && *badp != c && isspace((unsigned char) *badp))
163  badp++;
164 
165  if (*badp && *badp != c)
166  ereport(ERROR,
167  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
168  errmsg("invalid input syntax for type %s: \"%s\"",
169  "integer", s)));
170 
171  return (int32) l;
172 }
173 
174 /*
175  * Convert input string to a signed 16 bit integer.
176  *
177  * Allows any number of leading or trailing whitespace characters. Will throw
178  * ereport() upon bad input format or overflow.
179  *
180  * NB: Accumulate input as a negative number, to deal with two's complement
181  * representation of the most negative number, which can't be represented as a
182  * positive number.
183  */
184 int16
185 pg_strtoint16(const char *s)
186 {
187  const char *ptr = s;
188  int16 tmp = 0;
189  bool neg = false;
190 
191  /* skip leading spaces */
192  while (likely(*ptr) && isspace((unsigned char) *ptr))
193  ptr++;
194 
195  /* handle sign */
196  if (*ptr == '-')
197  {
198  ptr++;
199  neg = true;
200  }
201  else if (*ptr == '+')
202  ptr++;
203 
204  /* require at least one digit */
205  if (unlikely(!isdigit((unsigned char) *ptr)))
206  goto invalid_syntax;
207 
208  /* process digits */
209  while (*ptr && isdigit((unsigned char) *ptr))
210  {
211  int8 digit = (*ptr++ - '0');
212 
213  if (unlikely(pg_mul_s16_overflow(tmp, 10, &tmp)) ||
214  unlikely(pg_sub_s16_overflow(tmp, digit, &tmp)))
215  goto out_of_range;
216  }
217 
218  /* allow trailing whitespace, but not other trailing chars */
219  while (*ptr != '\0' && isspace((unsigned char) *ptr))
220  ptr++;
221 
222  if (unlikely(*ptr != '\0'))
223  goto invalid_syntax;
224 
225  if (!neg)
226  {
227  /* could fail if input is most negative number */
228  if (unlikely(tmp == PG_INT16_MIN))
229  goto out_of_range;
230  tmp = -tmp;
231  }
232 
233  return tmp;
234 
235 out_of_range:
236  ereport(ERROR,
237  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
238  errmsg("value \"%s\" is out of range for type %s",
239  s, "smallint")));
240 
241 invalid_syntax:
242  ereport(ERROR,
243  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
244  errmsg("invalid input syntax for type %s: \"%s\"",
245  "smallint", s)));
246 
247  return 0; /* keep compiler quiet */
248 }
249 
250 /*
251  * Convert input string to a signed 32 bit integer.
252  *
253  * Allows any number of leading or trailing whitespace characters. Will throw
254  * ereport() upon bad input format or overflow.
255  *
256  * NB: Accumulate input as a negative number, to deal with two's complement
257  * representation of the most negative number, which can't be represented as a
258  * positive number.
259  */
260 int32
261 pg_strtoint32(const char *s)
262 {
263  const char *ptr = s;
264  int32 tmp = 0;
265  bool neg = false;
266 
267  /* skip leading spaces */
268  while (likely(*ptr) && isspace((unsigned char) *ptr))
269  ptr++;
270 
271  /* handle sign */
272  if (*ptr == '-')
273  {
274  ptr++;
275  neg = true;
276  }
277  else if (*ptr == '+')
278  ptr++;
279 
280  /* require at least one digit */
281  if (unlikely(!isdigit((unsigned char) *ptr)))
282  goto invalid_syntax;
283 
284  /* process digits */
285  while (*ptr && isdigit((unsigned char) *ptr))
286  {
287  int8 digit = (*ptr++ - '0');
288 
289  if (unlikely(pg_mul_s32_overflow(tmp, 10, &tmp)) ||
290  unlikely(pg_sub_s32_overflow(tmp, digit, &tmp)))
291  goto out_of_range;
292  }
293 
294  /* allow trailing whitespace, but not other trailing chars */
295  while (*ptr != '\0' && isspace((unsigned char) *ptr))
296  ptr++;
297 
298  if (unlikely(*ptr != '\0'))
299  goto invalid_syntax;
300 
301  if (!neg)
302  {
303  /* could fail if input is most negative number */
304  if (unlikely(tmp == PG_INT32_MIN))
305  goto out_of_range;
306  tmp = -tmp;
307  }
308 
309  return tmp;
310 
311 out_of_range:
312  ereport(ERROR,
313  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
314  errmsg("value \"%s\" is out of range for type %s",
315  s, "integer")));
316 
317 invalid_syntax:
318  ereport(ERROR,
319  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
320  errmsg("invalid input syntax for type %s: \"%s\"",
321  "integer", s)));
322 
323  return 0; /* keep compiler quiet */
324 }
325 
326 /*
327  * pg_itoa: converts a signed 16-bit integer to its string representation
328  *
329  * Caller must ensure that 'a' points to enough memory to hold the result
330  * (at least 7 bytes, counting a leading sign and trailing NUL).
331  *
332  * It doesn't seem worth implementing this separately.
333  */
334 void
335 pg_itoa(int16 i, char *a)
336 {
337  pg_ltoa((int32) i, a);
338 }
339 
340 /*
341  * pg_ultoa_n: converts an unsigned 32-bit integer to its string representation,
342  * not NUL-terminated, and returns the length of that string representation
343  *
344  * Caller must ensure that 'a' points to enough memory to hold the result (at
345  * least 10 bytes)
346  */
347 int
349 {
350  int olength,
351  i = 0;
352 
353  /* Degenerate case */
354  if (value == 0)
355  {
356  *a = '0';
357  return 1;
358  }
359 
360  olength = decimalLength32(value);
361 
362  /* Compute the result string. */
363  while (value >= 10000)
364  {
365  const uint32 c = value - 10000 * (value / 10000);
366  const uint32 c0 = (c % 100) << 1;
367  const uint32 c1 = (c / 100) << 1;
368 
369  char *pos = a + olength - i;
370 
371  value /= 10000;
372 
373  memcpy(pos - 2, DIGIT_TABLE + c0, 2);
374  memcpy(pos - 4, DIGIT_TABLE + c1, 2);
375  i += 4;
376  }
377  if (value >= 100)
378  {
379  const uint32 c = (value % 100) << 1;
380 
381  char *pos = a + olength - i;
382 
383  value /= 100;
384 
385  memcpy(pos - 2, DIGIT_TABLE + c, 2);
386  i += 2;
387  }
388  if (value >= 10)
389  {
390  const uint32 c = value << 1;
391 
392  char *pos = a + olength - i;
393 
394  memcpy(pos - 2, DIGIT_TABLE + c, 2);
395  }
396  else
397  {
398  *a = (char) ('0' + value);
399  }
400 
401  return olength;
402 }
403 
404 /*
405  * NUL-terminate the output of pg_ultoa_n.
406  *
407  * It is the caller's responsibility to ensure that a is at least 12 bytes long,
408  * which is enough room to hold a minus sign, a maximally long int32, and the
409  * above terminating NUL.
410  */
411 void
412 pg_ltoa(int32 value, char *a)
413 {
414 
415  uint32 uvalue = (uint32) value;
416  int len;
417 
418  if (value < 0)
419  {
420  uvalue = (uint32) 0 - uvalue;
421  *a++ = '-';
422  }
423  len = pg_ultoa_n(uvalue, a);
424  a[len] = '\0';
425 }
426 
427 /*
428  * Get the decimal representation, not NUL-terminated, and return the length of
429  * same. Caller must ensure that a points to at least MAXINT8LEN bytes.
430  */
431 int
432 pg_ulltoa_n(uint64 value, char *a)
433 {
434  int olength,
435  i = 0;
436  uint32 value2;
437 
438 
439  /* Degenerate case */
440  if (value == 0)
441  {
442  *a = '0';
443  return 1;
444  }
445 
446  olength = decimalLength64(value);
447 
448  /* Compute the result string. */
449  while (value >= 100000000)
450  {
451  const uint64 q = value / 100000000;
452  uint32 value2 = (uint32) (value - 100000000 * q);
453 
454  const uint32 c = value2 % 10000;
455  const uint32 d = value2 / 10000;
456  const uint32 c0 = (c % 100) << 1;
457  const uint32 c1 = (c / 100) << 1;
458  const uint32 d0 = (d % 100) << 1;
459  const uint32 d1 = (d / 100) << 1;
460 
461  char *pos = a + olength - i;
462 
463  value = q;
464 
465  memcpy(pos - 2, DIGIT_TABLE + c0, 2);
466  memcpy(pos - 4, DIGIT_TABLE + c1, 2);
467  memcpy(pos - 6, DIGIT_TABLE + d0, 2);
468  memcpy(pos - 8, DIGIT_TABLE + d1, 2);
469  i += 8;
470  }
471 
472  /* Switch to 32-bit for speed */
473  value2 = (uint32) value;
474 
475  if (value2 >= 10000)
476  {
477  const uint32 c = value2 - 10000 * (value2 / 10000);
478  const uint32 c0 = (c % 100) << 1;
479  const uint32 c1 = (c / 100) << 1;
480 
481  char *pos = a + olength - i;
482 
483  value2 /= 10000;
484 
485  memcpy(pos - 2, DIGIT_TABLE + c0, 2);
486  memcpy(pos - 4, DIGIT_TABLE + c1, 2);
487  i += 4;
488  }
489  if (value2 >= 100)
490  {
491  const uint32 c = (value2 % 100) << 1;
492  char *pos = a + olength - i;
493 
494  value2 /= 100;
495 
496  memcpy(pos - 2, DIGIT_TABLE + c, 2);
497  i += 2;
498  }
499  if (value2 >= 10)
500  {
501  const uint32 c = value2 << 1;
502  char *pos = a + olength - i;
503 
504  memcpy(pos - 2, DIGIT_TABLE + c, 2);
505  }
506  else
507  *a = (char) ('0' + value2);
508 
509  return olength;
510 }
511 
512 /*
513  * pg_lltoa: convert a signed 64-bit integer to its string representation
514  *
515  * Caller must ensure that 'a' points to enough memory to hold the result
516  * (at least MAXINT8LEN + 1 bytes, counting a leading sign and trailing NUL).
517  */
518 void
519 pg_lltoa(int64 value, char *a)
520 {
521  int len;
522  uint64 uvalue = value;
523 
524  if (value < 0)
525  {
526  *a++ = '-';
527  uvalue = (uint64) 0 - uvalue;
528  }
529  len = pg_ulltoa_n(uvalue, a);
530  a[len] = 0;
531 }
532 
533 
534 /*
535  * pg_ultostr_zeropad
536  * Converts 'value' into a decimal string representation stored at 'str'.
537  * 'minwidth' specifies the minimum width of the result; any extra space
538  * is filled up by prefixing the number with zeros.
539  *
540  * Returns the ending address of the string result (the last character written
541  * plus 1). Note that no NUL terminator is written.
542  *
543  * The intended use-case for this function is to build strings that contain
544  * multiple individual numbers, for example:
545  *
546  * str = pg_ltostr_zeropad(str, hours, 2);
547  * *str++ = ':';
548  * str = pg_ltostr_zeropad(str, mins, 2);
549  * *str++ = ':';
550  * str = pg_ltostr_zeropad(str, secs, 2);
551  * *str = '\0';
552  *
553  * Note: Caller must ensure that 'str' points to enough memory to hold the
554  * result.
555  */
556 char *
558 {
559  int len;
560 
561  Assert(minwidth > 0);
562 
563  if (value < 100 && minwidth == 2) /* Short cut for common case */
564  {
565  memcpy(str, DIGIT_TABLE + value * 2, 2);
566  return str + 2;
567  }
568 
569  len = pg_ultoa_n(value, str);
570  if (len >= minwidth)
571  return str + len;
572 
573  memmove(str + minwidth - len, str, len);
574  memset(str, '0', minwidth - len);
575  return str + minwidth;
576 }
577 
578 /*
579  * pg_ltostr
580  * Converts 'value' into a decimal string representation stored at 'str'.
581  *
582  * Returns the ending address of the string result (the last character written
583  * plus 1). Note that no NUL terminator is written.
584  *
585  * The intended use-case for this function is to build strings that contain
586  * multiple individual numbers, for example:
587  *
588  * str = pg_ltostr(str, a);
589  * *str++ = ' ';
590  * str = pg_ltostr(str, b);
591  * *str = '\0';
592  *
593  * Note: Caller must ensure that 'str' points to enough memory to hold the
594  * result.
595  */
596 char *
598 {
599  int len = pg_ultoa_n(value, str);
600 
601  return str + len;
602 }
603 
604 /*
605  * pg_strtouint64
606  * Converts 'str' into an unsigned 64-bit integer.
607  *
608  * This has the identical API to strtoul(3), except that it will handle
609  * 64-bit ints even where "long" is narrower than that.
610  *
611  * For the moment it seems sufficient to assume that the platform has
612  * such a function somewhere; let's not roll our own.
613  */
614 uint64
615 pg_strtouint64(const char *str, char **endptr, int base)
616 {
617 #ifdef _MSC_VER /* MSVC only */
618  return _strtoui64(str, endptr, base);
619 #elif defined(HAVE_STRTOULL) && SIZEOF_LONG < 8
620  return strtoull(str, endptr, base);
621 #else
622  return strtoul(str, endptr, base);
623 #endif
624 }
signed short int16
Definition: c.h:354
#define likely(x)
Definition: c.h:205
static bool pg_mul_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:140
char * pg_ultostr_zeropad(char *str, uint32 value, int32 minwidth)
Definition: numutils.c:557
int errcode(int sqlerrcode)
Definition: elog.c:608
static int pg_leftmost_one_pos32(uint32 word)
Definition: pg_bitutils.h:26
char * pg_ultostr(char *str, uint32 value)
Definition: numutils.c:597
static bool pg_mul_s16_overflow(int16 a, int16 b, int16 *result)
Definition: int.h:83
static int pg_leftmost_one_pos64(uint64 word)
Definition: pg_bitutils.h:49
int pg_ulltoa_n(uint64 value, char *a)
Definition: numutils.c:432
signed int int32
Definition: c.h:355
static int decimalLength32(const uint32 v)
Definition: numutils.c:45
static int decimalLength64(const uint64 v)
Definition: numutils.c:62
#define ERROR
Definition: elog.h:43
char * c
int16 pg_strtoint16(const char *s)
Definition: numutils.c:185
unsigned int uint32
Definition: c.h:367
static bool pg_sub_s32_overflow(int32 a, int32 b, int32 *result)
Definition: int.h:122
#define PG_INT16_MIN
Definition: c.h:446
#define PG_INT32_MIN
Definition: c.h:449
#define ereport(elevel, rest)
Definition: elog.h:141
void pg_itoa(int16 i, char *a)
Definition: numutils.c:335
signed char int8
Definition: c.h:353
uint64 pg_strtouint64(const char *str, char **endptr, int base)
Definition: numutils.c:615
static struct @143 value
int pg_ultoa_n(uint32 value, char *a)
Definition: numutils.c:348
#define Assert(condition)
Definition: c.h:738
static bool pg_sub_s16_overflow(int16 a, int16 b, int16 *result)
Definition: int.h:65
int32 pg_strtoint32(const char *s)
Definition: numutils.c:261
void pg_lltoa(int64 value, char *a)
Definition: numutils.c:519
int errmsg(const char *fmt,...)
Definition: elog.c:822
#define elog(elevel,...)
Definition: elog.h:228
int i
static const char DIGIT_TABLE[200]
Definition: numutils.c:29
#define unlikely(x)
Definition: c.h:206
void pg_ltoa(int32 value, char *a)
Definition: numutils.c:412
int32 pg_atoi(const char *s, int size, int c)
Definition: numutils.c:100