Remove pg_atoi()
authorPeter Eisentraut <[email protected]>
Mon, 14 Feb 2022 20:29:45 +0000 (21:29 +0100)
committerPeter Eisentraut <[email protected]>
Tue, 15 Feb 2022 06:44:26 +0000 (07:44 +0100)
The last caller was int2vectorin(), and having such a general function
for one user didn't seem useful, so just put the required parts inline
and remove the function.

Reviewed-by: John Naylor <[email protected]>
Discussion: https://www.postgresql.org/message-id/flat/b239564c-cad0-b23e-c57e-166d883cb97d@enterprisedb.com

src/backend/utils/adt/int.c
src/backend/utils/adt/numutils.c
src/include/utils/builtins.h

index 8bd234c11c6ec9c58cb5984997311ffd94dfcdb6..42ddae99ef45a30fe36dba97ca478cfdb4bb5817 100644 (file)
@@ -146,15 +146,39 @@ int2vectorin(PG_FUNCTION_ARGS)
 
    result = (int2vector *) palloc0(Int2VectorSize(FUNC_MAX_ARGS));
 
-   for (n = 0; *intString && n < FUNC_MAX_ARGS; n++)
+   for (n = 0; n < FUNC_MAX_ARGS; n++)
    {
+       long        l;
+       char       *endp;
+
        while (*intString && isspace((unsigned char) *intString))
            intString++;
        if (*intString == '\0')
            break;
-       result->values[n] = pg_atoi(intString, sizeof(int16), ' ');
-       while (*intString && !isspace((unsigned char) *intString))
-           intString++;
+
+       errno = 0;
+       l = strtol(intString, &endp, 10);
+
+       if (intString == endp)
+           ereport(ERROR,
+                   (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
+                    errmsg("invalid input syntax for type %s: \"%s\"",
+                           "smallint", intString)));
+
+       if (errno == ERANGE || l < SHRT_MIN || l > SHRT_MAX)
+           ereport(ERROR,
+                   (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
+                    errmsg("value \"%s\" is out of range for type %s", intString,
+                           "smallint")));
+
+       if (*endp && *endp != ' ')
+           ereport(ERROR,
+                   (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
+                    errmsg("invalid input syntax for type %s: \"%s\"",
+                           "integer", intString)));
+
+       result->values[n] = l;
+       intString = endp;
    }
    while (*intString && isspace((unsigned char) *intString))
        intString++;
index e82d23a325e28f5936b56aa06215be8886048d3a..cc3f95d3990e275d988b188e73f1fcef9f5069aa 100644 (file)
@@ -85,94 +85,6 @@ decimalLength64(const uint64 v)
    return t + (v >= PowersOfTen[t]);
 }
 
-/*
- * pg_atoi: convert string to integer
- *
- * allows any number of leading or trailing whitespace characters.
- *
- * 'size' is the sizeof() the desired integral result (1, 2, or 4 bytes).
- *
- * c, if not 0, is a terminator character that may appear after the
- * integer (plus whitespace).  If 0, the string must end after the integer.
- *
- * Unlike plain atoi(), this will throw ereport() upon bad input format or
- * overflow.
- */
-int32
-pg_atoi(const char *s, int size, int c)
-{
-   long        l;
-   char       *badp;
-
-   /*
-    * Some versions of strtol treat the empty string as an error, but some
-    * seem not to.  Make an explicit test to be sure we catch it.
-    */
-   if (s == NULL)
-       elog(ERROR, "NULL pointer");
-   if (*s == 0)
-       ereport(ERROR,
-               (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
-                errmsg("invalid input syntax for type %s: \"%s\"",
-                       "integer", s)));
-
-   errno = 0;
-   l = strtol(s, &badp, 10);
-
-   /* We made no progress parsing the string, so bail out */
-   if (s == badp)
-       ereport(ERROR,
-               (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
-                errmsg("invalid input syntax for type %s: \"%s\"",
-                       "integer", s)));
-
-   switch (size)
-   {
-       case sizeof(int32):
-           if (errno == ERANGE
-#if defined(HAVE_LONG_INT_64)
-           /* won't get ERANGE on these with 64-bit longs... */
-               || l < INT_MIN || l > INT_MAX
-#endif
-               )
-               ereport(ERROR,
-                       (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
-                        errmsg("value \"%s\" is out of range for type %s", s,
-                               "integer")));
-           break;
-       case sizeof(int16):
-           if (errno == ERANGE || l < SHRT_MIN || l > SHRT_MAX)
-               ereport(ERROR,
-                       (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
-                        errmsg("value \"%s\" is out of range for type %s", s,
-                               "smallint")));
-           break;
-       case sizeof(int8):
-           if (errno == ERANGE || l < SCHAR_MIN || l > SCHAR_MAX)
-               ereport(ERROR,
-                       (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
-                        errmsg("value \"%s\" is out of range for 8-bit integer", s)));
-           break;
-       default:
-           elog(ERROR, "unsupported result size: %d", size);
-   }
-
-   /*
-    * Skip any trailing whitespace; if anything but whitespace remains before
-    * the terminating character, bail out
-    */
-   while (*badp && *badp != c && isspace((unsigned char) *badp))
-       badp++;
-
-   if (*badp && *badp != c)
-       ereport(ERROR,
-               (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
-                errmsg("invalid input syntax for type %s: \"%s\"",
-                       "integer", s)));
-
-   return (int32) l;
-}
-
 /*
  * Convert input string to a signed 16 bit integer.
  *
index 48ddfec9ebf602e28752a772ad46388da3b20427..666e545496916ca64ab14d564dc8240feeab9ce7 100644 (file)
@@ -43,7 +43,6 @@ extern void namestrcpy(Name name, const char *str);
 extern int namestrcmp(Name name, const char *str);
 
 /* numutils.c */
-extern int32 pg_atoi(const char *s, int size, int c);
 extern int16 pg_strtoint16(const char *s);
 extern int32 pg_strtoint32(const char *s);
 extern int64 pg_strtoint64(const char *s);