Fix xslt_process() to ensure that it inserts a NULL terminator after the
[PostgreSQL.git] / src / backend / tsearch / dict.c
blobaff8f6bb60e6283a177fa35f4a533b2745388740
1 /*-------------------------------------------------------------------------
3 * dict.c
4 * Standard interface to dictionary
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
9 * IDENTIFICATION
10 * $PostgreSQL$
12 *-------------------------------------------------------------------------
14 #include "postgres.h"
16 #include "catalog/pg_type.h"
17 #include "tsearch/ts_cache.h"
18 #include "tsearch/ts_utils.h"
19 #include "utils/builtins.h"
23 * Lexize one word by dictionary, mostly debug function
25 Datum
26 ts_lexize(PG_FUNCTION_ARGS)
28 Oid dictId = PG_GETARG_OID(0);
29 text *in = PG_GETARG_TEXT_P(1);
30 ArrayType *a;
31 TSDictionaryCacheEntry *dict;
32 TSLexeme *res,
33 *ptr;
34 Datum *da;
35 DictSubState dstate = {false, false, NULL};
37 dict = lookup_ts_dictionary_cache(dictId);
39 res = (TSLexeme *) DatumGetPointer(FunctionCall4(&dict->lexize,
40 PointerGetDatum(dict->dictData),
41 PointerGetDatum(VARDATA(in)),
42 Int32GetDatum(VARSIZE(in) - VARHDRSZ),
43 PointerGetDatum(&dstate)));
45 if (dstate.getnext)
47 dstate.isend = true;
48 ptr = (TSLexeme *) DatumGetPointer(FunctionCall4(&dict->lexize,
49 PointerGetDatum(dict->dictData),
50 PointerGetDatum(VARDATA(in)),
51 Int32GetDatum(VARSIZE(in) - VARHDRSZ),
52 PointerGetDatum(&dstate)));
53 if (ptr != NULL)
54 res = ptr;
57 if (!res)
58 PG_RETURN_NULL();
60 ptr = res;
61 while (ptr->lexeme)
62 ptr++;
63 da = (Datum *) palloc(sizeof(Datum) * (ptr - res));
64 ptr = res;
65 while (ptr->lexeme)
67 da[ptr - res] = CStringGetTextDatum(ptr->lexeme);
68 ptr++;
71 a = construct_array(da,
72 ptr - res,
73 TEXTOID,
74 -1,
75 false,
76 'i');
78 ptr = res;
79 while (ptr->lexeme)
81 pfree(DatumGetPointer(da[ptr - res]));
82 pfree(ptr->lexeme);
83 ptr++;
85 pfree(res);
86 pfree(da);
88 PG_RETURN_POINTER(a);