Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * ts_parse.c
4 : : * main parse functions for tsearch
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : *
8 : : *
9 : : * IDENTIFICATION
10 : : * src/backend/tsearch/ts_parse.c
11 : : *
12 : : *-------------------------------------------------------------------------
13 : : */
14 : :
15 : : #include "postgres.h"
16 : :
17 : : #include "tsearch/ts_cache.h"
18 : : #include "tsearch/ts_utils.h"
19 : : #include "varatt.h"
20 : :
21 : : #define IGNORE_LONGLEXEME 1
22 : :
23 : : /*
24 : : * Lexize subsystem
25 : : */
26 : :
27 : : typedef struct ParsedLex
28 : : {
29 : : int type;
30 : : char *lemm;
31 : : int lenlemm;
32 : : struct ParsedLex *next;
33 : : } ParsedLex;
34 : :
35 : : typedef struct ListParsedLex
36 : : {
37 : : ParsedLex *head;
38 : : ParsedLex *tail;
39 : : } ListParsedLex;
40 : :
41 : : typedef struct
42 : : {
43 : : TSConfigCacheEntry *cfg;
44 : : Oid curDictId;
45 : : int posDict;
46 : : DictSubState dictState;
47 : : ParsedLex *curSub;
48 : : ListParsedLex towork; /* current list to work */
49 : : ListParsedLex waste; /* list of lexemes that already lexized */
50 : :
51 : : /*
52 : : * fields to store last variant to lexize (basically, thesaurus or similar
53 : : * to, which wants several lexemes
54 : : */
55 : :
56 : : ParsedLex *lastRes;
57 : : TSLexeme *tmpRes;
58 : : } LexizeData;
59 : :
60 : : static void
61 : 3884 : LexizeInit(LexizeData *ld, TSConfigCacheEntry *cfg)
62 : : {
63 : 3884 : ld->cfg = cfg;
64 : 3884 : ld->curDictId = InvalidOid;
65 : 3884 : ld->posDict = 0;
66 : 3884 : ld->towork.head = ld->towork.tail = ld->curSub = NULL;
67 : 3884 : ld->waste.head = ld->waste.tail = NULL;
68 : 3884 : ld->lastRes = NULL;
69 : 3884 : ld->tmpRes = NULL;
70 : 3884 : }
71 : :
72 : : static void
73 : 45760 : LPLAddTail(ListParsedLex *list, ParsedLex *newpl)
74 : : {
75 [ + + ]: 45760 : if (list->tail)
76 : : {
77 : 185 : list->tail->next = newpl;
78 : 185 : list->tail = newpl;
79 : : }
80 : : else
81 : 45575 : list->head = list->tail = newpl;
82 : 45760 : newpl->next = NULL;
83 : 45760 : }
84 : :
85 : : static ParsedLex *
86 : 22880 : LPLRemoveHead(ListParsedLex *list)
87 : : {
88 : 22880 : ParsedLex *res = list->head;
89 : :
90 [ + - ]: 22880 : if (list->head)
91 : 22880 : list->head = list->head->next;
92 : :
93 [ + + ]: 22880 : if (list->head == NULL)
94 : 22775 : list->tail = NULL;
95 : :
96 : 22880 : return res;
97 : : }
98 : :
99 : : static void
100 : 22880 : LexizeAddLemm(LexizeData *ld, int type, char *lemm, int lenlemm)
101 : : {
102 : 22880 : ParsedLex *newpl = palloc_object(ParsedLex);
103 : :
104 : 22880 : newpl->type = type;
105 : 22880 : newpl->lemm = lemm;
106 : 22880 : newpl->lenlemm = lenlemm;
107 : 22880 : LPLAddTail(&ld->towork, newpl);
108 : 22880 : ld->curSub = ld->towork.tail;
109 : 22880 : }
110 : :
111 : : static void
112 : 22880 : RemoveHead(LexizeData *ld)
113 : : {
114 : 22880 : LPLAddTail(&ld->waste, LPLRemoveHead(&ld->towork));
115 : :
116 : 22880 : ld->posDict = 0;
117 : 22880 : }
118 : :
119 : : static void
120 : 34028 : setCorrLex(LexizeData *ld, ParsedLex **correspondLexem)
121 : : {
122 [ + + ]: 34028 : if (correspondLexem)
123 : : {
124 : 12499 : *correspondLexem = ld->waste.head;
125 : : }
126 : : else
127 : : {
128 : : ParsedLex *tmp,
129 : 21529 : *ptr = ld->waste.head;
130 : :
131 [ + + ]: 36008 : while (ptr)
132 : : {
133 : 14479 : tmp = ptr->next;
134 : 14479 : pfree(ptr);
135 : 14479 : ptr = tmp;
136 : : }
137 : : }
138 : 34028 : ld->waste.head = ld->waste.tail = NULL;
139 : 34028 : }
140 : :
141 : : static void
142 : 40 : moveToWaste(LexizeData *ld, ParsedLex *stop)
143 : : {
144 : 40 : bool go = true;
145 : :
146 [ + + + + ]: 150 : while (ld->towork.head && go)
147 : : {
148 [ + + ]: 110 : if (ld->towork.head == stop)
149 : : {
150 : 40 : ld->curSub = stop->next;
151 : 40 : go = false;
152 : : }
153 : 110 : RemoveHead(ld);
154 : : }
155 : 40 : }
156 : :
157 : : static void
158 : 40 : setNewTmpRes(LexizeData *ld, ParsedLex *lex, TSLexeme *res)
159 : : {
160 [ + + ]: 40 : if (ld->tmpRes)
161 : : {
162 : : TSLexeme *ptr;
163 : :
164 [ + + ]: 20 : for (ptr = ld->tmpRes; ptr->lexeme; ptr++)
165 : 10 : pfree(ptr->lexeme);
166 : 10 : pfree(ld->tmpRes);
167 : : }
168 : 40 : ld->tmpRes = res;
169 : 40 : ld->lastRes = lex;
170 : 40 : }
171 : :
172 : : static TSLexeme *
173 : 34068 : LexizeExec(LexizeData *ld, ParsedLex **correspondLexem)
174 : : {
175 : : int i;
176 : : ListDictionary *map;
177 : : TSDictionaryCacheEntry *dict;
178 : : TSLexeme *res;
179 : :
180 [ + + ]: 34068 : if (ld->curDictId == InvalidOid)
181 : : {
182 : : /*
183 : : * usual mode: dictionary wants only one word, but we should keep in
184 : : * mind that we should go through all stack
185 : : */
186 : :
187 [ + + ]: 45585 : while (ld->towork.head)
188 : : {
189 : 22810 : ParsedLex *curVal = ld->towork.head;
190 : 22810 : char *curValLemm = curVal->lemm;
191 : 22810 : int curValLenLemm = curVal->lenlemm;
192 : :
193 : 22810 : map = ld->cfg->map + curVal->type;
194 : :
195 [ + + + + : 22810 : if (curVal->type == 0 || curVal->type >= ld->cfg->lenmap || map->len == 0)
+ + ]
196 : : {
197 : : /* skip this type of lexeme */
198 : 11662 : RemoveHead(ld);
199 : 11662 : continue;
200 : : }
201 : :
202 [ + - ]: 11558 : for (i = ld->posDict; i < map->len; i++)
203 : : {
204 : 11558 : dict = lookup_ts_dictionary_cache(map->dictIds[i]);
205 : :
206 : 11558 : ld->dictState.isend = ld->dictState.getnext = false;
207 : 11558 : ld->dictState.private_state = NULL;
208 : 11558 : res = (TSLexeme *) DatumGetPointer(FunctionCall4(&(dict->lexize),
209 : : PointerGetDatum(dict->dictData),
210 : : PointerGetDatum(curValLemm),
211 : : Int32GetDatum(curValLenLemm),
212 : : PointerGetDatum(&ld->dictState)));
213 : :
214 [ + + ]: 11558 : if (ld->dictState.getnext)
215 : : {
216 : : /*
217 : : * dictionary wants next word, so setup and store current
218 : : * position and go to multiword mode
219 : : */
220 : :
221 : 40 : ld->curDictId = map->dictIds[i];
222 : 40 : ld->posDict = i + 1;
223 : 40 : ld->curSub = curVal->next;
224 [ + + ]: 40 : if (res)
225 : 30 : setNewTmpRes(ld, curVal, res);
226 : 40 : return LexizeExec(ld, correspondLexem);
227 : : }
228 : :
229 [ + + ]: 11518 : if (!res) /* dictionary doesn't know this lexeme */
230 : 410 : continue;
231 : :
232 [ - + ]: 11108 : if (res->flags & TSL_FILTER)
233 : : {
234 : 0 : curValLemm = res->lexeme;
235 : 0 : curValLenLemm = strlen(res->lexeme);
236 : 0 : continue;
237 : : }
238 : :
239 : 11108 : RemoveHead(ld);
240 : 11108 : setCorrLex(ld, correspondLexem);
241 : 11108 : return res;
242 : : }
243 : :
244 : 0 : RemoveHead(ld);
245 : : }
246 : : }
247 : : else
248 : : { /* curDictId is valid */
249 : 145 : dict = lookup_ts_dictionary_cache(ld->curDictId);
250 : :
251 : : /*
252 : : * Dictionary ld->curDictId asks us about following words
253 : : */
254 : :
255 [ + + ]: 210 : while (ld->curSub)
256 : : {
257 : 105 : ParsedLex *curVal = ld->curSub;
258 : :
259 : 105 : map = ld->cfg->map + curVal->type;
260 : :
261 [ + + ]: 105 : if (curVal->type != 0)
262 : : {
263 : 100 : bool dictExists = false;
264 : :
265 [ + - + + ]: 100 : if (curVal->type >= ld->cfg->lenmap || map->len == 0)
266 : : {
267 : : /* skip this type of lexeme */
268 : 50 : ld->curSub = curVal->next;
269 : 50 : continue;
270 : : }
271 : :
272 : : /*
273 : : * We should be sure that current type of lexeme is recognized
274 : : * by our dictionary: we just check is it exist in list of
275 : : * dictionaries ?
276 : : */
277 [ + - + + ]: 150 : for (i = 0; i < map->len && !dictExists; i++)
278 [ + + ]: 100 : if (ld->curDictId == map->dictIds[i])
279 : 50 : dictExists = true;
280 : :
281 [ - + ]: 50 : if (!dictExists)
282 : : {
283 : : /*
284 : : * Dictionary can't work with current type of lexeme,
285 : : * return to basic mode and redo all stored lexemes
286 : : */
287 : 0 : ld->curDictId = InvalidOid;
288 : 0 : return LexizeExec(ld, correspondLexem);
289 : : }
290 : : }
291 : :
292 : 55 : ld->dictState.isend = (curVal->type == 0);
293 : 55 : ld->dictState.getnext = false;
294 : :
295 : 55 : res = (TSLexeme *) DatumGetPointer(FunctionCall4(&(dict->lexize),
296 : : PointerGetDatum(dict->dictData),
297 : : PointerGetDatum(curVal->lemm),
298 : : Int32GetDatum(curVal->lenlemm),
299 : : PointerGetDatum(&ld->dictState)));
300 : :
301 [ + + ]: 55 : if (ld->dictState.getnext)
302 : : {
303 : : /* Dictionary wants one more */
304 : 15 : ld->curSub = curVal->next;
305 [ + + ]: 15 : if (res)
306 : 10 : setNewTmpRes(ld, curVal, res);
307 : 15 : continue;
308 : : }
309 : :
310 [ + + + - ]: 40 : if (res || ld->tmpRes)
311 : : {
312 : : /*
313 : : * Dictionary normalizes lexemes, so we remove from stack all
314 : : * used lexemes, return to basic mode and redo end of stack
315 : : * (if it exists)
316 : : */
317 [ + + ]: 40 : if (res)
318 : : {
319 : 20 : moveToWaste(ld, ld->curSub);
320 : : }
321 : : else
322 : : {
323 : 20 : res = ld->tmpRes;
324 : 20 : moveToWaste(ld, ld->lastRes);
325 : : }
326 : :
327 : : /* reset to initial state */
328 : 40 : ld->curDictId = InvalidOid;
329 : 40 : ld->posDict = 0;
330 : 40 : ld->lastRes = NULL;
331 : 40 : ld->tmpRes = NULL;
332 : 40 : setCorrLex(ld, correspondLexem);
333 : 40 : return res;
334 : : }
335 : :
336 : : /*
337 : : * Dict don't want next lexem and didn't recognize anything, redo
338 : : * from ld->towork.head
339 : : */
340 : 0 : ld->curDictId = InvalidOid;
341 : 0 : return LexizeExec(ld, correspondLexem);
342 : : }
343 : : }
344 : :
345 : 22880 : setCorrLex(ld, correspondLexem);
346 : 22880 : return NULL;
347 : : }
348 : :
349 : : /*
350 : : * Parse string and lexize words.
351 : : *
352 : : * prs will be filled in.
353 : : */
354 : : void
355 : 3581 : parsetext(Oid cfgId, ParsedText *prs, char *buf, int buflen)
356 : : {
357 : : int type,
358 : 3581 : lenlemm = 0; /* silence compiler warning */
359 : 3581 : char *lemm = NULL;
360 : : LexizeData ldata;
361 : : TSLexeme *norms;
362 : : TSConfigCacheEntry *cfg;
363 : : TSParserCacheEntry *prsobj;
364 : : void *prsdata;
365 : :
366 : 3581 : cfg = lookup_ts_config_cache(cfgId);
367 : 3581 : prsobj = lookup_ts_parser_cache(cfg->prsId);
368 : :
369 : 3581 : prsdata = DatumGetPointer(FunctionCall2(&prsobj->prsstart,
370 : : PointerGetDatum(buf),
371 : : Int32GetDatum(buflen)));
372 : :
373 : 3581 : LexizeInit(&ldata, cfg);
374 : :
375 : : do
376 : : {
377 : 14479 : type = DatumGetInt32(FunctionCall3(&(prsobj->prstoken),
378 : : PointerGetDatum(prsdata),
379 : : PointerGetDatum(&lemm),
380 : : PointerGetDatum(&lenlemm)));
381 : :
382 [ + + - + ]: 14479 : if (type > 0 && lenlemm > MAXSTRLEN)
383 : : {
384 : : #ifdef IGNORE_LONGLEXEME
385 [ # # ]: 0 : ereport(NOTICE,
386 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
387 : : errmsg("word is too long to be indexed"),
388 : : errdetail("Words longer than %d characters are ignored.",
389 : : MAXSTRLEN)));
390 : 0 : continue;
391 : : #else
392 : : ereport(ERROR,
393 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
394 : : errmsg("word is too long to be indexed"),
395 : : errdetail("Words longer than %d characters are ignored.",
396 : : MAXSTRLEN)));
397 : : #endif
398 : : }
399 : :
400 : 14479 : LexizeAddLemm(&ldata, type, lemm, lenlemm);
401 : :
402 [ + + ]: 21529 : while ((norms = LexizeExec(&ldata, NULL)) != NULL)
403 : : {
404 : 7050 : prs->pos++; /* set pos */
405 : :
406 [ + + ]: 13100 : for (TSLexeme *ptr = norms; ptr->lexeme; ptr++)
407 : : {
408 : 6050 : size_t lexeme_len = strlen(ptr->lexeme);
409 : :
410 [ - + ]: 6050 : if (lexeme_len > MAXSTRLEN)
411 : : {
412 : : #ifdef IGNORE_LONGLEXEME
413 [ # # ]: 0 : ereport(NOTICE,
414 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
415 : : errmsg("word is too long to be indexed"),
416 : : errdetail("Words longer than %d characters are ignored.",
417 : : MAXSTRLEN)));
418 : 0 : continue;
419 : : #else
420 : : ereport(ERROR,
421 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
422 : : errmsg("word is too long to be indexed"),
423 : : errdetail("Words longer than %d characters are ignored.",
424 : : MAXSTRLEN)));
425 : : #endif
426 : : }
427 : :
428 [ + + ]: 6050 : if (prs->curwords == prs->lenwords)
429 : : {
430 : 275 : prs->lenwords *= 2;
431 : 275 : prs->words = (ParsedWord *) repalloc(prs->words, prs->lenwords * sizeof(ParsedWord));
432 : : }
433 : :
434 [ + + ]: 6050 : if (ptr->flags & TSL_ADDPOS)
435 : 20 : prs->pos++;
436 : 6050 : prs->words[prs->curwords].len = lexeme_len;
437 : 6050 : prs->words[prs->curwords].word = ptr->lexeme;
438 : 6050 : prs->words[prs->curwords].nvariant = ptr->nvariant;
439 : 6050 : prs->words[prs->curwords].flags = ptr->flags & TSL_PREFIX;
440 : 6050 : prs->words[prs->curwords].alen = 0;
441 : 6050 : prs->words[prs->curwords].pos.pos = LIMITPOS(prs->pos);
442 : 6050 : prs->curwords++;
443 : : }
444 : 7050 : pfree(norms);
445 : : }
446 [ + + ]: 14479 : } while (type > 0);
447 : :
448 : 3581 : FunctionCall1(&(prsobj->prsend), PointerGetDatum(prsdata));
449 : 3581 : }
450 : :
451 : : /*
452 : : * Headline framework
453 : : */
454 : :
455 : : /* Add a word to prs->words[] */
456 : : static void
457 : 8098 : hladdword(HeadlineParsedText *prs, char *buf, int buflen, int type)
458 : : {
459 [ + + ]: 8098 : if (prs->curwords >= prs->lenwords)
460 : : {
461 : 45 : prs->lenwords *= 2;
462 : 45 : prs->words = (HeadlineWordEntry *) repalloc(prs->words, prs->lenwords * sizeof(HeadlineWordEntry));
463 : : }
464 : 8098 : memset(&(prs->words[prs->curwords]), 0, sizeof(HeadlineWordEntry));
465 : 8098 : prs->words[prs->curwords].type = (uint8) type;
466 : 8098 : prs->words[prs->curwords].len = buflen;
467 : 8098 : prs->words[prs->curwords].word = palloc(buflen);
468 : 8098 : memcpy(prs->words[prs->curwords].word, buf, buflen);
469 : 8098 : prs->curwords++;
470 : 8098 : }
471 : :
472 : : /*
473 : : * Add pos and matching-query-item data to the just-added word.
474 : : * Here, buf/buflen represent a processed lexeme, not raw token text.
475 : : *
476 : : * If the query contains more than one matching item, we replicate
477 : : * the last-added word so that each item can be pointed to. The
478 : : * duplicate entries are marked with repeated = 1.
479 : : */
480 : : static void
481 : 2598 : hlfinditem(HeadlineParsedText *prs, TSQuery query, int32 pos, char *buf, int buflen)
482 : : {
483 : : int i;
484 : 2598 : QueryItem *item = GETQUERY(query);
485 : : HeadlineWordEntry *word;
486 : :
487 [ + + ]: 2708 : while (prs->curwords + query->size >= prs->lenwords)
488 : : {
489 : 110 : prs->lenwords *= 2;
490 : 110 : prs->words = (HeadlineWordEntry *) repalloc(prs->words, prs->lenwords * sizeof(HeadlineWordEntry));
491 : : }
492 : :
493 : 2598 : word = &(prs->words[prs->curwords - 1]);
494 : 2598 : word->pos = LIMITPOS(pos);
495 [ + + ]: 12230 : for (i = 0; i < query->size; i++)
496 : : {
497 [ + + + + ]: 15612 : if (item->type == QI_VAL &&
498 : 5980 : tsCompareString(GETOPERAND(query) + item->qoperand.distance, item->qoperand.length,
499 : 5980 : buf, buflen, item->qoperand.prefix) == 0)
500 : : {
501 [ - + ]: 511 : if (word->item)
502 : : {
503 : 0 : memcpy(&(prs->words[prs->curwords]), word, sizeof(HeadlineWordEntry));
504 : 0 : prs->words[prs->curwords].item = &item->qoperand;
505 : 0 : prs->words[prs->curwords].repeated = 1;
506 : 0 : prs->curwords++;
507 : : }
508 : : else
509 : 511 : word->item = &item->qoperand;
510 : : }
511 : 9632 : item++;
512 : : }
513 : 2598 : }
514 : :
515 : : static void
516 : 12499 : addHLParsedLex(HeadlineParsedText *prs, TSQuery query, ParsedLex *lexs, TSLexeme *norms)
517 : : {
518 : : ParsedLex *tmplexs;
519 : : TSLexeme *ptr;
520 : : int32 savedpos;
521 : :
522 [ + + ]: 20900 : while (lexs)
523 : : {
524 [ + + ]: 8401 : if (lexs->type > 0)
525 : 8098 : hladdword(prs, lexs->lemm, lexs->lenlemm, lexs->type);
526 : :
527 : 8401 : ptr = norms;
528 : 8401 : savedpos = prs->vectorpos;
529 [ + + + + ]: 10999 : while (ptr && ptr->lexeme)
530 : : {
531 [ - + ]: 2598 : if (ptr->flags & TSL_ADDPOS)
532 : 0 : savedpos++;
533 : 2598 : hlfinditem(prs, query, savedpos, ptr->lexeme, strlen(ptr->lexeme));
534 : 2598 : ptr++;
535 : : }
536 : :
537 : 8401 : tmplexs = lexs->next;
538 : 8401 : pfree(lexs);
539 : 8401 : lexs = tmplexs;
540 : : }
541 : :
542 [ + + ]: 12499 : if (norms)
543 : : {
544 : 4098 : ptr = norms;
545 [ + + ]: 6696 : while (ptr->lexeme)
546 : : {
547 [ - + ]: 2598 : if (ptr->flags & TSL_ADDPOS)
548 : 0 : prs->vectorpos++;
549 : 2598 : pfree(ptr->lexeme);
550 : 2598 : ptr++;
551 : : }
552 : 4098 : pfree(norms);
553 : : }
554 : 12499 : }
555 : :
556 : : void
557 : 303 : hlparsetext(Oid cfgId, HeadlineParsedText *prs, TSQuery query, char *buf, int buflen)
558 : : {
559 : : int type,
560 : 303 : lenlemm = 0; /* silence compiler warning */
561 : 303 : char *lemm = NULL;
562 : : LexizeData ldata;
563 : : TSLexeme *norms;
564 : : ParsedLex *lexs;
565 : : TSConfigCacheEntry *cfg;
566 : : TSParserCacheEntry *prsobj;
567 : : void *prsdata;
568 : :
569 : 303 : cfg = lookup_ts_config_cache(cfgId);
570 : 303 : prsobj = lookup_ts_parser_cache(cfg->prsId);
571 : :
572 : 303 : prsdata = DatumGetPointer(FunctionCall2(&(prsobj->prsstart),
573 : : PointerGetDatum(buf),
574 : : Int32GetDatum(buflen)));
575 : :
576 : 303 : LexizeInit(&ldata, cfg);
577 : :
578 : : do
579 : : {
580 : 8401 : type = DatumGetInt32(FunctionCall3(&(prsobj->prstoken),
581 : : PointerGetDatum(prsdata),
582 : : PointerGetDatum(&lemm),
583 : : PointerGetDatum(&lenlemm)));
584 : :
585 [ + + - + ]: 8401 : if (type > 0 && lenlemm > MAXSTRLEN)
586 : : {
587 : : #ifdef IGNORE_LONGLEXEME
588 [ # # ]: 0 : ereport(NOTICE,
589 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
590 : : errmsg("word is too long to be indexed"),
591 : : errdetail("Words longer than %d characters are ignored.",
592 : : MAXSTRLEN)));
593 : 0 : continue;
594 : : #else
595 : : ereport(ERROR,
596 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
597 : : errmsg("word is too long to be indexed"),
598 : : errdetail("Words longer than %d characters are ignored.",
599 : : MAXSTRLEN)));
600 : : #endif
601 : : }
602 : :
603 : 8401 : LexizeAddLemm(&ldata, type, lemm, lenlemm);
604 : :
605 : : do
606 : : {
607 [ + + ]: 12499 : if ((norms = LexizeExec(&ldata, &lexs)) != NULL)
608 : : {
609 : 4098 : prs->vectorpos++;
610 : 4098 : addHLParsedLex(prs, query, lexs, norms);
611 : : }
612 : : else
613 : 8401 : addHLParsedLex(prs, query, lexs, NULL);
614 [ + + ]: 12499 : } while (norms);
615 [ + + ]: 8401 : } while (type > 0);
616 : :
617 : 303 : FunctionCall1(&(prsobj->prsend), PointerGetDatum(prsdata));
618 : 303 : }
619 : :
620 : : /*
621 : : * Generate the headline, as a text object, from HeadlineParsedText.
622 : : */
623 : : text *
624 : 291 : generateHeadline(HeadlineParsedText *prs)
625 : : {
626 : : text *out;
627 : : char *ptr;
628 : 291 : int len = 128;
629 : 291 : int numfragments = 0;
630 : 291 : int16 infrag = 0;
631 : :
632 : 291 : HeadlineWordEntry *wrd = prs->words;
633 : :
634 : 291 : out = (text *) palloc(len);
635 : 291 : ptr = ((char *) out) + VARHDRSZ;
636 : :
637 [ + + ]: 8353 : while (wrd - prs->words < prs->curwords)
638 : : {
639 [ + + ]: 8142 : while (wrd->len + prs->stopsellen + prs->startsellen + prs->fragdelimlen + (ptr - ((char *) out)) >= len)
640 : : {
641 : 80 : int dist = ptr - ((char *) out);
642 : :
643 : 80 : len *= 2;
644 : 80 : out = (text *) repalloc(out, len);
645 : 80 : ptr = ((char *) out) + dist;
646 : : }
647 : :
648 [ + + + - ]: 8062 : if (wrd->in && !wrd->repeated)
649 : : {
650 [ + + ]: 4312 : if (!infrag)
651 : : {
652 : :
653 : : /* start of a new fragment */
654 : 296 : infrag = 1;
655 : 296 : numfragments++;
656 : : /* add a fragment delimiter if this is after the first one */
657 [ + + ]: 296 : if (numfragments > 1)
658 : : {
659 : 10 : memcpy(ptr, prs->fragdelim, prs->fragdelimlen);
660 : 10 : ptr += prs->fragdelimlen;
661 : : }
662 : : }
663 [ - + ]: 4312 : if (wrd->replace)
664 : : {
665 : 0 : *ptr = ' ';
666 : 0 : ptr++;
667 : : }
668 [ + + ]: 4312 : else if (!wrd->skip)
669 : : {
670 [ + + ]: 4307 : if (wrd->selected)
671 : : {
672 : 404 : memcpy(ptr, prs->startsel, prs->startsellen);
673 : 404 : ptr += prs->startsellen;
674 : : }
675 : 4307 : memcpy(ptr, wrd->word, wrd->len);
676 : 4307 : ptr += wrd->len;
677 [ + + ]: 4307 : if (wrd->selected)
678 : : {
679 : 404 : memcpy(ptr, prs->stopsel, prs->stopsellen);
680 : 404 : ptr += prs->stopsellen;
681 : : }
682 : : }
683 : : }
684 [ + - ]: 3750 : else if (!wrd->repeated)
685 : : {
686 [ + + ]: 3750 : if (infrag)
687 : 100 : infrag = 0;
688 : 3750 : pfree(wrd->word);
689 : : }
690 : :
691 : 8062 : wrd++;
692 : : }
693 : :
694 : 291 : SET_VARSIZE(out, ptr - ((char *) out));
695 : 291 : return out;
696 : : }
|