Text <-> C string

From: "Brendan Jurd" <direvus(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Text <-> C string
Date: 2007-09-21 18:43:18
Message-ID: 37ed240d0709211143v6c55bbebx2f1ffbe354208d8c@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers pgsql-patches

Hi hackers,

I've noticed that there is a lot of code, particularly in src/backend,
that goes through the motions of making a text datum into a cstring to
perform some work on it, and likewise for making a cstring into a text
datum.

Is there not a nice macro somewhere to handle this consistently? And
if not, shouldn't there be?

I noticed a comment for StrNCpy() in src/include/c.h that seems related:

/* BTW: when you need to copy a non-null-terminated string (like a
text datum) and add a null, do not do it with StrNCpy [snip] Do it
honestly with "memcpy(dst,src,len); dst[len] = '\0'; instead."

Okay, I can see why using StrNCpy is a bad idea, but why not "Do it
honestly with TEXT_CSTRING(src, dst)", or similar?

Surely having the exact same four lines of code written out in dozens
of places is a Bad Thing, but perhaps there is some reasoning behind
this that I am missing?

Thanks for your time,
BJ

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2007-09-21 19:00:22 Re: HOT is applied
Previous Message Tom Lane 2007-09-21 18:29:05 Re: HOT is applied

Browse pgsql-patches by date

  From Date Subject
Next Message Gregory Stark 2007-09-21 20:00:06 Re: Text <-> C string
Previous Message Gregory Stark 2007-09-21 17:41:08 Re: like/ilike improvements