PostgreSQL Source Code  git master
numutils.c File Reference
#include "postgres.h"
#include <math.h>
#include <limits.h>
#include <ctype.h>
#include "common/int.h"
#include "utils/builtins.h"
#include "port/pg_bitutils.h"
Include dependency graph for numutils.c:

Go to the source code of this file.

Functions

static int decimalLength32 (const uint32 v)
 
static int decimalLength64 (const uint64 v)
 
int16 pg_strtoint16 (const char *s)
 
int16 pg_strtoint16_safe (const char *s, Node *escontext)
 
int32 pg_strtoint32 (const char *s)
 
int32 pg_strtoint32_safe (const char *s, Node *escontext)
 
int64 pg_strtoint64 (const char *s)
 
int64 pg_strtoint64_safe (const char *s, Node *escontext)
 
uint32 uint32in_subr (const char *s, char **endloc, const char *typname, Node *escontext)
 
uint64 uint64in_subr (const char *s, char **endloc, const char *typname, Node *escontext)
 
int pg_itoa (int16 i, char *a)
 
int pg_ultoa_n (uint32 value, char *a)
 
int pg_ltoa (int32 value, char *a)
 
int pg_ulltoa_n (uint64 value, char *a)
 
int pg_lltoa (int64 value, char *a)
 
char * pg_ultostr_zeropad (char *str, uint32 value, int32 minwidth)
 
char * pg_ultostr (char *str, uint32 value)
 

Variables

static const char DIGIT_TABLE [200]
 
static const int8 hexlookup [128]
 

Function Documentation

◆ decimalLength32()

static int decimalLength32 ( const uint32  v)
inlinestatic

Definition at line 45 of file numutils.c.

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 }
unsigned int uint32
Definition: c.h:495
static int pg_leftmost_one_pos32(uint32 word)
Definition: pg_bitutils.h:41

References pg_leftmost_one_pos32().

Referenced by pg_ultoa_n().

◆ decimalLength64()

static int decimalLength64 ( const uint64  v)
inlinestatic

Definition at line 64 of file numutils.c.

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 }
static int pg_leftmost_one_pos64(uint64 word)
Definition: pg_bitutils.h:72

References pg_leftmost_one_pos64().

Referenced by pg_ulltoa_n().

◆ pg_itoa()

int pg_itoa ( int16  i,
char *  a 
)

Definition at line 1045 of file numutils.c.

1046 {
1047  return pg_ltoa((int32) i, a);
1048 }
signed int int32
Definition: c.h:483
int a
Definition: isn.c:69
int i
Definition: isn.c:73
int pg_ltoa(int32 value, char *a)
Definition: numutils.c:1123

References a, i, and pg_ltoa().

Referenced by int2out(), int2vectorout(), LogicalTapeImport(), and LogicalTapeSetCreate().

◆ pg_lltoa()

int pg_lltoa ( int64  value,
char *  a 
)

Definition at line 1230 of file numutils.c.

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 }
static struct @148 value
int pg_ulltoa_n(uint64 value, char *a)
Definition: numutils.c:1143
const void size_t len

References a, len, pg_ulltoa_n(), and value.

Referenced by int8out(), and printsimple().

◆ pg_ltoa()

int pg_ltoa ( int32  value,
char *  a 
)

Definition at line 1123 of file numutils.c.

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 }
int pg_ultoa_n(uint32 value, char *a)
Definition: numutils.c:1058

References a, len, pg_ultoa_n(), and value.

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

◆ pg_strtoint16()

int16 pg_strtoint16 ( const char *  s)

Definition at line 122 of file numutils.c.

123 {
124  return pg_strtoint16_safe(s, NULL);
125 }
int16 pg_strtoint16_safe(const char *s, Node *escontext)
Definition: numutils.c:128

References pg_strtoint16_safe().

◆ pg_strtoint16_safe()

int16 pg_strtoint16_safe ( const char *  s,
Node escontext 
)

Definition at line 128 of file numutils.c.

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 }
unsigned short uint16
Definition: c.h:494
#define likely(x)
Definition: c.h:299
signed short int16
Definition: c.h:482
#define PG_INT16_MIN
Definition: c.h:574
#define unlikely(x)
Definition: c.h:300
#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 const int8 hexlookup[128]
Definition: numutils.c:88

References ereturn, errcode(), errmsg(), hexlookup, likely, PG_INT16_MAX, PG_INT16_MIN, and unlikely.

Referenced by int2in(), and pg_strtoint16().

◆ pg_strtoint32()

int32 pg_strtoint32 ( const char *  s)

Definition at line 384 of file numutils.c.

385 {
386  return pg_strtoint32_safe(s, NULL);
387 }
int32 pg_strtoint32_safe(const char *s, Node *escontext)
Definition: numutils.c:390

References pg_strtoint32_safe().

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

◆ pg_strtoint32_safe()

int32 pg_strtoint32_safe ( const char *  s,
Node escontext 
)

Definition at line 390 of file numutils.c.

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 }
#define PG_INT32_MAX
Definition: c.h:578
#define PG_INT32_MIN
Definition: c.h:577

References ereturn, errcode(), errmsg(), hexlookup, likely, PG_INT32_MAX, PG_INT32_MIN, and unlikely.

Referenced by int4in(), and pg_strtoint32().

◆ pg_strtoint64()

int64 pg_strtoint64 ( const char *  s)

Definition at line 646 of file numutils.c.

647 {
648  return pg_strtoint64_safe(s, NULL);
649 }
int64 pg_strtoint64_safe(const char *s, Node *escontext)
Definition: numutils.c:652

References pg_strtoint64_safe().

◆ pg_strtoint64_safe()

int64 pg_strtoint64_safe ( const char *  s,
Node escontext 
)

Definition at line 652 of file numutils.c.

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 }
#define PG_INT64_MAX
Definition: c.h:581
#define PG_INT64_MIN
Definition: c.h:580

References ereturn, errcode(), errmsg(), hexlookup, likely, PG_INT64_MAX, PG_INT64_MIN, and unlikely.

Referenced by int8in(), make_const(), and pg_strtoint64().

◆ pg_ulltoa_n()

int pg_ulltoa_n ( uint64  value,
char *  a 
)

Definition at line 1143 of file numutils.c.

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 }
static const char DIGIT_TABLE[200]
Definition: numutils.c:29
static int decimalLength64(const uint64 v)
Definition: numutils.c:64
char * c

References a, decimalLength64(), DIGIT_TABLE, i, and value.

Referenced by BuildQueryCompletionString(), and pg_lltoa().

◆ pg_ultoa_n()

int pg_ultoa_n ( uint32  value,
char *  a 
)

Definition at line 1058 of file numutils.c.

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 }
static int decimalLength32(const uint32 v)
Definition: numutils.c:45

References a, decimalLength32(), DIGIT_TABLE, i, and value.

Referenced by pg_ltoa(), pg_ultostr(), pg_ultostr_zeropad(), and printsimple().

◆ pg_ultostr()

char* pg_ultostr ( char *  str,
uint32  value 
)

Definition at line 1310 of file numutils.c.

1311 {
1312  int len = pg_ultoa_n(value, str);
1313 
1314  return str + len;
1315 }

References len, pg_ultoa_n(), generate_unaccent_rules::str, and value.

Referenced by AppendSeconds().

◆ pg_ultostr_zeropad()

char* pg_ultostr_zeropad ( char *  str,
uint32  value,
int32  minwidth 
)

Definition at line 1270 of file numutils.c.

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 }
Assert(fmt[strlen(fmt) - 1] !='\n')

References Assert(), DIGIT_TABLE, len, pg_ultoa_n(), generate_unaccent_rules::str, and value.

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

◆ uint32in_subr()

uint32 uint32in_subr ( const char *  s,
char **  endloc,
const char *  typname,
Node escontext 
)

Definition at line 901 of file numutils.c.

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 }
NameData typname
Definition: pg_type.h:41

References ereturn, errcode(), errmsg(), and typname.

Referenced by cidin(), oidin(), oidparse(), oidvectorin(), and xidin().

◆ uint64in_subr()

uint64 uint64in_subr ( const char *  s,
char **  endloc,
const char *  typname,
Node escontext 
)

Definition at line 988 of file numutils.c.

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 }
#define strtou64(str, endptr, base)
Definition: c.h:1308

References ereturn, errcode(), errmsg(), strtou64, and typname.

Referenced by xid8in().

Variable Documentation

◆ DIGIT_TABLE

const char DIGIT_TABLE[200]
static
Initial value:
=
"00" "01" "02" "03" "04" "05" "06" "07" "08" "09"
"10" "11" "12" "13" "14" "15" "16" "17" "18" "19"
"20" "21" "22" "23" "24" "25" "26" "27" "28" "29"
"30" "31" "32" "33" "34" "35" "36" "37" "38" "39"
"40" "41" "42" "43" "44" "45" "46" "47" "48" "49"
"50" "51" "52" "53" "54" "55" "56" "57" "58" "59"
"60" "61" "62" "63" "64" "65" "66" "67" "68" "69"
"70" "71" "72" "73" "74" "75" "76" "77" "78" "79"
"80" "81" "82" "83" "84" "85" "86" "87" "88" "89"
"90" "91" "92" "93" "94" "95" "96" "97" "98" "99"

Definition at line 29 of file numutils.c.

Referenced by pg_ulltoa_n(), pg_ultoa_n(), pg_ultostr_zeropad(), to_chars(), to_chars_df(), and to_chars_f().

◆ hexlookup

const int8 hexlookup[128]
static
Initial value:
= {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
}

Definition at line 88 of file numutils.c.

Referenced by pg_strtoint16_safe(), pg_strtoint32_safe(), and pg_strtoint64_safe().