diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2008-03-25 22:42:46 +0000 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2008-03-25 22:42:46 +0000 |
commit | 220db7ccd8c88aafea4629f00e8be6f9f073ed00 (patch) | |
tree | 772c9dca19736eb9d417cb665a281686c3570b4c /src/backend/utils/adt/formatting.c | |
parent | f948197b4055bb0e22e508e9d6966217b7dbea3d (diff) | |
download | postgresql-220db7ccd8c88aafea4629f00e8be6f9f073ed00.tar.gz postgresql-220db7ccd8c88aafea4629f00e8be6f9f073ed00.zip |
Simplify and standardize conversions between TEXT datums and ordinary C
strings. This patch introduces four support functions cstring_to_text,
cstring_to_text_with_len, text_to_cstring, and text_to_cstring_buffer, and
two macros CStringGetTextDatum and TextDatumGetCString. A number of
existing macros that provided variants on these themes were removed.
Most of the places that need to make such conversions now require just one
function or macro call, in place of the multiple notational layers that used
to be needed. There are no longer any direct calls of textout or textin,
and we got most of the places that were using handmade conversions via
memcpy (there may be a few still lurking, though).
This commit doesn't make any serious effort to eliminate transient memory
leaks caused by detoasting toasted text objects before they reach
text_to_cstring. We changed PG_GETARG_TEXT_P to PG_GETARG_TEXT_PP in a few
places where it was easy, but much more could be done.
Brendan Jurd and Tom Lane
Diffstat (limited to 'src/backend/utils/adt/formatting.c')
-rw-r--r-- | src/backend/utils/adt/formatting.c | 49 |
1 files changed, 15 insertions, 34 deletions
diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index d1cc69a7322..103543817d2 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -1,7 +1,7 @@ /* ----------------------------------------------------------------------- * formatting.c * - * $PostgreSQL: pgsql/src/backend/utils/adt/formatting.c,v 1.138 2008/03/22 22:32:19 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/formatting.c,v 1.139 2008/03/25 22:42:44 tgl Exp $ * * * Portions Copyright (c) 1999-2008, PostgreSQL Global Development Group @@ -924,7 +924,7 @@ static int seq_search(char *name, char **array, int type, int max, int *len); static void do_to_timestamp(text *date_txt, text *fmt, struct pg_tm * tm, fsec_t *fsec); static char *fill_str(char *str, int c, int max); -static FormatNode *NUM_cache(int len, NUMDesc *Num, char *pars_str, bool *shouldFree); +static FormatNode *NUM_cache(int len, NUMDesc *Num, text *pars_str, bool *shouldFree); static char *int_to_roman(int number); static void NUM_prepare_locale(NUMProc *Np); static char *get_last_relevant_decnum(char *num); @@ -2709,16 +2709,14 @@ datetime_to_char_body(TmToChar *tmtc, text *fmt, bool is_interval) char *fmt_str, *result; bool incache; - int fmt_len = VARSIZE(fmt) - VARHDRSZ; - int reslen; + int fmt_len; text *res; /* * Convert fmt to C string */ - fmt_str = (char *) palloc(fmt_len + 1); - memcpy(fmt_str, VARDATA(fmt), fmt_len); - *(fmt_str + fmt_len) = '\0'; + fmt_str = text_to_cstring(fmt); + fmt_len = strlen(fmt_str); /* * Allocate workspace for result as C string @@ -2779,10 +2777,7 @@ datetime_to_char_body(TmToChar *tmtc, text *fmt, bool is_interval) pfree(fmt_str); /* convert C-string result to TEXT format */ - reslen = strlen(result); - res = (text *) palloc(reslen + VARHDRSZ); - memcpy(VARDATA(res), result, reslen); - SET_VARSIZE(res, reslen + VARHDRSZ); + res = cstring_to_text(result); pfree(result); return res; @@ -3135,18 +3130,15 @@ do_to_timestamp(text *date_txt, text *fmt, ZERO_tm(tm); *fsec = 0; - fmt_len = VARSIZE(fmt) - VARHDRSZ; + fmt_len = VARSIZE_ANY_EXHDR(fmt); if (fmt_len) { - int date_len; char *fmt_str; char *date_str; bool incache; - fmt_str = (char *) palloc(fmt_len + 1); - memcpy(fmt_str, VARDATA(fmt), fmt_len); - *(fmt_str + fmt_len) = '\0'; + fmt_str = text_to_cstring(fmt); /* * Allocate new memory if format picture is bigger than static cache @@ -3195,13 +3187,7 @@ do_to_timestamp(text *date_txt, text *fmt, /* dump_node(format, fmt_len); */ #endif - /* - * Convert date to C string - */ - date_len = VARSIZE(date_txt) - VARHDRSZ; - date_str = (char *) palloc(date_len + 1); - memcpy(date_str, VARDATA(date_txt), date_len); - *(date_str + date_len) = '\0'; + date_str = text_to_cstring(date_txt); DCH_from_char(format, date_str, &tmfc); @@ -3548,17 +3534,12 @@ NUM_cache_remove(NUMCacheEntry *ent) * ---------- */ static FormatNode * -NUM_cache(int len, NUMDesc *Num, char *pars_str, bool *shouldFree) +NUM_cache(int len, NUMDesc *Num, text *pars_str, bool *shouldFree) { FormatNode *format = NULL; char *str; - /* - * Convert VARDATA() to string - */ - str = (char *) palloc(len + 1); - memcpy(str, pars_str, len); - *(str + len) = '\0'; + str = text_to_cstring(pars_str); /* * Allocate new memory if format picture is bigger than static cache and @@ -4597,11 +4578,11 @@ NUM_processor(FormatNode *node, NUMDesc *Num, char *inout, char *number, */ #define NUM_TOCHAR_prepare \ do { \ - len = VARSIZE(fmt) - VARHDRSZ; \ + len = VARSIZE_ANY_EXHDR(fmt); \ if (len <= 0 || len >= (INT_MAX-VARHDRSZ)/NUM_MAX_ITEM_SIZ) \ - return DirectFunctionCall1(textin, CStringGetDatum("")); \ + PG_RETURN_TEXT_P(cstring_to_text("")); \ result = (text *) palloc0((len * NUM_MAX_ITEM_SIZ) + 1 + VARHDRSZ); \ - format = NUM_cache(len, &Num, VARDATA(fmt), &shouldFree); \ + format = NUM_cache(len, &Num, fmt, &shouldFree); \ } while (0) /* ---------- @@ -4647,7 +4628,7 @@ numeric_to_number(PG_FUNCTION_ARGS) if (len <= 0 || len >= INT_MAX / NUM_MAX_ITEM_SIZ) PG_RETURN_NULL(); - format = NUM_cache(len, &Num, VARDATA(fmt), &shouldFree); + format = NUM_cache(len, &Num, fmt, &shouldFree); numstr = (char *) palloc((len * NUM_MAX_ITEM_SIZ) + 1); |