1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-27 12:41:57 +03:00

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
This commit is contained in:
Tom Lane
2008-03-25 22:42:46 +00:00
parent f948197b40
commit 220db7ccd8
94 changed files with 771 additions and 1211 deletions

View File

@ -1,7 +1,7 @@
/*
* This is a port of the Double Metaphone algorithm for use in PostgreSQL.
*
* $PostgreSQL: pgsql/contrib/fuzzystrmatch/dmetaphone.c,v 1.11 2007/02/27 23:48:05 tgl Exp $
* $PostgreSQL: pgsql/contrib/fuzzystrmatch/dmetaphone.c,v 1.12 2008/03/25 22:42:41 tgl Exp $
*
* Double Metaphone computes 2 "sounds like" strings - a primary and an
* alternate. In most cases they are the same, but for foreign names
@ -101,7 +101,7 @@ The remaining code is authored by Andrew Dunstan <amdunstan@ncshp.org> and
#include "postgres.h"
#include "fmgr.h"
#include "utils/builtins.h"
/* turn off assertions for embedded function */
#define NDEBUG
@ -118,14 +118,12 @@ extern Datum dmetaphone(PG_FUNCTION_ARGS);
extern Datum dmetaphone_alt(PG_FUNCTION_ARGS);
/* prototype for the main function we got from the perl module */
static void
DoubleMetaphone(char *, char **);
static void DoubleMetaphone(char *, char **);
#ifndef DMETAPHONE_MAIN
/*
* The PostgreSQL visible dmetaphone function.
*
*/
PG_FUNCTION_INFO_V1(dmetaphone);
@ -133,47 +131,28 @@ PG_FUNCTION_INFO_V1(dmetaphone);
Datum
dmetaphone(PG_FUNCTION_ARGS)
{
text *arg,
*result;
int alen,
rsize;
text *arg;
char *aptr,
*codes[2],
*code,
*rptr;
*code;
#ifdef DMETAPHONE_NOSTRICT
if (PG_ARGISNULL(0))
PG_RETURNNULL();
PG_RETURN_NULL();
#endif
arg = PG_GETARG_TEXT_P(0);
alen = VARSIZE(arg) - VARHDRSZ;
aptr = text_to_cstring(arg);
/*
* Postgres' string values might not have trailing nuls. The VARSIZE will
* not include the nul in any case so we copy things out and add a
* trailing nul. When we copy back we ignore the nul (and we don't make
* space for it).
*/
aptr = palloc(alen + 1);
memcpy(aptr, VARDATA(arg), alen);
aptr[alen] = 0;
DoubleMetaphone(aptr, codes);
code = codes[0];
if (!code)
code = "";
rsize = VARHDRSZ + strlen(code);
result = (text *) palloc(rsize);
rptr = VARDATA(result);
memcpy(rptr, code, rsize - VARHDRSZ);
SET_VARSIZE(result, rsize);
PG_RETURN_TEXT_P(result);
PG_RETURN_TEXT_P(cstring_to_text(code));
}
/*
* The PostgreSQL visible dmetaphone_alt function.
*
*/
PG_FUNCTION_INFO_V1(dmetaphone_alt);
@ -181,34 +160,24 @@ PG_FUNCTION_INFO_V1(dmetaphone_alt);
Datum
dmetaphone_alt(PG_FUNCTION_ARGS)
{
text *arg,
*result;
int alen,
rsize;
text *arg;
char *aptr,
*codes[2],
*code,
*rptr;
*code;
#ifdef DMETAPHONE_NOSTRICT
if (PG_ARGISNULL(0))
PG_RETURNNULL();
PG_RETURN_NULL();
#endif
arg = PG_GETARG_TEXT_P(0);
alen = VARSIZE(arg) - VARHDRSZ;
aptr = palloc(alen + 1);
memcpy(aptr, VARDATA(arg), alen);
aptr[alen] = 0;
aptr = text_to_cstring(arg);
DoubleMetaphone(aptr, codes);
code = codes[1];
if (!code)
code = "";
rsize = VARHDRSZ + strlen(code);
result = (text *) palloc(rsize);
rptr = VARDATA(result);
memcpy(rptr, code, rsize - VARHDRSZ);
SET_VARSIZE(result, rsize);
PG_RETURN_TEXT_P(result);
PG_RETURN_TEXT_P(cstring_to_text(code));
}

View File

@ -5,7 +5,7 @@
*
* Joe Conway <mail@joeconway.com>
*
* $PostgreSQL: pgsql/contrib/fuzzystrmatch/fuzzystrmatch.c,v 1.25 2008/01/01 19:45:45 momjian Exp $
* $PostgreSQL: pgsql/contrib/fuzzystrmatch/fuzzystrmatch.c,v 1.26 2008/03/25 22:42:41 tgl Exp $
* Copyright (c) 2001-2008, PostgreSQL Global Development Group
* ALL RIGHTS RESERVED;
*
@ -56,11 +56,11 @@ PG_FUNCTION_INFO_V1(levenshtein);
Datum
levenshtein(PG_FUNCTION_ARGS)
{
char *str_s;
char *str_s = TextDatumGetCString(PG_GETARG_DATUM(0));
char *str_t = TextDatumGetCString(PG_GETARG_DATUM(1));
int cols = strlen(str_s) + 1;
int rows = strlen(str_t) + 1;
char *str_s0;
char *str_t;
int cols = 0;
int rows = 0;
int *u_cells;
int *l_cells;
int *tmp;
@ -68,16 +68,10 @@ levenshtein(PG_FUNCTION_ARGS)
int j;
/*
* Fetch the arguments. str_s is referred to as the "source" cols = length
* of source + 1 to allow for the initialization column str_t is referred
* to as the "target", rows = length of target + 1 rows = length of target
* + 1 to allow for the initialization row
* str_s is referred to as the "source", str_t is referred to as the
* "target", cols = length of source + 1 to allow for the initialization
* column, rows = length of target + 1 to allow for the initialization row
*/
str_s = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(PG_GETARG_TEXT_P(0))));
str_t = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(PG_GETARG_TEXT_P(1))));
cols = strlen(str_s) + 1;
rows = strlen(str_t) + 1;
/*
* Restrict the length of the strings being compared to something
@ -201,25 +195,19 @@ levenshtein(PG_FUNCTION_ARGS)
* Returns number of characters requested
* (suggested value is 4)
*/
#define GET_TEXT(cstrp) DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(cstrp)))
PG_FUNCTION_INFO_V1(metaphone);
Datum
metaphone(PG_FUNCTION_ARGS)
{
char *str_i = TextDatumGetCString(PG_GETARG_DATUM(0));
size_t str_i_len = strlen(str_i);
int reqlen;
char *str_i;
size_t str_i_len;
char *metaph;
text *result_text;
int retval;
str_i = DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(PG_GETARG_TEXT_P(0))));
str_i_len = strlen(str_i);
/* return an empty string if we receive one */
if (!(str_i_len > 0))
PG_RETURN_TEXT_P(GET_TEXT(""));
PG_RETURN_TEXT_P(cstring_to_text(""));
if (str_i_len > MAX_METAPHONE_STRLEN)
ereport(ERROR,
@ -247,18 +235,12 @@ metaphone(PG_FUNCTION_ARGS)
retval = _metaphone(str_i, reqlen, &metaph);
if (retval == META_SUCCESS)
{
result_text = DatumGetTextP(DirectFunctionCall1(textin, CStringGetDatum(metaph)));
PG_RETURN_TEXT_P(result_text);
}
PG_RETURN_TEXT_P(cstring_to_text(metaph));
else
{
/* internal error */
elog(ERROR, "metaphone: failure");
/*
* Keep the compiler quiet
*/
/* keep the compiler quiet */
PG_RETURN_NULL();
}
}
@ -695,11 +677,11 @@ soundex(PG_FUNCTION_ARGS)
char outstr[SOUNDEX_LEN + 1];
char *arg;
arg = _textout(PG_GETARG_TEXT_P(0));
arg = text_to_cstring(PG_GETARG_TEXT_P(0));
_soundex(arg, outstr);
PG_RETURN_TEXT_P(_textin(outstr));
PG_RETURN_TEXT_P(cstring_to_text(outstr));
}
static void
@ -761,8 +743,8 @@ difference(PG_FUNCTION_ARGS)
int i,
result;
_soundex(_textout(PG_GETARG_TEXT_P(0)), sndx1);
_soundex(_textout(PG_GETARG_TEXT_P(1)), sndx2);
_soundex(text_to_cstring(PG_GETARG_TEXT_P(0)), sndx1);
_soundex(text_to_cstring(PG_GETARG_TEXT_P(1)), sndx2);
result = 0;
for (i = 0; i < SOUNDEX_LEN; i++)

View File

@ -5,7 +5,7 @@
*
* Joe Conway <mail@joeconway.com>
*
* $PostgreSQL: pgsql/contrib/fuzzystrmatch/fuzzystrmatch.h,v 1.16 2008/01/01 19:45:45 momjian Exp $
* $PostgreSQL: pgsql/contrib/fuzzystrmatch/fuzzystrmatch.h,v 1.17 2008/03/25 22:42:41 tgl Exp $
* Copyright (c) 2001-2008, PostgreSQL Global Development Group
* ALL RIGHTS RESERVED;
*
@ -69,8 +69,6 @@ extern Datum difference(PG_FUNCTION_ARGS);
static void _soundex(const char *instr, char *outstr);
#define SOUNDEX_LEN 4
#define _textin(str) DirectFunctionCall1(textin, CStringGetDatum(str))
#define _textout(str) DatumGetPointer(DirectFunctionCall1(textout, PointerGetDatum(str)))
/* ABCDEFGHIJKLMNOPQRSTUVWXYZ */
static const char *soundex_table = "01230120022455012623010202";