Header And Logo

PostgreSQL
| The world's most advanced open source database.

Functions

numutils.c File Reference

#include "postgres.h"
#include <math.h>
#include <limits.h>
#include <ctype.h>
#include "utils/builtins.h"
Include dependency graph for numutils.c:

Go to the source code of this file.

Functions

int32 pg_atoi (char *s, int size, int c)
void pg_itoa (int16 i, char *a)
void pg_ltoa (int32 value, char *a)
void pg_lltoa (int64 value, char *a)

Function Documentation

int32 pg_atoi ( char *  s,
int  size,
int  c 
)

Definition at line 37 of file numutils.c.

References elog, ereport, errcode(), errmsg(), ERROR, and NULL.

Referenced by ArrayGetIntegerTypmods(), check_foreign_key(), int2in(), int2vectorin(), int4in(), libpqrcv_endstreaming(), libpqrcv_identify_system(), prsd_headline(), and text_format().

{
    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 integer: \"%s\"",
                        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 integer: \"%s\"",
                        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 integer", s)));
            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 smallint", s)));
            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 integer: \"%s\"",
                        s)));

    return (int32) l;
}

void pg_itoa ( int16  i,
char *  a 
)

Definition at line 118 of file numutils.c.

References pg_ltoa().

Referenced by int2out(), and int2vectorout().

{
    pg_ltoa((int32) i, a);
}

void pg_lltoa ( int64  value,
char *  a 
)

Definition at line 184 of file numutils.c.

References INT64CONST, and swap.

Referenced by int8out().

{
    char       *start = a;
    bool        neg = false;

    /*
     * Avoid problems with the most negative integer not being representable
     * as a positive integer.
     */
    if (value == (-INT64CONST(0x7FFFFFFFFFFFFFFF) - 1))
    {
        memcpy(a, "-9223372036854775808", 21);
        return;
    }
    else if (value < 0)
    {
        value = -value;
        neg = true;
    }

    /* Compute the result string backwards. */
    do
    {
        int64       remainder;
        int64       oldval = value;

        value /= 10;
        remainder = oldval - value * 10;
        *a++ = '0' + remainder;
    } while (value != 0);

    if (neg)
        *a++ = '-';

    /* Add trailing NUL byte, and back up 'a' to the last character. */
    *a-- = '\0';

    /* Reverse string. */
    while (start < a)
    {
        char        swap = *start;

        *start++ = *a;
        *a-- = swap;
    }
}

void pg_ltoa ( int32  value,
char *  a 
)

Definition at line 130 of file numutils.c.

References swap.

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

{
    char       *start = a;
    bool        neg = false;

    /*
     * Avoid problems with the most negative integer not being representable
     * as a positive integer.
     */
    if (value == (-2147483647 - 1))
    {
        memcpy(a, "-2147483648", 12);
        return;
    }
    else if (value < 0)
    {
        value = -value;
        neg = true;
    }

    /* Compute the result string backwards. */
    do
    {
        int32       remainder;
        int32       oldval = value;

        value /= 10;
        remainder = oldval - value * 10;
        *a++ = '0' + remainder;
    } while (value != 0);

    if (neg)
        *a++ = '-';

    /* Add trailing NUL byte, and back up 'a' to the last character. */
    *a-- = '\0';

    /* Reverse string. */
    while (start < a)
    {
        char        swap = *start;

        *start++ = *a;
        *a-- = swap;
    }
}