PostgreSQL Source Code  git master
int8.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

bool scanint8 (const char *str, bool errorOK, int64 *result)
 

Function Documentation

◆ scanint8()

bool scanint8 ( const char *  str,
bool  errorOK,
int64 *  result 
)

Definition at line 55 of file int8.c.

References ereport, errcode(), errmsg(), ERROR, PG_INT64_MIN, pg_mul_s64_overflow(), pg_sub_s64_overflow(), generate_unaccent_rules::str, and unlikely.

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

56 {
57  const char *ptr = str;
58  int64 tmp = 0;
59  bool neg = false;
60 
61  /*
62  * Do our own scan, rather than relying on sscanf which might be broken
63  * for long long.
64  *
65  * As INT64_MIN can't be stored as a positive 64 bit integer, accumulate
66  * value as a negative number.
67  */
68 
69  /* skip leading spaces */
70  while (*ptr && isspace((unsigned char) *ptr))
71  ptr++;
72 
73  /* handle sign */
74  if (*ptr == '-')
75  {
76  ptr++;
77  neg = true;
78  }
79  else if (*ptr == '+')
80  ptr++;
81 
82  /* require at least one digit */
83  if (unlikely(!isdigit((unsigned char) *ptr)))
84  goto invalid_syntax;
85 
86  /* process digits */
87  while (*ptr && isdigit((unsigned char) *ptr))
88  {
89  int8 digit = (*ptr++ - '0');
90 
91  if (unlikely(pg_mul_s64_overflow(tmp, 10, &tmp)) ||
92  unlikely(pg_sub_s64_overflow(tmp, digit, &tmp)))
93  goto out_of_range;
94  }
95 
96  /* allow trailing whitespace, but not other trailing chars */
97  while (*ptr != '\0' && isspace((unsigned char) *ptr))
98  ptr++;
99 
100  if (unlikely(*ptr != '\0'))
101  goto invalid_syntax;
102 
103  if (!neg)
104  {
105  if (unlikely(tmp == PG_INT64_MIN))
106  goto out_of_range;
107  tmp = -tmp;
108  }
109 
110  *result = tmp;
111  return true;
112 
113 out_of_range:
114  if (!errorOK)
115  ereport(ERROR,
116  (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
117  errmsg("value \"%s\" is out of range for type %s",
118  str, "bigint")));
119  return false;
120 
121 invalid_syntax:
122  if (!errorOK)
123  ereport(ERROR,
124  (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
125  errmsg("invalid input syntax for integer: \"%s\"",
126  str)));
127  return false;
128 }
static bool pg_sub_s64_overflow(int64 a, int64 b, int64 *result)
Definition: int.h:175
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define PG_INT64_MIN
Definition: c.h:391
#define ereport(elevel, rest)
Definition: elog.h:122
signed char int8
Definition: c.h:292
static bool pg_mul_s64_overflow(int64 a, int64 b, int64 *result)
Definition: int.h:201
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define unlikely(x)
Definition: c.h:200