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-2023, 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 static const int8 hexlookup[128] = {
89  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
90  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
91  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
92  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
93  -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
94  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
95  -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
96  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
97 };
98 
99 /*
100  * Convert input string to a signed 16 bit integer. Input strings may be
101  * expressed in base-10, hexadecimal, octal, or binary format, all of which
102  * can be prefixed by an optional sign character, either '+' (the default) or
103  * '-' for negative numbers. Hex strings are recognized by the digits being
104  * prefixed by 0x or 0X while octal strings are recognized by the 0o or 0O
105  * prefix. The binary representation is recognized by the 0b or 0B prefix.
106  *
107  * Allows any number of leading or trailing whitespace characters. Digits may
108  * optionally be separated by a single underscore character. These can only
109  * come between digits and not before or after the digits. Underscores have
110  * no effect on the return value and are supported only to assist in improving
111  * the human readability of the input strings.
112  *
113  * pg_strtoint16() will throw ereport() upon bad input format or overflow;
114  * while pg_strtoint16_safe() instead returns such complaints in *escontext,
115  * if it's an ErrorSaveContext.
116 *
117  * NB: Accumulate input as an unsigned number, to deal with two's complement
118  * representation of the most negative number, which can't be represented as a
119  * signed positive number.
120  */
121 int16
122 pg_strtoint16(const char *s)
123 {
124  return pg_strtoint16_safe(s, NULL);
125 }
126 
127 int16
128 pg_strtoint16_safe(const char *s, Node *escontext)
129 {
130  const char *ptr = s;
131  const char *firstdigit;
132  uint16 tmp = 0;
133  bool neg = false;
134  unsigned char digit;
135 
136  /*
137  * The majority of cases are likely to be base-10 digits without any
138  * underscore separator characters. We'll first try to parse the string
139  * with the assumption that's the case and only fallback on a slower
140  * implementation which handles hex, octal and binary strings and
141  * underscores if the fastpath version cannot parse the string.
142  */
143 
144  /* leave it up to the slow path to look for leading spaces */
145 
146  if (*ptr == '-')
147  {
148  ptr++;
149  neg = true;
150  }
151 
152  /* a leading '+' is uncommon so leave that for the slow path */
153 
154  /* process the first digit */
155  digit = (*ptr - '0');
156 
157  /*
158  * Exploit unsigned arithmetic to save having to check both the upper and
159  * lower bounds of the digit.
160  */
161  if (likely(digit < 10))
162  {
163  ptr++;
164  tmp = digit;
165  }
166  else
167  {
168  /* we need at least one digit */
169  goto slow;
170  }
171 
172  /* process remaining digits */
173  for (;;)
174  {
175  digit = (*ptr - '0');
176 
177  if (digit >= 10)
178  break;
179 
180  ptr++;
181 
182  if (unlikely(tmp > -(PG_INT16_MIN / 10)))
183  goto out_of_range;
184 
185  tmp = tmp * 10 + digit;
186  }
187 
188  /* when the string does not end in a digit, let the slow path handle it */
189  if (unlikely(*ptr != '\0'))
190  goto slow;
191 
192  if (neg)
193  {
194  /* check the negative equivalent will fit without overflowing */
195  if (unlikely(tmp > (uint16) (-(PG_INT16_MIN + 1)) + 1))
196  goto out_of_range;
197  return -((int16) tmp);
198  }
199 
200  if (unlikely(tmp > PG_INT16_MAX))
201  goto out_of_range;
202 
203  return (int16) tmp;
204 
205 slow:
206  tmp = 0;
207  ptr = s;
208  /* no need to reset neg */
209 
210  /* skip leading spaces */
211  while (isspace((unsigned char) *ptr))
212  ptr++;
213 
214  /* handle sign */
215  if (*ptr == '-')
216  {
217  ptr++;
218  neg = true;
219  }
220  else if (*ptr == '+')
221  ptr++;
222 
223  /* process digits */
224  if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
225  {
226  firstdigit = ptr += 2;
227 
228  for (;;)
229  {
230  if (isxdigit((unsigned char) *ptr))
231  {
232  if (unlikely(tmp > -(PG_INT16_MIN / 16)))
233  goto out_of_range;
234 
235  tmp = tmp * 16 + hexlookup[(unsigned char) *ptr++];
236  }
237  else if (*ptr == '_')
238  {
239  /* underscore must be followed by more digits */
240  ptr++;
241  if (*ptr == '\0' || !isxdigit((unsigned char) *ptr))
242  goto invalid_syntax;
243  }
244  else
245  break;
246  }
247  }
248  else if (ptr[0] == '0' && (ptr[1] == 'o' || ptr[1] == 'O'))
249  {
250  firstdigit = ptr += 2;
251 
252  for (;;)
253  {
254  if (*ptr >= '0' && *ptr <= '7')
255  {
256  if (unlikely(tmp > -(PG_INT16_MIN / 8)))
257  goto out_of_range;
258 
259  tmp = tmp * 8 + (*ptr++ - '0');
260  }
261  else if (*ptr == '_')
262  {
263  /* underscore must be followed by more digits */
264  ptr++;
265  if (*ptr == '\0' || *ptr < '0' || *ptr > '7')
266  goto invalid_syntax;
267  }
268  else
269  break;
270  }
271  }
272  else if (ptr[0] == '0' && (ptr[1] == 'b' || ptr[1] == 'B'))
273  {
274  firstdigit = ptr += 2;
275 
276  for (;;)
277  {
278  if (*ptr >= '0' && *ptr <= '1')
279  {
280  if (unlikely(tmp > -(PG_INT16_MIN / 2)))
281  goto out_of_range;
282 
283  tmp = tmp * 2 + (*ptr++ - '0');
284  }
285  else if (*ptr == '_')
286  {
287  /* underscore must be followed by more digits */
288  ptr++;
289  if (*ptr == '\0' || *ptr < '0' || *ptr > '1')
290  goto invalid_syntax;
291  }
292  else
293  break;
294  }
295  }
296  else
297  {
298  firstdigit = ptr;
299 
300  for (;;)
301  {
302  if (*ptr >= '0' && *ptr <= '9')
303  {
304  if (unlikely(tmp > -(PG_INT16_MIN / 10)))
305  goto out_of_range;
306 
307  tmp = tmp * 10 + (*ptr++ - '0');
308  }
309  else if (*ptr == '_')
310  {
311  /* underscore may not be first */
312  if (unlikely(ptr == firstdigit))
313  goto invalid_syntax;
314  /* and it must be followed by more digits */
315  ptr++;
316  if (*ptr == '\0' || !isdigit((unsigned char) *ptr))
317  goto invalid_syntax;
318  }
319  else
320  break;
321  }
322  }
323 
324  /* require at least one digit */
325  if (unlikely(ptr == firstdigit))
326  goto invalid_syntax;
327 
328  /* allow trailing whitespace, but not other trailing chars */
329  while (isspace((unsigned char) *ptr))
330  ptr++;
331 
332  if (unlikely(*ptr != '\0'))
333  goto invalid_syntax;
334 
335  if (neg)
336  {
337  /* check the negative equivalent will fit without overflowing */
338  if (tmp > (uint16) (-(PG_INT16_MIN + 1)) + 1)
339  goto out_of_range;
340  return -((int16) tmp);
341  }
342 
343  if (tmp > PG_INT16_MAX)
344  goto out_of_range;
345 
346  return (int16) tmp;
347 
348 out_of_range:
349  ereturn(escontext, 0,
350  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
351  errmsg("value \"%s\" is out of range for type %s",
352  s, "smallint")));
353 
354 invalid_syntax:
355  ereturn(escontext, 0,
356  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
357  errmsg("invalid input syntax for type %s: \"%s\"",
358  "smallint", s)));
359 }
360 
361 /*
362  * Convert input string to a signed 32 bit integer. Input strings may be
363  * expressed in base-10, hexadecimal, octal, or binary format, all of which
364  * can be prefixed by an optional sign character, either '+' (the default) or
365  * '-' for negative numbers. Hex strings are recognized by the digits being
366  * prefixed by 0x or 0X while octal strings are recognized by the 0o or 0O
367  * prefix. The binary representation is recognized by the 0b or 0B prefix.
368  *
369  * Allows any number of leading or trailing whitespace characters. Digits may
370  * optionally be separated by a single underscore character. These can only
371  * come between digits and not before or after the digits. Underscores have
372  * no effect on the return value and are supported only to assist in improving
373  * the human readability of the input strings.
374  *
375  * pg_strtoint32() will throw ereport() upon bad input format or overflow;
376  * while pg_strtoint32_safe() instead returns such complaints in *escontext,
377  * if it's an ErrorSaveContext.
378  *
379  * NB: Accumulate input as an unsigned number, to deal with two's complement
380  * representation of the most negative number, which can't be represented as a
381  * signed positive number.
382  */
383 int32
384 pg_strtoint32(const char *s)
385 {
386  return pg_strtoint32_safe(s, NULL);
387 }
388 
389 int32
390 pg_strtoint32_safe(const char *s, Node *escontext)
391 {
392  const char *ptr = s;
393  const char *firstdigit;
394  uint32 tmp = 0;
395  bool neg = false;
396  unsigned char digit;
397 
398  /*
399  * The majority of cases are likely to be base-10 digits without any
400  * underscore separator characters. We'll first try to parse the string
401  * with the assumption that's the case and only fallback on a slower
402  * implementation which handles hex, octal and binary strings and
403  * underscores if the fastpath version cannot parse the string.
404  */
405 
406  /* leave it up to the slow path to look for leading spaces */
407 
408  if (*ptr == '-')
409  {
410  ptr++;
411  neg = true;
412  }
413 
414  /* a leading '+' is uncommon so leave that for the slow path */
415 
416  /* process the first digit */
417  digit = (*ptr - '0');
418 
419  /*
420  * Exploit unsigned arithmetic to save having to check both the upper and
421  * lower bounds of the digit.
422  */
423  if (likely(digit < 10))
424  {
425  ptr++;
426  tmp = digit;
427  }
428  else
429  {
430  /* we need at least one digit */
431  goto slow;
432  }
433 
434  /* process remaining digits */
435  for (;;)
436  {
437  digit = (*ptr - '0');
438 
439  if (digit >= 10)
440  break;
441 
442  ptr++;
443 
444  if (unlikely(tmp > -(PG_INT32_MIN / 10)))
445  goto out_of_range;
446 
447  tmp = tmp * 10 + digit;
448  }
449 
450  /* when the string does not end in a digit, let the slow path handle it */
451  if (unlikely(*ptr != '\0'))
452  goto slow;
453 
454  if (neg)
455  {
456  /* check the negative equivalent will fit without overflowing */
457  if (unlikely(tmp > (uint32) (-(PG_INT32_MIN + 1)) + 1))
458  goto out_of_range;
459  return -((int32) tmp);
460  }
461 
462  if (unlikely(tmp > PG_INT32_MAX))
463  goto out_of_range;
464 
465  return (int32) tmp;
466 
467 slow:
468  tmp = 0;
469  ptr = s;
470  /* no need to reset neg */
471 
472  /* skip leading spaces */
473  while (isspace((unsigned char) *ptr))
474  ptr++;
475 
476  /* handle sign */
477  if (*ptr == '-')
478  {
479  ptr++;
480  neg = true;
481  }
482  else if (*ptr == '+')
483  ptr++;
484 
485  /* process digits */
486  if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
487  {
488  firstdigit = ptr += 2;
489 
490  for (;;)
491  {
492  if (isxdigit((unsigned char) *ptr))
493  {
494  if (unlikely(tmp > -(PG_INT32_MIN / 16)))
495  goto out_of_range;
496 
497  tmp = tmp * 16 + hexlookup[(unsigned char) *ptr++];
498  }
499  else if (*ptr == '_')
500  {
501  /* underscore must be followed by more digits */
502  ptr++;
503  if (*ptr == '\0' || !isxdigit((unsigned char) *ptr))
504  goto invalid_syntax;
505  }
506  else
507  break;
508  }
509  }
510  else if (ptr[0] == '0' && (ptr[1] == 'o' || ptr[1] == 'O'))
511  {
512  firstdigit = ptr += 2;
513 
514  for (;;)
515  {
516  if (*ptr >= '0' && *ptr <= '7')
517  {
518  if (unlikely(tmp > -(PG_INT32_MIN / 8)))
519  goto out_of_range;
520 
521  tmp = tmp * 8 + (*ptr++ - '0');
522  }
523  else if (*ptr == '_')
524  {
525  /* underscore must be followed by more digits */
526  ptr++;
527  if (*ptr == '\0' || *ptr < '0' || *ptr > '7')
528  goto invalid_syntax;
529  }
530  else
531  break;
532  }
533  }
534  else if (ptr[0] == '0' && (ptr[1] == 'b' || ptr[1] == 'B'))
535  {
536  firstdigit = ptr += 2;
537 
538  for (;;)
539  {
540  if (*ptr >= '0' && *ptr <= '1')
541  {
542  if (unlikely(tmp > -(PG_INT32_MIN / 2)))
543  goto out_of_range;
544 
545  tmp = tmp * 2 + (*ptr++ - '0');
546  }
547  else if (*ptr == '_')
548  {
549  /* underscore must be followed by more digits */
550  ptr++;
551  if (*ptr == '\0' || *ptr < '0' || *ptr > '1')
552  goto invalid_syntax;
553  }
554  else
555  break;
556  }
557  }
558  else
559  {
560  firstdigit = ptr;
561 
562  for (;;)
563  {
564  if (*ptr >= '0' && *ptr <= '9')
565  {
566  if (unlikely(tmp > -(PG_INT32_MIN / 10)))
567  goto out_of_range;
568 
569  tmp = tmp * 10 + (*ptr++ - '0');
570  }
571  else if (*ptr == '_')
572  {
573  /* underscore may not be first */
574  if (unlikely(ptr == firstdigit))
575  goto invalid_syntax;
576  /* and it must be followed by more digits */
577  ptr++;
578  if (*ptr == '\0' || !isdigit((unsigned char) *ptr))
579  goto invalid_syntax;
580  }
581  else
582  break;
583  }
584  }
585 
586  /* require at least one digit */
587  if (unlikely(ptr == firstdigit))
588  goto invalid_syntax;
589 
590  /* allow trailing whitespace, but not other trailing chars */
591  while (isspace((unsigned char) *ptr))
592  ptr++;
593 
594  if (unlikely(*ptr != '\0'))
595  goto invalid_syntax;
596 
597  if (neg)
598  {
599  /* check the negative equivalent will fit without overflowing */
600  if (tmp > (uint32) (-(PG_INT32_MIN + 1)) + 1)
601  goto out_of_range;
602  return -((int32) tmp);
603  }
604 
605  if (tmp > PG_INT32_MAX)
606  goto out_of_range;
607 
608  return (int32) tmp;
609 
610 out_of_range:
611  ereturn(escontext, 0,
612  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
613  errmsg("value \"%s\" is out of range for type %s",
614  s, "integer")));
615 
616 invalid_syntax:
617  ereturn(escontext, 0,
618  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
619  errmsg("invalid input syntax for type %s: \"%s\"",
620  "integer", s)));
621 }
622 
623 /*
624  * Convert input string to a signed 64 bit integer. Input strings may be
625  * expressed in base-10, hexadecimal, octal, or binary format, all of which
626  * can be prefixed by an optional sign character, either '+' (the default) or
627  * '-' for negative numbers. Hex strings are recognized by the digits being
628  * prefixed by 0x or 0X while octal strings are recognized by the 0o or 0O
629  * prefix. The binary representation is recognized by the 0b or 0B prefix.
630  *
631  * Allows any number of leading or trailing whitespace characters. Digits may
632  * optionally be separated by a single underscore character. These can only
633  * come between digits and not before or after the digits. Underscores have
634  * no effect on the return value and are supported only to assist in improving
635  * the human readability of the input strings.
636  *
637  * pg_strtoint64() will throw ereport() upon bad input format or overflow;
638  * while pg_strtoint64_safe() instead returns such complaints in *escontext,
639  * if it's an ErrorSaveContext.
640  *
641  * NB: Accumulate input as an unsigned number, to deal with two's complement
642  * representation of the most negative number, which can't be represented as a
643  * signed positive number.
644  */
645 int64
646 pg_strtoint64(const char *s)
647 {
648  return pg_strtoint64_safe(s, NULL);
649 }
650 
651 int64
652 pg_strtoint64_safe(const char *s, Node *escontext)
653 {
654  const char *ptr = s;
655  const char *firstdigit;
656  uint64 tmp = 0;
657  bool neg = false;
658  unsigned char digit;
659 
660  /*
661  * The majority of cases are likely to be base-10 digits without any
662  * underscore separator characters. We'll first try to parse the string
663  * with the assumption that's the case and only fallback on a slower
664  * implementation which handles hex, octal and binary strings and
665  * underscores if the fastpath version cannot parse the string.
666  */
667 
668  /* leave it up to the slow path to look for leading spaces */
669 
670  if (*ptr == '-')
671  {
672  ptr++;
673  neg = true;
674  }
675 
676  /* a leading '+' is uncommon so leave that for the slow path */
677 
678  /* process the first digit */
679  digit = (*ptr - '0');
680 
681  /*
682  * Exploit unsigned arithmetic to save having to check both the upper and
683  * lower bounds of the digit.
684  */
685  if (likely(digit < 10))
686  {
687  ptr++;
688  tmp = digit;
689  }
690  else
691  {
692  /* we need at least one digit */
693  goto slow;
694  }
695 
696  /* process remaining digits */
697  for (;;)
698  {
699  digit = (*ptr - '0');
700 
701  if (digit >= 10)
702  break;
703 
704  ptr++;
705 
706  if (unlikely(tmp > -(PG_INT64_MIN / 10)))
707  goto out_of_range;
708 
709  tmp = tmp * 10 + digit;
710  }
711 
712  /* when the string does not end in a digit, let the slow path handle it */
713  if (unlikely(*ptr != '\0'))
714  goto slow;
715 
716  if (neg)
717  {
718  /* check the negative equivalent will fit without overflowing */
719  if (unlikely(tmp > (uint64) (-(PG_INT64_MIN + 1)) + 1))
720  goto out_of_range;
721  return -((int64) tmp);
722  }
723 
724  if (unlikely(tmp > PG_INT64_MAX))
725  goto out_of_range;
726 
727  return (int64) tmp;
728 
729 slow:
730  tmp = 0;
731  ptr = s;
732  /* no need to reset neg */
733 
734  /* skip leading spaces */
735  while (isspace((unsigned char) *ptr))
736  ptr++;
737 
738  /* handle sign */
739  if (*ptr == '-')
740  {
741  ptr++;
742  neg = true;
743  }
744  else if (*ptr == '+')
745  ptr++;
746 
747  /* process digits */
748  if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
749  {
750  firstdigit = ptr += 2;
751 
752  for (;;)
753  {
754  if (isxdigit((unsigned char) *ptr))
755  {
756  if (unlikely(tmp > -(PG_INT64_MIN / 16)))
757  goto out_of_range;
758 
759  tmp = tmp * 16 + hexlookup[(unsigned char) *ptr++];
760  }
761  else if (*ptr == '_')
762  {
763  /* underscore must be followed by more digits */
764  ptr++;
765  if (*ptr == '\0' || !isxdigit((unsigned char) *ptr))
766  goto invalid_syntax;
767  }
768  else
769  break;
770  }
771  }
772  else if (ptr[0] == '0' && (ptr[1] == 'o' || ptr[1] == 'O'))
773  {
774  firstdigit = ptr += 2;
775 
776  for (;;)
777  {
778  if (*ptr >= '0' && *ptr <= '7')
779  {
780  if (unlikely(tmp > -(PG_INT64_MIN / 8)))
781  goto out_of_range;
782 
783  tmp = tmp * 8 + (*ptr++ - '0');
784  }
785  else if (*ptr == '_')
786  {
787  /* underscore must be followed by more digits */
788  ptr++;
789  if (*ptr == '\0' || *ptr < '0' || *ptr > '7')
790  goto invalid_syntax;
791  }
792  else
793  break;
794  }
795  }
796  else if (ptr[0] == '0' && (ptr[1] == 'b' || ptr[1] == 'B'))
797  {
798  firstdigit = ptr += 2;
799 
800  for (;;)
801  {
802  if (*ptr >= '0' && *ptr <= '1')
803  {
804  if (unlikely(tmp > -(PG_INT64_MIN / 2)))
805  goto out_of_range;
806 
807  tmp = tmp * 2 + (*ptr++ - '0');
808  }
809  else if (*ptr == '_')
810  {
811  /* underscore must be followed by more digits */
812  ptr++;
813  if (*ptr == '\0' || *ptr < '0' || *ptr > '1')
814  goto invalid_syntax;
815  }
816  else
817  break;
818  }
819  }
820  else
821  {
822  firstdigit = ptr;
823 
824  for (;;)
825  {
826  if (*ptr >= '0' && *ptr <= '9')
827  {
828  if (unlikely(tmp > -(PG_INT64_MIN / 10)))
829  goto out_of_range;
830 
831  tmp = tmp * 10 + (*ptr++ - '0');
832  }
833  else if (*ptr == '_')
834  {
835  /* underscore may not be first */
836  if (unlikely(ptr == firstdigit))
837  goto invalid_syntax;
838  /* and it must be followed by more digits */
839  ptr++;
840  if (*ptr == '\0' || !isdigit((unsigned char) *ptr))
841  goto invalid_syntax;
842  }
843  else
844  break;
845  }
846  }
847 
848  /* require at least one digit */
849  if (unlikely(ptr == firstdigit))
850  goto invalid_syntax;
851 
852  /* allow trailing whitespace, but not other trailing chars */
853  while (isspace((unsigned char) *ptr))
854  ptr++;
855 
856  if (unlikely(*ptr != '\0'))
857  goto invalid_syntax;
858 
859  if (neg)
860  {
861  /* check the negative equivalent will fit without overflowing */
862  if (tmp > (uint64) (-(PG_INT64_MIN + 1)) + 1)
863  goto out_of_range;
864  return -((int64) tmp);
865  }
866 
867  if (tmp > PG_INT64_MAX)
868  goto out_of_range;
869 
870  return (int64) tmp;
871 
872 out_of_range:
873  ereturn(escontext, 0,
874  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
875  errmsg("value \"%s\" is out of range for type %s",
876  s, "bigint")));
877 
878 invalid_syntax:
879  ereturn(escontext, 0,
880  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
881  errmsg("invalid input syntax for type %s: \"%s\"",
882  "bigint", s)));
883 }
884 
885 /*
886  * Convert input string to an unsigned 32 bit integer.
887  *
888  * Allows any number of leading or trailing whitespace characters.
889  *
890  * If endloc isn't NULL, store a pointer to the rest of the string there,
891  * so that caller can parse the rest. Otherwise, it's an error if anything
892  * but whitespace follows.
893  *
894  * typname is what is reported in error messages.
895  *
896  * If escontext points to an ErrorSaveContext node, that is filled instead
897  * of throwing an error; the caller must check SOFT_ERROR_OCCURRED()
898  * to detect errors.
899  */
900 uint32
901 uint32in_subr(const char *s, char **endloc,
902  const char *typname, Node *escontext)
903 {
904  uint32 result;
905  unsigned long cvt;
906  char *endptr;
907 
908  errno = 0;
909  cvt = strtoul(s, &endptr, 0);
910 
911  /*
912  * strtoul() normally only sets ERANGE. On some systems it may also set
913  * EINVAL, which simply means it couldn't parse the input string. Be sure
914  * to report that the same way as the standard error indication (that
915  * endptr == s).
916  */
917  if ((errno && errno != ERANGE) || endptr == s)
918  ereturn(escontext, 0,
919  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
920  errmsg("invalid input syntax for type %s: \"%s\"",
921  typname, s)));
922 
923  if (errno == ERANGE)
924  ereturn(escontext, 0,
925  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
926  errmsg("value \"%s\" is out of range for type %s",
927  s, typname)));
928 
929  if (endloc)
930  {
931  /* caller wants to deal with rest of string */
932  *endloc = endptr;
933  }
934  else
935  {
936  /* allow only whitespace after number */
937  while (*endptr && isspace((unsigned char) *endptr))
938  endptr++;
939  if (*endptr)
940  ereturn(escontext, 0,
941  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
942  errmsg("invalid input syntax for type %s: \"%s\"",
943  typname, s)));
944  }
945 
946  result = (uint32) cvt;
947 
948  /*
949  * Cope with possibility that unsigned long is wider than uint32, in which
950  * case strtoul will not raise an error for some values that are out of
951  * the range of uint32.
952  *
953  * For backwards compatibility, we want to accept inputs that are given
954  * with a minus sign, so allow the input value if it matches after either
955  * signed or unsigned extension to long.
956  *
957  * To ensure consistent results on 32-bit and 64-bit platforms, make sure
958  * the error message is the same as if strtoul() had returned ERANGE.
959  */
960 #if PG_UINT32_MAX != ULONG_MAX
961  if (cvt != (unsigned long) result &&
962  cvt != (unsigned long) ((int) result))
963  ereturn(escontext, 0,
964  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
965  errmsg("value \"%s\" is out of range for type %s",
966  s, typname)));
967 #endif
968 
969  return result;
970 }
971 
972 /*
973  * Convert input string to an unsigned 64 bit integer.
974  *
975  * Allows any number of leading or trailing whitespace characters.
976  *
977  * If endloc isn't NULL, store a pointer to the rest of the string there,
978  * so that caller can parse the rest. Otherwise, it's an error if anything
979  * but whitespace follows.
980  *
981  * typname is what is reported in error messages.
982  *
983  * If escontext points to an ErrorSaveContext node, that is filled instead
984  * of throwing an error; the caller must check SOFT_ERROR_OCCURRED()
985  * to detect errors.
986  */
987 uint64
988 uint64in_subr(const char *s, char **endloc,
989  const char *typname, Node *escontext)
990 {
991  uint64 result;
992  char *endptr;
993 
994  errno = 0;
995  result = strtou64(s, &endptr, 0);
996 
997  /*
998  * strtoul[l] normally only sets ERANGE. On some systems it may also set
999  * EINVAL, which simply means it couldn't parse the input string. Be sure
1000  * to report that the same way as the standard error indication (that
1001  * endptr == s).
1002  */
1003  if ((errno && errno != ERANGE) || endptr == s)
1004  ereturn(escontext, 0,
1005  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1006  errmsg("invalid input syntax for type %s: \"%s\"",
1007  typname, s)));
1008 
1009  if (errno == ERANGE)
1010  ereturn(escontext, 0,
1011  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1012  errmsg("value \"%s\" is out of range for type %s",
1013  s, typname)));
1014 
1015  if (endloc)
1016  {
1017  /* caller wants to deal with rest of string */
1018  *endloc = endptr;
1019  }
1020  else
1021  {
1022  /* allow only whitespace after number */
1023  while (*endptr && isspace((unsigned char) *endptr))
1024  endptr++;
1025  if (*endptr)
1026  ereturn(escontext, 0,
1027  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
1028  errmsg("invalid input syntax for type %s: \"%s\"",
1029  typname, s)));
1030  }
1031 
1032  return result;
1033 }
1034 
1035 /*
1036  * pg_itoa: converts a signed 16-bit integer to its string representation
1037  * and returns strlen(a).
1038  *
1039  * Caller must ensure that 'a' points to enough memory to hold the result
1040  * (at least 7 bytes, counting a leading sign and trailing NUL).
1041  *
1042  * It doesn't seem worth implementing this separately.
1043  */
1044 int
1045 pg_itoa(int16 i, char *a)
1046 {
1047  return pg_ltoa((int32) i, a);
1048 }
1049 
1050 /*
1051  * pg_ultoa_n: converts an unsigned 32-bit integer to its string representation,
1052  * not NUL-terminated, and returns the length of that string representation
1053  *
1054  * Caller must ensure that 'a' points to enough memory to hold the result (at
1055  * least 10 bytes)
1056  */
1057 int
1059 {
1060  int olength,
1061  i = 0;
1062 
1063  /* Degenerate case */
1064  if (value == 0)
1065  {
1066  *a = '0';
1067  return 1;
1068  }
1069 
1070  olength = decimalLength32(value);
1071 
1072  /* Compute the result string. */
1073  while (value >= 10000)
1074  {
1075  const uint32 c = value - 10000 * (value / 10000);
1076  const uint32 c0 = (c % 100) << 1;
1077  const uint32 c1 = (c / 100) << 1;
1078 
1079  char *pos = a + olength - i;
1080 
1081  value /= 10000;
1082 
1083  memcpy(pos - 2, DIGIT_TABLE + c0, 2);
1084  memcpy(pos - 4, DIGIT_TABLE + c1, 2);
1085  i += 4;
1086  }
1087  if (value >= 100)
1088  {
1089  const uint32 c = (value % 100) << 1;
1090 
1091  char *pos = a + olength - i;
1092 
1093  value /= 100;
1094 
1095  memcpy(pos - 2, DIGIT_TABLE + c, 2);
1096  i += 2;
1097  }
1098  if (value >= 10)
1099  {
1100  const uint32 c = value << 1;
1101 
1102  char *pos = a + olength - i;
1103 
1104  memcpy(pos - 2, DIGIT_TABLE + c, 2);
1105  }
1106  else
1107  {
1108  *a = (char) ('0' + value);
1109  }
1110 
1111  return olength;
1112 }
1113 
1114 /*
1115  * pg_ltoa: converts a signed 32-bit integer to its string representation and
1116  * returns strlen(a).
1117  *
1118  * It is the caller's responsibility to ensure that a is at least 12 bytes long,
1119  * which is enough room to hold a minus sign, a maximally long int32, and the
1120  * above terminating NUL.
1121  */
1122 int
1124 {
1125  uint32 uvalue = (uint32) value;
1126  int len = 0;
1127 
1128  if (value < 0)
1129  {
1130  uvalue = (uint32) 0 - uvalue;
1131  a[len++] = '-';
1132  }
1133  len += pg_ultoa_n(uvalue, a + len);
1134  a[len] = '\0';
1135  return len;
1136 }
1137 
1138 /*
1139  * Get the decimal representation, not NUL-terminated, and return the length of
1140  * same. Caller must ensure that a points to at least MAXINT8LEN bytes.
1141  */
1142 int
1143 pg_ulltoa_n(uint64 value, char *a)
1144 {
1145  int olength,
1146  i = 0;
1147  uint32 value2;
1148 
1149  /* Degenerate case */
1150  if (value == 0)
1151  {
1152  *a = '0';
1153  return 1;
1154  }
1155 
1156  olength = decimalLength64(value);
1157 
1158  /* Compute the result string. */
1159  while (value >= 100000000)
1160  {
1161  const uint64 q = value / 100000000;
1162  uint32 value3 = (uint32) (value - 100000000 * q);
1163 
1164  const uint32 c = value3 % 10000;
1165  const uint32 d = value3 / 10000;
1166  const uint32 c0 = (c % 100) << 1;
1167  const uint32 c1 = (c / 100) << 1;
1168  const uint32 d0 = (d % 100) << 1;
1169  const uint32 d1 = (d / 100) << 1;
1170 
1171  char *pos = a + olength - i;
1172 
1173  value = q;
1174 
1175  memcpy(pos - 2, DIGIT_TABLE + c0, 2);
1176  memcpy(pos - 4, DIGIT_TABLE + c1, 2);
1177  memcpy(pos - 6, DIGIT_TABLE + d0, 2);
1178  memcpy(pos - 8, DIGIT_TABLE + d1, 2);
1179  i += 8;
1180  }
1181 
1182  /* Switch to 32-bit for speed */
1183  value2 = (uint32) value;
1184 
1185  if (value2 >= 10000)
1186  {
1187  const uint32 c = value2 - 10000 * (value2 / 10000);
1188  const uint32 c0 = (c % 100) << 1;
1189  const uint32 c1 = (c / 100) << 1;
1190 
1191  char *pos = a + olength - i;
1192 
1193  value2 /= 10000;
1194 
1195  memcpy(pos - 2, DIGIT_TABLE + c0, 2);
1196  memcpy(pos - 4, DIGIT_TABLE + c1, 2);
1197  i += 4;
1198  }
1199  if (value2 >= 100)
1200  {
1201  const uint32 c = (value2 % 100) << 1;
1202  char *pos = a + olength - i;
1203 
1204  value2 /= 100;
1205 
1206  memcpy(pos - 2, DIGIT_TABLE + c, 2);
1207  i += 2;
1208  }
1209  if (value2 >= 10)
1210  {
1211  const uint32 c = value2 << 1;
1212  char *pos = a + olength - i;
1213 
1214  memcpy(pos - 2, DIGIT_TABLE + c, 2);
1215  }
1216  else
1217  *a = (char) ('0' + value2);
1218 
1219  return olength;
1220 }
1221 
1222 /*
1223  * pg_lltoa: converts a signed 64-bit integer to its string representation and
1224  * returns strlen(a).
1225  *
1226  * Caller must ensure that 'a' points to enough memory to hold the result
1227  * (at least MAXINT8LEN + 1 bytes, counting a leading sign and trailing NUL).
1228  */
1229 int
1230 pg_lltoa(int64 value, char *a)
1231 {
1232  uint64 uvalue = value;
1233  int len = 0;
1234 
1235  if (value < 0)
1236  {
1237  uvalue = (uint64) 0 - uvalue;
1238  a[len++] = '-';
1239  }
1240 
1241  len += pg_ulltoa_n(uvalue, a + len);
1242  a[len] = '\0';
1243  return len;
1244 }
1245 
1246 
1247 /*
1248  * pg_ultostr_zeropad
1249  * Converts 'value' into a decimal string representation stored at 'str'.
1250  * 'minwidth' specifies the minimum width of the result; any extra space
1251  * is filled up by prefixing the number with zeros.
1252  *
1253  * Returns the ending address of the string result (the last character written
1254  * plus 1). Note that no NUL terminator is written.
1255  *
1256  * The intended use-case for this function is to build strings that contain
1257  * multiple individual numbers, for example:
1258  *
1259  * str = pg_ultostr_zeropad(str, hours, 2);
1260  * *str++ = ':';
1261  * str = pg_ultostr_zeropad(str, mins, 2);
1262  * *str++ = ':';
1263  * str = pg_ultostr_zeropad(str, secs, 2);
1264  * *str = '\0';
1265  *
1266  * Note: Caller must ensure that 'str' points to enough memory to hold the
1267  * result.
1268  */
1269 char *
1271 {
1272  int len;
1273 
1274  Assert(minwidth > 0);
1275 
1276  if (value < 100 && minwidth == 2) /* Short cut for common case */
1277  {
1278  memcpy(str, DIGIT_TABLE + value * 2, 2);
1279  return str + 2;
1280  }
1281 
1282  len = pg_ultoa_n(value, str);
1283  if (len >= minwidth)
1284  return str + len;
1285 
1286  memmove(str + minwidth - len, str, len);
1287  memset(str, '0', minwidth - len);
1288  return str + minwidth;
1289 }
1290 
1291 /*
1292  * pg_ultostr
1293  * Converts 'value' into a decimal string representation stored at 'str'.
1294  *
1295  * Returns the ending address of the string result (the last character written
1296  * plus 1). Note that no NUL terminator is written.
1297  *
1298  * The intended use-case for this function is to build strings that contain
1299  * multiple individual numbers, for example:
1300  *
1301  * str = pg_ultostr(str, a);
1302  * *str++ = ' ';
1303  * str = pg_ultostr(str, b);
1304  * *str = '\0';
1305  *
1306  * Note: Caller must ensure that 'str' points to enough memory to hold the
1307  * result.
1308  */
1309 char *
1311 {
1312  int len = pg_ultoa_n(value, str);
1313 
1314  return str + len;
1315 }
unsigned short uint16
Definition: c.h:494
unsigned int uint32
Definition: c.h:495
signed char int8
Definition: c.h:481
#define PG_INT32_MAX
Definition: c.h:578
#define likely(x)
Definition: c.h:299
signed short int16
Definition: c.h:482
signed int int32
Definition: c.h:483
#define strtou64(str, endptr, base)
Definition: c.h:1308
#define PG_INT16_MIN
Definition: c.h:574
#define PG_INT64_MAX
Definition: c.h:581
#define PG_INT64_MIN
Definition: c.h:580
#define unlikely(x)
Definition: c.h:300
#define PG_INT32_MIN
Definition: c.h:577
#define PG_INT16_MAX
Definition: c.h:575
int errcode(int sqlerrcode)
Definition: elog.c:858
int errmsg(const char *fmt,...)
Definition: elog.c:1069
#define ereturn(context, dummy_value,...)
Definition: elog.h:276
static struct @148 value
int a
Definition: isn.c:69
int i
Definition: isn.c:73
Assert(fmt[strlen(fmt) - 1] !='\n')
int64 pg_strtoint64_safe(const char *s, Node *escontext)
Definition: numutils.c:652
uint64 uint64in_subr(const char *s, char **endloc, const char *typname, Node *escontext)
Definition: numutils.c:988
int64 pg_strtoint64(const char *s)
Definition: numutils.c:646
int pg_ltoa(int32 value, char *a)
Definition: numutils.c:1123
int32 pg_strtoint32_safe(const char *s, Node *escontext)
Definition: numutils.c:390
int pg_ulltoa_n(uint64 value, char *a)
Definition: numutils.c:1143
int16 pg_strtoint16(const char *s)
Definition: numutils.c:122
static const char DIGIT_TABLE[200]
Definition: numutils.c:29
static int decimalLength32(const uint32 v)
Definition: numutils.c:45
uint32 uint32in_subr(const char *s, char **endloc, const char *typname, Node *escontext)
Definition: numutils.c:901
int pg_lltoa(int64 value, char *a)
Definition: numutils.c:1230
static int decimalLength64(const uint64 v)
Definition: numutils.c:64
int pg_itoa(int16 i, char *a)
Definition: numutils.c:1045
int32 pg_strtoint32(const char *s)
Definition: numutils.c:384
char * pg_ultostr(char *str, uint32 value)
Definition: numutils.c:1310
char * pg_ultostr_zeropad(char *str, uint32 value, int32 minwidth)
Definition: numutils.c:1270
int16 pg_strtoint16_safe(const char *s, Node *escontext)
Definition: numutils.c:128
static const int8 hexlookup[128]
Definition: numutils.c:88
int pg_ultoa_n(uint32 value, char *a)
Definition: numutils.c:1058
static int pg_leftmost_one_pos32(uint32 word)
Definition: pg_bitutils.h:41
static int pg_leftmost_one_pos64(uint64 word)
Definition: pg_bitutils.h:72
const void size_t len
NameData typname
Definition: pg_type.h:41
char * c
Definition: nodes.h:129