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.
101  *
102  * Allows any number of leading or trailing whitespace characters.
103  *
104  * pg_strtoint16() will throw ereport() upon bad input format or overflow;
105  * while pg_strtoint16_safe() instead returns such complaints in *escontext,
106  * if it's an ErrorSaveContext.
107 *
108  * NB: Accumulate input as an unsigned number, to deal with two's complement
109  * representation of the most negative number, which can't be represented as a
110  * signed positive number.
111  */
112 int16
113 pg_strtoint16(const char *s)
114 {
115  return pg_strtoint16_safe(s, NULL);
116 }
117 
118 int16
119 pg_strtoint16_safe(const char *s, Node *escontext)
120 {
121  const char *ptr = s;
122  const char *firstdigit;
123  uint16 tmp = 0;
124  bool neg = false;
125 
126  /* skip leading spaces */
127  while (likely(*ptr) && isspace((unsigned char) *ptr))
128  ptr++;
129 
130  /* handle sign */
131  if (*ptr == '-')
132  {
133  ptr++;
134  neg = true;
135  }
136  else if (*ptr == '+')
137  ptr++;
138 
139  /* process digits */
140  if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
141  {
142  firstdigit = ptr += 2;
143 
144  while (*ptr)
145  {
146  if (isxdigit((unsigned char) *ptr))
147  {
148  if (unlikely(tmp > -(PG_INT16_MIN / 16)))
149  goto out_of_range;
150 
151  tmp = tmp * 16 + hexlookup[(unsigned char) *ptr++];
152  }
153  else if (*ptr == '_')
154  {
155  /* underscore must be followed by more digits */
156  ptr++;
157  if (*ptr == '\0' || !isxdigit((unsigned char) *ptr))
158  goto invalid_syntax;
159  }
160  else
161  break;
162  }
163  }
164  else if (ptr[0] == '0' && (ptr[1] == 'o' || ptr[1] == 'O'))
165  {
166  firstdigit = ptr += 2;
167 
168  while (*ptr)
169  {
170  if (*ptr >= '0' && *ptr <= '7')
171  {
172  if (unlikely(tmp > -(PG_INT16_MIN / 8)))
173  goto out_of_range;
174 
175  tmp = tmp * 8 + (*ptr++ - '0');
176  }
177  else if (*ptr == '_')
178  {
179  /* underscore must be followed by more digits */
180  ptr++;
181  if (*ptr == '\0' || *ptr < '0' || *ptr > '7')
182  goto invalid_syntax;
183  }
184  else
185  break;
186  }
187  }
188  else if (ptr[0] == '0' && (ptr[1] == 'b' || ptr[1] == 'B'))
189  {
190  firstdigit = ptr += 2;
191 
192  while (*ptr)
193  {
194  if (*ptr >= '0' && *ptr <= '1')
195  {
196  if (unlikely(tmp > -(PG_INT16_MIN / 2)))
197  goto out_of_range;
198 
199  tmp = tmp * 2 + (*ptr++ - '0');
200  }
201  else if (*ptr == '_')
202  {
203  /* underscore must be followed by more digits */
204  ptr++;
205  if (*ptr == '\0' || *ptr < '0' || *ptr > '1')
206  goto invalid_syntax;
207  }
208  else
209  break;
210  }
211  }
212  else
213  {
214  firstdigit = ptr;
215 
216  while (*ptr)
217  {
218  if (isdigit((unsigned char) *ptr))
219  {
220  if (unlikely(tmp > -(PG_INT16_MIN / 10)))
221  goto out_of_range;
222 
223  tmp = tmp * 10 + (*ptr++ - '0');
224  }
225  else if (*ptr == '_')
226  {
227  /* underscore may not be first */
228  if (unlikely(ptr == firstdigit))
229  goto invalid_syntax;
230  /* and it must be followed by more digits */
231  ptr++;
232  if (*ptr == '\0' || !isdigit((unsigned char) *ptr))
233  goto invalid_syntax;
234  }
235  else
236  break;
237  }
238  }
239 
240  /* require at least one digit */
241  if (unlikely(ptr == firstdigit))
242  goto invalid_syntax;
243 
244  /* allow trailing whitespace, but not other trailing chars */
245  while (*ptr != '\0' && isspace((unsigned char) *ptr))
246  ptr++;
247 
248  if (unlikely(*ptr != '\0'))
249  goto invalid_syntax;
250 
251  if (neg)
252  {
253  /* check the negative equivalent will fit without overflowing */
254  if (tmp > (uint16) (-(PG_INT16_MIN + 1)) + 1)
255  goto out_of_range;
256  return -((int16) tmp);
257  }
258 
259  if (tmp > PG_INT16_MAX)
260  goto out_of_range;
261 
262  return (int16) tmp;
263 
264 out_of_range:
265  ereturn(escontext, 0,
266  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
267  errmsg("value \"%s\" is out of range for type %s",
268  s, "smallint")));
269 
270 invalid_syntax:
271  ereturn(escontext, 0,
272  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
273  errmsg("invalid input syntax for type %s: \"%s\"",
274  "smallint", s)));
275 }
276 
277 /*
278  * Convert input string to a signed 32 bit integer.
279  *
280  * Allows any number of leading or trailing whitespace characters.
281  *
282  * pg_strtoint32() will throw ereport() upon bad input format or overflow;
283  * while pg_strtoint32_safe() instead returns such complaints in *escontext,
284  * if it's an ErrorSaveContext.
285  *
286  * NB: Accumulate input as an unsigned number, to deal with two's complement
287  * representation of the most negative number, which can't be represented as a
288  * signed positive number.
289  */
290 int32
291 pg_strtoint32(const char *s)
292 {
293  return pg_strtoint32_safe(s, NULL);
294 }
295 
296 int32
297 pg_strtoint32_safe(const char *s, Node *escontext)
298 {
299  const char *ptr = s;
300  const char *firstdigit;
301  uint32 tmp = 0;
302  bool neg = false;
303 
304  /* skip leading spaces */
305  while (likely(*ptr) && isspace((unsigned char) *ptr))
306  ptr++;
307 
308  /* handle sign */
309  if (*ptr == '-')
310  {
311  ptr++;
312  neg = true;
313  }
314  else if (*ptr == '+')
315  ptr++;
316 
317  /* process digits */
318  if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
319  {
320  firstdigit = ptr += 2;
321 
322  while (*ptr)
323  {
324  if (isxdigit((unsigned char) *ptr))
325  {
326  if (unlikely(tmp > -(PG_INT32_MIN / 16)))
327  goto out_of_range;
328 
329  tmp = tmp * 16 + hexlookup[(unsigned char) *ptr++];
330  }
331  else if (*ptr == '_')
332  {
333  /* underscore must be followed by more digits */
334  ptr++;
335  if (*ptr == '\0' || !isxdigit((unsigned char) *ptr))
336  goto invalid_syntax;
337  }
338  else
339  break;
340  }
341  }
342  else if (ptr[0] == '0' && (ptr[1] == 'o' || ptr[1] == 'O'))
343  {
344  firstdigit = ptr += 2;
345 
346  while (*ptr)
347  {
348  if (*ptr >= '0' && *ptr <= '7')
349  {
350  if (unlikely(tmp > -(PG_INT32_MIN / 8)))
351  goto out_of_range;
352 
353  tmp = tmp * 8 + (*ptr++ - '0');
354  }
355  else if (*ptr == '_')
356  {
357  /* underscore must be followed by more digits */
358  ptr++;
359  if (*ptr == '\0' || *ptr < '0' || *ptr > '7')
360  goto invalid_syntax;
361  }
362  else
363  break;
364  }
365  }
366  else if (ptr[0] == '0' && (ptr[1] == 'b' || ptr[1] == 'B'))
367  {
368  firstdigit = ptr += 2;
369 
370  while (*ptr)
371  {
372  if (*ptr >= '0' && *ptr <= '1')
373  {
374  if (unlikely(tmp > -(PG_INT32_MIN / 2)))
375  goto out_of_range;
376 
377  tmp = tmp * 2 + (*ptr++ - '0');
378  }
379  else if (*ptr == '_')
380  {
381  /* underscore must be followed by more digits */
382  ptr++;
383  if (*ptr == '\0' || *ptr < '0' || *ptr > '1')
384  goto invalid_syntax;
385  }
386  else
387  break;
388  }
389  }
390  else
391  {
392  firstdigit = ptr;
393 
394  while (*ptr)
395  {
396  if (isdigit((unsigned char) *ptr))
397  {
398  if (unlikely(tmp > -(PG_INT32_MIN / 10)))
399  goto out_of_range;
400 
401  tmp = tmp * 10 + (*ptr++ - '0');
402  }
403  else if (*ptr == '_')
404  {
405  /* underscore may not be first */
406  if (unlikely(ptr == firstdigit))
407  goto invalid_syntax;
408  /* and it must be followed by more digits */
409  ptr++;
410  if (*ptr == '\0' || !isdigit((unsigned char) *ptr))
411  goto invalid_syntax;
412  }
413  else
414  break;
415  }
416  }
417 
418  /* require at least one digit */
419  if (unlikely(ptr == firstdigit))
420  goto invalid_syntax;
421 
422  /* allow trailing whitespace, but not other trailing chars */
423  while (*ptr != '\0' && isspace((unsigned char) *ptr))
424  ptr++;
425 
426  if (unlikely(*ptr != '\0'))
427  goto invalid_syntax;
428 
429  if (neg)
430  {
431  /* check the negative equivalent will fit without overflowing */
432  if (tmp > (uint32) (-(PG_INT32_MIN + 1)) + 1)
433  goto out_of_range;
434  return -((int32) tmp);
435  }
436 
437  if (tmp > PG_INT32_MAX)
438  goto out_of_range;
439 
440  return (int32) tmp;
441 
442 out_of_range:
443  ereturn(escontext, 0,
444  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
445  errmsg("value \"%s\" is out of range for type %s",
446  s, "integer")));
447 
448 invalid_syntax:
449  ereturn(escontext, 0,
450  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
451  errmsg("invalid input syntax for type %s: \"%s\"",
452  "integer", s)));
453 }
454 
455 /*
456  * Convert input string to a signed 64 bit integer.
457  *
458  * Allows any number of leading or trailing whitespace characters.
459  *
460  * pg_strtoint64() will throw ereport() upon bad input format or overflow;
461  * while pg_strtoint64_safe() instead returns such complaints in *escontext,
462  * if it's an ErrorSaveContext.
463  *
464  * NB: Accumulate input as an unsigned number, to deal with two's complement
465  * representation of the most negative number, which can't be represented as a
466  * signed positive number.
467  */
468 int64
469 pg_strtoint64(const char *s)
470 {
471  return pg_strtoint64_safe(s, NULL);
472 }
473 
474 int64
475 pg_strtoint64_safe(const char *s, Node *escontext)
476 {
477  const char *ptr = s;
478  const char *firstdigit;
479  uint64 tmp = 0;
480  bool neg = false;
481 
482  /* skip leading spaces */
483  while (*ptr && isspace((unsigned char) *ptr))
484  ptr++;
485 
486  /* handle sign */
487  if (*ptr == '-')
488  {
489  ptr++;
490  neg = true;
491  }
492  else if (*ptr == '+')
493  ptr++;
494 
495  /* process digits */
496  if (ptr[0] == '0' && (ptr[1] == 'x' || ptr[1] == 'X'))
497  {
498  firstdigit = ptr += 2;
499 
500  while (*ptr)
501  {
502  if (isxdigit((unsigned char) *ptr))
503  {
504  if (unlikely(tmp > -(PG_INT64_MIN / 16)))
505  goto out_of_range;
506 
507  tmp = tmp * 16 + hexlookup[(unsigned char) *ptr++];
508  }
509  else if (*ptr == '_')
510  {
511  /* underscore must be followed by more digits */
512  ptr++;
513  if (*ptr == '\0' || !isxdigit((unsigned char) *ptr))
514  goto invalid_syntax;
515  }
516  else
517  break;
518  }
519  }
520  else if (ptr[0] == '0' && (ptr[1] == 'o' || ptr[1] == 'O'))
521  {
522  firstdigit = ptr += 2;
523 
524  while (*ptr)
525  {
526  if (*ptr >= '0' && *ptr <= '7')
527  {
528  if (unlikely(tmp > -(PG_INT64_MIN / 8)))
529  goto out_of_range;
530 
531  tmp = tmp * 8 + (*ptr++ - '0');
532  }
533  else if (*ptr == '_')
534  {
535  /* underscore must be followed by more digits */
536  ptr++;
537  if (*ptr == '\0' || *ptr < '0' || *ptr > '7')
538  goto invalid_syntax;
539  }
540  else
541  break;
542  }
543  }
544  else if (ptr[0] == '0' && (ptr[1] == 'b' || ptr[1] == 'B'))
545  {
546  firstdigit = ptr += 2;
547 
548  while (*ptr)
549  {
550  if (*ptr >= '0' && *ptr <= '1')
551  {
552  if (unlikely(tmp > -(PG_INT64_MIN / 2)))
553  goto out_of_range;
554 
555  tmp = tmp * 2 + (*ptr++ - '0');
556  }
557  else if (*ptr == '_')
558  {
559  /* underscore must be followed by more digits */
560  ptr++;
561  if (*ptr == '\0' || *ptr < '0' || *ptr > '1')
562  goto invalid_syntax;
563  }
564  else
565  break;
566  }
567  }
568  else
569  {
570  firstdigit = ptr;
571 
572  while (*ptr)
573  {
574  if (isdigit((unsigned char) *ptr))
575  {
576  if (unlikely(tmp > -(PG_INT64_MIN / 10)))
577  goto out_of_range;
578 
579  tmp = tmp * 10 + (*ptr++ - '0');
580  }
581  else if (*ptr == '_')
582  {
583  /* underscore may not be first */
584  if (unlikely(ptr == firstdigit))
585  goto invalid_syntax;
586  /* and it must be followed by more digits */
587  ptr++;
588  if (*ptr == '\0' || !isdigit((unsigned char) *ptr))
589  goto invalid_syntax;
590  }
591  else
592  break;
593  }
594  }
595 
596  /* require at least one digit */
597  if (unlikely(ptr == firstdigit))
598  goto invalid_syntax;
599 
600  /* allow trailing whitespace, but not other trailing chars */
601  while (*ptr != '\0' && isspace((unsigned char) *ptr))
602  ptr++;
603 
604  if (unlikely(*ptr != '\0'))
605  goto invalid_syntax;
606 
607  if (neg)
608  {
609  /* check the negative equivalent will fit without overflowing */
610  if (tmp > (uint64) (-(PG_INT64_MIN + 1)) + 1)
611  goto out_of_range;
612  return -((int64) tmp);
613  }
614 
615  if (tmp > PG_INT64_MAX)
616  goto out_of_range;
617 
618  return (int64) tmp;
619 
620 out_of_range:
621  ereturn(escontext, 0,
622  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
623  errmsg("value \"%s\" is out of range for type %s",
624  s, "bigint")));
625 
626 invalid_syntax:
627  ereturn(escontext, 0,
628  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
629  errmsg("invalid input syntax for type %s: \"%s\"",
630  "bigint", s)));
631 }
632 
633 /*
634  * Convert input string to an unsigned 32 bit integer.
635  *
636  * Allows any number of leading or trailing whitespace characters.
637  *
638  * If endloc isn't NULL, store a pointer to the rest of the string there,
639  * so that caller can parse the rest. Otherwise, it's an error if anything
640  * but whitespace follows.
641  *
642  * typname is what is reported in error messges.
643  *
644  * If escontext points to an ErrorSaveContext node, that is filled instead
645  * of throwing an error; the caller must check SOFT_ERROR_OCCURRED()
646  * to detect errors.
647  */
648 uint32
649 uint32in_subr(const char *s, char **endloc,
650  const char *typname, Node *escontext)
651 {
652  uint32 result;
653  unsigned long cvt;
654  char *endptr;
655 
656  errno = 0;
657  cvt = strtoul(s, &endptr, 0);
658 
659  /*
660  * strtoul() normally only sets ERANGE. On some systems it may also set
661  * EINVAL, which simply means it couldn't parse the input string. Be sure
662  * to report that the same way as the standard error indication (that
663  * endptr == s).
664  */
665  if ((errno && errno != ERANGE) || endptr == s)
666  ereturn(escontext, 0,
667  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
668  errmsg("invalid input syntax for type %s: \"%s\"",
669  typname, s)));
670 
671  if (errno == ERANGE)
672  ereturn(escontext, 0,
673  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
674  errmsg("value \"%s\" is out of range for type %s",
675  s, typname)));
676 
677  if (endloc)
678  {
679  /* caller wants to deal with rest of string */
680  *endloc = endptr;
681  }
682  else
683  {
684  /* allow only whitespace after number */
685  while (*endptr && isspace((unsigned char) *endptr))
686  endptr++;
687  if (*endptr)
688  ereturn(escontext, 0,
689  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
690  errmsg("invalid input syntax for type %s: \"%s\"",
691  typname, s)));
692  }
693 
694  result = (uint32) cvt;
695 
696  /*
697  * Cope with possibility that unsigned long is wider than uint32, in which
698  * case strtoul will not raise an error for some values that are out of
699  * the range of uint32.
700  *
701  * For backwards compatibility, we want to accept inputs that are given
702  * with a minus sign, so allow the input value if it matches after either
703  * signed or unsigned extension to long.
704  *
705  * To ensure consistent results on 32-bit and 64-bit platforms, make sure
706  * the error message is the same as if strtoul() had returned ERANGE.
707  */
708 #if PG_UINT32_MAX != ULONG_MAX
709  if (cvt != (unsigned long) result &&
710  cvt != (unsigned long) ((int) result))
711  ereturn(escontext, 0,
712  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
713  errmsg("value \"%s\" is out of range for type %s",
714  s, typname)));
715 #endif
716 
717  return result;
718 }
719 
720 /*
721  * Convert input string to an unsigned 64 bit integer.
722  *
723  * Allows any number of leading or trailing whitespace characters.
724  *
725  * If endloc isn't NULL, store a pointer to the rest of the string there,
726  * so that caller can parse the rest. Otherwise, it's an error if anything
727  * but whitespace follows.
728  *
729  * typname is what is reported in error messges.
730  *
731  * If escontext points to an ErrorSaveContext node, that is filled instead
732  * of throwing an error; the caller must check SOFT_ERROR_OCCURRED()
733  * to detect errors.
734  */
735 uint64
736 uint64in_subr(const char *s, char **endloc,
737  const char *typname, Node *escontext)
738 {
739  uint64 result;
740  char *endptr;
741 
742  errno = 0;
743  result = strtou64(s, &endptr, 0);
744 
745  /*
746  * strtoul[l] normally only sets ERANGE. On some systems it may also set
747  * EINVAL, which simply means it couldn't parse the input string. Be sure
748  * to report that the same way as the standard error indication (that
749  * endptr == s).
750  */
751  if ((errno && errno != ERANGE) || endptr == s)
752  ereturn(escontext, 0,
753  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
754  errmsg("invalid input syntax for type %s: \"%s\"",
755  typname, s)));
756 
757  if (errno == ERANGE)
758  ereturn(escontext, 0,
759  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
760  errmsg("value \"%s\" is out of range for type %s",
761  s, typname)));
762 
763  if (endloc)
764  {
765  /* caller wants to deal with rest of string */
766  *endloc = endptr;
767  }
768  else
769  {
770  /* allow only whitespace after number */
771  while (*endptr && isspace((unsigned char) *endptr))
772  endptr++;
773  if (*endptr)
774  ereturn(escontext, 0,
775  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
776  errmsg("invalid input syntax for type %s: \"%s\"",
777  typname, s)));
778  }
779 
780  return result;
781 }
782 
783 /*
784  * pg_itoa: converts a signed 16-bit integer to its string representation
785  * and returns strlen(a).
786  *
787  * Caller must ensure that 'a' points to enough memory to hold the result
788  * (at least 7 bytes, counting a leading sign and trailing NUL).
789  *
790  * It doesn't seem worth implementing this separately.
791  */
792 int
793 pg_itoa(int16 i, char *a)
794 {
795  return pg_ltoa((int32) i, a);
796 }
797 
798 /*
799  * pg_ultoa_n: converts an unsigned 32-bit integer to its string representation,
800  * not NUL-terminated, and returns the length of that string representation
801  *
802  * Caller must ensure that 'a' points to enough memory to hold the result (at
803  * least 10 bytes)
804  */
805 int
807 {
808  int olength,
809  i = 0;
810 
811  /* Degenerate case */
812  if (value == 0)
813  {
814  *a = '0';
815  return 1;
816  }
817 
818  olength = decimalLength32(value);
819 
820  /* Compute the result string. */
821  while (value >= 10000)
822  {
823  const uint32 c = value - 10000 * (value / 10000);
824  const uint32 c0 = (c % 100) << 1;
825  const uint32 c1 = (c / 100) << 1;
826 
827  char *pos = a + olength - i;
828 
829  value /= 10000;
830 
831  memcpy(pos - 2, DIGIT_TABLE + c0, 2);
832  memcpy(pos - 4, DIGIT_TABLE + c1, 2);
833  i += 4;
834  }
835  if (value >= 100)
836  {
837  const uint32 c = (value % 100) << 1;
838 
839  char *pos = a + olength - i;
840 
841  value /= 100;
842 
843  memcpy(pos - 2, DIGIT_TABLE + c, 2);
844  i += 2;
845  }
846  if (value >= 10)
847  {
848  const uint32 c = value << 1;
849 
850  char *pos = a + olength - i;
851 
852  memcpy(pos - 2, DIGIT_TABLE + c, 2);
853  }
854  else
855  {
856  *a = (char) ('0' + value);
857  }
858 
859  return olength;
860 }
861 
862 /*
863  * pg_ltoa: converts a signed 32-bit integer to its string representation and
864  * returns strlen(a).
865  *
866  * It is the caller's responsibility to ensure that a is at least 12 bytes long,
867  * which is enough room to hold a minus sign, a maximally long int32, and the
868  * above terminating NUL.
869  */
870 int
872 {
873  uint32 uvalue = (uint32) value;
874  int len = 0;
875 
876  if (value < 0)
877  {
878  uvalue = (uint32) 0 - uvalue;
879  a[len++] = '-';
880  }
881  len += pg_ultoa_n(uvalue, a + len);
882  a[len] = '\0';
883  return len;
884 }
885 
886 /*
887  * Get the decimal representation, not NUL-terminated, and return the length of
888  * same. Caller must ensure that a points to at least MAXINT8LEN bytes.
889  */
890 int
891 pg_ulltoa_n(uint64 value, char *a)
892 {
893  int olength,
894  i = 0;
895  uint32 value2;
896 
897  /* Degenerate case */
898  if (value == 0)
899  {
900  *a = '0';
901  return 1;
902  }
903 
904  olength = decimalLength64(value);
905 
906  /* Compute the result string. */
907  while (value >= 100000000)
908  {
909  const uint64 q = value / 100000000;
910  uint32 value3 = (uint32) (value - 100000000 * q);
911 
912  const uint32 c = value3 % 10000;
913  const uint32 d = value3 / 10000;
914  const uint32 c0 = (c % 100) << 1;
915  const uint32 c1 = (c / 100) << 1;
916  const uint32 d0 = (d % 100) << 1;
917  const uint32 d1 = (d / 100) << 1;
918 
919  char *pos = a + olength - i;
920 
921  value = q;
922 
923  memcpy(pos - 2, DIGIT_TABLE + c0, 2);
924  memcpy(pos - 4, DIGIT_TABLE + c1, 2);
925  memcpy(pos - 6, DIGIT_TABLE + d0, 2);
926  memcpy(pos - 8, DIGIT_TABLE + d1, 2);
927  i += 8;
928  }
929 
930  /* Switch to 32-bit for speed */
931  value2 = (uint32) value;
932 
933  if (value2 >= 10000)
934  {
935  const uint32 c = value2 - 10000 * (value2 / 10000);
936  const uint32 c0 = (c % 100) << 1;
937  const uint32 c1 = (c / 100) << 1;
938 
939  char *pos = a + olength - i;
940 
941  value2 /= 10000;
942 
943  memcpy(pos - 2, DIGIT_TABLE + c0, 2);
944  memcpy(pos - 4, DIGIT_TABLE + c1, 2);
945  i += 4;
946  }
947  if (value2 >= 100)
948  {
949  const uint32 c = (value2 % 100) << 1;
950  char *pos = a + olength - i;
951 
952  value2 /= 100;
953 
954  memcpy(pos - 2, DIGIT_TABLE + c, 2);
955  i += 2;
956  }
957  if (value2 >= 10)
958  {
959  const uint32 c = value2 << 1;
960  char *pos = a + olength - i;
961 
962  memcpy(pos - 2, DIGIT_TABLE + c, 2);
963  }
964  else
965  *a = (char) ('0' + value2);
966 
967  return olength;
968 }
969 
970 /*
971  * pg_lltoa: converts a signed 64-bit integer to its string representation and
972  * returns strlen(a).
973  *
974  * Caller must ensure that 'a' points to enough memory to hold the result
975  * (at least MAXINT8LEN + 1 bytes, counting a leading sign and trailing NUL).
976  */
977 int
978 pg_lltoa(int64 value, char *a)
979 {
980  uint64 uvalue = value;
981  int len = 0;
982 
983  if (value < 0)
984  {
985  uvalue = (uint64) 0 - uvalue;
986  a[len++] = '-';
987  }
988 
989  len += pg_ulltoa_n(uvalue, a + len);
990  a[len] = '\0';
991  return len;
992 }
993 
994 
995 /*
996  * pg_ultostr_zeropad
997  * Converts 'value' into a decimal string representation stored at 'str'.
998  * 'minwidth' specifies the minimum width of the result; any extra space
999  * is filled up by prefixing the number with zeros.
1000  *
1001  * Returns the ending address of the string result (the last character written
1002  * plus 1). Note that no NUL terminator is written.
1003  *
1004  * The intended use-case for this function is to build strings that contain
1005  * multiple individual numbers, for example:
1006  *
1007  * str = pg_ultostr_zeropad(str, hours, 2);
1008  * *str++ = ':';
1009  * str = pg_ultostr_zeropad(str, mins, 2);
1010  * *str++ = ':';
1011  * str = pg_ultostr_zeropad(str, secs, 2);
1012  * *str = '\0';
1013  *
1014  * Note: Caller must ensure that 'str' points to enough memory to hold the
1015  * result.
1016  */
1017 char *
1019 {
1020  int len;
1021 
1022  Assert(minwidth > 0);
1023 
1024  if (value < 100 && minwidth == 2) /* Short cut for common case */
1025  {
1026  memcpy(str, DIGIT_TABLE + value * 2, 2);
1027  return str + 2;
1028  }
1029 
1030  len = pg_ultoa_n(value, str);
1031  if (len >= minwidth)
1032  return str + len;
1033 
1034  memmove(str + minwidth - len, str, len);
1035  memset(str, '0', minwidth - len);
1036  return str + minwidth;
1037 }
1038 
1039 /*
1040  * pg_ultostr
1041  * Converts 'value' into a decimal string representation stored at 'str'.
1042  *
1043  * Returns the ending address of the string result (the last character written
1044  * plus 1). Note that no NUL terminator is written.
1045  *
1046  * The intended use-case for this function is to build strings that contain
1047  * multiple individual numbers, for example:
1048  *
1049  * str = pg_ultostr(str, a);
1050  * *str++ = ' ';
1051  * str = pg_ultostr(str, b);
1052  * *str = '\0';
1053  *
1054  * Note: Caller must ensure that 'str' points to enough memory to hold the
1055  * result.
1056  */
1057 char *
1059 {
1060  int len = pg_ultoa_n(value, str);
1061 
1062  return str + len;
1063 }
unsigned short uint16
Definition: c.h:489
unsigned int uint32
Definition: c.h:490
signed char int8
Definition: c.h:476
#define PG_INT32_MAX
Definition: c.h:573
#define likely(x)
Definition: c.h:294
signed short int16
Definition: c.h:477
signed int int32
Definition: c.h:478
#define strtou64(str, endptr, base)
Definition: c.h:1285
#define PG_INT16_MIN
Definition: c.h:569
#define PG_INT64_MAX
Definition: c.h:576
#define PG_INT64_MIN
Definition: c.h:575
#define unlikely(x)
Definition: c.h:295
#define PG_INT32_MIN
Definition: c.h:572
#define PG_INT16_MAX
Definition: c.h:570
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 @143 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:475
uint64 uint64in_subr(const char *s, char **endloc, const char *typname, Node *escontext)
Definition: numutils.c:736
int64 pg_strtoint64(const char *s)
Definition: numutils.c:469
int pg_ltoa(int32 value, char *a)
Definition: numutils.c:871
int32 pg_strtoint32_safe(const char *s, Node *escontext)
Definition: numutils.c:297
int pg_ulltoa_n(uint64 value, char *a)
Definition: numutils.c:891
int16 pg_strtoint16(const char *s)
Definition: numutils.c:113
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:649
int pg_lltoa(int64 value, char *a)
Definition: numutils.c:978
static int decimalLength64(const uint64 v)
Definition: numutils.c:64
int pg_itoa(int16 i, char *a)
Definition: numutils.c:793
int32 pg_strtoint32(const char *s)
Definition: numutils.c:291
char * pg_ultostr(char *str, uint32 value)
Definition: numutils.c:1058
char * pg_ultostr_zeropad(char *str, uint32 value, int32 minwidth)
Definition: numutils.c:1018
int16 pg_strtoint16_safe(const char *s, Node *escontext)
Definition: numutils.c:119
static const int8 hexlookup[128]
Definition: numutils.c:88
int pg_ultoa_n(uint32 value, char *a)
Definition: numutils.c:806
static int pg_leftmost_one_pos32(uint32 word)
Definition: pg_bitutils.h:26
static int pg_leftmost_one_pos64(uint64 word)
Definition: pg_bitutils.h:49
const void size_t len
NameData typname
Definition: pg_type.h:41
char * c
Definition: nodes.h:129