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