Debian Codegraph @

contrib/dict_int/dict_int.c

raw
1 /*-------------------------------------------------------------------------
2 *
3 * dict_int.c
4 * Text search dictionary for integers
5 *
6 * Copyright (c) 2007-2025, PostgreSQL Global Development Group
7 *
8 * IDENTIFICATION
9 * contrib/dict_int/dict_int.c
10 *
11 *-------------------------------------------------------------------------
12 */
13 #include "postgres.h"
14
15 #include "commands/defrem.h"
16 #include "tsearch/ts_public.h"
17
18 PG_MODULE_MAGIC_EXT(
19 .name = "dict_int",
20 .version = PG_VERSION
21 );
22
23 typedef struct
24 {
25 int maxlen;
26 bool rejectlong;
27 bool absval;
28 } DictInt;
29
30
31 PG_FUNCTION_INFO_V1(dintdict_init);
32 PG_FUNCTION_INFO_V1(dintdict_lexize);
33
34 Datum
35 dintdict_init(PG_FUNCTION_ARGS)
36 {
37 List *dictoptions = (List *) PG_GETARG_POINTER(0);
38 DictInt *d;
39 ListCell *l;
40
41 d = (DictInt *) palloc0(sizeof(DictInt));
42 d->maxlen = 6;
43 d->rejectlong = false;
44 d->absval = false;
45
46 foreach(l, dictoptions)
47 {
48 DefElem *defel = (DefElem *) lfirst(l);
49
50 if (strcmp(defel->defname, "maxlen") == 0)
51 {
52 d->maxlen = atoi(defGetString(defel));
53
54 if (d->maxlen < 1)
55 ereport(ERROR,
56 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
57 errmsg("maxlen value has to be >= 1")));
58 }
59 else if (strcmp(defel->defname, "rejectlong") == 0)
60 {
61 d->rejectlong = defGetBoolean(defel);
62 }
63 else if (strcmp(defel->defname, "absval") == 0)
64 {
65 d->absval = defGetBoolean(defel);
66 }
67 else
68 {
69 ereport(ERROR,
70 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
71 errmsg("unrecognized intdict parameter: \"%s\"",
72 defel->defname)));
73 }
74 }
75
76 PG_RETURN_POINTER(d);
77 }
78
79 Datum
80 dintdict_lexize(PG_FUNCTION_ARGS)
81 {
82 DictInt *d = (DictInt *) PG_GETARG_POINTER(0);
83 char *in = (char *) PG_GETARG_POINTER(1);
84 int len = PG_GETARG_INT32(2);
85 char *txt;
86 TSLexeme *res = palloc0(sizeof(TSLexeme) * 2);
87
88 res[1].lexeme = NULL;
89
90 if (d->absval && (in[0] == '+' || in[0] == '-'))
91 {
92 len--;
93 txt = pnstrdup(in + 1, len);
94 }
95 else
96 txt = pnstrdup(in, len);
97
98 if (len > d->maxlen)
99 {
100 if (d->rejectlong)
101 {
102 /* reject by returning void array */
103 pfree(txt);
104 res[0].lexeme = NULL;
105 }
106 else
107 {
108 /* trim integer */
109 txt[d->maxlen] = '\0';
110 res[0].lexeme = txt;
111 }
112 }
113 else
114 {
115 res[0].lexeme = txt;
116 }
117
118 PG_RETURN_POINTER(res);
119 }