| 1 | /* |
| 2 | Copyright (C) 2009-2016 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 | #pragma once |
| 21 | |
| 22 | #include "grn.h" |
| 23 | #include "grn_str.h" |
| 24 | #include "grn_db.h" |
| 25 | #include "grn_onigmo.h" |
| 26 | |
| 27 | #define ASIZE 256U |
| 28 | #define MAX_SNIP_TAG_COUNT 512U |
| 29 | #define DEFAULT_SNIP_COND_CAPACITY 32U |
| 30 | #define MAX_SNIP_RESULT_COUNT 16U |
| 31 | |
| 32 | #ifdef __cplusplus |
| 33 | extern "C" { |
| 34 | #endif |
| 35 | |
| 36 | #define SNIPCOND_NONSTOP 0 |
| 37 | #define SNIPCOND_STOP 1 |
| 38 | #define SNIPCOND_ACROSS 2 |
| 39 | |
| 40 | #define GRN_QUERY_SCAN_ALLOCCONDS 0x0002 |
| 41 | |
| 42 | typedef struct _snip_cond { |
| 43 | /* initial parameters */ |
| 44 | const char *opentag; |
| 45 | const char *closetag; |
| 46 | size_t opentag_len; |
| 47 | size_t closetag_len; |
| 48 | grn_obj *keyword; |
| 49 | |
| 50 | /* Tuned BM pre */ |
| 51 | ptrdiff_t bmBc[ASIZE]; |
| 52 | ptrdiff_t shift; |
| 53 | |
| 54 | /* Tuned BM temporal result */ |
| 55 | ptrdiff_t found; |
| 56 | ptrdiff_t last_found; |
| 57 | size_t last_offset; |
| 58 | size_t start_offset; |
| 59 | size_t end_offset; |
| 60 | ptrdiff_t found_alpha_head; |
| 61 | |
| 62 | /* search result */ |
| 63 | int count; |
| 64 | |
| 65 | /* stop flag */ |
| 66 | int_least8_t stopflag; |
| 67 | } snip_cond; |
| 68 | |
| 69 | typedef struct { |
| 70 | size_t start_offset; |
| 71 | size_t end_offset; |
| 72 | snip_cond *cond; |
| 73 | } _snip_tag_result; |
| 74 | |
| 75 | typedef struct { |
| 76 | size_t start_offset; |
| 77 | size_t end_offset; |
| 78 | unsigned int first_tag_result_idx; |
| 79 | unsigned int last_tag_result_idx; |
| 80 | unsigned int tag_count; |
| 81 | } _snip_result; |
| 82 | |
| 83 | typedef struct _grn_snip { |
| 84 | grn_db_obj obj; |
| 85 | grn_encoding encoding; |
| 86 | int flags; |
| 87 | size_t width; |
| 88 | unsigned int max_results; |
| 89 | const char *defaultopentag; |
| 90 | const char *defaultclosetag; |
| 91 | size_t defaultopentag_len; |
| 92 | size_t defaultclosetag_len; |
| 93 | |
| 94 | grn_snip_mapping *mapping; |
| 95 | |
| 96 | snip_cond *cond; |
| 97 | size_t cond_capacity; |
| 98 | size_t cond_len; |
| 99 | |
| 100 | unsigned int tag_count; |
| 101 | unsigned int snip_count; |
| 102 | |
| 103 | const char *string; |
| 104 | grn_obj *nstr; |
| 105 | |
| 106 | _snip_result snip_result[MAX_SNIP_RESULT_COUNT]; |
| 107 | _snip_tag_result tag_result[MAX_SNIP_TAG_COUNT]; |
| 108 | |
| 109 | size_t max_tagged_len; |
| 110 | |
| 111 | grn_obj *normalizer; |
| 112 | grn_obj *lexicon; |
| 113 | grn_obj normalizers; |
| 114 | |
| 115 | char *delimiter_pattern; |
| 116 | size_t delimiter_pattern_length; |
| 117 | #ifdef GRN_SUPPORT_REGEXP |
| 118 | OnigRegex delimiter_regex; |
| 119 | #endif |
| 120 | } grn_snip; |
| 121 | |
| 122 | grn_rc |
| 123 | grn_snip_close(grn_ctx *ctx, grn_snip *snip); |
| 124 | grn_rc |
| 125 | grn_snip_cond_close(grn_ctx *ctx, snip_cond *cond); |
| 126 | void |
| 127 | grn_bm_tunedbm(grn_ctx *ctx, snip_cond *cond, grn_obj *string, int flags); |
| 128 | |
| 129 | #ifdef __cplusplus |
| 130 | } |
| 131 | #endif |