Header And Logo

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

Functions

varchar.c File Reference

#include "postgres.h"
#include "access/hash.h"
#include "access/tuptoaster.h"
#include "libpq/pqformat.h"
#include "nodes/nodeFuncs.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "mb/pg_wchar.h"
Include dependency graph for varchar.c:

Go to the source code of this file.

Functions

static int32 anychar_typmodin (ArrayType *ta, const char *typename)
static char * anychar_typmodout (int32 typmod)
static BpCharbpchar_input (const char *s, size_t len, int32 atttypmod)
Datum bpcharin (PG_FUNCTION_ARGS)
Datum bpcharout (PG_FUNCTION_ARGS)
Datum bpcharrecv (PG_FUNCTION_ARGS)
Datum bpcharsend (PG_FUNCTION_ARGS)
Datum bpchar (PG_FUNCTION_ARGS)
Datum char_bpchar (PG_FUNCTION_ARGS)
Datum bpchar_name (PG_FUNCTION_ARGS)
Datum name_bpchar (PG_FUNCTION_ARGS)
Datum bpchartypmodin (PG_FUNCTION_ARGS)
Datum bpchartypmodout (PG_FUNCTION_ARGS)
static VarCharvarchar_input (const char *s, size_t len, int32 atttypmod)
Datum varcharin (PG_FUNCTION_ARGS)
Datum varcharout (PG_FUNCTION_ARGS)
Datum varcharrecv (PG_FUNCTION_ARGS)
Datum varcharsend (PG_FUNCTION_ARGS)
Datum varchar_transform (PG_FUNCTION_ARGS)
Datum varchar (PG_FUNCTION_ARGS)
Datum varchartypmodin (PG_FUNCTION_ARGS)
Datum varchartypmodout (PG_FUNCTION_ARGS)
static int bcTruelen (BpChar *arg)
Datum bpcharlen (PG_FUNCTION_ARGS)
Datum bpcharoctetlen (PG_FUNCTION_ARGS)
Datum bpchareq (PG_FUNCTION_ARGS)
Datum bpcharne (PG_FUNCTION_ARGS)
Datum bpcharlt (PG_FUNCTION_ARGS)
Datum bpcharle (PG_FUNCTION_ARGS)
Datum bpchargt (PG_FUNCTION_ARGS)
Datum bpcharge (PG_FUNCTION_ARGS)
Datum bpcharcmp (PG_FUNCTION_ARGS)
Datum bpchar_larger (PG_FUNCTION_ARGS)
Datum bpchar_smaller (PG_FUNCTION_ARGS)
Datum hashbpchar (PG_FUNCTION_ARGS)
static int internal_bpchar_pattern_compare (BpChar *arg1, BpChar *arg2)
Datum bpchar_pattern_lt (PG_FUNCTION_ARGS)
Datum bpchar_pattern_le (PG_FUNCTION_ARGS)
Datum bpchar_pattern_ge (PG_FUNCTION_ARGS)
Datum bpchar_pattern_gt (PG_FUNCTION_ARGS)
Datum btbpchar_pattern_cmp (PG_FUNCTION_ARGS)

Function Documentation

static int32 anychar_typmodin ( ArrayType ta,
const char *  typename 
) [static]

Definition at line 29 of file varchar.c.

References ArrayGetIntegerTypmods(), ereport, errcode(), errmsg(), ERROR, MaxAttrSize, and VARHDRSZ.

Referenced by bpchartypmodin(), and varchartypmodin().

{
    int32       typmod;
    int32      *tl;
    int         n;

    tl = ArrayGetIntegerTypmods(ta, &n);

    /*
     * we're not too tense about good error message here because grammar
     * shouldn't allow wrong number of modifiers for CHAR
     */
    if (n != 1)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("invalid type modifier")));

    if (*tl < 1)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("length for type %s must be at least 1", typename)));
    if (*tl > MaxAttrSize)
        ereport(ERROR,
                (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                 errmsg("length for type %s cannot exceed %d",
                        typename, MaxAttrSize)));

    /*
     * For largely historical reasons, the typmod is VARHDRSZ plus the number
     * of characters; there is enough client-side code that knows about that
     * that we'd better not change it.
     */
    typmod = VARHDRSZ + *tl;

    return typmod;
}

static char* anychar_typmodout ( int32  typmod  )  [static]

Definition at line 68 of file varchar.c.

References palloc(), snprintf(), and VARHDRSZ.

Referenced by bpchartypmodout(), and varchartypmodout().

{
    char       *res = (char *) palloc(64);

    if (typmod > VARHDRSZ)
        snprintf(res, 64, "(%d)", (int) (typmod - VARHDRSZ));
    else
        *res = '\0';

    return res;
}

static int bcTruelen ( BpChar arg  )  [static]

Definition at line 653 of file varchar.c.

References i, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by bpchar_larger(), bpchar_smaller(), bpcharcmp(), bpchareq(), bpcharge(), bpchargt(), bpcharle(), bpcharlen(), bpcharlt(), bpcharne(), hashbpchar(), and internal_bpchar_pattern_compare().

{
    char       *s = VARDATA_ANY(arg);
    int         i;
    int         len;

    len = VARSIZE_ANY_EXHDR(arg);
    for (i = len - 1; i >= 0; i--)
    {
        if (s[i] != ' ')
            break;
    }
    return i + 1;
}

Datum bpchar ( PG_FUNCTION_ARGS   ) 

Definition at line 265 of file varchar.c.

References Assert, ereport, errcode(), errmsg(), ERROR, i, palloc(), PG_GETARG_BOOL, PG_GETARG_BPCHAR_PP, PG_GETARG_INT32, pg_mbcharcliplen(), pg_mbstrlen_with_len(), PG_RETURN_BPCHAR_P, SET_VARSIZE, VARDATA, VARDATA_ANY, VARHDRSZ, and VARSIZE_ANY_EXHDR.

{
    BpChar     *source = PG_GETARG_BPCHAR_PP(0);
    int32       maxlen = PG_GETARG_INT32(1);
    bool        isExplicit = PG_GETARG_BOOL(2);
    BpChar     *result;
    int32       len;
    char       *r;
    char       *s;
    int         i;
    int         charlen;        /* number of characters in the input string +
                                 * VARHDRSZ */

    /* No work if typmod is invalid */
    if (maxlen < (int32) VARHDRSZ)
        PG_RETURN_BPCHAR_P(source);

    maxlen -= VARHDRSZ;

    len = VARSIZE_ANY_EXHDR(source);
    s = VARDATA_ANY(source);

    charlen = pg_mbstrlen_with_len(s, len);

    /* No work if supplied data matches typmod already */
    if (charlen == maxlen)
        PG_RETURN_BPCHAR_P(source);

    if (charlen > maxlen)
    {
        /* Verify that extra characters are spaces, and clip them off */
        size_t      maxmblen;

        maxmblen = pg_mbcharcliplen(s, len, maxlen);

        if (!isExplicit)
        {
            for (i = maxmblen; i < len; i++)
                if (s[i] != ' ')
                    ereport(ERROR,
                            (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
                             errmsg("value too long for type character(%d)",
                                    maxlen)));
        }

        len = maxmblen;

        /*
         * At this point, maxlen is the necessary byte length, not the number
         * of CHARACTERS!
         */
        maxlen = len;
    }
    else
    {
        /*
         * At this point, maxlen is the necessary byte length, not the number
         * of CHARACTERS!
         */
        maxlen = len + (maxlen - charlen);
    }

    Assert(maxlen >= len);

    result = palloc(maxlen + VARHDRSZ);
    SET_VARSIZE(result, maxlen + VARHDRSZ);
    r = VARDATA(result);

    memcpy(r, s, len);

    /* blank pad the string if necessary */
    if (maxlen > len)
        memset(r + len, ' ', maxlen - len);

    PG_RETURN_BPCHAR_P(result);
}

static BpChar* bpchar_input ( const char *  s,
size_t  len,
int32  atttypmod 
) [static]

Definition at line 122 of file varchar.c.

References ereport, errcode(), errmsg(), ERROR, palloc(), pg_mbcharcliplen(), pg_mbstrlen_with_len(), SET_VARSIZE, VARDATA, and VARHDRSZ.

Referenced by bpcharin(), and bpcharrecv().

{
    BpChar     *result;
    char       *r;
    size_t      maxlen;

    /* If typmod is -1 (or invalid), use the actual string length */
    if (atttypmod < (int32) VARHDRSZ)
        maxlen = len;
    else
    {
        size_t      charlen;    /* number of CHARACTERS in the input */

        maxlen = atttypmod - VARHDRSZ;
        charlen = pg_mbstrlen_with_len(s, len);
        if (charlen > maxlen)
        {
            /* Verify that extra characters are spaces, and clip them off */
            size_t      mbmaxlen = pg_mbcharcliplen(s, len, maxlen);
            size_t      j;

            /*
             * at this point, len is the actual BYTE length of the input
             * string, maxlen is the max number of CHARACTERS allowed for this
             * bpchar type, mbmaxlen is the length in BYTES of those chars.
             */
            for (j = mbmaxlen; j < len; j++)
            {
                if (s[j] != ' ')
                    ereport(ERROR,
                            (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
                             errmsg("value too long for type character(%d)",
                                    (int) maxlen)));
            }

            /*
             * Now we set maxlen to the necessary byte length, not the number
             * of CHARACTERS!
             */
            maxlen = len = mbmaxlen;
        }
        else
        {
            /*
             * Now we set maxlen to the necessary byte length, not the number
             * of CHARACTERS!
             */
            maxlen = len + (maxlen - charlen);
        }
    }

    result = (BpChar *) palloc(maxlen + VARHDRSZ);
    SET_VARSIZE(result, maxlen + VARHDRSZ);
    r = VARDATA(result);
    memcpy(r, s, len);

    /* blank pad the string if necessary */
    if (maxlen > len)
        memset(r + len, ' ', maxlen - len);

    return result;
}

Datum bpchar_larger ( PG_FUNCTION_ARGS   ) 

Definition at line 862 of file varchar.c.

References bcTruelen(), PG_GET_COLLATION, PG_GETARG_BPCHAR_PP, PG_RETURN_BPCHAR_P, VARDATA_ANY, and varstr_cmp().

{
    BpChar     *arg1 = PG_GETARG_BPCHAR_PP(0);
    BpChar     *arg2 = PG_GETARG_BPCHAR_PP(1);
    int         len1,
                len2;
    int         cmp;

    len1 = bcTruelen(arg1);
    len2 = bcTruelen(arg2);

    cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
                     PG_GET_COLLATION());

    PG_RETURN_BPCHAR_P((cmp >= 0) ? arg1 : arg2);
}

Datum bpchar_name ( PG_FUNCTION_ARGS   ) 

Definition at line 365 of file varchar.c.

References NAMEDATALEN, NameStr, palloc0(), PG_GETARG_BPCHAR_PP, pg_mbcliplen(), PG_RETURN_NAME, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

{
    BpChar     *s = PG_GETARG_BPCHAR_PP(0);
    char       *s_data;
    Name        result;
    int         len;

    len = VARSIZE_ANY_EXHDR(s);
    s_data = VARDATA_ANY(s);

    /* Truncate oversize input */
    if (len >= NAMEDATALEN)
        len = pg_mbcliplen(s_data, len, NAMEDATALEN - 1);

    /* Remove trailing blanks */
    while (len > 0)
    {
        if (s_data[len - 1] != ' ')
            break;
        len--;
    }

    /* We use palloc0 here to ensure result is zero-padded */
    result = (Name) palloc0(NAMEDATALEN);
    memcpy(NameStr(*result), s_data, len);

    PG_RETURN_NAME(result);
}

Datum bpchar_pattern_ge ( PG_FUNCTION_ARGS   ) 

Definition at line 988 of file varchar.c.

References internal_bpchar_pattern_compare(), PG_FREE_IF_COPY, PG_GETARG_BPCHAR_PP, and PG_RETURN_BOOL.

{
    BpChar     *arg1 = PG_GETARG_BPCHAR_PP(0);
    BpChar     *arg2 = PG_GETARG_BPCHAR_PP(1);
    int         result;

    result = internal_bpchar_pattern_compare(arg1, arg2);

    PG_FREE_IF_COPY(arg1, 0);
    PG_FREE_IF_COPY(arg2, 1);

    PG_RETURN_BOOL(result >= 0);
}

Datum bpchar_pattern_gt ( PG_FUNCTION_ARGS   ) 

Definition at line 1004 of file varchar.c.

References internal_bpchar_pattern_compare(), PG_FREE_IF_COPY, PG_GETARG_BPCHAR_PP, and PG_RETURN_BOOL.

{
    BpChar     *arg1 = PG_GETARG_BPCHAR_PP(0);
    BpChar     *arg2 = PG_GETARG_BPCHAR_PP(1);
    int         result;

    result = internal_bpchar_pattern_compare(arg1, arg2);

    PG_FREE_IF_COPY(arg1, 0);
    PG_FREE_IF_COPY(arg2, 1);

    PG_RETURN_BOOL(result > 0);
}

Datum bpchar_pattern_le ( PG_FUNCTION_ARGS   ) 

Definition at line 972 of file varchar.c.

References internal_bpchar_pattern_compare(), PG_FREE_IF_COPY, PG_GETARG_BPCHAR_PP, and PG_RETURN_BOOL.

{
    BpChar     *arg1 = PG_GETARG_BPCHAR_PP(0);
    BpChar     *arg2 = PG_GETARG_BPCHAR_PP(1);
    int         result;

    result = internal_bpchar_pattern_compare(arg1, arg2);

    PG_FREE_IF_COPY(arg1, 0);
    PG_FREE_IF_COPY(arg2, 1);

    PG_RETURN_BOOL(result <= 0);
}

Datum bpchar_pattern_lt ( PG_FUNCTION_ARGS   ) 

Definition at line 956 of file varchar.c.

References internal_bpchar_pattern_compare(), PG_FREE_IF_COPY, PG_GETARG_BPCHAR_PP, and PG_RETURN_BOOL.

{
    BpChar     *arg1 = PG_GETARG_BPCHAR_PP(0);
    BpChar     *arg2 = PG_GETARG_BPCHAR_PP(1);
    int         result;

    result = internal_bpchar_pattern_compare(arg1, arg2);

    PG_FREE_IF_COPY(arg1, 0);
    PG_FREE_IF_COPY(arg2, 1);

    PG_RETURN_BOOL(result < 0);
}

Datum bpchar_smaller ( PG_FUNCTION_ARGS   ) 

Definition at line 880 of file varchar.c.

References bcTruelen(), PG_GET_COLLATION, PG_GETARG_BPCHAR_PP, PG_RETURN_BPCHAR_P, VARDATA_ANY, and varstr_cmp().

{
    BpChar     *arg1 = PG_GETARG_BPCHAR_PP(0);
    BpChar     *arg2 = PG_GETARG_BPCHAR_PP(1);
    int         len1,
                len2;
    int         cmp;

    len1 = bcTruelen(arg1);
    len2 = bcTruelen(arg2);

    cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
                     PG_GET_COLLATION());

    PG_RETURN_BPCHAR_P((cmp <= 0) ? arg1 : arg2);
}

Datum bpcharcmp ( PG_FUNCTION_ARGS   ) 

Definition at line 841 of file varchar.c.

References bcTruelen(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_BPCHAR_PP, PG_RETURN_INT32, VARDATA_ANY, and varstr_cmp().

{
    BpChar     *arg1 = PG_GETARG_BPCHAR_PP(0);
    BpChar     *arg2 = PG_GETARG_BPCHAR_PP(1);
    int         len1,
                len2;
    int         cmp;

    len1 = bcTruelen(arg1);
    len2 = bcTruelen(arg2);

    cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
                     PG_GET_COLLATION());

    PG_FREE_IF_COPY(arg1, 0);
    PG_FREE_IF_COPY(arg2, 1);

    PG_RETURN_INT32(cmp);
}

Datum bpchareq ( PG_FUNCTION_ARGS   ) 

Definition at line 703 of file varchar.c.

References bcTruelen(), memcmp(), PG_FREE_IF_COPY, PG_GETARG_BPCHAR_PP, PG_RETURN_BOOL, and VARDATA_ANY.

{
    BpChar     *arg1 = PG_GETARG_BPCHAR_PP(0);
    BpChar     *arg2 = PG_GETARG_BPCHAR_PP(1);
    int         len1,
                len2;
    bool        result;

    len1 = bcTruelen(arg1);
    len2 = bcTruelen(arg2);

    /*
     * Since we only care about equality or not-equality, we can avoid all the
     * expense of strcoll() here, and just do bitwise comparison.
     */
    if (len1 != len2)
        result = false;
    else
        result = (memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), len1) == 0);

    PG_FREE_IF_COPY(arg1, 0);
    PG_FREE_IF_COPY(arg2, 1);

    PG_RETURN_BOOL(result);
}

Datum bpcharge ( PG_FUNCTION_ARGS   ) 

Definition at line 820 of file varchar.c.

References bcTruelen(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_BPCHAR_PP, PG_RETURN_BOOL, VARDATA_ANY, and varstr_cmp().

{
    BpChar     *arg1 = PG_GETARG_BPCHAR_PP(0);
    BpChar     *arg2 = PG_GETARG_BPCHAR_PP(1);
    int         len1,
                len2;
    int         cmp;

    len1 = bcTruelen(arg1);
    len2 = bcTruelen(arg2);

    cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
                     PG_GET_COLLATION());

    PG_FREE_IF_COPY(arg1, 0);
    PG_FREE_IF_COPY(arg2, 1);

    PG_RETURN_BOOL(cmp >= 0);
}

Datum bpchargt ( PG_FUNCTION_ARGS   ) 

Definition at line 799 of file varchar.c.

References bcTruelen(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_BPCHAR_PP, PG_RETURN_BOOL, VARDATA_ANY, and varstr_cmp().

{
    BpChar     *arg1 = PG_GETARG_BPCHAR_PP(0);
    BpChar     *arg2 = PG_GETARG_BPCHAR_PP(1);
    int         len1,
                len2;
    int         cmp;

    len1 = bcTruelen(arg1);
    len2 = bcTruelen(arg2);

    cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
                     PG_GET_COLLATION());

    PG_FREE_IF_COPY(arg1, 0);
    PG_FREE_IF_COPY(arg2, 1);

    PG_RETURN_BOOL(cmp > 0);
}

Datum bpcharin ( PG_FUNCTION_ARGS   ) 

Definition at line 190 of file varchar.c.

References bpchar_input(), PG_GETARG_CSTRING, PG_GETARG_INT32, PG_GETARG_OID, and PG_RETURN_BPCHAR_P.

{
    char       *s = PG_GETARG_CSTRING(0);

#ifdef NOT_USED
    Oid         typelem = PG_GETARG_OID(1);
#endif
    int32       atttypmod = PG_GETARG_INT32(2);
    BpChar     *result;

    result = bpchar_input(s, strlen(s), atttypmod);
    PG_RETURN_BPCHAR_P(result);
}

Datum bpcharle ( PG_FUNCTION_ARGS   ) 

Definition at line 778 of file varchar.c.

References bcTruelen(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_BPCHAR_PP, PG_RETURN_BOOL, VARDATA_ANY, and varstr_cmp().

{
    BpChar     *arg1 = PG_GETARG_BPCHAR_PP(0);
    BpChar     *arg2 = PG_GETARG_BPCHAR_PP(1);
    int         len1,
                len2;
    int         cmp;

    len1 = bcTruelen(arg1);
    len2 = bcTruelen(arg2);

    cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
                     PG_GET_COLLATION());

    PG_FREE_IF_COPY(arg1, 0);
    PG_FREE_IF_COPY(arg2, 1);

    PG_RETURN_BOOL(cmp <= 0);
}

Datum bpcharlen ( PG_FUNCTION_ARGS   ) 

Definition at line 669 of file varchar.c.

References arg, bcTruelen(), pg_database_encoding_max_length(), PG_GETARG_BPCHAR_PP, pg_mbstrlen_with_len(), PG_RETURN_INT32, and VARDATA_ANY.

{
    BpChar     *arg = PG_GETARG_BPCHAR_PP(0);
    int         len;

    /* get number of bytes, ignoring trailing spaces */
    len = bcTruelen(arg);

    /* in multibyte encoding, convert to number of characters */
    if (pg_database_encoding_max_length() != 1)
        len = pg_mbstrlen_with_len(VARDATA_ANY(arg), len);

    PG_RETURN_INT32(len);
}

Datum bpcharlt ( PG_FUNCTION_ARGS   ) 

Definition at line 757 of file varchar.c.

References bcTruelen(), PG_FREE_IF_COPY, PG_GET_COLLATION, PG_GETARG_BPCHAR_PP, PG_RETURN_BOOL, VARDATA_ANY, and varstr_cmp().

{
    BpChar     *arg1 = PG_GETARG_BPCHAR_PP(0);
    BpChar     *arg2 = PG_GETARG_BPCHAR_PP(1);
    int         len1,
                len2;
    int         cmp;

    len1 = bcTruelen(arg1);
    len2 = bcTruelen(arg2);

    cmp = varstr_cmp(VARDATA_ANY(arg1), len1, VARDATA_ANY(arg2), len2,
                     PG_GET_COLLATION());

    PG_FREE_IF_COPY(arg1, 0);
    PG_FREE_IF_COPY(arg2, 1);

    PG_RETURN_BOOL(cmp < 0);
}

Datum bpcharne ( PG_FUNCTION_ARGS   ) 

Definition at line 730 of file varchar.c.

References bcTruelen(), memcmp(), PG_FREE_IF_COPY, PG_GETARG_BPCHAR_PP, PG_RETURN_BOOL, and VARDATA_ANY.

{
    BpChar     *arg1 = PG_GETARG_BPCHAR_PP(0);
    BpChar     *arg2 = PG_GETARG_BPCHAR_PP(1);
    int         len1,
                len2;
    bool        result;

    len1 = bcTruelen(arg1);
    len2 = bcTruelen(arg2);

    /*
     * Since we only care about equality or not-equality, we can avoid all the
     * expense of strcoll() here, and just do bitwise comparison.
     */
    if (len1 != len2)
        result = true;
    else
        result = (memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), len1) != 0);

    PG_FREE_IF_COPY(arg1, 0);
    PG_FREE_IF_COPY(arg2, 1);

    PG_RETURN_BOOL(result);
}

Datum bpcharoctetlen ( PG_FUNCTION_ARGS   ) 

Definition at line 685 of file varchar.c.

References arg, PG_GETARG_DATUM, PG_RETURN_INT32, toast_raw_datum_size(), and VARHDRSZ.

{
    Datum       arg = PG_GETARG_DATUM(0);

    /* We need not detoast the input at all */
    PG_RETURN_INT32(toast_raw_datum_size(arg) - VARHDRSZ);
}

Datum bpcharout ( PG_FUNCTION_ARGS   ) 
Datum bpcharrecv ( PG_FUNCTION_ARGS   ) 

Definition at line 223 of file varchar.c.

References bpchar_input(), buf, StringInfoData::cursor, StringInfoData::len, pfree(), PG_GETARG_INT32, PG_GETARG_OID, PG_GETARG_POINTER, PG_RETURN_BPCHAR_P, and pq_getmsgtext().

{
    StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);

#ifdef NOT_USED
    Oid         typelem = PG_GETARG_OID(1);
#endif
    int32       atttypmod = PG_GETARG_INT32(2);
    BpChar     *result;
    char       *str;
    int         nbytes;

    str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
    result = bpchar_input(str, nbytes, atttypmod);
    pfree(str);
    PG_RETURN_BPCHAR_P(result);
}

Datum bpcharsend ( PG_FUNCTION_ARGS   ) 

Definition at line 245 of file varchar.c.

References textsend().

{
    /* Exactly the same as textsend, so share code */
    return textsend(fcinfo);
}

Datum bpchartypmodin ( PG_FUNCTION_ARGS   ) 
Datum bpchartypmodout ( PG_FUNCTION_ARGS   ) 

Definition at line 419 of file varchar.c.

References anychar_typmodout(), PG_GETARG_INT32, and PG_RETURN_CSTRING.

Datum btbpchar_pattern_cmp ( PG_FUNCTION_ARGS   ) 

Definition at line 1020 of file varchar.c.

References internal_bpchar_pattern_compare(), PG_FREE_IF_COPY, PG_GETARG_BPCHAR_PP, and PG_RETURN_INT32.

{
    BpChar     *arg1 = PG_GETARG_BPCHAR_PP(0);
    BpChar     *arg2 = PG_GETARG_BPCHAR_PP(1);
    int         result;

    result = internal_bpchar_pattern_compare(arg1, arg2);

    PG_FREE_IF_COPY(arg1, 0);
    PG_FREE_IF_COPY(arg2, 1);

    PG_RETURN_INT32(result);
}

Datum char_bpchar ( PG_FUNCTION_ARGS   ) 

Definition at line 347 of file varchar.c.

References palloc(), PG_GETARG_CHAR, PG_RETURN_BPCHAR_P, SET_VARSIZE, VARDATA, and VARHDRSZ.

{
    char        c = PG_GETARG_CHAR(0);
    BpChar     *result;

    result = (BpChar *) palloc(VARHDRSZ + 1);

    SET_VARSIZE(result, VARHDRSZ + 1);
    *(VARDATA(result)) = c;

    PG_RETURN_BPCHAR_P(result);
}

Datum hashbpchar ( PG_FUNCTION_ARGS   ) 

Definition at line 907 of file varchar.c.

References bcTruelen(), hash_any(), PG_FREE_IF_COPY, PG_GETARG_BPCHAR_PP, and VARDATA_ANY.

{
    BpChar     *key = PG_GETARG_BPCHAR_PP(0);
    char       *keydata;
    int         keylen;
    Datum       result;

    keydata = VARDATA_ANY(key);
    keylen = bcTruelen(key);

    result = hash_any((unsigned char *) keydata, keylen);

    /* Avoid leaking memory for toasted inputs */
    PG_FREE_IF_COPY(key, 0);

    return result;
}

static int internal_bpchar_pattern_compare ( BpChar arg1,
BpChar arg2 
) [static]

Definition at line 934 of file varchar.c.

References bcTruelen(), memcmp(), Min, and VARDATA_ANY.

Referenced by bpchar_pattern_ge(), bpchar_pattern_gt(), bpchar_pattern_le(), bpchar_pattern_lt(), and btbpchar_pattern_cmp().

{
    int         result;
    int         len1,
                len2;

    len1 = bcTruelen(arg1);
    len2 = bcTruelen(arg2);

    result = memcmp(VARDATA_ANY(arg1), VARDATA_ANY(arg2), Min(len1, len2));
    if (result != 0)
        return result;
    else if (len1 < len2)
        return -1;
    else if (len1 > len2)
        return 1;
    else
        return 0;
}

Datum name_bpchar ( PG_FUNCTION_ARGS   ) 

Definition at line 401 of file varchar.c.

References cstring_to_text(), NameStr, PG_GETARG_NAME, and PG_RETURN_BPCHAR_P.

{
    Name        s = PG_GETARG_NAME(0);
    BpChar     *result;

    result = (BpChar *) cstring_to_text(NameStr(*s));
    PG_RETURN_BPCHAR_P(result);
}

Datum varchar ( PG_FUNCTION_ARGS   ) 

Definition at line 592 of file varchar.c.

References cstring_to_text_with_len(), ereport, errcode(), errmsg(), ERROR, i, PG_GETARG_BOOL, PG_GETARG_INT32, PG_GETARG_VARCHAR_PP, pg_mbcharcliplen(), PG_RETURN_VARCHAR_P, VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by main().

{
    VarChar    *source = PG_GETARG_VARCHAR_PP(0);
    int32       typmod = PG_GETARG_INT32(1);
    bool        isExplicit = PG_GETARG_BOOL(2);
    int32       len,
                maxlen;
    size_t      maxmblen;
    int         i;
    char       *s_data;

    len = VARSIZE_ANY_EXHDR(source);
    s_data = VARDATA_ANY(source);
    maxlen = typmod - VARHDRSZ;

    /* No work if typmod is invalid or supplied data fits it already */
    if (maxlen < 0 || len <= maxlen)
        PG_RETURN_VARCHAR_P(source);

    /* only reach here if string is too long... */

    /* truncate multibyte string preserving multibyte boundary */
    maxmblen = pg_mbcharcliplen(s_data, len, maxlen);

    if (!isExplicit)
    {
        for (i = maxmblen; i < len; i++)
            if (s_data[i] != ' ')
                ereport(ERROR,
                        (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
                      errmsg("value too long for type character varying(%d)",
                             maxlen)));
    }

    PG_RETURN_VARCHAR_P((VarChar *) cstring_to_text_with_len(s_data,
                                                             maxmblen));
}

static VarChar* varchar_input ( const char *  s,
size_t  len,
int32  atttypmod 
) [static]

Definition at line 450 of file varchar.c.

References cstring_to_text_with_len(), ereport, errcode(), errmsg(), ERROR, and pg_mbcharcliplen().

Referenced by varcharin(), and varcharrecv().

{
    VarChar    *result;
    size_t      maxlen;

    maxlen = atttypmod - VARHDRSZ;

    if (atttypmod >= (int32) VARHDRSZ && len > maxlen)
    {
        /* Verify that extra characters are spaces, and clip them off */
        size_t      mbmaxlen = pg_mbcharcliplen(s, len, maxlen);
        size_t      j;

        for (j = mbmaxlen; j < len; j++)
        {
            if (s[j] != ' ')
                ereport(ERROR,
                        (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
                      errmsg("value too long for type character varying(%d)",
                             (int) maxlen)));
        }

        len = mbmaxlen;
    }

    result = (VarChar *) cstring_to_text_with_len(s, len);
    return result;
}

Datum varchar_transform ( PG_FUNCTION_ARGS   ) 

Definition at line 553 of file varchar.c.

References FuncExpr::args, Assert, DatumGetInt32, exprTypmod(), IsA, linitial, list_length(), lsecond, PG_GETARG_POINTER, PG_RETURN_POINTER, and relabel_to_typmod().

{
    FuncExpr   *expr = (FuncExpr *) PG_GETARG_POINTER(0);
    Node       *ret = NULL;
    Node       *typmod;

    Assert(IsA(expr, FuncExpr));
    Assert(list_length(expr->args) >= 2);

    typmod = (Node *) lsecond(expr->args);

    if (IsA(typmod, Const) &&!((Const *) typmod)->constisnull)
    {
        Node       *source = (Node *) linitial(expr->args);
        int32       old_typmod = exprTypmod(source);
        int32       new_typmod = DatumGetInt32(((Const *) typmod)->constvalue);
        int32       old_max = old_typmod - VARHDRSZ;
        int32       new_max = new_typmod - VARHDRSZ;

        if (new_typmod < 0 || (old_typmod >= 0 && old_max <= new_max))
            ret = relabel_to_typmod(source, new_typmod);
    }

    PG_RETURN_POINTER(ret);
}

Datum varcharin ( PG_FUNCTION_ARGS   ) 

Definition at line 484 of file varchar.c.

References PG_GETARG_CSTRING, PG_GETARG_INT32, PG_GETARG_OID, PG_RETURN_VARCHAR_P, and varchar_input().

{
    char       *s = PG_GETARG_CSTRING(0);

#ifdef NOT_USED
    Oid         typelem = PG_GETARG_OID(1);
#endif
    int32       atttypmod = PG_GETARG_INT32(2);
    VarChar    *result;

    result = varchar_input(s, strlen(s), atttypmod);
    PG_RETURN_VARCHAR_P(result);
}

Datum varcharout ( PG_FUNCTION_ARGS   ) 
Datum varcharrecv ( PG_FUNCTION_ARGS   ) 

Definition at line 517 of file varchar.c.

References buf, StringInfoData::cursor, StringInfoData::len, pfree(), PG_GETARG_INT32, PG_GETARG_OID, PG_GETARG_POINTER, PG_RETURN_VARCHAR_P, pq_getmsgtext(), and varchar_input().

{
    StringInfo  buf = (StringInfo) PG_GETARG_POINTER(0);

#ifdef NOT_USED
    Oid         typelem = PG_GETARG_OID(1);
#endif
    int32       atttypmod = PG_GETARG_INT32(2);
    VarChar    *result;
    char       *str;
    int         nbytes;

    str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
    result = varchar_input(str, nbytes, atttypmod);
    pfree(str);
    PG_RETURN_VARCHAR_P(result);
}

Datum varcharsend ( PG_FUNCTION_ARGS   ) 

Definition at line 539 of file varchar.c.

References textsend().

{
    /* Exactly the same as textsend, so share code */
    return textsend(fcinfo);
}

Datum varchartypmodin ( PG_FUNCTION_ARGS   ) 

Definition at line 631 of file varchar.c.

References anychar_typmodin(), PG_GETARG_ARRAYTYPE_P, and PG_RETURN_INT32.

Datum varchartypmodout ( PG_FUNCTION_ARGS   ) 

Definition at line 639 of file varchar.c.

References anychar_typmodout(), PG_GETARG_INT32, and PG_RETURN_CSTRING.