Debian Codegraph @

lib/highlighter.c

raw symbols SCIP
1 /*
2 Copyright (C) 2018 Brazil
3 Copyright (C) 2018-2024 Sutou Kouhei <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "grn.h"
21 #include "grn_token_cursor.h"
22
23 #include "grn_pat.h"
24 #include "grn_str.h"
25
26 #define GRN_HIGHLIGHTER_DEFAULT_OPEN_TAG "<span class=\"keyword\">"
27 #define GRN_HIGHLIGHTER_DEFAULT_CLOSE_TAG "</span>"
28
29 typedef struct {
30 grn_id chunk_id;
31 uint64_t offset;
32 uint32_t length;
33 bool have_overlap;
34 uint32_t first_character_length;
35 } grn_highlighter_location;
36
37 static int
38 grn_highlighter_location_compare(const void *data1, const void *data2)
39 {
40 const grn_highlighter_location *location1 = data1;
41 const grn_highlighter_location *location2 = data2;
42
43 if (location1->offset == location2->offset) {
44 return (int)(location1->length - location2->length);
45 } else {
46 return (int)(location1->offset - location2->offset);
47 }
48 }
49
50 struct _grn_highlighter {
51 grn_obj_header header;
52
53 bool is_html_mode;
54 bool need_prepared;
55 grn_obj raw_keywords;
56
57 bool is_sequential_class_tag_mode;
58 struct {
59 grn_obj open;
60 grn_obj close;
61 } sequential_class_tag;
62
63 struct {
64 grn_obj open;
65 grn_obj close;
66 } default_tag;
67
68 grn_obj open_tags;
69 grn_obj close_tags;
70 size_t n_tags;
71
72 /* For lexicon mode */
73 struct {
74 grn_obj *object;
75 grn_encoding encoding;
76 grn_obj lazy_keywords;
77 grn_obj lazy_keyword_ids;
78 grn_obj *token_id_chunks;
79 grn_obj token_id_chunk;
80 grn_obj token_ids;
81 grn_obj token_locations;
82 grn_obj candidates;
83 } lexicon;
84
85 /* For patricia trie mode */
86 struct {
87 grn_obj *keywords;
88 grn_obj normalizers;
89 } pat;
90 };
91
92 grn_highlighter *
93 grn_highlighter_open(grn_ctx *ctx)
94 {
95 grn_highlighter *highlighter;
96
97 GRN_API_ENTER;
98
99 highlighter = GRN_CALLOC(sizeof(grn_highlighter));
100 if (!highlighter) {
101 char errbuf[GRN_CTX_MSGSIZE];
102 grn_strcpy(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf);
103 ERR(ctx->rc, "[highlighter][open] failed to allocate memory: %s", errbuf);
104 GRN_API_RETURN(NULL);
105 }
106
107 highlighter->header.type = GRN_HIGHLIGHTER;
108 highlighter->header.impl_flags = 0;
109 highlighter->header.flags = 0;
110 highlighter->header.domain = GRN_ID_NIL;
111
112 highlighter->is_html_mode = true;
113 highlighter->need_prepared = true;
114 GRN_TEXT_INIT(&(highlighter->raw_keywords), GRN_OBJ_VECTOR);
115
116 highlighter->is_sequential_class_tag_mode = false;
117 GRN_TEXT_INIT(&(highlighter->sequential_class_tag.open), 0);
118 GRN_TEXT_INIT(&(highlighter->sequential_class_tag.close), 0);
119
120 GRN_TEXT_INIT(&(highlighter->default_tag.open), 0);
121 grn_highlighter_set_default_open_tag(ctx,
122 highlighter,
123 GRN_HIGHLIGHTER_DEFAULT_OPEN_TAG,
124 -1);
125 GRN_TEXT_INIT(&(highlighter->default_tag.close), 0);
126 grn_highlighter_set_default_close_tag(ctx,
127 highlighter,
128 GRN_HIGHLIGHTER_DEFAULT_CLOSE_TAG,
129 -1);
130
131 GRN_TEXT_INIT(&(highlighter->open_tags), GRN_OBJ_VECTOR);
132 GRN_TEXT_INIT(&(highlighter->close_tags), GRN_OBJ_VECTOR);
133 highlighter->n_tags = 0;
134
135 highlighter->lexicon.object = NULL;
136 highlighter->lexicon.encoding = GRN_ENC_NONE;
137 GRN_TEXT_INIT(&(highlighter->lexicon.lazy_keywords), GRN_OBJ_VECTOR);
138 GRN_RECORD_INIT(&(highlighter->lexicon.lazy_keyword_ids),
139 GRN_OBJ_VECTOR,
140 GRN_ID_NIL);
141 highlighter->lexicon.token_id_chunks = NULL;
142 GRN_TEXT_INIT(&(highlighter->lexicon.token_id_chunk), 0);
143 GRN_RECORD_INIT(&(highlighter->lexicon.token_ids),
144 GRN_OBJ_VECTOR,
145 GRN_ID_NIL);
146 GRN_TEXT_INIT(&(highlighter->lexicon.token_locations), 0);
147 GRN_TEXT_INIT(&(highlighter->lexicon.candidates), 0);
148
149 highlighter->pat.keywords = NULL;
150 GRN_TEXT_INIT(&(highlighter->pat.normalizers), 0);
151 grn_highlighter_set_normalizers(ctx, highlighter, "NormalizerAuto", -1);
152
153 GRN_API_RETURN(highlighter);
154 }
155
156 grn_rc
157 grn_highlighter_close(grn_ctx *ctx, grn_highlighter *highlighter)
158 {
159 GRN_API_ENTER;
160
161 if (!highlighter) {
162 GRN_API_RETURN(ctx->rc);
163 }
164
165 GRN_OBJ_FIN(ctx, &(highlighter->pat.normalizers));
166 if (highlighter->pat.keywords) {
167 grn_obj_close(ctx, highlighter->pat.keywords);
168 }
169
170 GRN_OBJ_FIN(ctx, &(highlighter->lexicon.lazy_keywords));
171 GRN_OBJ_FIN(ctx, &(highlighter->lexicon.lazy_keyword_ids));
172 if (highlighter->lexicon.token_id_chunks) {
173 grn_obj_close(ctx, highlighter->lexicon.token_id_chunks);
174 }
175 GRN_OBJ_FIN(ctx, &(highlighter->lexicon.token_id_chunk));
176 GRN_OBJ_FIN(ctx, &(highlighter->lexicon.candidates));
177 GRN_OBJ_FIN(ctx, &(highlighter->lexicon.token_locations));
178 GRN_OBJ_FIN(ctx, &(highlighter->lexicon.token_ids));
179
180 GRN_OBJ_FIN(ctx, &(highlighter->open_tags));
181 GRN_OBJ_FIN(ctx, &(highlighter->close_tags));
182
183 GRN_OBJ_FIN(ctx, &(highlighter->default_tag.open));
184 GRN_OBJ_FIN(ctx, &(highlighter->default_tag.close));
185
186 GRN_OBJ_FIN(ctx, &(highlighter->sequential_class_tag.open));
187 GRN_OBJ_FIN(ctx, &(highlighter->sequential_class_tag.close));
188
189 GRN_OBJ_FIN(ctx, &(highlighter->raw_keywords));
190 GRN_FREE(highlighter);
191
192 GRN_API_RETURN(ctx->rc);
193 }
194
195 static void
196 grn_highlighter_prepare_lexicon_add_token_id_chunks(
197 grn_ctx *ctx,
198 grn_highlighter *highlighter,
199 grn_token **tokens,
200 size_t n_tokens,
201 grn_obj *token_id_chunk)
202 {
203 if (n_tokens == 0) {
204 if (GRN_TEXT_LEN(token_id_chunk) == 0) {
205 return;
206 }
207 grn_encoding encoding = ctx->encoding;
208 /* token_id_chunk is a binary data */
209 ctx->encoding = GRN_ENC_NONE;
210 grn_table_add(ctx,
211 highlighter->lexicon.token_id_chunks,
212 GRN_TEXT_VALUE(token_id_chunk),
213 (unsigned int)GRN_TEXT_LEN(token_id_chunk),
214 NULL);
215 ctx->encoding = encoding;
216 } else {
217 grn_token *token = tokens[0];
218 grn_obj *token_data = grn_token_get_data(ctx, token);
219 /* Expand immature token later. */
220 if (grn_token_get_force_prefix_search(ctx, token) &&
221 highlighter->lexicon.object->header.type != GRN_TABLE_HASH_KEY) {
222 grn_obj *lazy_keywords = &(highlighter->lexicon.lazy_keywords);
223 grn_vector_add_element(ctx,
224 lazy_keywords,
225 GRN_TEXT_VALUE(token_data),
226 (uint32_t)GRN_TEXT_LEN(token_data),
227 0,
228 GRN_DB_TEXT);
229 }
230 grn_token_status token_status = grn_token_get_status(ctx, token);
231 if (token_status & GRN_TOKEN_UNMATURED &&
232 token_status & GRN_TOKEN_OVERLAP) {
233 /* This skips the current token.
234 *
235 * GRN_TOKENIZE_GET don't emit immature and overlapped token
236 * such as "c" in ["ab", "bc", "c"] for 'abc' with
237 * TokenNgram. But GRN_TOKENIZE_ONLY doesn't do it. So we do it
238 * manually here. */
239 grn_highlighter_prepare_lexicon_add_token_id_chunks(ctx,
240 highlighter,
241 tokens + 1,
242 n_tokens - 1,
243 token_id_chunk);
244 } else {
245 grn_id token_id = GRN_ID_NIL;
246 /* We should not use grn_table_get() here. If we use
247 * grn_table_get(), token is normalized twice. */
248 switch (highlighter->lexicon.object->header.type) {
249 case GRN_TABLE_HASH_KEY:
250 token_id = grn_hash_get(ctx,
251 (grn_hash *)(highlighter->lexicon.object),
252 GRN_TEXT_VALUE(token_data),
253 GRN_TEXT_LEN(token_data),
254 NULL);
255 break;
256 case GRN_TABLE_PAT_KEY:
257 token_id = grn_pat_get(ctx,
258 (grn_pat *)(highlighter->lexicon.object),
259 GRN_TEXT_VALUE(token_data),
260 GRN_TEXT_LEN(token_data),
261 NULL);
262 break;
263 case GRN_TABLE_DAT_KEY:
264 token_id = grn_dat_get(ctx,
265 (grn_dat *)(highlighter->lexicon.object),
266 GRN_TEXT_VALUE(token_data),
267 GRN_TEXT_LEN(token_data),
268 NULL);
269 break;
270 }
271 /* GRN_TOKENIZE_ONLY may emit a token that doesn't exist in
272 * lexicon. */
273 if (token_id == GRN_ID_NIL) {
274 /* Lexicon: ["ab", "bc", "c"]
275 * Input: "abd"
276 * Tokens: ["ab", "bd", "d"]
277 * ^
278 * The current token
279 *
280 * Register the current token ID chunk (["ab"]), ignore "bd"
281 * and start a new token ID chunk ([]) with the next token
282 * ("d").
283 */
284
285 /* Register the current token ID chunk. */
286 grn_highlighter_prepare_lexicon_add_token_id_chunks(ctx,
287 highlighter,
288 NULL,
289 0,
290 token_id_chunk);
291 /* Ignore the current token and start a new token ID chunk
292 * from here. */
293 GRN_BULK_REWIND(token_id_chunk);
294 grn_highlighter_prepare_lexicon_add_token_id_chunks(ctx,
295 highlighter,
296 tokens + 1,
297 n_tokens - 1,
298 token_id_chunk);
299 } else {
300 /* This is for loose mode. In loose mode, we have two
301 * tokenized blocks that are separated by
302 * GRN_TOKENIER_END_MARK_UTF8 with GRN_TOKENIZE_ONLY.
303 *
304 * Example:
305 * Tokenizer: TokenNgram("loose_blank", true)
306 * Normalizer: NormalizerNFKC150
307 * Input: "a b"
308 * Tokens: ["a", "b", "U+FFF0", "ab"]
309 * ^
310 * GRN_TOKENIZER_END_MARK_UTF8
311 *
312 * If we found the separator, we split tokens with the
313 * separator and create two token ID chunks. */
314 if (GRN_TEXT_LEN(token_data) == GRN_TOKENIZER_END_MARK_UTF8_LEN &&
315 memcmp(GRN_TEXT_VALUE(token_data),
316 GRN_TOKENIZER_END_MARK_UTF8,
317 GRN_TOKENIZER_END_MARK_UTF8_LEN) == 0) {
318 /* Register the current token ID chunk. */
319 grn_highlighter_prepare_lexicon_add_token_id_chunks(ctx,
320 highlighter,
321 NULL,
322 0,
323 token_id_chunk);
324 if (n_tokens > 1) {
325 /* If we have more tokens after we skip the
326 * GRN_TOKENIZER_END_MARK_UTF8 token, clear the current
327 * token ID chunk and start a new token ID chunk for
328 * tokens followed by the GRN_TOKENIZER_END_MARK_UTF8
329 * token. */
330 GRN_BULK_REWIND(token_id_chunk);
331 grn_highlighter_prepare_lexicon_add_token_id_chunks(ctx,
332 highlighter,
333 tokens + 1,
334 n_tokens - 1,
335 token_id_chunk);
336 }
337 } else {
338 /* This is an existing token. We just add the current token
339 * to the current token ID chunk and continue collecting
340 * more tokens. */
341 GRN_RECORD_PUT(ctx, token_id_chunk, token_id);
342 grn_highlighter_prepare_lexicon_add_token_id_chunks(ctx,
343 highlighter,
344 tokens + 1,
345 n_tokens - 1,
346 token_id_chunk);
347 grn_id used_token_id;
348 GRN_RECORD_POP(token_id_chunk, used_token_id);
349 }
350 }
351 }
352 }
353 }
354
355 static void
356 grn_highlighter_prepare_lexicon(grn_ctx *ctx, grn_highlighter *highlighter)
357 {
358 grn_obj *lazy_keywords = &(highlighter->lexicon.lazy_keywords);
359 grn_id lexicon_id;
360 size_t i, n_keywords;
361 grn_obj *token_id_chunk = &(highlighter->lexicon.token_id_chunk);
362
363 GRN_BULK_REWIND(lazy_keywords);
364
365 lexicon_id = grn_obj_id(ctx, highlighter->lexicon.object);
366 highlighter->lexicon.lazy_keyword_ids.header.domain = lexicon_id;
367 highlighter->lexicon.token_ids.header.domain = lexicon_id;
368
369 grn_table_get_info(ctx,
370 highlighter->lexicon.object,
371 NULL,
372 &(highlighter->lexicon.encoding),
373 NULL,
374 NULL,
375 NULL);
376
377 if (highlighter->lexicon.token_id_chunks) {
378 grn_table_truncate(ctx, highlighter->lexicon.token_id_chunks);
379 } else {
380 highlighter->lexicon.token_id_chunks =
381 grn_table_create(ctx,
382 NULL,
383 0,
384 NULL,
385 GRN_OBJ_TABLE_PAT_KEY,
386 grn_ctx_at(ctx, GRN_DB_SHORT_TEXT),
387 NULL);
388 if (!highlighter->lexicon.token_id_chunks) {
389 grn_rc rc = ctx->rc;
390 char errbuf[GRN_CTX_MSGSIZE];
391 if (rc == GRN_SUCCESS) {
392 rc = GRN_UNKNOWN_ERROR;
393 }
394 grn_strcpy(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf);
395 ERR(rc,
396 "[highlighter][prepare][lexicon] "
397 "failed to create an internal patricia trie: %s",
398 errbuf);
399 return;
400 }
401 }
402
403 n_keywords = grn_vector_size(ctx, &(highlighter->raw_keywords));
404 for (i = 0; i < n_keywords; i++) {
405 const char *keyword;
406 unsigned int keyword_length;
407 grn_token_cursor *cursor;
408
409 keyword_length = grn_vector_get_element(ctx,
410 &(highlighter->raw_keywords),
411 (uint32_t)i,
412 &keyword,
413 NULL,
414 NULL);
415
416 grn_obj tokens;
417 /* grn_token isn't grn_obj but we use GRN_PTR here for easy to implement...
418 */
419 GRN_PTR_INIT(&tokens, GRN_OBJ_VECTOR, GRN_ID_NIL);
420 cursor = grn_token_cursor_open(ctx,
421 highlighter->lexicon.object,
422 keyword,
423 keyword_length,
424 GRN_TOKENIZE_ONLY,
425 0);
426 if (!cursor) {
427 continue;
428 }
429 GRN_BULK_REWIND(token_id_chunk);
430 while (cursor->status != GRN_TOKEN_CURSOR_DONE) {
431 grn_token_cursor_next(ctx, cursor);
432 grn_token *token = grn_token_cursor_get_token(ctx, cursor);
433 /* This may be needless. */
434 grn_obj *token_data = grn_token_get_data(ctx, token);
435 if (GRN_TEXT_LEN(token_data) == 0) {
436 break;
437 }
438 grn_token *copied_token = GRN_MALLOC(sizeof(grn_token));
439 grn_token_init_deep(ctx, copied_token);
440 grn_token_copy(ctx, copied_token, token);
441 GRN_PTR_PUT(ctx, &tokens, copied_token);
442 }
443 grn_token_cursor_close(ctx, cursor);
444
445 size_t n_tokens = GRN_PTR_VECTOR_SIZE(&tokens);
446 if (n_tokens == 0) {
447 GRN_OBJ_FIN(ctx, &tokens);
448 continue;
449 }
450 grn_token **raw_tokens = (grn_token **)GRN_BULK_HEAD(&tokens);
451 grn_highlighter_prepare_lexicon_add_token_id_chunks(ctx,
452 highlighter,
453 raw_tokens,
454 n_tokens,
455 token_id_chunk);
456 size_t i;
457 for (i = 0; i < n_tokens; i++) {
458 grn_token_fin(ctx, raw_tokens[i]);
459 GRN_FREE(raw_tokens[i]);
460 }
461 GRN_OBJ_FIN(ctx, &tokens);
462 }
463 }
464
465 static void
466 grn_highlighter_prepare_patricia_trie(grn_ctx *ctx,
467 grn_highlighter *highlighter)
468 {
469 if (highlighter->pat.keywords) {
470 grn_table_truncate(ctx, highlighter->pat.keywords);
471 } else {
472 highlighter->pat.keywords =
473 grn_table_create(ctx,
474 NULL,
475 0,
476 NULL,
477 GRN_OBJ_TABLE_PAT_KEY,
478 grn_ctx_at(ctx, GRN_DB_SHORT_TEXT),
479 NULL);
480 if (!highlighter->pat.keywords) {
481 grn_rc rc = ctx->rc;
482 char errbuf[GRN_CTX_MSGSIZE];
483 if (rc == GRN_SUCCESS) {
484 rc = GRN_UNKNOWN_ERROR;
485 }
486 grn_strcpy(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf);
487 ERR(rc,
488 "[highlighter][prepare][no-lexicon] "
489 "failed to create an internal patricia trie: %s",
490 errbuf);
491 return;
492 }
493 }
494
495 if (highlighter->lexicon.object) {
496 grn_obj normalizers;
497 GRN_TEXT_INIT(&normalizers, 0);
498 grn_table_get_normalizers_string(ctx,
499 highlighter->lexicon.object,
500 &normalizers);
501 grn_obj_set_info(ctx,
502 highlighter->pat.keywords,
503 GRN_INFO_NORMALIZERS,
504 &normalizers);
505 GRN_OBJ_FIN(ctx, &normalizers);
506 } else {
507 grn_obj_set_info(ctx,
508 highlighter->pat.keywords,
509 GRN_INFO_NORMALIZERS,
510 &(highlighter->pat.normalizers));
511 }
512 if (ctx->rc != GRN_SUCCESS) {
513 return;
514 }
515
516 {
517 unsigned int i, n;
518
519 n = grn_vector_size(ctx, &(highlighter->raw_keywords));
520 for (i = 0; i < n; i++) {
521 const char *keyword;
522 unsigned int keyword_size;
523
524 keyword_size = grn_vector_get_element(ctx,
525 &(highlighter->raw_keywords),
526 i,
527 &keyword,
528 NULL,
529 NULL);
530 grn_table_add(ctx,
531 highlighter->pat.keywords,
532 keyword,
533 keyword_size,
534 NULL);
535 }
536 }
537 }
538
539 static void
540 grn_highlighter_prepare(grn_ctx *ctx, grn_highlighter *highlighter)
541 {
542 bool use_lexicon = false;
543 if (highlighter->lexicon.object) {
544 grn_obj *tokenizer;
545 grn_table_get_info(ctx,
546 highlighter->lexicon.object,
547 NULL,
548 NULL,
549 &tokenizer,
550 NULL,
551 NULL);
552 use_lexicon = (tokenizer != NULL);
553 }
554 if (use_lexicon) {
555 grn_highlighter_prepare_lexicon(ctx, highlighter);
556 if (highlighter->pat.keywords) {
557 grn_obj_close(ctx, highlighter->pat.keywords);
558 highlighter->pat.keywords = NULL;
559 }
560 } else {
561 grn_highlighter_prepare_patricia_trie(ctx, highlighter);
562 }
563 size_t n_open_tags = grn_vector_size(ctx, &(highlighter->open_tags));
564 size_t n_close_tags = grn_vector_size(ctx, &(highlighter->close_tags));
565 highlighter->n_tags = (n_open_tags == n_close_tags ? n_open_tags : 0);
566 highlighter->need_prepared = false;
567 }
568
569 static bool
570 grn_ids_is_included(grn_id *ids, size_t n_ids, grn_id id)
571 {
572 size_t i;
573
574 for (i = 0; i < n_ids; i++) {
575 if (ids[i] == id) {
576 return true;
577 }
578 }
579
580 return false;
581 }
582
583 static void
584 grn_highlighter_log_location(grn_ctx *ctx,
585 grn_log_level level,
586 const char *tag,
587 grn_highlighter_location *location,
588 const char *text,
589 size_t text_length)
590 {
591 if (!grn_logger_pass(ctx, level)) {
592 return;
593 }
594
595 GRN_LOG(ctx,
596 level,
597 "%s[location] "
598 "[%" GRN_FMT_INT64U "...%" GRN_FMT_INT64U "](%u)[%s] <%.*s>",
599 tag,
600 location->offset,
601 location->offset + location->length,
602 location->first_character_length,
603 location->have_overlap ? "overlapped" : "not-overlapped",
604 (int)location->length,
605 text + location->offset);
606 }
607
608 static void
609 grn_highlighter_highlight_get_ith_tag(grn_ctx *ctx,
610 grn_highlighter *highlighter,
611 size_t i,
612 const char **open_tag,
613 size_t *open_tag_length,
614 const char **close_tag,
615 size_t *close_tag_length)
616 {
617 if (highlighter->is_sequential_class_tag_mode) {
618 size_t n_keywords = grn_vector_size(ctx, &(highlighter->raw_keywords));
619 i = i % n_keywords;
620 GRN_BULK_REWIND(&(highlighter->sequential_class_tag.open));
621 grn_text_printf(ctx,
622 &(highlighter->sequential_class_tag.open),
623 "<mark class=\"keyword-%" GRN_FMT_SIZE "\">",
624 i);
625 GRN_TEXT_SETS(ctx, &(highlighter->sequential_class_tag.close), "</mark>");
626 *open_tag = GRN_TEXT_VALUE(&(highlighter->sequential_class_tag.open));
627 *open_tag_length = GRN_TEXT_LEN(&(highlighter->sequential_class_tag.open));
628 *close_tag = GRN_TEXT_VALUE(&(highlighter->sequential_class_tag.close));
629 *close_tag_length =
630 GRN_TEXT_LEN(&(highlighter->sequential_class_tag.close));
631 } else if (highlighter->n_tags == 0) {
632 *open_tag = GRN_TEXT_VALUE(&(highlighter->default_tag.open));
633 *open_tag_length = GRN_TEXT_LEN(&(highlighter->default_tag.open)) - 1;
634 *close_tag = GRN_TEXT_VALUE(&(highlighter->default_tag.close));
635 *close_tag_length = GRN_TEXT_LEN(&(highlighter->default_tag.close)) - 1;
636 } else {
637 i = i % highlighter->n_tags;
638 *open_tag_length = grn_vector_get_element(ctx,
639 &(highlighter->open_tags),
640 i,
641 open_tag,
642 NULL,
643 NULL);
644 *close_tag_length = grn_vector_get_element(ctx,
645 &(highlighter->close_tags),
646 i,
647 close_tag,
648 NULL,
649 NULL);
650 }
651 }
652
653 static void
654 grn_highlighter_highlight_add_normal_text(grn_ctx *ctx,
655 grn_highlighter *highlighter,
656 grn_obj *output,
657 const char *text,
658 size_t text_length)
659 {
660 if (highlighter->is_html_mode) {
661 grn_text_escape_xml(ctx, output, text, text_length);
662 } else {
663 GRN_TEXT_PUT(ctx, output, text, text_length);
664 }
665 }
666
667 static uint64_t
668 grn_highlighter_highlight_lexicon_flush(grn_ctx *ctx,
669 grn_log_level log_level,
670 const char *tag,
671 grn_highlighter *highlighter,
672 const char *text,
673 size_t text_length,
674 grn_obj *output,
675 grn_highlighter_location *location,
676 uint64_t offset)
677 {
678 grn_highlighter_log_location(ctx,
679 log_level,
680 tag,
681 location,
682 text,
683 text_length);
684 if (location->offset > offset) {
685 grn_highlighter_highlight_add_normal_text(
686 ctx,
687 highlighter,
688 output,
689 text + offset,
690 (size_t)(location->offset - offset));
691 }
692
693 const char *open_tag;
694 size_t open_tag_length;
695 const char *close_tag;
696 size_t close_tag_length;
697 grn_highlighter_highlight_get_ith_tag(ctx,
698 highlighter,
699 /* We need "- 1" here
700 * because grn_id doesn't
701 * use 0 (GRN_ID_NIL). */
702 location->chunk_id - 1,
703 &open_tag,
704 &open_tag_length,
705 &close_tag,
706 &close_tag_length);
707 GRN_TEXT_PUT(ctx, output, open_tag, open_tag_length);
708 grn_highlighter_highlight_add_normal_text(ctx,
709 highlighter,
710 output,
711 text + location->offset,
712 location->length);
713 GRN_TEXT_PUT(ctx, output, close_tag, close_tag_length);
714 return location->offset + location->length;
715 }
716
717 static void
718 grn_highlighter_tokenize_lexicon(grn_ctx *ctx,
719 grn_highlighter *highlighter,
720 const char *text,
721 size_t text_length)
722 {
723 const char *tag = "[highlighter][highlight][lexicon]";
724 grn_log_level log_level = GRN_LOG_DEBUG;
725 grn_obj *token_ids = &(highlighter->lexicon.token_ids);
726 grn_obj *token_locations = &(highlighter->lexicon.token_locations);
727
728 GRN_BULK_REWIND(token_ids);
729 GRN_BULK_REWIND(token_locations);
730 grn_token_cursor *cursor = grn_token_cursor_open(ctx,
731 highlighter->lexicon.object,
732 text,
733 text_length,
734 GRN_TOKENIZE_ADD,
735 0);
736 if (!cursor) {
737 char errbuf[GRN_CTX_MSGSIZE];
738 grn_strcpy(errbuf, GRN_CTX_MSGSIZE, ctx->errbuf);
739 ERR(ctx->rc, "%s failed to start tokenizing: %s", tag, errbuf);
740 return;
741 }
742
743 GRN_LOG(ctx, log_level, "%s[tokenize][start]", tag);
744 while (cursor->status == GRN_TOKEN_CURSOR_DOING) {
745 grn_id token_id = grn_token_cursor_next(ctx, cursor);
746 grn_highlighter_location location;
747 grn_token *token;
748
749 if (token_id == GRN_ID_NIL) {
750 continue;
751 }
752 token = grn_token_cursor_get_token(ctx, cursor);
753 GRN_RECORD_PUT(ctx, token_ids, token_id);
754 location.offset = grn_token_get_source_offset(ctx, token);
755 location.length = grn_token_get_source_length(ctx, token);
756 location.first_character_length =
757 grn_token_get_source_first_character_length(ctx, token);
758 location.have_overlap = grn_token_have_overlap(ctx, token);
759 GRN_TEXT_PUT(ctx, token_locations, &location, sizeof(location));
760 grn_highlighter_log_location(ctx,
761 log_level,
762 tag,
763 &location,
764 text,
765 text_length);
766 }
767 grn_token_cursor_close(ctx, cursor);
768 GRN_LOG(ctx, log_level, "%s[tokenize][end]", tag);
769 }
770
771 static void
772 grn_highlighter_highlight_lexicon(grn_ctx *ctx,
773 grn_highlighter *highlighter,
774 const char *text,
775 size_t text_length,
776 grn_obj *output)
777 {
778 const char *tag = "[highlighter][highlight][lexicon]";
779 grn_log_level log_level = GRN_LOG_DEBUG;
780 grn_obj *lazy_keyword_ids = &(highlighter->lexicon.lazy_keyword_ids);
781 grn_obj *token_ids = &(highlighter->lexicon.token_ids);
782 grn_obj *token_locations = &(highlighter->lexicon.token_locations);
783 grn_obj *candidates = &(highlighter->lexicon.candidates);
784
785 GRN_BULK_REWIND(lazy_keyword_ids);
786
787 {
788 grn_obj *lexicon = highlighter->lexicon.object;
789 grn_obj *chunks = highlighter->lexicon.token_id_chunks;
790 grn_obj *lazy_keywords = &(highlighter->lexicon.lazy_keywords);
791 size_t i;
792 size_t n_keywords;
793
794 n_keywords = grn_vector_size(ctx, lazy_keywords);
795 for (i = 0; i < n_keywords; i++) {
796 const char *keyword;
797 unsigned int keyword_length;
798
799 keyword_length = grn_vector_get_element(ctx,
800 lazy_keywords,
801 (uint32_t)i,
802 &keyword,
803 NULL,
804 NULL);
805 GRN_LOG(ctx,
806 log_level,
807 "%s[prefix-search][start] %" GRN_FMT_SIZE ":<%.*s>",
808 tag,
809 i,
810 (int)keyword_length,
811 keyword);
812 GRN_TABLE_EACH_BEGIN_MIN(ctx,
813 lexicon,
814 cursor,
815 id,
816 keyword,
817 keyword_length,
818 GRN_CURSOR_PREFIX)
819 {
820 int added = 0;
821
822 {
823 grn_encoding encoding = ctx->encoding;
824 ctx->encoding = GRN_ENC_NONE;
825 grn_table_add(ctx, chunks, &id, sizeof(grn_id), &added);
826 ctx->encoding = encoding;
827 }
828 if (grn_logger_pass(ctx, log_level)) {
829 void *key;
830 int key_size;
831 key_size = grn_table_cursor_get_key(ctx, cursor, &key);
832 GRN_LOG(ctx,
833 log_level,
834 "%s[prefix-search][%s] %" GRN_FMT_SIZE ":<%.*s>:<%.*s>",
835 tag,
836 added ? "added" : "not-added",
837 i,
838 (int)keyword_length,
839 keyword,
840 key_size,
841 (const char *)key);
842 }
843 if (added) {
844 GRN_RECORD_PUT(ctx, lazy_keyword_ids, id);
845 }
846 }
847 GRN_TABLE_EACH_END(ctx, cursor);
848 GRN_LOG(ctx,
849 log_level,
850 "%s[prefix-search][end] %" GRN_FMT_SIZE ":<%.*s>",
851 tag,
852 i,
853 (int)keyword_length,
854 keyword);
855 }
856 }
857
858 GRN_BULK_REWIND(candidates);
859 {
860 grn_obj *lazy_keyword_ids = &(highlighter->lexicon.lazy_keyword_ids);
861 grn_id *raw_lazy_keyword_ids;
862 size_t n_lazy_keyword_ids;
863 grn_pat *chunks = (grn_pat *)(highlighter->lexicon.token_id_chunks);
864 size_t i;
865 size_t n_token_ids = GRN_BULK_VSIZE(token_ids) / sizeof(grn_id);
866 grn_id *raw_token_ids = (grn_id *)GRN_BULK_HEAD(token_ids);
867 grn_highlighter_location *raw_token_locations =
868 (grn_highlighter_location *)GRN_BULK_HEAD(token_locations);
869
870 raw_lazy_keyword_ids = (grn_id *)GRN_BULK_HEAD(lazy_keyword_ids);
871 n_lazy_keyword_ids = GRN_BULK_VSIZE(lazy_keyword_ids) / sizeof(grn_id);
872
873 for (i = 0; i < n_token_ids; i++) {
874 grn_id chunk_id;
875
876 GRN_LOG(ctx, log_level, "%s[lcp-search][start] %" GRN_FMT_SIZE, tag, i);
877 {
878 grn_encoding encoding = ctx->encoding;
879 /* token_id_chunk is a binary data */
880 ctx->encoding = GRN_ENC_NONE;
881 chunk_id =
882 grn_pat_lcp_search(ctx,
883 chunks,
884 raw_token_ids + i,
885 (unsigned int)(n_token_ids - i) * sizeof(grn_id));
886 ctx->encoding = encoding;
887 }
888 if (chunk_id == GRN_ID_NIL) {
889 GRN_LOG(ctx,
890 log_level,
891 "%s[lcp-search][end][nonexistent] %" GRN_FMT_SIZE,
892 tag,
893 i);
894 continue;
895 }
896
897 {
898 grn_id *ids;
899 uint32_t key_size;
900 size_t n_ids;
901 grn_highlighter_location candidate;
902 grn_highlighter_location *first = raw_token_locations + i;
903
904 candidate.chunk_id = chunk_id;
905 candidate.have_overlap = false;
906 candidate.first_character_length = 0;
907
908 {
909 grn_encoding encoding = ctx->encoding;
910 ctx->encoding = GRN_ENC_NONE;
911 ids = (grn_id *)_grn_pat_key(ctx, chunks, chunk_id, &key_size);
912 ctx->encoding = encoding;
913 }
914 n_ids = key_size / sizeof(grn_id);
915 candidate.offset = first->offset;
916 if (n_ids == 1) {
917 if (grn_ids_is_included(raw_lazy_keyword_ids,
918 n_lazy_keyword_ids,
919 ids[0])) {
920 candidate.length = first->first_character_length;
921 } else {
922 candidate.length = first->length;
923 }
924 } else {
925 grn_highlighter_location *last = raw_token_locations + i + n_ids - 1;
926 candidate.length = last->offset + last->length - candidate.offset;
927 }
928 GRN_TEXT_PUT(ctx, candidates, &candidate, sizeof(candidate));
929 grn_highlighter_log_location(ctx,
930 log_level,
931 tag,
932 &candidate,
933 text,
934 text_length);
935 GRN_LOG(ctx, log_level, "%s[lcp-search][end] %" GRN_FMT_SIZE, tag, i);
936 i += n_ids - 1;
937 }
938 }
939 }
940
941 {
942 grn_obj *chunks = highlighter->lexicon.token_id_chunks;
943 size_t i, n_ids;
944
945 n_ids = GRN_BULK_VSIZE(lazy_keyword_ids) / sizeof(grn_id);
946 for (i = 0; i < n_ids; i++) {
947 grn_id id = GRN_RECORD_VALUE_AT(lazy_keyword_ids, i);
948 grn_encoding encoding = ctx->encoding;
949 ctx->encoding = GRN_ENC_NONE;
950 grn_table_delete(ctx, chunks, &id, sizeof(grn_id));
951 ctx->encoding = encoding;
952 }
953 }
954
955 {
956 size_t i;
957 size_t n_candidates =
958 GRN_BULK_VSIZE(candidates) / sizeof(grn_highlighter_location);
959 grn_highlighter_location *raw_candidates =
960 (grn_highlighter_location *)GRN_BULK_HEAD(candidates);
961
962 GRN_LOG(ctx,
963 log_level,
964 "%s[highlight][start] %" GRN_FMT_SIZE,
965 tag,
966 n_candidates);
967 if (n_candidates == 0) {
968 grn_text_escape_xml(ctx, output, text, text_length);
969 } else {
970 grn_highlighter_location *previous = NULL;
971 uint64_t offset = 0;
972
973 qsort(raw_candidates,
974 n_candidates,
975 sizeof(grn_highlighter_location),
976 grn_highlighter_location_compare);
977 previous = raw_candidates;
978 for (i = 1; i < n_candidates; i++) {
979 grn_highlighter_location *current = raw_candidates + i;
980 if (previous->offset + previous->length >= current->offset) {
981 if (previous->offset + previous->length >
982 current->offset + current->length) {
983 current->length = previous->length;
984 } else {
985 current->length += (uint32_t)(current->offset - previous->offset);
986 }
987 current->offset = previous->offset;
988 previous = current;
989 continue;
990 }
991 if (current->offset > previous->offset) {
992 offset = grn_highlighter_highlight_lexicon_flush(ctx,
993 log_level,
994 tag,
995 highlighter,
996 text,
997 text_length,
998 output,
999 previous,
1000 offset);
1001 }
1002 previous = current;
1003 }
1004 offset = grn_highlighter_highlight_lexicon_flush(ctx,
1005 log_level,
1006 tag,
1007 highlighter,
1008 text,
1009 text_length,
1010 output,
1011 previous,
1012 offset);
1013 if (offset < text_length) {
1014 grn_highlighter_highlight_add_normal_text(
1015 ctx,
1016 highlighter,
1017 output,
1018 text + offset,
1019 (size_t)(text_length - offset));
1020 }
1021 }
1022 GRN_LOG(ctx,
1023 log_level,
1024 "%s[highlight][end] %" GRN_FMT_SIZE,
1025 tag,
1026 n_candidates);
1027 }
1028 }
1029
1030 static void
1031 grn_highlighter_highlight_patricia_trie(grn_ctx *ctx,
1032 grn_highlighter *highlighter,
1033 const char *text,
1034 size_t text_length,
1035 grn_obj *output)
1036 {
1037 const char *current = text;
1038 size_t current_length = text_length;
1039
1040 while (current_length > 0) {
1041 #define MAX_N_HITS 16
1042 grn_pat_scan_hit hits[MAX_N_HITS];
1043 const char *rest;
1044 int i, n_hits;
1045 size_t previous_length = 0;
1046 size_t chunk_length;
1047
1048 n_hits = grn_pat_scan(ctx,
1049 (grn_pat *)(highlighter->pat.keywords),
1050 current,
1051 (unsigned int)current_length,
1052 hits,
1053 MAX_N_HITS,
1054 &rest);
1055 for (i = 0; i < n_hits; i++) {
1056 const char *open_tag;
1057 size_t open_tag_length;
1058 const char *close_tag;
1059 size_t close_tag_length;
1060 grn_highlighter_highlight_get_ith_tag(ctx,
1061 highlighter,
1062 /* We need "- 1" here
1063 * because grn_id doesn't
1064 * use 0 (GRN_ID_NIL). */
1065 hits[i].id - 1,
1066 &open_tag,
1067 &open_tag_length,
1068 &close_tag,
1069 &close_tag_length);
1070
1071 if ((hits[i].offset - previous_length) > 0) {
1072 grn_highlighter_highlight_add_normal_text(ctx,
1073 highlighter,
1074 output,
1075 current + previous_length,
1076 hits[i].offset -
1077 previous_length);
1078 }
1079 GRN_TEXT_PUT(ctx, output, open_tag, open_tag_length);
1080 grn_highlighter_highlight_add_normal_text(ctx,
1081 highlighter,
1082 output,
1083 current + hits[i].offset,
1084 hits[i].length);
1085 GRN_TEXT_PUT(ctx, output, close_tag, close_tag_length);
1086 previous_length = hits[i].offset + hits[i].length;
1087 }
1088
1089 chunk_length = (size_t)(rest - current);
1090 if ((chunk_length - previous_length) > 0) {
1091 grn_highlighter_highlight_add_normal_text(ctx,
1092 highlighter,
1093 output,
1094 current + previous_length,
1095 current_length -
1096 previous_length);
1097 }
1098 current_length -= chunk_length;
1099 current = rest;
1100 #undef MAX_N_HITS
1101 }
1102 }
1103
1104 grn_rc
1105 grn_highlighter_highlight(grn_ctx *ctx,
1106 grn_highlighter *highlighter,
1107 const char *text,
1108 int64_t text_length,
1109 grn_obj *output)
1110 {
1111 GRN_API_ENTER;
1112
1113 if (text_length < 0) {
1114 text_length = (int64_t)strlen(text);
1115 }
1116
1117 if (text_length == 0) {
1118 goto exit;
1119 }
1120
1121 if (grn_vector_size(ctx, &(highlighter->raw_keywords)) == 0) {
1122 grn_highlighter_highlight_add_normal_text(ctx,
1123 highlighter,
1124 output,
1125 text,
1126 (size_t)text_length);
1127 goto exit;
1128 }
1129
1130 if (highlighter->lexicon.object) {
1131 /* We need to tokenize the target text with GRN_TOKENIZE_ADD
1132 * before we call grn_highlighter_prepare_lexicon() because
1133 * grn_highlighter_prepare_lexicon() assumes that the tokens of
1134 * the target text exists in highlighter->lexicon.object.
1135 *
1136 * If we have any new tokens, we need to re-prepare. */
1137 uint32_t size_before = grn_table_size(ctx, highlighter->lexicon.object);
1138 grn_highlighter_tokenize_lexicon(ctx, highlighter, text, text_length);
1139 if (ctx->rc != GRN_SUCCESS) {
1140 goto exit;
1141 }
1142 uint32_t size_after = grn_table_size(ctx, highlighter->lexicon.object);
1143 if (size_after > size_before) {
1144 highlighter->need_prepared = true;
1145 }
1146 }
1147
1148 if (highlighter->need_prepared) {
1149 grn_highlighter_prepare(ctx, highlighter);
1150 if (ctx->rc != GRN_SUCCESS) {
1151 goto exit;
1152 }
1153 }
1154
1155 if (highlighter->pat.keywords) {
1156 grn_highlighter_highlight_patricia_trie(ctx,
1157 highlighter,
1158 text,
1159 (size_t)text_length,
1160 output);
1161 } else {
1162 grn_highlighter_highlight_lexicon(ctx,
1163 highlighter,
1164 text,
1165 (size_t)text_length,
1166 output);
1167 }
1168
1169 exit:
1170 GRN_API_RETURN(ctx->rc);
1171 }
1172
1173 grn_rc
1174 grn_highlighter_set_lexicon(grn_ctx *ctx,
1175 grn_highlighter *highlighter,
1176 grn_obj *lexicon)
1177 {
1178 GRN_API_ENTER;
1179
1180 if (highlighter->lexicon.object != lexicon) {
1181 highlighter->need_prepared = true;
1182 highlighter->lexicon.object = lexicon;
1183 }
1184
1185 GRN_API_RETURN(ctx->rc);
1186 }
1187
1188 grn_obj *
1189 grn_highlighter_get_lexicon(grn_ctx *ctx, grn_highlighter *highlighter)
1190 {
1191 GRN_API_ENTER;
1192
1193 GRN_API_RETURN(highlighter->lexicon.object);
1194 }
1195
1196 grn_rc
1197 grn_highlighter_set_default_open_tag(grn_ctx *ctx,
1198 grn_highlighter *highlighter,
1199 const char *tag,
1200 int64_t tag_length)
1201 {
1202 GRN_API_ENTER;
1203 if (tag_length < 0) {
1204 tag_length = strlen(tag);
1205 }
1206 GRN_TEXT_SET(ctx, &(highlighter->default_tag.open), tag, tag_length);
1207 GRN_TEXT_PUTC(ctx, &(highlighter->default_tag.open), '\0');
1208 GRN_API_RETURN(ctx->rc);
1209 }
1210
1211 const char *
1212 grn_highlighter_get_default_open_tag(grn_ctx *ctx, grn_highlighter *highlighter)
1213 {
1214 return GRN_TEXT_VALUE(&(highlighter->default_tag.open));
1215 }
1216
1217 grn_rc
1218 grn_highlighter_set_default_close_tag(grn_ctx *ctx,
1219 grn_highlighter *highlighter,
1220 const char *tag,
1221 int64_t tag_length)
1222 {
1223 GRN_API_ENTER;
1224 if (tag_length < 0) {
1225 tag_length = strlen(tag);
1226 }
1227 GRN_TEXT_SET(ctx, &(highlighter->default_tag.close), tag, tag_length);
1228 GRN_TEXT_PUTC(ctx, &(highlighter->default_tag.close), '\0');
1229 GRN_API_RETURN(ctx->rc);
1230 }
1231
1232 const char *
1233 grn_highlighter_get_default_close_tag(grn_ctx *ctx,
1234 grn_highlighter *highlighter)
1235 {
1236 return GRN_TEXT_VALUE(&(highlighter->default_tag.close));
1237 }
1238
1239 grn_rc
1240 grn_highlighter_set_sequential_class_tag_mode(grn_ctx *ctx,
1241 grn_highlighter *highlighter,
1242 bool mode)
1243 {
1244 GRN_API_ENTER;
1245 highlighter->is_sequential_class_tag_mode = mode;
1246 GRN_API_RETURN(ctx->rc);
1247 }
1248
1249 bool
1250 grn_highlighter_get_sequential_class_tag_mode(grn_ctx *ctx,
1251 grn_highlighter *highlighter)
1252 {
1253 return highlighter->is_sequential_class_tag_mode;
1254 }
1255
1256 grn_rc
1257 grn_highlighter_set_normalizers(grn_ctx *ctx,
1258 grn_highlighter *highlighter,
1259 const char *normalizers,
1260 int64_t normalizers_length)
1261 {
1262 GRN_API_ENTER;
1263 if (normalizers_length < 0) {
1264 normalizers_length = strlen(normalizers);
1265 }
1266 GRN_TEXT_SET(ctx,
1267 &(highlighter->pat.normalizers),
1268 normalizers,
1269 normalizers_length);
1270 highlighter->need_prepared = true;
1271 GRN_API_RETURN(ctx->rc);
1272 }
1273
1274 grn_obj *
1275 grn_highlighter_get_normalizers(grn_ctx *ctx, grn_highlighter *highlighter)
1276 {
1277 return &(highlighter->pat.normalizers);
1278 }
1279
1280 grn_rc
1281 grn_highlighter_set_html_mode(grn_ctx *ctx,
1282 grn_highlighter *highlighter,
1283 bool html_mode)
1284 {
1285 GRN_API_ENTER;
1286 highlighter->is_html_mode = html_mode;
1287 GRN_API_RETURN(ctx->rc);
1288 }
1289
1290 bool
1291 grn_highlighter_get_html_mode(grn_ctx *ctx, grn_highlighter *highlighter)
1292 {
1293 return highlighter->is_html_mode;
1294 }
1295
1296 grn_rc
1297 grn_highlighter_add_keyword(grn_ctx *ctx,
1298 grn_highlighter *highlighter,
1299 const char *keyword,
1300 int64_t keyword_length)
1301 {
1302 unsigned int i, n_keywords;
1303 grn_obj *raw_keywords = &(highlighter->raw_keywords);
1304
1305 GRN_API_ENTER;
1306
1307 if (keyword_length < 0) {
1308 keyword_length = (int64_t)strlen(keyword);
1309 }
1310
1311 if (keyword_length == 0) {
1312 goto exit;
1313 }
1314
1315 n_keywords = grn_vector_size(ctx, raw_keywords);
1316 for (i = 0; i < n_keywords; i++) {
1317 const char *existing_keyword;
1318 unsigned int length;
1319
1320 length = grn_vector_get_element(ctx,
1321 raw_keywords,
1322 i,
1323 &existing_keyword,
1324 NULL,
1325 NULL);
1326 if (length == keyword_length &&
1327 memcmp(keyword, existing_keyword, length) == 0) {
1328 goto exit;
1329 }
1330 }
1331
1332 grn_vector_add_element(ctx,
1333 raw_keywords,
1334 keyword,
1335 (uint32_t)keyword_length,
1336 0,
1337 GRN_DB_TEXT);
1338 highlighter->need_prepared = true;
1339
1340 exit:
1341 GRN_API_RETURN(ctx->rc);
1342 }
1343
1344 grn_rc
1345 grn_highlighter_clear_keywords(grn_ctx *ctx, grn_highlighter *highlighter)
1346 {
1347 grn_obj *raw_keywords = &(highlighter->raw_keywords);
1348
1349 GRN_API_ENTER;
1350
1351 GRN_BULK_REWIND(raw_keywords);
1352 highlighter->need_prepared = true;
1353
1354 GRN_API_RETURN(ctx->rc);
1355 }
1356
1357 grn_rc
1358 grn_highlighter_add_open_tag(grn_ctx *ctx,
1359 grn_highlighter *highlighter,
1360 const char *tag,
1361 int64_t tag_length)
1362 {
1363 GRN_API_ENTER;
1364 if (tag_length < 0) {
1365 tag_length = strlen(tag);
1366 }
1367 grn_vector_add_element(ctx,
1368 &(highlighter->open_tags),
1369 tag,
1370 tag_length,
1371 0,
1372 GRN_DB_TEXT);
1373 highlighter->need_prepared = true;
1374 GRN_API_RETURN(ctx->rc);
1375 }
1376
1377 grn_rc
1378 grn_highlighter_add_close_tag(grn_ctx *ctx,
1379 grn_highlighter *highlighter,
1380 const char *tag,
1381 int64_t tag_length)
1382 {
1383 GRN_API_ENTER;
1384 if (tag_length < 0) {
1385 tag_length = strlen(tag);
1386 }
1387 grn_vector_add_element(ctx,
1388 &(highlighter->close_tags),
1389 tag,
1390 tag_length,
1391 0,
1392 GRN_DB_TEXT);
1393 highlighter->need_prepared = true;
1394 GRN_API_RETURN(ctx->rc);
1395 }
1396
1397 grn_rc
1398 grn_highlighter_clear_tags(grn_ctx *ctx, grn_highlighter *highlighter)
1399 {
1400 GRN_API_ENTER;
1401 GRN_BULK_REWIND(&(highlighter->open_tags));
1402 GRN_BULK_REWIND(&(highlighter->close_tags));
1403 highlighter->need_prepared = true;
1404 GRN_API_RETURN(ctx->rc);
1405 }