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 : 784 : LexizeInit(LexizeData *ld, TSConfigCacheEntry *cfg)
62 : : {
63 : 784 : ld->cfg = cfg;
64 : 784 : ld->curDictId = InvalidOid;
65 : 784 : ld->posDict = 0;
66 : 784 : ld->towork.head = ld->towork.tail = ld->curSub = NULL;
67 : 784 : ld->waste.head = ld->waste.tail = NULL;
68 : 784 : ld->lastRes = NULL;
69 : 784 : ld->tmpRes = NULL;
70 : 784 : }
71 : :
72 : : static void
73 : 9206 : LPLAddTail(ListParsedLex *list, ParsedLex *newpl)
74 : : {
75 [ + + ]: 9206 : if (list->tail)
76 : : {
77 : 37 : list->tail->next = newpl;
78 : 37 : list->tail = newpl;
79 : 37 : }
80 : : else
81 : 9169 : list->head = list->tail = newpl;
82 : 9206 : newpl->next = NULL;
83 : 9206 : }
84 : :
85 : : static ParsedLex *
86 : 4603 : LPLRemoveHead(ListParsedLex *list)
87 : : {
88 : 4603 : ParsedLex *res = list->head;
89 : :
90 [ - + ]: 4603 : if (list->head)
91 : 4603 : list->head = list->head->next;
92 : :
93 [ + + ]: 4603 : if (list->head == NULL)
94 : 4582 : list->tail = NULL;
95 : :
96 : 9206 : return res;
97 : 4603 : }
98 : :
99 : : static void
100 : 4603 : LexizeAddLemm(LexizeData *ld, int type, char *lemm, int lenlemm)
101 : : {
102 : 4603 : ParsedLex *newpl = palloc_object(ParsedLex);
103 : :
104 : 4603 : newpl->type = type;
105 : 4603 : newpl->lemm = lemm;
106 : 4603 : newpl->lenlemm = lenlemm;
107 : 4603 : LPLAddTail(&ld->towork, newpl);
108 : 4603 : ld->curSub = ld->towork.tail;
109 : 4603 : }
110 : :
111 : : static void
112 : 4603 : RemoveHead(LexizeData *ld)
113 : : {
114 : 4603 : LPLAddTail(&ld->waste, LPLRemoveHead(&ld->towork));
115 : :
116 : 4603 : ld->posDict = 0;
117 : 4603 : }
118 : :
119 : : static void
120 : 6845 : setCorrLex(LexizeData *ld, ParsedLex **correspondLexem)
121 : : {
122 [ + + ]: 6845 : if (correspondLexem)
123 : : {
124 : 2507 : *correspondLexem = ld->waste.head;
125 : 2507 : }
126 : : else
127 : : {
128 : 4338 : ParsedLex *tmp,
129 : 4338 : *ptr = ld->waste.head;
130 : :
131 [ + + ]: 7256 : while (ptr)
132 : : {
133 : 2918 : tmp = ptr->next;
134 : 2918 : pfree(ptr);
135 : 2918 : ptr = tmp;
136 : : }
137 : 4338 : }
138 : 6845 : ld->waste.head = ld->waste.tail = NULL;
139 : 6845 : }
140 : :
141 : : static void
142 : 8 : moveToWaste(LexizeData *ld, ParsedLex *stop)
143 : : {
144 : 8 : bool go = true;
145 : :
146 [ + + + + ]: 30 : while (ld->towork.head && go)
147 : : {
148 [ + + ]: 22 : if (ld->towork.head == stop)
149 : : {
150 : 8 : ld->curSub = stop->next;
151 : 8 : go = false;
152 : 8 : }
153 : 22 : RemoveHead(ld);
154 : : }
155 : 8 : }
156 : :
157 : : static void
158 : 8 : setNewTmpRes(LexizeData *ld, ParsedLex *lex, TSLexeme *res)
159 : : {
160 [ + + ]: 8 : if (ld->tmpRes)
161 : : {
162 : 2 : TSLexeme *ptr;
163 : :
164 [ + + ]: 4 : for (ptr = ld->tmpRes; ptr->lexeme; ptr++)
165 : 2 : pfree(ptr->lexeme);
166 : 2 : pfree(ld->tmpRes);
167 : 2 : }
168 : 8 : ld->tmpRes = res;
169 : 8 : ld->lastRes = lex;
170 : 8 : }
171 : :
172 : : static TSLexeme *
173 : 6853 : LexizeExec(LexizeData *ld, ParsedLex **correspondLexem)
174 : : {
175 : 6853 : int i;
176 : 6853 : ListDictionary *map;
177 : 6853 : TSDictionaryCacheEntry *dict;
178 : 6853 : TSLexeme *res;
179 : :
180 [ + + ]: 6853 : 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 [ + + ]: 9171 : while (ld->towork.head)
188 : : {
189 : 4589 : ParsedLex *curVal = ld->towork.head;
190 : 4589 : char *curValLemm = curVal->lemm;
191 : 4589 : int curValLenLemm = curVal->lenlemm;
192 : :
193 : 4589 : map = ld->cfg->map + curVal->type;
194 : :
195 [ + + + + : 4589 : if (curVal->type == 0 || curVal->type >= ld->cfg->lenmap || map->len == 0)
+ + ]
196 : : {
197 : : /* skip this type of lexeme */
198 : 2347 : RemoveHead(ld);
199 : 2347 : continue;
200 : : }
201 : :
202 [ + - ]: 2324 : for (i = ld->posDict; i < map->len; i++)
203 : : {
204 : 2324 : dict = lookup_ts_dictionary_cache(map->dictIds[i]);
205 : :
206 : 2324 : ld->dictState.isend = ld->dictState.getnext = false;
207 : 2324 : ld->dictState.private_state = NULL;
208 : 2324 : res = (TSLexeme *) DatumGetPointer(FunctionCall4(&(dict->lexize),
209 : : PointerGetDatum(dict->dictData),
210 : : PointerGetDatum(curValLemm),
211 : : Int32GetDatum(curValLenLemm),
212 : : PointerGetDatum(&ld->dictState)));
213 : :
214 [ + + ]: 2324 : 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 : 8 : ld->curDictId = map->dictIds[i];
222 : 8 : ld->posDict = i + 1;
223 : 8 : ld->curSub = curVal->next;
224 [ + + ]: 8 : if (res)
225 : 6 : setNewTmpRes(ld, curVal, res);
226 : 8 : return LexizeExec(ld, correspondLexem);
227 : : }
228 : :
229 [ + + ]: 2316 : if (!res) /* dictionary doesn't know this lexeme */
230 : 82 : continue;
231 : :
232 [ - + ]: 2234 : if (res->flags & TSL_FILTER)
233 : : {
234 : 0 : curValLemm = res->lexeme;
235 : 0 : curValLenLemm = strlen(res->lexeme);
236 : 0 : continue;
237 : : }
238 : :
239 : 2234 : RemoveHead(ld);
240 : 2234 : setCorrLex(ld, correspondLexem);
241 : 2234 : return res;
242 : : }
243 : :
244 : 0 : RemoveHead(ld);
245 [ + + - ]: 4589 : }
246 : 4582 : }
247 : : else
248 : : { /* curDictId is valid */
249 : 29 : dict = lookup_ts_dictionary_cache(ld->curDictId);
250 : :
251 : : /*
252 : : * Dictionary ld->curDictId asks us about following words
253 : : */
254 : :
255 [ + + ]: 42 : while (ld->curSub)
256 : : {
257 : 21 : ParsedLex *curVal = ld->curSub;
258 : :
259 : 21 : map = ld->cfg->map + curVal->type;
260 : :
261 [ + + ]: 21 : if (curVal->type != 0)
262 : : {
263 : 20 : bool dictExists = false;
264 : :
265 [ + - + + ]: 20 : if (curVal->type >= ld->cfg->lenmap || map->len == 0)
266 : : {
267 : : /* skip this type of lexeme */
268 : 10 : ld->curSub = curVal->next;
269 : 10 : 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 [ - + + + ]: 30 : for (i = 0; i < map->len && !dictExists; i++)
278 [ + + ]: 30 : if (ld->curDictId == map->dictIds[i])
279 : 10 : dictExists = true;
280 : :
281 [ - + ]: 10 : 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 [ + + ]: 20 : }
291 : :
292 : 11 : ld->dictState.isend = (curVal->type == 0);
293 : 11 : ld->dictState.getnext = false;
294 : :
295 : 11 : res = (TSLexeme *) DatumGetPointer(FunctionCall4(&(dict->lexize),
296 : : PointerGetDatum(dict->dictData),
297 : : PointerGetDatum(curVal->lemm),
298 : : Int32GetDatum(curVal->lenlemm),
299 : : PointerGetDatum(&ld->dictState)));
300 : :
301 [ + + ]: 11 : if (ld->dictState.getnext)
302 : : {
303 : : /* Dictionary wants one more */
304 : 3 : ld->curSub = curVal->next;
305 [ + + ]: 3 : if (res)
306 : 2 : setNewTmpRes(ld, curVal, res);
307 : 3 : continue;
308 : : }
309 : :
310 [ + + + - ]: 8 : 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 [ + + ]: 8 : if (res)
318 : : {
319 : 4 : moveToWaste(ld, ld->curSub);
320 : 4 : }
321 : : else
322 : : {
323 : 4 : res = ld->tmpRes;
324 : 4 : moveToWaste(ld, ld->lastRes);
325 : : }
326 : :
327 : : /* reset to initial state */
328 : 8 : ld->curDictId = InvalidOid;
329 : 8 : ld->posDict = 0;
330 : 8 : ld->lastRes = NULL;
331 : 8 : ld->tmpRes = NULL;
332 : 8 : setCorrLex(ld, correspondLexem);
333 : 8 : 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 [ + + ]: 21 : }
343 : : }
344 : :
345 : 4603 : setCorrLex(ld, correspondLexem);
346 : 4603 : return NULL;
347 : 6853 : }
348 : :
349 : : /*
350 : : * Parse string and lexize words.
351 : : *
352 : : * prs will be filled in.
353 : : */
354 : : void
355 : 722 : parsetext(Oid cfgId, ParsedText *prs, char *buf, int buflen)
356 : : {
357 : 722 : int type,
358 : 722 : lenlemm = 0; /* silence compiler warning */
359 : 722 : char *lemm = NULL;
360 : 722 : LexizeData ldata;
361 : 722 : TSLexeme *norms;
362 : 722 : TSConfigCacheEntry *cfg;
363 : 722 : TSParserCacheEntry *prsobj;
364 : 722 : void *prsdata;
365 : :
366 : 722 : cfg = lookup_ts_config_cache(cfgId);
367 : 722 : prsobj = lookup_ts_parser_cache(cfg->prsId);
368 : :
369 : 722 : prsdata = DatumGetPointer(FunctionCall2(&prsobj->prsstart,
370 : : PointerGetDatum(buf),
371 : : Int32GetDatum(buflen)));
372 : :
373 : 722 : LexizeInit(&ldata, cfg);
374 : :
375 : 722 : do
376 : : {
377 : 2918 : type = DatumGetInt32(FunctionCall3(&(prsobj->prstoken),
378 : : PointerGetDatum(prsdata),
379 : : PointerGetDatum(&lemm),
380 : : PointerGetDatum(&lenlemm)));
381 : :
382 [ + + + - ]: 2918 : 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 : 2918 : LexizeAddLemm(&ldata, type, lemm, lenlemm);
401 : :
402 [ + + ]: 4338 : while ((norms = LexizeExec(&ldata, NULL)) != NULL)
403 : : {
404 : 1420 : TSLexeme *ptr = norms;
405 : :
406 : 1420 : prs->pos++; /* set pos */
407 : :
408 [ + + ]: 2639 : while (ptr->lexeme)
409 : : {
410 [ + + ]: 1219 : if (prs->curwords == prs->lenwords)
411 : : {
412 : 55 : prs->lenwords *= 2;
413 : 55 : prs->words = (ParsedWord *) repalloc(prs->words, prs->lenwords * sizeof(ParsedWord));
414 : 55 : }
415 : :
416 [ + + ]: 1219 : if (ptr->flags & TSL_ADDPOS)
417 : 4 : prs->pos++;
418 : 1219 : prs->words[prs->curwords].len = strlen(ptr->lexeme);
419 : 1219 : prs->words[prs->curwords].word = ptr->lexeme;
420 : 1219 : prs->words[prs->curwords].nvariant = ptr->nvariant;
421 : 1219 : prs->words[prs->curwords].flags = ptr->flags & TSL_PREFIX;
422 : 1219 : prs->words[prs->curwords].alen = 0;
423 [ - + ]: 1219 : prs->words[prs->curwords].pos.pos = LIMITPOS(prs->pos);
424 : 1219 : ptr++;
425 : 1219 : prs->curwords++;
426 : : }
427 : 1420 : pfree(norms);
428 : 1420 : }
429 [ + + ]: 2918 : } while (type > 0);
430 : :
431 : 722 : FunctionCall1(&(prsobj->prsend), PointerGetDatum(prsdata));
432 : 722 : }
433 : :
434 : : /*
435 : : * Headline framework
436 : : */
437 : :
438 : : /* Add a word to prs->words[] */
439 : : static void
440 : 1623 : hladdword(HeadlineParsedText *prs, char *buf, int buflen, int type)
441 : : {
442 [ + + ]: 1623 : if (prs->curwords >= prs->lenwords)
443 : : {
444 : 9 : prs->lenwords *= 2;
445 : 9 : prs->words = (HeadlineWordEntry *) repalloc(prs->words, prs->lenwords * sizeof(HeadlineWordEntry));
446 : 9 : }
447 : 1623 : memset(&(prs->words[prs->curwords]), 0, sizeof(HeadlineWordEntry));
448 : 1623 : prs->words[prs->curwords].type = (uint8) type;
449 : 1623 : prs->words[prs->curwords].len = buflen;
450 : 1623 : prs->words[prs->curwords].word = palloc(buflen);
451 : 1623 : memcpy(prs->words[prs->curwords].word, buf, buflen);
452 : 1623 : prs->curwords++;
453 : 1623 : }
454 : :
455 : : /*
456 : : * Add pos and matching-query-item data to the just-added word.
457 : : * Here, buf/buflen represent a processed lexeme, not raw token text.
458 : : *
459 : : * If the query contains more than one matching item, we replicate
460 : : * the last-added word so that each item can be pointed to. The
461 : : * duplicate entries are marked with repeated = 1.
462 : : */
463 : : static void
464 : 522 : hlfinditem(HeadlineParsedText *prs, TSQuery query, int32 pos, char *buf, int buflen)
465 : : {
466 : 522 : int i;
467 : 522 : QueryItem *item = GETQUERY(query);
468 : 522 : HeadlineWordEntry *word;
469 : :
470 [ + + ]: 544 : while (prs->curwords + query->size >= prs->lenwords)
471 : : {
472 : 22 : prs->lenwords *= 2;
473 : 22 : prs->words = (HeadlineWordEntry *) repalloc(prs->words, prs->lenwords * sizeof(HeadlineWordEntry));
474 : : }
475 : :
476 : 522 : word = &(prs->words[prs->curwords - 1]);
477 [ - + ]: 522 : word->pos = LIMITPOS(pos);
478 [ + + ]: 2486 : for (i = 0; i < query->size; i++)
479 : : {
480 [ + + + + ]: 1964 : if (item->type == QI_VAL &&
481 : 2432 : tsCompareString(GETOPERAND(query) + item->qoperand.distance, item->qoperand.length,
482 : 2432 : buf, buflen, item->qoperand.prefix) == 0)
483 : : {
484 [ - + ]: 102 : if (word->item)
485 : : {
486 : 0 : memcpy(&(prs->words[prs->curwords]), word, sizeof(HeadlineWordEntry));
487 : 0 : prs->words[prs->curwords].item = &item->qoperand;
488 : 0 : prs->words[prs->curwords].repeated = 1;
489 : 0 : prs->curwords++;
490 : 0 : }
491 : : else
492 : 102 : word->item = &item->qoperand;
493 : 102 : }
494 : 1964 : item++;
495 : 1964 : }
496 : 522 : }
497 : :
498 : : static void
499 : 2507 : addHLParsedLex(HeadlineParsedText *prs, TSQuery query, ParsedLex *lexs, TSLexeme *norms)
500 : : {
501 : 2507 : ParsedLex *tmplexs;
502 : 2507 : TSLexeme *ptr;
503 : 2507 : int32 savedpos;
504 : :
505 [ + + ]: 4192 : while (lexs)
506 : : {
507 [ + + ]: 1685 : if (lexs->type > 0)
508 : 1623 : hladdword(prs, lexs->lemm, lexs->lenlemm, lexs->type);
509 : :
510 : 1685 : ptr = norms;
511 : 1685 : savedpos = prs->vectorpos;
512 [ + + + + ]: 2207 : while (ptr && ptr->lexeme)
513 : : {
514 [ + - ]: 522 : if (ptr->flags & TSL_ADDPOS)
515 : 0 : savedpos++;
516 : 522 : hlfinditem(prs, query, savedpos, ptr->lexeme, strlen(ptr->lexeme));
517 : 522 : ptr++;
518 : : }
519 : :
520 : 1685 : tmplexs = lexs->next;
521 : 1685 : pfree(lexs);
522 : 1685 : lexs = tmplexs;
523 : : }
524 : :
525 [ + + ]: 2507 : if (norms)
526 : : {
527 : 822 : ptr = norms;
528 [ + + ]: 1344 : while (ptr->lexeme)
529 : : {
530 [ + - ]: 522 : if (ptr->flags & TSL_ADDPOS)
531 : 0 : prs->vectorpos++;
532 : 522 : pfree(ptr->lexeme);
533 : 522 : ptr++;
534 : : }
535 : 822 : pfree(norms);
536 : 822 : }
537 : 2507 : }
538 : :
539 : : void
540 : 62 : hlparsetext(Oid cfgId, HeadlineParsedText *prs, TSQuery query, char *buf, int buflen)
541 : : {
542 : 62 : int type,
543 : 62 : lenlemm = 0; /* silence compiler warning */
544 : 62 : char *lemm = NULL;
545 : 62 : LexizeData ldata;
546 : 62 : TSLexeme *norms;
547 : 62 : ParsedLex *lexs;
548 : 62 : TSConfigCacheEntry *cfg;
549 : 62 : TSParserCacheEntry *prsobj;
550 : 62 : void *prsdata;
551 : :
552 : 62 : cfg = lookup_ts_config_cache(cfgId);
553 : 62 : prsobj = lookup_ts_parser_cache(cfg->prsId);
554 : :
555 : 62 : prsdata = DatumGetPointer(FunctionCall2(&(prsobj->prsstart),
556 : : PointerGetDatum(buf),
557 : : Int32GetDatum(buflen)));
558 : :
559 : 62 : LexizeInit(&ldata, cfg);
560 : :
561 : 62 : do
562 : : {
563 : 1685 : type = DatumGetInt32(FunctionCall3(&(prsobj->prstoken),
564 : : PointerGetDatum(prsdata),
565 : : PointerGetDatum(&lemm),
566 : : PointerGetDatum(&lenlemm)));
567 : :
568 [ + + + - ]: 1685 : if (type > 0 && lenlemm >= MAXSTRLEN)
569 : : {
570 : : #ifdef IGNORE_LONGLEXEME
571 [ # # # # ]: 0 : ereport(NOTICE,
572 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
573 : : errmsg("word is too long to be indexed"),
574 : : errdetail("Words longer than %d characters are ignored.",
575 : : MAXSTRLEN)));
576 : 0 : continue;
577 : : #else
578 : : ereport(ERROR,
579 : : (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
580 : : errmsg("word is too long to be indexed"),
581 : : errdetail("Words longer than %d characters are ignored.",
582 : : MAXSTRLEN)));
583 : : #endif
584 : : }
585 : :
586 : 1685 : LexizeAddLemm(&ldata, type, lemm, lenlemm);
587 : :
588 : 1685 : do
589 : : {
590 [ + + ]: 2507 : if ((norms = LexizeExec(&ldata, &lexs)) != NULL)
591 : : {
592 : 822 : prs->vectorpos++;
593 : 822 : addHLParsedLex(prs, query, lexs, norms);
594 : 822 : }
595 : : else
596 : 1685 : addHLParsedLex(prs, query, lexs, NULL);
597 [ + + ]: 2507 : } while (norms);
598 [ + + ]: 1685 : } while (type > 0);
599 : :
600 : 62 : FunctionCall1(&(prsobj->prsend), PointerGetDatum(prsdata));
601 : 62 : }
602 : :
603 : : /*
604 : : * Generate the headline, as a text object, from HeadlineParsedText.
605 : : */
606 : : text *
607 : 62 : generateHeadline(HeadlineParsedText *prs)
608 : : {
609 : 62 : text *out;
610 : 62 : char *ptr;
611 : 62 : int len = 128;
612 : 62 : int numfragments = 0;
613 : 62 : int16 infrag = 0;
614 : :
615 : 62 : HeadlineWordEntry *wrd = prs->words;
616 : :
617 : 62 : out = (text *) palloc(len);
618 : 62 : ptr = ((char *) out) + VARHDRSZ;
619 : :
620 [ + + ]: 1685 : while (wrd - prs->words < prs->curwords)
621 : : {
622 [ + + ]: 1639 : while (wrd->len + prs->stopsellen + prs->startsellen + prs->fragdelimlen + (ptr - ((char *) out)) >= len)
623 : : {
624 : 16 : int dist = ptr - ((char *) out);
625 : :
626 : 16 : len *= 2;
627 : 16 : out = (text *) repalloc(out, len);
628 : 16 : ptr = ((char *) out) + dist;
629 : 16 : }
630 : :
631 [ + + - + ]: 1623 : if (wrd->in && !wrd->repeated)
632 : : {
633 [ + + ]: 873 : if (!infrag)
634 : : {
635 : :
636 : : /* start of a new fragment */
637 : 63 : infrag = 1;
638 : 63 : numfragments++;
639 : : /* add a fragment delimiter if this is after the first one */
640 [ + + ]: 63 : if (numfragments > 1)
641 : : {
642 : 2 : memcpy(ptr, prs->fragdelim, prs->fragdelimlen);
643 : 2 : ptr += prs->fragdelimlen;
644 : 2 : }
645 : 63 : }
646 [ - + ]: 873 : if (wrd->replace)
647 : : {
648 : 0 : *ptr = ' ';
649 : 0 : ptr++;
650 : 0 : }
651 [ + + ]: 873 : else if (!wrd->skip)
652 : : {
653 [ + + ]: 872 : if (wrd->selected)
654 : : {
655 : 83 : memcpy(ptr, prs->startsel, prs->startsellen);
656 : 83 : ptr += prs->startsellen;
657 : 83 : }
658 : 872 : memcpy(ptr, wrd->word, wrd->len);
659 : 872 : ptr += wrd->len;
660 [ + + ]: 872 : if (wrd->selected)
661 : : {
662 : 83 : memcpy(ptr, prs->stopsel, prs->stopsellen);
663 : 83 : ptr += prs->stopsellen;
664 : 83 : }
665 : 872 : }
666 : 873 : }
667 [ - + ]: 750 : else if (!wrd->repeated)
668 : : {
669 [ + + ]: 750 : if (infrag)
670 : 20 : infrag = 0;
671 : 750 : pfree(wrd->word);
672 : 750 : }
673 : :
674 : 1623 : wrd++;
675 : : }
676 : :
677 : 62 : SET_VARSIZE(out, ptr - ((char *) out));
678 : 124 : return out;
679 : 62 : }
|