| 1 | /* |
| 2 | Copyright (C) 2009-2014 Brazil |
| 3 | Copyright (C) 2021-2023 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 <string.h> |
| 22 | #include <stddef.h> |
| 23 | #include "grn_snip.h" |
| 24 | #include "grn_ctx.h" |
| 25 | |
| 26 | #if !defined MAX |
| 27 | # define MAX(a, b) ((a) > (b) ? (a) : (b)) |
| 28 | #endif |
| 29 | |
| 30 | #if !defined MIN |
| 31 | # define MIN(a, b) ((a) < (b) ? (a) : (b)) |
| 32 | #endif |
| 33 | |
| 34 | static int |
| 35 | grn_bm_check_euc(const unsigned char *x, const size_t y) |
| 36 | { |
| 37 | const unsigned char *p; |
| 38 | for (p = x + y - 1; p >= x && *p >= 0x80U; p--) |
| 39 | ; |
| 40 | return (int)((x + y - p) & 1); |
| 41 | } |
| 42 | |
| 43 | static int |
| 44 | grn_bm_check_sjis(const unsigned char *x, const size_t y) |
| 45 | { |
| 46 | const unsigned char *p; |
| 47 | for (p = x + y - 1; p >= x; p--) |
| 48 | if ((*p < 0x81U) || (*p > 0x9fU && *p < 0xe0U) || (*p > 0xfcU)) break; |
| 49 | return (int)((x + y - p) & 1); |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | static void |
| 54 | grn_bm_suffixes(const unsigned char *x, size_t m, size_t *suff) |
| 55 | { |
| 56 | size_t f, g; |
| 57 | intptr_t i; |
| 58 | f = 0; |
| 59 | suff[m - 1] = m; |
| 60 | g = m - 1; |
| 61 | for (i = m - 2; i >= 0; --i) { |
| 62 | if (i > (intptr_t) g && suff[i + m - 1 - f] < i - g) |
| 63 | suff[i] = suff[i + m - 1 - f]; |
| 64 | else { |
| 65 | if (i < (intptr_t) g) |
| 66 | g = i; |
| 67 | f = i; |
| 68 | while (g > 0 && x[g] == x[g + m - 1 - f]) |
| 69 | --g; |
| 70 | suff[i] = f - g; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | */ |
| 75 | |
| 76 | static void |
| 77 | grn_bm_preBmBc(const unsigned char *x, size_t m, size_t *bmBc) |
| 78 | { |
| 79 | size_t i; |
| 80 | for (i = 0; i < ASIZE; ++i) { |
| 81 | bmBc[i] = m; |
| 82 | } |
| 83 | for (i = 0; i < m - 1; ++i) { |
| 84 | bmBc[(unsigned int)x[i]] = m - (i + 1); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | #define GRN_BM_COMPARE \ |
| 89 | do { \ |
| 90 | if (string_checks[found]) { \ |
| 91 | size_t offset = cond->last_offset; \ |
| 92 | ptrdiff_t found_alpha_head = cond->found_alpha_head; \ |
| 93 | /* calc real offset */ \ |
| 94 | for (i = cond->last_found; i < found; i++) { \ |
| 95 | if (string_checks[i] > 0) { \ |
| 96 | found_alpha_head = i; \ |
| 97 | offset += (size_t)(string_checks[i]); \ |
| 98 | } \ |
| 99 | } \ |
| 100 | /* if real offset is in a character, move it the head of the character \ |
| 101 | */ \ |
| 102 | if (string_checks[found] < 0) { \ |
| 103 | offset -= (size_t)(string_checks[found_alpha_head]); \ |
| 104 | cond->last_found = found_alpha_head; \ |
| 105 | } else { \ |
| 106 | cond->last_found = found; \ |
| 107 | } \ |
| 108 | cond->start_offset = cond->last_offset = offset; \ |
| 109 | if (flags & GRN_SNIP_SKIP_LEADING_SPACES) { \ |
| 110 | while (cond->start_offset < string_original_length_in_bytes && \ |
| 111 | (i = grn_isspace(string_original + cond->start_offset, \ |
| 112 | string_encoding))) { \ |
| 113 | cond->start_offset += (size_t)(i); \ |
| 114 | } \ |
| 115 | } \ |
| 116 | for (i = cond->last_found; i < found + m; i++) { \ |
| 117 | if (string_checks[i] > 0) { \ |
| 118 | offset += (size_t)(string_checks[i]); \ |
| 119 | } \ |
| 120 | } \ |
| 121 | cond->end_offset = offset; \ |
| 122 | cond->found = found + shift; \ |
| 123 | cond->found_alpha_head = found_alpha_head; \ |
| 124 | /* printf("bm: cond:%p found:%zd last_found:%zd st_off:%zd \ |
| 125 | * ed_off:%zd\n", cond, \ |
| 126 | * cond->found,cond->last_found,cond->start_offset,cond->end_offset); */ \ |
| 127 | return; \ |
| 128 | } \ |
| 129 | } while (0) |
| 130 | |
| 131 | #define GRN_BM_BM_COMPARE \ |
| 132 | do { \ |
| 133 | if (p[-2] == ck) { \ |
| 134 | for (i = 3; i <= m && p[-(intptr_t)i] == cp[-(intptr_t)i]; ++i) { \ |
| 135 | } \ |
| 136 | if (i > m) { \ |
| 137 | found = p - y - m; \ |
| 138 | GRN_BM_COMPARE; \ |
| 139 | } \ |
| 140 | } \ |
| 141 | } while (0) |
| 142 | |
| 143 | void |
| 144 | grn_bm_tunedbm(grn_ctx *ctx, snip_cond *cond, grn_obj *string, int flags) |
| 145 | { |
| 146 | register unsigned char *limit, ck; |
| 147 | register const unsigned char *p, *cp; |
| 148 | register size_t *bmBc, delta1; |
| 149 | register ptrdiff_t i; |
| 150 | |
| 151 | const unsigned char *x; |
| 152 | unsigned char *y; |
| 153 | ptrdiff_t shift; |
| 154 | ptrdiff_t found; |
| 155 | |
| 156 | const char *string_original; |
| 157 | unsigned int string_original_length_in_bytes; |
| 158 | const short *string_checks; |
| 159 | grn_encoding string_encoding; |
| 160 | const char *string_norm, *keyword_norm; |
| 161 | unsigned int n, m; |
| 162 | |
| 163 | grn_string_get_original(ctx, |
| 164 | string, |
| 165 | &string_original, |
| 166 | &string_original_length_in_bytes); |
| 167 | string_checks = grn_string_get_checks(ctx, string); |
| 168 | string_encoding = grn_string_get_encoding(ctx, string); |
| 169 | grn_string_get_normalized(ctx, string, &string_norm, &n, NULL); |
| 170 | grn_string_get_normalized(ctx, cond->keyword, &keyword_norm, &m, NULL); |
| 171 | |
| 172 | y = (unsigned char *)string_norm; |
| 173 | if (m == 1) { |
| 174 | if (n > cond->found) { |
| 175 | shift = 1; |
| 176 | p = memchr(y + cond->found, keyword_norm[0], (size_t)(n - cond->found)); |
| 177 | if (p != NULL) { |
| 178 | found = p - y; |
| 179 | GRN_BM_COMPARE; |
| 180 | } |
| 181 | } |
| 182 | cond->stopflag = SNIPCOND_STOP; |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | x = (unsigned char *)keyword_norm; |
| 187 | bmBc = cond->bmBc; |
| 188 | shift = cond->shift; |
| 189 | |
| 190 | /* Restart */ |
| 191 | p = y + m + cond->found; |
| 192 | cp = x + m; |
| 193 | ck = cp[-2]; |
| 194 | |
| 195 | /* 12 means 1(initial offset) + 10 (in loop) + 1 (shift) */ |
| 196 | if (n - cond->found > 12 * m) { |
| 197 | limit = y + n - 11 * m; |
| 198 | while (p <= limit) { |
| 199 | p += bmBc[p[-1]]; |
| 200 | if (!(delta1 = bmBc[p[-1]])) { |
| 201 | goto check; |
| 202 | } |
| 203 | p += delta1; |
| 204 | p += bmBc[p[-1]]; |
| 205 | p += bmBc[p[-1]]; |
| 206 | if (!(delta1 = bmBc[p[-1]])) { |
| 207 | goto check; |
| 208 | } |
| 209 | p += delta1; |
| 210 | p += bmBc[p[-1]]; |
| 211 | p += bmBc[p[-1]]; |
| 212 | if (!(delta1 = bmBc[p[-1]])) { |
| 213 | goto check; |
| 214 | } |
| 215 | p += delta1; |
| 216 | p += bmBc[p[-1]]; |
| 217 | p += bmBc[p[-1]]; |
| 218 | continue; |
| 219 | check: |
| 220 | GRN_BM_BM_COMPARE; |
| 221 | p += shift; |
| 222 | } |
| 223 | } |
| 224 | /* limit check + search */ |
| 225 | limit = y + n; |
| 226 | while (p <= limit) { |
| 227 | if (!(delta1 = bmBc[p[-1]])) { |
| 228 | GRN_BM_BM_COMPARE; |
| 229 | p += shift; |
| 230 | } |
| 231 | p += delta1; |
| 232 | } |
| 233 | cond->stopflag = SNIPCOND_STOP; |
| 234 | } |
| 235 | |
| 236 | static size_t |
| 237 | count_mapped_chars(const char *str, const char *end) |
| 238 | { |
| 239 | const char *p; |
| 240 | size_t dl; |
| 241 | |
| 242 | dl = 0; |
| 243 | for (p = str; p != end; p++) { |
| 244 | switch (*p) { |
| 245 | case '<': |
| 246 | case '>': |
| 247 | dl += 4; /* < or > */ |
| 248 | break; |
| 249 | case '&': |
| 250 | dl += 5; /* & */ |
| 251 | break; |
| 252 | case '"': |
| 253 | dl += 6; /* " */ |
| 254 | break; |
| 255 | default: |
| 256 | dl++; |
| 257 | break; |
| 258 | } |
| 259 | } |
| 260 | return dl; |
| 261 | } |
| 262 | |
| 263 | grn_rc |
| 264 | grn_snip_cond_close(grn_ctx *ctx, snip_cond *cond) |
| 265 | { |
| 266 | if (!cond) { |
| 267 | return GRN_INVALID_ARGUMENT; |
| 268 | } |
| 269 | if (cond->keyword) { |
| 270 | grn_obj_close(ctx, cond->keyword); |
| 271 | } |
| 272 | return GRN_SUCCESS; |
| 273 | } |
| 274 | |
| 275 | static grn_rc |
| 276 | grn_snip_cond_init(grn_ctx *ctx, |
| 277 | snip_cond *sc, |
| 278 | const char *keyword, |
| 279 | unsigned int keyword_len, |
| 280 | grn_encoding enc, |
| 281 | grn_obj *normalizer, |
| 282 | int flags) |
| 283 | { |
| 284 | const char *norm; |
| 285 | unsigned int norm_blen; |
| 286 | int f = GRN_STR_REMOVEBLANK; |
| 287 | memset(sc, 0, sizeof(snip_cond)); |
| 288 | if (!(sc->keyword = |
| 289 | grn_string_open(ctx, keyword, keyword_len, normalizer, f))) { |
| 290 | GRN_LOG(ctx, GRN_LOG_ALERT, "grn_string_open on snip_cond_init failed!"); |
| 291 | return GRN_NO_MEMORY_AVAILABLE; |
| 292 | } |
| 293 | grn_string_get_normalized(ctx, sc->keyword, &norm, &norm_blen, NULL); |
| 294 | if (!norm_blen) { |
| 295 | grn_snip_cond_close(ctx, sc); |
| 296 | return GRN_INVALID_ARGUMENT; |
| 297 | } |
| 298 | if (norm_blen != 1) { |
| 299 | grn_bm_preBmBc((unsigned char *)norm, norm_blen, sc->bmBc); |
| 300 | sc->shift = sc->bmBc[(unsigned char)norm[norm_blen - 1]]; |
| 301 | sc->bmBc[(unsigned char)norm[norm_blen - 1]] = 0; |
| 302 | } |
| 303 | return GRN_SUCCESS; |
| 304 | } |
| 305 | |
| 306 | static void |
| 307 | grn_snip_cond_reinit(snip_cond *cond) |
| 308 | { |
| 309 | cond->found = 0; |
| 310 | cond->last_found = 0; |
| 311 | cond->last_offset = 0; |
| 312 | cond->start_offset = 0; |
| 313 | cond->end_offset = 0; |
| 314 | |
| 315 | cond->count = 0; |
| 316 | cond->stopflag = SNIPCOND_NONSTOP; |
| 317 | } |
| 318 | |
| 319 | static inline char * |
| 320 | grn_snip_strndup(grn_ctx *ctx, const char *string, size_t string_len) |
| 321 | { |
| 322 | char *copied_string; |
| 323 | |
| 324 | copied_string = GRN_MALLOC(string_len + 1); |
| 325 | if (!copied_string) { |
| 326 | return NULL; |
| 327 | } |
| 328 | grn_memcpy(copied_string, string, string_len); |
| 329 | copied_string[string_len] = '\0'; /* not required, but for ql use */ |
| 330 | return copied_string; |
| 331 | } |
| 332 | |
| 333 | static inline grn_rc |
| 334 | grn_snip_cond_set_tag(grn_ctx *ctx, |
| 335 | const char **dest_tag, |
| 336 | size_t *dest_tag_len, |
| 337 | const char *tag, |
| 338 | size_t tag_len, |
| 339 | const char *default_tag, |
| 340 | size_t default_tag_len, |
| 341 | int copy_tag) |
| 342 | { |
| 343 | if (tag) { |
| 344 | if (copy_tag) { |
| 345 | char *copied_tag; |
| 346 | copied_tag = grn_snip_strndup(ctx, tag, tag_len); |
| 347 | if (!copied_tag) { |
| 348 | return GRN_NO_MEMORY_AVAILABLE; |
| 349 | } |
| 350 | *dest_tag = copied_tag; |
| 351 | } else { |
| 352 | *dest_tag = tag; |
| 353 | } |
| 354 | *dest_tag_len = tag_len; |
| 355 | } else { |
| 356 | *dest_tag = default_tag; |
| 357 | *dest_tag_len = default_tag_len; |
| 358 | } |
| 359 | return GRN_SUCCESS; |
| 360 | } |
| 361 | |
| 362 | grn_rc |
| 363 | grn_snip_set_normalizer(grn_ctx *ctx, grn_obj *snip, grn_obj *normalizer) |
| 364 | { |
| 365 | grn_snip *snip_; |
| 366 | if (!snip) { |
| 367 | return GRN_INVALID_ARGUMENT; |
| 368 | } |
| 369 | |
| 370 | snip_ = (grn_snip *)snip; |
| 371 | if (snip_->normalizer != normalizer) { |
| 372 | if (snip_->normalizer && snip_->normalizer != GRN_NORMALIZER_AUTO) { |
| 373 | grn_obj_unref(ctx, snip_->normalizer); |
| 374 | } |
| 375 | snip_->normalizer = normalizer; |
| 376 | if (snip_->normalizer && snip_->normalizer != GRN_NORMALIZER_AUTO) { |
| 377 | grn_obj_refer(ctx, snip_->normalizer); |
| 378 | } |
| 379 | } |
| 380 | return GRN_SUCCESS; |
| 381 | } |
| 382 | |
| 383 | grn_obj * |
| 384 | grn_snip_get_normalizer(grn_ctx *ctx, grn_obj *snip) |
| 385 | { |
| 386 | grn_snip *snip_; |
| 387 | |
| 388 | if (!snip) { |
| 389 | return NULL; |
| 390 | } |
| 391 | |
| 392 | snip_ = (grn_snip *)snip; |
| 393 | return snip_->normalizer; |
| 394 | } |
| 395 | |
| 396 | grn_rc |
| 397 | grn_snip_set_normalizers(grn_ctx *ctx, grn_obj *snip, grn_obj *normalizers) |
| 398 | { |
| 399 | grn_snip *snip_; |
| 400 | if (!snip) { |
| 401 | return GRN_INVALID_ARGUMENT; |
| 402 | } |
| 403 | |
| 404 | if (!grn_obj_is_text_family_bulk(ctx, normalizers)) { |
| 405 | return GRN_INVALID_ARGUMENT; |
| 406 | } |
| 407 | |
| 408 | snip_ = (grn_snip *)snip; |
| 409 | if (GRN_TEXT_LEN(normalizers) == 0) { |
| 410 | GRN_BULK_REWIND(&(snip_->normalizers)); |
| 411 | return GRN_SUCCESS; |
| 412 | } |
| 413 | |
| 414 | if (!snip_->lexicon) { |
| 415 | /* This is only used for passing normalizer options to grn_string. */ |
| 416 | snip_->lexicon = grn_table_create(ctx, |
| 417 | NULL, |
| 418 | 0, |
| 419 | NULL, |
| 420 | GRN_OBJ_TABLE_HASH_KEY, |
| 421 | grn_ctx_at(ctx, GRN_DB_SHORT_TEXT), |
| 422 | NULL); |
| 423 | if (!snip_->lexicon) { |
| 424 | return ctx->rc; |
| 425 | } |
| 426 | } |
| 427 | grn_obj_set_info(ctx, snip_->lexicon, GRN_INFO_NORMALIZERS, normalizers); |
| 428 | if (ctx->rc == GRN_SUCCESS) { |
| 429 | GRN_TEXT_SET(ctx, |
| 430 | &(snip_->normalizers), |
| 431 | GRN_TEXT_VALUE(normalizers), |
| 432 | GRN_TEXT_LEN(normalizers)); |
| 433 | } |
| 434 | return ctx->rc; |
| 435 | } |
| 436 | |
| 437 | grn_obj * |
| 438 | grn_snip_get_normalizers(grn_ctx *ctx, grn_obj *snip) |
| 439 | { |
| 440 | grn_snip *snip_; |
| 441 | |
| 442 | if (!snip) { |
| 443 | return NULL; |
| 444 | } |
| 445 | |
| 446 | snip_ = (grn_snip *)snip; |
| 447 | return &(snip_->normalizers); |
| 448 | } |
| 449 | |
| 450 | grn_rc |
| 451 | grn_snip_set_delimiter_regexp(grn_ctx *ctx, |
| 452 | grn_obj *snip, |
| 453 | const char *pattern, |
| 454 | int pattern_length) |
| 455 | { |
| 456 | const char *tag = "[snip][delimiter-regexp][set]"; |
| 457 | |
| 458 | if (!snip) { |
| 459 | return GRN_INVALID_ARGUMENT; |
| 460 | } |
| 461 | |
| 462 | GRN_API_ENTER; |
| 463 | #ifdef GRN_SUPPORT_REGEXP |
| 464 | grn_snip *snip_ = (grn_snip *)snip; |
| 465 | if (snip_->delimiter_regex) { |
| 466 | onig_free(snip_->delimiter_regex); |
| 467 | } |
| 468 | if (snip_->delimiter_pattern) { |
| 469 | GRN_FREE(snip_->delimiter_pattern); |
| 470 | } |
| 471 | if (pattern) { |
| 472 | if (pattern_length < 0) { |
| 473 | snip_->delimiter_pattern_length = strlen(pattern); |
| 474 | } else { |
| 475 | snip_->delimiter_pattern_length = (size_t)pattern_length; |
| 476 | } |
| 477 | snip_->delimiter_pattern = |
| 478 | grn_snip_strndup(ctx, pattern, snip_->delimiter_pattern_length); |
| 479 | if (snip_->delimiter_pattern) { |
| 480 | snip_->delimiter_regex = grn_onigmo_new(ctx, |
| 481 | pattern, |
| 482 | snip_->delimiter_pattern_length, |
| 483 | GRN_ONIGMO_OPTION_DEFAULT, |
| 484 | GRN_ONIGMO_SYNTAX_DEFAULT, |
| 485 | tag); |
| 486 | } |
| 487 | } else { |
| 488 | snip_->delimiter_regex = NULL; |
| 489 | snip_->delimiter_pattern = NULL; |
| 490 | } |
| 491 | #else |
| 492 | ERR(GRN_FUNCTION_NOT_IMPLEMENTED, "%s Onigmo isn't enabled", tag); |
| 493 | #endif |
| 494 | GRN_API_RETURN(ctx->rc); |
| 495 | } |
| 496 | |
| 497 | const char * |
| 498 | grn_snip_get_delimiter_regexp(grn_ctx *ctx, grn_obj *snip, size_t *length) |
| 499 | { |
| 500 | grn_snip *snip_; |
| 501 | |
| 502 | if (!snip) { |
| 503 | if (length) { |
| 504 | *length = 0; |
| 505 | } |
| 506 | return NULL; |
| 507 | } |
| 508 | |
| 509 | snip_ = (grn_snip *)snip; |
| 510 | if (length) { |
| 511 | *length = snip_->delimiter_pattern_length; |
| 512 | } |
| 513 | return snip_->delimiter_pattern; |
| 514 | } |
| 515 | |
| 516 | static grn_rc |
| 517 | grn_snip_expand_cond(grn_ctx *ctx, grn_snip *snip) |
| 518 | { |
| 519 | size_t new_cond_capacity = snip->cond_capacity * 2; |
| 520 | size_t new_cond_size = sizeof(snip_cond) * new_cond_capacity; |
| 521 | snip_cond *new_cond = (snip_cond *)GRN_REALLOC(snip->cond, new_cond_size); |
| 522 | if (!new_cond) { |
| 523 | return ctx->rc; |
| 524 | } |
| 525 | snip->cond = new_cond; |
| 526 | snip->cond_capacity = new_cond_capacity; |
| 527 | return GRN_SUCCESS; |
| 528 | } |
| 529 | |
| 530 | grn_rc |
| 531 | grn_snip_add_cond(grn_ctx *ctx, |
| 532 | grn_obj *snip, |
| 533 | const char *keyword, |
| 534 | unsigned int keyword_len, |
| 535 | const char *opentag, |
| 536 | unsigned int opentag_len, |
| 537 | const char *closetag, |
| 538 | unsigned int closetag_len) |
| 539 | { |
| 540 | grn_rc rc; |
| 541 | int copy_tag; |
| 542 | snip_cond *cond; |
| 543 | unsigned int norm_blen; |
| 544 | grn_snip *snip_; |
| 545 | |
| 546 | snip_ = (grn_snip *)snip; |
| 547 | if (!snip_ || !keyword || !keyword_len) { |
| 548 | return GRN_INVALID_ARGUMENT; |
| 549 | } |
| 550 | |
| 551 | if (snip_->cond_len >= snip_->cond_capacity) { |
| 552 | rc = grn_snip_expand_cond(ctx, snip_); |
| 553 | if (rc) { |
| 554 | return rc; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | cond = snip_->cond + snip_->cond_len; |
| 559 | rc = grn_snip_cond_init(ctx, |
| 560 | cond, |
| 561 | keyword, |
| 562 | keyword_len, |
| 563 | snip_->encoding, |
| 564 | (GRN_TEXT_LEN(&(snip_->normalizers)) > 0) |
| 565 | ? snip_->lexicon |
| 566 | : snip_->normalizer, |
| 567 | snip_->flags); |
| 568 | if (rc) { |
| 569 | return rc; |
| 570 | } |
| 571 | grn_string_get_normalized(ctx, cond->keyword, NULL, &norm_blen, NULL); |
| 572 | if (norm_blen > snip_->width) { |
| 573 | grn_snip_cond_close(ctx, cond); |
| 574 | return GRN_INVALID_ARGUMENT; |
| 575 | } |
| 576 | |
| 577 | copy_tag = snip_->flags & GRN_SNIP_COPY_TAG; |
| 578 | rc = grn_snip_cond_set_tag(ctx, |
| 579 | &(cond->opentag), |
| 580 | &(cond->opentag_len), |
| 581 | opentag, |
| 582 | opentag_len, |
| 583 | snip_->defaultopentag, |
| 584 | snip_->defaultopentag_len, |
| 585 | copy_tag); |
| 586 | if (rc) { |
| 587 | grn_snip_cond_close(ctx, cond); |
| 588 | return rc; |
| 589 | } |
| 590 | |
| 591 | rc = grn_snip_cond_set_tag(ctx, |
| 592 | &(cond->closetag), |
| 593 | &(cond->closetag_len), |
| 594 | closetag, |
| 595 | closetag_len, |
| 596 | snip_->defaultclosetag, |
| 597 | snip_->defaultclosetag_len, |
| 598 | copy_tag); |
| 599 | if (rc) { |
| 600 | if (opentag && copy_tag) { |
| 601 | GRN_FREE((void *)cond->opentag); |
| 602 | } |
| 603 | grn_snip_cond_close(ctx, cond); |
| 604 | return rc; |
| 605 | } |
| 606 | |
| 607 | snip_->cond_len++; |
| 608 | return GRN_SUCCESS; |
| 609 | } |
| 610 | |
| 611 | static size_t |
| 612 | grn_snip_find_firstbyte(const char *string, |
| 613 | grn_encoding encoding, |
| 614 | size_t offset, |
| 615 | ssize_t doffset) |
| 616 | { |
| 617 | switch (encoding) { |
| 618 | case GRN_ENC_EUC_JP: |
| 619 | while (!(grn_bm_check_euc((unsigned char *)string, offset))) |
| 620 | offset = (size_t)((ssize_t)offset + doffset); |
| 621 | break; |
| 622 | case GRN_ENC_SJIS: |
| 623 | if (!(grn_bm_check_sjis((unsigned char *)string, offset))) |
| 624 | offset = (size_t)((ssize_t)offset + doffset); |
| 625 | break; |
| 626 | case GRN_ENC_UTF8: |
| 627 | while ((signed char)string[offset] <= (signed char)0xc0) |
| 628 | offset = (size_t)((ssize_t)offset + doffset); |
| 629 | break; |
| 630 | default: |
| 631 | break; |
| 632 | } |
| 633 | return offset; |
| 634 | } |
| 635 | |
| 636 | static inline grn_rc |
| 637 | grn_snip_set_default_tag(grn_ctx *ctx, |
| 638 | const char **dest_tag, |
| 639 | size_t *dest_tag_len, |
| 640 | const char *tag, |
| 641 | unsigned int tag_len, |
| 642 | int copy_tag) |
| 643 | { |
| 644 | if (copy_tag && tag) { |
| 645 | char *copied_tag; |
| 646 | copied_tag = grn_snip_strndup(ctx, tag, tag_len); |
| 647 | if (!copied_tag) { |
| 648 | return GRN_NO_MEMORY_AVAILABLE; |
| 649 | } |
| 650 | *dest_tag = copied_tag; |
| 651 | } else { |
| 652 | *dest_tag = tag; |
| 653 | } |
| 654 | *dest_tag_len = tag_len; |
| 655 | return GRN_SUCCESS; |
| 656 | } |
| 657 | |
| 658 | grn_obj * |
| 659 | grn_snip_open(grn_ctx *ctx, |
| 660 | int flags, |
| 661 | unsigned int width, |
| 662 | unsigned int max_results, |
| 663 | const char *defaultopentag, |
| 664 | unsigned int defaultopentag_len, |
| 665 | const char *defaultclosetag, |
| 666 | unsigned int defaultclosetag_len, |
| 667 | grn_snip_mapping *mapping) |
| 668 | { |
| 669 | int copy_tag; |
| 670 | grn_snip *ret = NULL; |
| 671 | if (!(ret = GRN_CALLOC(sizeof(grn_snip)))) { |
| 672 | GRN_LOG(ctx, GRN_LOG_ALERT, "grn_snip allocation failed on grn_snip_open"); |
| 673 | return NULL; |
| 674 | } |
| 675 | if (max_results > MAX_SNIP_RESULT_COUNT || max_results == 0) { |
| 676 | GRN_LOG(ctx, GRN_LOG_WARNING, "max_results is invalid on grn_snip_open"); |
| 677 | GRN_FREE(ret); |
| 678 | return NULL; |
| 679 | } |
| 680 | GRN_API_ENTER; |
| 681 | ret->encoding = ctx->encoding; |
| 682 | ret->flags = flags; |
| 683 | ret->width = width; |
| 684 | ret->max_results = max_results; |
| 685 | ret->defaultopentag = NULL; |
| 686 | ret->defaultclosetag = NULL; |
| 687 | |
| 688 | copy_tag = flags & GRN_SNIP_COPY_TAG; |
| 689 | if (grn_snip_set_default_tag(ctx, |
| 690 | &(ret->defaultopentag), |
| 691 | &(ret->defaultopentag_len), |
| 692 | defaultopentag, |
| 693 | defaultopentag_len, |
| 694 | copy_tag)) { |
| 695 | GRN_FREE(ret); |
| 696 | GRN_API_RETURN(NULL); |
| 697 | } |
| 698 | |
| 699 | if (grn_snip_set_default_tag(ctx, |
| 700 | &(ret->defaultclosetag), |
| 701 | &(ret->defaultclosetag_len), |
| 702 | defaultclosetag, |
| 703 | defaultclosetag_len, |
| 704 | copy_tag)) { |
| 705 | if (copy_tag && ret->defaultopentag) { |
| 706 | GRN_FREE((void *)ret->defaultopentag); |
| 707 | } |
| 708 | GRN_FREE(ret); |
| 709 | GRN_API_RETURN(NULL); |
| 710 | } |
| 711 | |
| 712 | ret->cond_capacity = DEFAULT_SNIP_COND_CAPACITY; |
| 713 | ret->cond = (snip_cond *)GRN_MALLOC(sizeof(snip_cond) * ret->cond_capacity); |
| 714 | |
| 715 | if (!ret->cond) { |
| 716 | if (copy_tag) { |
| 717 | if (ret->defaultopentag) { |
| 718 | GRN_FREE((void *)ret->defaultopentag); |
| 719 | } |
| 720 | if (ret->defaultclosetag) { |
| 721 | GRN_FREE((void *)ret->defaultclosetag); |
| 722 | } |
| 723 | } |
| 724 | GRN_FREE(ret); |
| 725 | GRN_API_RETURN(NULL); |
| 726 | } |
| 727 | |
| 728 | ret->cond_len = 0; |
| 729 | ret->mapping = mapping; |
| 730 | ret->nstr = NULL; |
| 731 | ret->tag_count = 0; |
| 732 | ret->snip_count = 0; |
| 733 | if (ret->flags & GRN_SNIP_NORMALIZE) { |
| 734 | ret->normalizer = GRN_NORMALIZER_AUTO; |
| 735 | } else { |
| 736 | ret->normalizer = NULL; |
| 737 | } |
| 738 | ret->lexicon = NULL; |
| 739 | GRN_TEXT_INIT(&(ret->normalizers), 0); |
| 740 | ret->delimiter_pattern = NULL; |
| 741 | ret->delimiter_pattern_length = 0; |
| 742 | #ifdef GRN_SUPPORT_REGEXP |
| 743 | ret->delimiter_regex = NULL; |
| 744 | #endif |
| 745 | |
| 746 | GRN_DB_OBJ_SET_TYPE(ret, GRN_SNIP); |
| 747 | { |
| 748 | grn_obj *db; |
| 749 | grn_id id; |
| 750 | db = grn_ctx_db(ctx); |
| 751 | id = grn_obj_register(ctx, db, NULL, 0); |
| 752 | DB_OBJ(ret)->header.domain = GRN_ID_NIL; |
| 753 | DB_OBJ(ret)->range = GRN_ID_NIL; |
| 754 | grn_db_obj_init(ctx, db, id, DB_OBJ(ret)); |
| 755 | } |
| 756 | |
| 757 | GRN_API_RETURN((grn_obj *)ret); |
| 758 | } |
| 759 | |
| 760 | static grn_rc |
| 761 | exec_clean(grn_ctx *ctx, grn_snip *snip) |
| 762 | { |
| 763 | snip_cond *cond, *cond_end; |
| 764 | if (snip->nstr) { |
| 765 | grn_obj_close(ctx, snip->nstr); |
| 766 | snip->nstr = NULL; |
| 767 | } |
| 768 | snip->tag_count = 0; |
| 769 | snip->snip_count = 0; |
| 770 | for (cond = snip->cond, cond_end = cond + snip->cond_len; cond < cond_end; |
| 771 | cond++) { |
| 772 | grn_snip_cond_reinit(cond); |
| 773 | } |
| 774 | return GRN_SUCCESS; |
| 775 | } |
| 776 | |
| 777 | grn_rc |
| 778 | grn_snip_close(grn_ctx *ctx, grn_snip *snip) |
| 779 | { |
| 780 | snip_cond *cond, *cond_end; |
| 781 | if (!snip) { |
| 782 | return GRN_INVALID_ARGUMENT; |
| 783 | } |
| 784 | GRN_API_ENTER; |
| 785 | #ifdef GRN_SUPPORT_REGEXP |
| 786 | if (snip->delimiter_regex) { |
| 787 | onig_free(snip->delimiter_regex); |
| 788 | } |
| 789 | #endif |
| 790 | if (snip->delimiter_pattern) { |
| 791 | GRN_FREE(snip->delimiter_pattern); |
| 792 | } |
| 793 | if (snip->normalizer && snip->normalizer != GRN_NORMALIZER_AUTO) { |
| 794 | grn_obj_unref(ctx, snip->normalizer); |
| 795 | } |
| 796 | if (snip->lexicon) { |
| 797 | grn_obj_close(ctx, snip->lexicon); |
| 798 | } |
| 799 | GRN_OBJ_FIN(ctx, &(snip->normalizers)); |
| 800 | if (snip->flags & GRN_SNIP_COPY_TAG) { |
| 801 | size_t i; |
| 802 | snip_cond *sc; |
| 803 | const char *dot = snip->defaultopentag, *dct = snip->defaultclosetag; |
| 804 | for (i = snip->cond_len, sc = snip->cond; i; i--, sc++) { |
| 805 | if (sc->opentag != dot) { |
| 806 | GRN_FREE((void *)sc->opentag); |
| 807 | } |
| 808 | if (sc->closetag != dct) { |
| 809 | GRN_FREE((void *)sc->closetag); |
| 810 | } |
| 811 | } |
| 812 | if (dot) { |
| 813 | GRN_FREE((void *)dot); |
| 814 | } |
| 815 | if (dct) { |
| 816 | GRN_FREE((void *)dct); |
| 817 | } |
| 818 | } |
| 819 | if (snip->nstr) { |
| 820 | grn_obj_close(ctx, snip->nstr); |
| 821 | } |
| 822 | for (cond = snip->cond, cond_end = cond + snip->cond_len; cond < cond_end; |
| 823 | cond++) { |
| 824 | grn_snip_cond_close(ctx, cond); |
| 825 | } |
| 826 | GRN_FREE(snip->cond); |
| 827 | GRN_FREE(snip); |
| 828 | GRN_API_RETURN(GRN_SUCCESS); |
| 829 | } |
| 830 | |
| 831 | grn_rc |
| 832 | grn_snip_exec(grn_ctx *ctx, |
| 833 | grn_obj *snip, |
| 834 | const char *string, |
| 835 | unsigned int string_len, |
| 836 | unsigned int *nresults, |
| 837 | unsigned int *max_tagged_len) |
| 838 | { |
| 839 | size_t i; |
| 840 | grn_snip *snip_; |
| 841 | int f = GRN_STR_WITH_CHECKS | GRN_STR_REMOVEBLANK; |
| 842 | if (!snip || !string || !nresults || !max_tagged_len) { |
| 843 | return GRN_INVALID_ARGUMENT; |
| 844 | } |
| 845 | GRN_API_ENTER; |
| 846 | snip_ = (grn_snip *)snip; |
| 847 | exec_clean(ctx, snip_); |
| 848 | *nresults = 0; |
| 849 | grn_obj *lexicon_or_normalizer = (GRN_TEXT_LEN(&(snip_->normalizers)) > 0) |
| 850 | ? snip_->lexicon |
| 851 | : snip_->normalizer; |
| 852 | snip_->nstr = |
| 853 | grn_string_open(ctx, string, string_len, lexicon_or_normalizer, f); |
| 854 | if (!snip_->nstr) { |
| 855 | exec_clean(ctx, snip_); |
| 856 | GRN_LOG(ctx, GRN_LOG_ALERT, "grn_string_open on grn_snip_exec failed !"); |
| 857 | GRN_API_RETURN(ctx->rc); |
| 858 | } |
| 859 | for (i = 0; i < snip_->cond_len; i++) { |
| 860 | grn_bm_tunedbm(ctx, snip_->cond + i, snip_->nstr, snip_->flags); |
| 861 | } |
| 862 | |
| 863 | { |
| 864 | _snip_tag_result *tag_result = snip_->tag_result; |
| 865 | _snip_result *snip_result = snip_->snip_result; |
| 866 | size_t last_end_offset = 0, last_last_end_offset = 0; |
| 867 | size_t unfound_cond_count = snip_->cond_len; |
| 868 | |
| 869 | *max_tagged_len = 0; |
| 870 | while (1) { |
| 871 | size_t tagged_len = 0, last_tag_end = 0; |
| 872 | int_least8_t all_stop = 1, found_cond = 0; |
| 873 | snip_result->tag_count = 0; |
| 874 | |
| 875 | while (1) { |
| 876 | size_t min_start_offset = (size_t)-1; |
| 877 | size_t max_end_offset = 0; |
| 878 | snip_cond *cond = NULL; |
| 879 | |
| 880 | /* get condition which have minimum offset and is not stopped */ |
| 881 | for (i = 0; i < snip_->cond_len; i++) { |
| 882 | if (snip_->cond[i].stopflag == SNIPCOND_NONSTOP && |
| 883 | (min_start_offset > snip_->cond[i].start_offset || |
| 884 | (min_start_offset == snip_->cond[i].start_offset && |
| 885 | max_end_offset < snip_->cond[i].end_offset))) { |
| 886 | min_start_offset = snip_->cond[i].start_offset; |
| 887 | max_end_offset = snip_->cond[i].end_offset; |
| 888 | cond = &snip_->cond[i]; |
| 889 | } |
| 890 | } |
| 891 | if (!cond) { |
| 892 | break; |
| 893 | } |
| 894 | /* check whether condition is the first condition in snippet */ |
| 895 | if (snip_result->tag_count == 0) { |
| 896 | /* skip condition if the number of rest snippet field is smaller than |
| 897 | */ |
| 898 | /* the number of unfound keywords. */ |
| 899 | if (snip_->max_results - *nresults <= unfound_cond_count && |
| 900 | cond->count > 0) { |
| 901 | bool exclude_other_cond = true; |
| 902 | for (i = 0; i < snip_->cond_len; i++) { |
| 903 | if ((snip_->cond + i) != cond && |
| 904 | snip_->cond[i].end_offset <= |
| 905 | cond->start_offset + snip_->width && |
| 906 | snip_->cond[i].count == 0) { |
| 907 | exclude_other_cond = false; |
| 908 | break; |
| 909 | } |
| 910 | } |
| 911 | if (exclude_other_cond) { |
| 912 | grn_bm_tunedbm(ctx, cond, snip_->nstr, snip_->flags); |
| 913 | continue; |
| 914 | } |
| 915 | } |
| 916 | snip_result->start_offset = cond->start_offset; |
| 917 | snip_result->first_tag_result_idx = snip_->tag_count; |
| 918 | } else { |
| 919 | #ifdef GRN_SUPPORT_REGEXP |
| 920 | if (snip_->delimiter_regex) { |
| 921 | size_t range_offset = |
| 922 | MIN(snip_result->start_offset + snip_->width, cond->start_offset); |
| 923 | OnigPosition position = |
| 924 | onig_search(snip_->delimiter_regex, |
| 925 | string, |
| 926 | string + string_len, |
| 927 | string + snip_result->start_offset, |
| 928 | string + range_offset, |
| 929 | NULL, |
| 930 | ONIG_OPTION_NONE); |
| 931 | if (position != ONIG_MISMATCH) { |
| 932 | break; |
| 933 | } |
| 934 | } |
| 935 | #endif |
| 936 | if (cond->start_offset >= snip_result->start_offset + snip_->width) { |
| 937 | break; |
| 938 | } |
| 939 | /* check nesting to make valid HTML */ |
| 940 | /* ToDo: allow <test><te>te</te><st>st</st></test> */ |
| 941 | if (cond->start_offset < last_tag_end) { |
| 942 | grn_bm_tunedbm(ctx, cond, snip_->nstr, snip_->flags); |
| 943 | continue; |
| 944 | } |
| 945 | } |
| 946 | if (cond->end_offset > snip_result->start_offset + snip_->width) { |
| 947 | /* If a keyword gets across a snippet, */ |
| 948 | /* it was skipped and never to be tagged. */ |
| 949 | cond->stopflag = SNIPCOND_ACROSS; |
| 950 | grn_bm_tunedbm(ctx, cond, snip_->nstr, snip_->flags); |
| 951 | } else { |
| 952 | found_cond = 1; |
| 953 | if (cond->count == 0) { |
| 954 | unfound_cond_count--; |
| 955 | } |
| 956 | cond->count++; |
| 957 | last_end_offset = cond->end_offset; |
| 958 | |
| 959 | tag_result->cond = cond; |
| 960 | tag_result->start_offset = cond->start_offset; |
| 961 | tag_result->end_offset = last_tag_end = cond->end_offset; |
| 962 | |
| 963 | snip_result->tag_count++; |
| 964 | tag_result++; |
| 965 | tagged_len += cond->opentag_len + cond->closetag_len; |
| 966 | if (++snip_->tag_count >= MAX_SNIP_TAG_COUNT) { |
| 967 | break; |
| 968 | } |
| 969 | grn_bm_tunedbm(ctx, cond, snip_->nstr, snip_->flags); |
| 970 | } |
| 971 | } |
| 972 | if (!found_cond) { |
| 973 | break; |
| 974 | } |
| 975 | { |
| 976 | /* |
| 977 | No overlap case: |
| 978 | keyword = hij |
| 979 | width = 10 |
| 980 | string = abcdefghijklmnopqrstuvwxyz |
| 981 | |
| 982 | abcdefghijklmnopqrstuvwxyz |
| 983 | ^ |
| 984 | match_start_offset |
| 985 | abcdefghijklmnopqrstuvwxyz |
| 986 | ^ |
| 987 | match_end_offset |
| 988 | abcdefghijklmnopqrstuvwxyz |
| 989 | <-> |
| 990 | match_width |
| 991 | abcdefghijklmnopqrstuvwxyz |
| 992 | <--> <-> |
| 993 | around_width |
| 994 | abcdefghijklmnopqrstuvwxyz |
| 995 | <--> |
| 996 | pre_width |
| 997 | abcdefghijklmnopqrstuvwxyz |
| 998 | <-> |
| 999 | post_width |
| 1000 | abcdefghijklmnopqrstuvwxyz |
| 1001 | <--------> |
| 1002 | snip_->width |
| 1003 | |
| 1004 | Overlapped case: |
| 1005 | keyword = hij |
| 1006 | width = 15 |
| 1007 | string = abcdefghijklhijklmnopq |
| 1008 | |
| 1009 | abcdefghijklhijklmnopq |
| 1010 | ^ |
| 1011 | match_start_offset |
| 1012 | abcdefghijklhijklmnopq |
| 1013 | ^ |
| 1014 | match_end_offset |
| 1015 | abcdefghijklhijklmnopq |
| 1016 | <------> |
| 1017 | match_width |
| 1018 | abcdefghijklhijklmnopq |
| 1019 | <--> <-> |
| 1020 | around_width |
| 1021 | abcdefghijklhijklmnopq |
| 1022 | <--> |
| 1023 | pre_width |
| 1024 | abcdefghijklhijklmnopq |
| 1025 | <-> |
| 1026 | post_width |
| 1027 | abcdefghijklhijklmnopq |
| 1028 | <-------------> |
| 1029 | snip_->width |
| 1030 | |
| 1031 | Delimiter regexp case: |
| 1032 | keyword = hij |
| 1033 | width = 10 |
| 1034 | delimiter_regexp = X |
| 1035 | string = abcdXfghijkXmnopqrstuvwxyz |
| 1036 | |
| 1037 | abcdXfghijkXmnopqrstuvwxyz |
| 1038 | ^ |
| 1039 | match_start_offset |
| 1040 | abcdXfghijkXmnopqrstuvwxyz |
| 1041 | ^ |
| 1042 | match_end_offset |
| 1043 | abcdXfghijkXmnopqrstuvwxyz |
| 1044 | <-> |
| 1045 | match_width |
| 1046 | abcdXfghijkXmnopqrstuvwxyz |
| 1047 | <--> <-> |
| 1048 | around_width |
| 1049 | abcdXfghijkXmnopqrstuvwxyz |
| 1050 | <--> |
| 1051 | pre_width |
| 1052 | abcdXfghijkXmnopqrstuvwxyz |
| 1053 | <-> |
| 1054 | post_width |
| 1055 | abcdXfghijkXmnopqrstuvwxyz |
| 1056 | <--------> |
| 1057 | snip_->width |
| 1058 | abcdXfghijkXmnopqrstuvwxyz |
| 1059 | ^ |
| 1060 | snip_result->start_offset (after processed) |
| 1061 | abcdXfghijkXmnopqrstuvwxyz |
| 1062 | ^ |
| 1063 | snip_result->end_offset (after processed) |
| 1064 | */ |
| 1065 | size_t match_start_offset = snip_result->start_offset; |
| 1066 | size_t match_end_offset = last_end_offset; |
| 1067 | size_t match_width = match_end_offset - match_start_offset; |
| 1068 | size_t pre_width; |
| 1069 | size_t post_width; |
| 1070 | if (match_width >= snip_->width) { |
| 1071 | pre_width = post_width = 0; |
| 1072 | } else { |
| 1073 | size_t around_width = snip_->width - match_width; |
| 1074 | pre_width = post_width = around_width / 2; |
| 1075 | if (around_width % 2 == 1) { |
| 1076 | pre_width++; |
| 1077 | } |
| 1078 | } |
| 1079 | { |
| 1080 | size_t start_offset_min; |
| 1081 | if (match_start_offset < pre_width) { |
| 1082 | start_offset_min = 0; |
| 1083 | } else { |
| 1084 | if (match_end_offset + post_width >= string_len) { |
| 1085 | if (string_len < snip_->width) { |
| 1086 | start_offset_min = 0; |
| 1087 | } else { |
| 1088 | start_offset_min = string_len - snip_->width; |
| 1089 | } |
| 1090 | } else { |
| 1091 | start_offset_min = match_start_offset - pre_width; |
| 1092 | } |
| 1093 | start_offset_min = grn_snip_find_firstbyte(string, |
| 1094 | snip_->encoding, |
| 1095 | start_offset_min, |
| 1096 | 1); |
| 1097 | } |
| 1098 | #ifdef GRN_SUPPORT_REGEXP |
| 1099 | if (snip_->delimiter_regex) { |
| 1100 | OnigRegion region; |
| 1101 | onig_region_init(®ion); |
| 1102 | OnigPosition position = onig_search(snip_->delimiter_regex, |
| 1103 | string, |
| 1104 | string + string_len, |
| 1105 | string + match_start_offset, |
| 1106 | string + start_offset_min, |
| 1107 | ®ion, |
| 1108 | ONIG_OPTION_NONE); |
| 1109 | if (position != ONIG_MISMATCH) { |
| 1110 | start_offset_min = (size_t)(region.end[0]); |
| 1111 | onig_region_free(®ion, 0); |
| 1112 | } |
| 1113 | } |
| 1114 | #endif |
| 1115 | start_offset_min = MAX(start_offset_min, last_last_end_offset); |
| 1116 | snip_result->start_offset = grn_snip_find_firstbyte(string, |
| 1117 | snip_->encoding, |
| 1118 | start_offset_min, |
| 1119 | 1); |
| 1120 | } |
| 1121 | |
| 1122 | { |
| 1123 | size_t end_offset_max = snip_result->start_offset + snip_->width; |
| 1124 | if (end_offset_max < match_end_offset) { |
| 1125 | end_offset_max = match_end_offset + post_width; |
| 1126 | } |
| 1127 | #ifdef GRN_SUPPORT_REGEXP |
| 1128 | if (snip_->delimiter_regex) { |
| 1129 | size_t valid_end_offset_max; |
| 1130 | if (end_offset_max >= string_len) { |
| 1131 | valid_end_offset_max = string_len; |
| 1132 | } else { |
| 1133 | valid_end_offset_max = grn_snip_find_firstbyte(string, |
| 1134 | snip_->encoding, |
| 1135 | end_offset_max, |
| 1136 | -1); |
| 1137 | } |
| 1138 | OnigRegion region; |
| 1139 | onig_region_init(®ion); |
| 1140 | OnigPosition position = onig_search(snip_->delimiter_regex, |
| 1141 | string, |
| 1142 | string + string_len, |
| 1143 | string + match_end_offset, |
| 1144 | string + valid_end_offset_max, |
| 1145 | ®ion, |
| 1146 | ONIG_OPTION_NONE); |
| 1147 | if (position != ONIG_MISMATCH) { |
| 1148 | end_offset_max = (size_t)(region.beg[0]); |
| 1149 | onig_region_free(®ion, 0); |
| 1150 | } |
| 1151 | } |
| 1152 | #endif |
| 1153 | if (end_offset_max >= string_len) { |
| 1154 | snip_result->end_offset = string_len; |
| 1155 | } else { |
| 1156 | snip_result->end_offset = grn_snip_find_firstbyte(string, |
| 1157 | snip_->encoding, |
| 1158 | end_offset_max, |
| 1159 | -1); |
| 1160 | } |
| 1161 | } |
| 1162 | } |
| 1163 | last_last_end_offset = snip_result->end_offset; |
| 1164 | |
| 1165 | if (snip_->mapping == GRN_SNIP_MAPPING_HTML_ESCAPE) { |
| 1166 | tagged_len += count_mapped_chars(&string[snip_result->start_offset], |
| 1167 | &string[snip_result->end_offset]) + |
| 1168 | 1; |
| 1169 | } else { |
| 1170 | tagged_len += snip_result->end_offset - snip_result->start_offset + 1; |
| 1171 | } |
| 1172 | |
| 1173 | *max_tagged_len = MAX(*max_tagged_len, (unsigned int)tagged_len); |
| 1174 | |
| 1175 | snip_result->last_tag_result_idx = snip_->tag_count - 1; |
| 1176 | (*nresults)++; |
| 1177 | snip_result++; |
| 1178 | |
| 1179 | if (*nresults == snip_->max_results || |
| 1180 | snip_->tag_count == MAX_SNIP_TAG_COUNT) { |
| 1181 | break; |
| 1182 | } |
| 1183 | for (i = 0; i < snip_->cond_len; i++) { |
| 1184 | if (snip_->cond[i].stopflag != SNIPCOND_STOP) { |
| 1185 | all_stop = 0; |
| 1186 | snip_->cond[i].stopflag = SNIPCOND_NONSTOP; |
| 1187 | } |
| 1188 | } |
| 1189 | if (all_stop) { |
| 1190 | break; |
| 1191 | } |
| 1192 | } |
| 1193 | } |
| 1194 | snip_->snip_count = *nresults; |
| 1195 | snip_->string = string; |
| 1196 | |
| 1197 | snip_->max_tagged_len = *max_tagged_len; |
| 1198 | |
| 1199 | GRN_API_RETURN(ctx->rc); |
| 1200 | } |
| 1201 | |
| 1202 | grn_rc |
| 1203 | grn_snip_get_result(grn_ctx *ctx, |
| 1204 | grn_obj *snip, |
| 1205 | const unsigned int index, |
| 1206 | char *result, |
| 1207 | unsigned int *result_len) |
| 1208 | { |
| 1209 | char *p; |
| 1210 | size_t i, j, k; |
| 1211 | _snip_result *sres; |
| 1212 | grn_snip *snip_; |
| 1213 | |
| 1214 | snip_ = (grn_snip *)snip; |
| 1215 | if (snip_->snip_count <= index || !snip_->nstr) { |
| 1216 | return GRN_INVALID_ARGUMENT; |
| 1217 | } |
| 1218 | |
| 1219 | GRN_ASSERT(snip_->snip_count != 0 && snip_->tag_count != 0); |
| 1220 | |
| 1221 | GRN_API_ENTER; |
| 1222 | sres = &snip_->snip_result[index]; |
| 1223 | j = sres->first_tag_result_idx; |
| 1224 | for (p = result, i = sres->start_offset; i < sres->end_offset; i++) { |
| 1225 | for (; j <= sres->last_tag_result_idx && |
| 1226 | snip_->tag_result[j].start_offset == i; |
| 1227 | j++) { |
| 1228 | if (snip_->tag_result[j].end_offset > sres->end_offset) { |
| 1229 | continue; |
| 1230 | } |
| 1231 | grn_memcpy(p, |
| 1232 | snip_->tag_result[j].cond->opentag, |
| 1233 | snip_->tag_result[j].cond->opentag_len); |
| 1234 | p += snip_->tag_result[j].cond->opentag_len; |
| 1235 | } |
| 1236 | |
| 1237 | if (snip_->mapping == GRN_SNIP_MAPPING_HTML_ESCAPE) { |
| 1238 | switch (snip_->string[i]) { |
| 1239 | case '<': |
| 1240 | *p++ = '&'; |
| 1241 | *p++ = 'l'; |
| 1242 | *p++ = 't'; |
| 1243 | *p++ = ';'; |
| 1244 | break; |
| 1245 | case '>': |
| 1246 | *p++ = '&'; |
| 1247 | *p++ = 'g'; |
| 1248 | *p++ = 't'; |
| 1249 | *p++ = ';'; |
| 1250 | break; |
| 1251 | case '&': |
| 1252 | *p++ = '&'; |
| 1253 | *p++ = 'a'; |
| 1254 | *p++ = 'm'; |
| 1255 | *p++ = 'p'; |
| 1256 | *p++ = ';'; |
| 1257 | break; |
| 1258 | case '"': |
| 1259 | *p++ = '&'; |
| 1260 | *p++ = 'q'; |
| 1261 | *p++ = 'u'; |
| 1262 | *p++ = 'o'; |
| 1263 | *p++ = 't'; |
| 1264 | *p++ = ';'; |
| 1265 | break; |
| 1266 | default: |
| 1267 | *p++ = snip_->string[i]; |
| 1268 | break; |
| 1269 | } |
| 1270 | } else { |
| 1271 | *p++ = snip_->string[i]; |
| 1272 | } |
| 1273 | |
| 1274 | for (k = sres->last_tag_result_idx; |
| 1275 | snip_->tag_result[k].end_offset <= sres->end_offset; |
| 1276 | k--) { |
| 1277 | /* TODO: avoid all loop */ |
| 1278 | if (snip_->tag_result[k].end_offset == i + 1) { |
| 1279 | grn_memcpy(p, |
| 1280 | snip_->tag_result[k].cond->closetag, |
| 1281 | snip_->tag_result[k].cond->closetag_len); |
| 1282 | p += snip_->tag_result[k].cond->closetag_len; |
| 1283 | } |
| 1284 | if (k <= sres->first_tag_result_idx) { |
| 1285 | break; |
| 1286 | } |
| 1287 | }; |
| 1288 | } |
| 1289 | *p = '\0'; |
| 1290 | |
| 1291 | if (result_len) { |
| 1292 | *result_len = (unsigned int)(p - result); |
| 1293 | } |
| 1294 | GRN_ASSERT((unsigned int)(p - result) <= snip_->max_tagged_len); |
| 1295 | |
| 1296 | GRN_API_RETURN(ctx->rc); |
| 1297 | } |