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:490
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:71

References pg_leftmost_one_pos64().

Referenced by pg_ulltoa_n().

◆ pg_itoa()

int pg_itoa ( int16  i,
char *  a 
)

Definition at line 793 of file numutils.c.

794 {
795  return pg_ltoa((int32) i, a);
796 }
signed int int32
Definition: c.h:478
int a
Definition: isn.c:69
int i
Definition: isn.c:73
int pg_ltoa(int32 value, char *a)
Definition: numutils.c:871

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 978 of file numutils.c.

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 }
static struct @145 value
int pg_ulltoa_n(uint64 value, char *a)
Definition: numutils.c:891
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 871 of file numutils.c.

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

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 113 of file numutils.c.

114 {
115  return pg_strtoint16_safe(s, NULL);
116 }
int16 pg_strtoint16_safe(const char *s, Node *escontext)
Definition: numutils.c:119

References pg_strtoint16_safe().

◆ pg_strtoint16_safe()

int16 pg_strtoint16_safe ( const char *  s,
Node escontext 
)

Definition at line 119 of file numutils.c.

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 }
unsigned short uint16
Definition: c.h:489
#define likely(x)
Definition: c.h:294
signed short int16
Definition: c.h:477
#define PG_INT16_MIN
Definition: c.h:569
#define unlikely(x)
Definition: c.h:295
#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 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 291 of file numutils.c.

292 {
293  return pg_strtoint32_safe(s, NULL);
294 }
int32 pg_strtoint32_safe(const char *s, Node *escontext)
Definition: numutils.c:297

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 297 of file numutils.c.

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

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 469 of file numutils.c.

470 {
471  return pg_strtoint64_safe(s, NULL);
472 }
int64 pg_strtoint64_safe(const char *s, Node *escontext)
Definition: numutils.c:475

References pg_strtoint64_safe().

◆ pg_strtoint64_safe()

int64 pg_strtoint64_safe ( const char *  s,
Node escontext 
)

Definition at line 475 of file numutils.c.

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

References ereturn, errcode(), errmsg(), hexlookup, 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 891 of file numutils.c.

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 }
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 806 of file numutils.c.

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 }
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 1058 of file numutils.c.

1059 {
1060  int len = pg_ultoa_n(value, str);
1061 
1062  return str + len;
1063 }

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 1018 of file numutils.c.

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 }
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 649 of file numutils.c.

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 }
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 736 of file numutils.c.

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

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().