1 /* -*- c-basic-offset: 2 -*- */
2 /*
3   Copyright(C) 2009-2016 Brazil
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 version 2.1 as published by the Free Software Foundation.
8 
9   This library is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   Lesser General Public License for more details.
13 
14   You should have received a copy of the GNU Lesser General Public
15   License along with this library; if not, write to the Free Software
16   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1335  USA
17 */
18 
19 #pragma once
20 
21 #include "grn.h"
22 #include "grn_str.h"
23 #include "grn_db.h"
24 
25 #define ASIZE                   256U
26 #define MAX_SNIP_TAG_COUNT      512U
27 #define MAX_SNIP_COND_COUNT     32U
28 #define MAX_SNIP_RESULT_COUNT   16U
29 
30 #ifdef __cplusplus
31 extern "C"
32 {
33 #endif
34 
35 #define SNIPCOND_NONSTOP 0
36 #define SNIPCOND_STOP    1
37 #define SNIPCOND_ACROSS  2
38 
39 #define GRN_QUERY_SCAN_ALLOCCONDS 0x0002
40 
41 typedef struct _snip_cond
42 {
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   size_t bmBc[ASIZE];
52   size_t shift;
53 
54   /* Tuned BM temporal result */
55   size_t found;
56   size_t last_found;
57   size_t last_offset;
58   size_t start_offset;
59   size_t end_offset;
60   size_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 {
71   size_t start_offset;
72   size_t end_offset;
73   snip_cond *cond;
74 } _snip_tag_result;
75 
76 typedef struct
77 {
78   size_t start_offset;
79   size_t end_offset;
80   unsigned int first_tag_result_idx;
81   unsigned int last_tag_result_idx;
82   unsigned int tag_count;
83 } _snip_result;
84 
85 typedef struct _grn_snip
86 {
87   grn_db_obj obj;
88   grn_encoding encoding;
89   int flags;
90   size_t width;
91   unsigned int max_results;
92   const char *defaultopentag;
93   const char *defaultclosetag;
94   size_t defaultopentag_len;
95   size_t defaultclosetag_len;
96 
97   grn_snip_mapping *mapping;
98 
99   snip_cond cond[MAX_SNIP_COND_COUNT];
100   unsigned int cond_len;
101 
102   unsigned int tag_count;
103   unsigned int snip_count;
104 
105   const char *string;
106   grn_obj *nstr;
107 
108   _snip_result snip_result[MAX_SNIP_RESULT_COUNT];
109   _snip_tag_result tag_result[MAX_SNIP_TAG_COUNT];
110 
111   size_t max_tagged_len;
112 
113   grn_obj *normalizer;
114 } grn_snip;
115 
116 grn_rc grn_snip_close(grn_ctx *ctx, grn_snip *snip);
117 grn_rc grn_snip_cond_init(grn_ctx *ctx, snip_cond *sc, const char *keyword, unsigned int keyword_len,
118                           grn_encoding enc, grn_obj *normalizer, int flags);
119 void grn_snip_cond_reinit(snip_cond *cond);
120 grn_rc grn_snip_cond_close(grn_ctx *ctx, snip_cond *cond);
121 void grn_bm_tunedbm(grn_ctx *ctx, snip_cond *cond, grn_obj *string, int flags);
122 
123 #ifdef __cplusplus
124 }
125 #endif
126