more custom C function fun - Mailing list pgsql-general

From Dan \"Heron\" Myers
Subject more custom C function fun
Date
Msg-id 481FF00C.4060408@xnapid.com
Whole thread Raw
Responses Re: more custom C function fun  (Martijn van Oosterhout <kleptog@svana.org>)
List pgsql-general
I have a custom C function that takes two text*s and returns a text*.

My problem is with this code:


PG_FUNCTION_INFO_V1(get_agent);

PGMODULEEXPORT Datum get_agent(PG_FUNCTION_ARGS)
{
    if(!PG_ARGISNULL(0))
    {
        text* calling_party = PG_GETARG_TEXT_P(0);

        char* thestr = VARDATA(calling_party);
        if(thestr[20] == ')')
        {
            PG_RETURN_TEXT_P(calling_party);
        }
    }
/* the other argument is ignored for now */
    PG_RETURN_NULL();
}

The problem is, the comparison in the inner if statement is always true.
  If I change to compare, say, thestr[0] == 'N', then it works as
expected (returning only those text*s whose first letter is N, returning
null for the rest).

However if I try to compare any character inside the text* with a
parenthesis (both '(' and ')'), then the equality is apparently always
true (the function never returns null, always returning calling_party),
whether or not there is any data in that column that contains a
parenthesis in that column.

Does anyone know of any oddities or whatnot I should be accounting for
when reading character data out of a text*?

- Dan

pgsql-general by date:

Previous
From: Martin Marques
Date:
Subject: Re: checkpoint_segments warning?
Next
From: "Dan \"Heron\" Myers"
Date:
Subject: Re: more custom C function fun