| 1 | /* |
| 2 | Copyright (C) 2012-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_token.h" |
| 21 | #include "grn_token_metadata.h" |
| 22 | |
| 23 | static inline grn_rc |
| 24 | grn_token_init_internal(grn_ctx *ctx, grn_token *token, bool deep) |
| 25 | { |
| 26 | GRN_API_ENTER; |
| 27 | GRN_TEXT_INIT(&(token->data), deep ? 0 : GRN_OBJ_DO_SHALLOW_COPY); |
| 28 | token->status = GRN_TOKEN_CONTINUE; |
| 29 | token->source_offset = 0; |
| 30 | token->source_length = 0; |
| 31 | token->source_first_character_length = 0; |
| 32 | token->have_overlap = false; |
| 33 | grn_token_metadata_init(ctx, &(token->metadata)); |
| 34 | token->force_prefix_search = false; |
| 35 | token->position = 0; |
| 36 | token->weight = 0; |
| 37 | GRN_API_RETURN(ctx->rc); |
| 38 | } |
| 39 | |
| 40 | grn_rc |
| 41 | grn_token_init(grn_ctx *ctx, grn_token *token) |
| 42 | { |
| 43 | return grn_token_init_internal(ctx, token, false); |
| 44 | } |
| 45 | |
| 46 | grn_rc |
| 47 | grn_token_init_deep(grn_ctx *ctx, grn_token *token) |
| 48 | { |
| 49 | return grn_token_init_internal(ctx, token, true); |
| 50 | } |
| 51 | |
| 52 | grn_rc |
| 53 | grn_token_fin(grn_ctx *ctx, grn_token *token) |
| 54 | { |
| 55 | GRN_API_ENTER; |
| 56 | grn_token_metadata_fin(ctx, &(token->metadata)); |
| 57 | GRN_OBJ_FIN(ctx, &(token->data)); |
| 58 | GRN_API_RETURN(ctx->rc); |
| 59 | } |
| 60 | |
| 61 | grn_obj * |
| 62 | grn_token_get_data(grn_ctx *ctx, grn_token *token) |
| 63 | { |
| 64 | GRN_API_ENTER; |
| 65 | if (!token) { |
| 66 | ERR(GRN_INVALID_ARGUMENT, "[token][data][get] token must not be NULL"); |
| 67 | GRN_API_RETURN(NULL); |
| 68 | } |
| 69 | GRN_API_RETURN(&(token->data)); |
| 70 | } |
| 71 | |
| 72 | const char * |
| 73 | grn_token_get_data_raw(grn_ctx *ctx, grn_token *token, size_t *length) |
| 74 | { |
| 75 | GRN_API_ENTER; |
| 76 | if (!token) { |
| 77 | ERR(GRN_INVALID_ARGUMENT, "[token][data][get][raw] token must not be NULL"); |
| 78 | if (length) { |
| 79 | *length = 0; |
| 80 | } |
| 81 | GRN_API_RETURN(NULL); |
| 82 | } |
| 83 | if (length) { |
| 84 | *length = GRN_TEXT_LEN(&(token->data)); |
| 85 | } |
| 86 | GRN_API_RETURN(GRN_TEXT_VALUE(&(token->data))); |
| 87 | } |
| 88 | |
| 89 | grn_rc |
| 90 | grn_token_set_data(grn_ctx *ctx, |
| 91 | grn_token *token, |
| 92 | const char *str_ptr, |
| 93 | int str_length) |
| 94 | { |
| 95 | GRN_API_ENTER; |
| 96 | if (!token) { |
| 97 | ERR(GRN_INVALID_ARGUMENT, "[token][data][set] token must not be NULL"); |
| 98 | goto exit; |
| 99 | } |
| 100 | if (str_length == -1) { |
| 101 | str_length = (int)strlen(str_ptr); |
| 102 | } |
| 103 | GRN_TEXT_SET(ctx, &(token->data), str_ptr, str_length); |
| 104 | exit: |
| 105 | GRN_API_RETURN(ctx->rc); |
| 106 | } |
| 107 | |
| 108 | grn_id |
| 109 | grn_token_get_domain(grn_ctx *ctx, grn_token *token) |
| 110 | { |
| 111 | GRN_API_ENTER; |
| 112 | grn_id domain = GRN_ID_NIL; |
| 113 | if (!token) { |
| 114 | ERR(GRN_INVALID_ARGUMENT, "[token][data][set] token must not be NULL"); |
| 115 | goto exit; |
| 116 | } |
| 117 | domain = token->data.header.domain; |
| 118 | exit: |
| 119 | GRN_API_RETURN(domain); |
| 120 | } |
| 121 | |
| 122 | grn_rc |
| 123 | grn_token_set_domain(grn_ctx *ctx, grn_token *token, grn_id domain) |
| 124 | { |
| 125 | GRN_API_ENTER; |
| 126 | if (!token) { |
| 127 | ERR(GRN_INVALID_ARGUMENT, "[token][data][set] token must not be NULL"); |
| 128 | goto exit; |
| 129 | } |
| 130 | token->data.header.domain = domain; |
| 131 | exit: |
| 132 | GRN_API_RETURN(ctx->rc); |
| 133 | } |
| 134 | |
| 135 | grn_token_status |
| 136 | grn_token_get_status(grn_ctx *ctx, grn_token *token) |
| 137 | { |
| 138 | GRN_API_ENTER; |
| 139 | if (!token) { |
| 140 | ERR(GRN_INVALID_ARGUMENT, "[token][status][get] token must not be NULL"); |
| 141 | GRN_API_RETURN(GRN_TOKEN_CONTINUE); |
| 142 | } |
| 143 | GRN_API_RETURN(token->status); |
| 144 | } |
| 145 | |
| 146 | grn_rc |
| 147 | grn_token_set_status(grn_ctx *ctx, grn_token *token, grn_token_status status) |
| 148 | { |
| 149 | GRN_API_ENTER; |
| 150 | if (!token) { |
| 151 | ERR(GRN_INVALID_ARGUMENT, "[token][status][set] token must not be NULL"); |
| 152 | goto exit; |
| 153 | } |
| 154 | token->status = status; |
| 155 | exit: |
| 156 | GRN_API_RETURN(ctx->rc); |
| 157 | } |
| 158 | |
| 159 | grn_rc |
| 160 | grn_token_add_status(grn_ctx *ctx, grn_token *token, grn_token_status status) |
| 161 | { |
| 162 | GRN_API_ENTER; |
| 163 | if (!token) { |
| 164 | ERR(GRN_INVALID_ARGUMENT, "[token][status][add] token must not be NULL"); |
| 165 | goto exit; |
| 166 | } |
| 167 | token->status |= status; |
| 168 | exit: |
| 169 | GRN_API_RETURN(ctx->rc); |
| 170 | } |
| 171 | |
| 172 | grn_rc |
| 173 | grn_token_remove_status(grn_ctx *ctx, grn_token *token, grn_token_status status) |
| 174 | { |
| 175 | GRN_API_ENTER; |
| 176 | if (!token) { |
| 177 | ERR(GRN_INVALID_ARGUMENT, "[token][status][remove] token must not be NULL"); |
| 178 | goto exit; |
| 179 | } |
| 180 | token->status &= ~status; |
| 181 | exit: |
| 182 | GRN_API_RETURN(ctx->rc); |
| 183 | } |
| 184 | |
| 185 | uint64_t |
| 186 | grn_token_get_source_offset(grn_ctx *ctx, grn_token *token) |
| 187 | { |
| 188 | GRN_API_ENTER; |
| 189 | if (!token) { |
| 190 | ERR(GRN_INVALID_ARGUMENT, |
| 191 | "[token][source-offset][get] token must not be NULL"); |
| 192 | GRN_API_RETURN(0); |
| 193 | } |
| 194 | GRN_API_RETURN(token->source_offset); |
| 195 | } |
| 196 | |
| 197 | grn_rc |
| 198 | grn_token_set_source_offset(grn_ctx *ctx, grn_token *token, uint64_t offset) |
| 199 | { |
| 200 | GRN_API_ENTER; |
| 201 | if (!token) { |
| 202 | ERR(GRN_INVALID_ARGUMENT, |
| 203 | "[token][source-offset][set] token must not be NULL"); |
| 204 | goto exit; |
| 205 | } |
| 206 | token->source_offset = offset; |
| 207 | exit: |
| 208 | GRN_API_RETURN(ctx->rc); |
| 209 | } |
| 210 | |
| 211 | uint32_t |
| 212 | grn_token_get_source_length(grn_ctx *ctx, grn_token *token) |
| 213 | { |
| 214 | GRN_API_ENTER; |
| 215 | if (!token) { |
| 216 | ERR(GRN_INVALID_ARGUMENT, |
| 217 | "[token][source-length][get] token must not be NULL"); |
| 218 | GRN_API_RETURN(0); |
| 219 | } |
| 220 | GRN_API_RETURN(token->source_length); |
| 221 | } |
| 222 | |
| 223 | grn_rc |
| 224 | grn_token_set_source_length(grn_ctx *ctx, grn_token *token, uint32_t length) |
| 225 | { |
| 226 | GRN_API_ENTER; |
| 227 | if (!token) { |
| 228 | ERR(GRN_INVALID_ARGUMENT, |
| 229 | "[token][source-length][set] token must not be NULL"); |
| 230 | goto exit; |
| 231 | } |
| 232 | token->source_length = length; |
| 233 | exit: |
| 234 | GRN_API_RETURN(ctx->rc); |
| 235 | } |
| 236 | |
| 237 | uint32_t |
| 238 | grn_token_get_source_first_character_length(grn_ctx *ctx, grn_token *token) |
| 239 | { |
| 240 | GRN_API_ENTER; |
| 241 | if (!token) { |
| 242 | ERR(GRN_INVALID_ARGUMENT, |
| 243 | "[token][source-first-character-length][get] token must not be NULL"); |
| 244 | GRN_API_RETURN(0); |
| 245 | } |
| 246 | GRN_API_RETURN(token->source_first_character_length); |
| 247 | } |
| 248 | |
| 249 | grn_rc |
| 250 | grn_token_set_source_first_character_length(grn_ctx *ctx, |
| 251 | grn_token *token, |
| 252 | uint32_t length) |
| 253 | { |
| 254 | GRN_API_ENTER; |
| 255 | if (!token) { |
| 256 | ERR(GRN_INVALID_ARGUMENT, |
| 257 | "[token][source-first-character-length][set] token must not be NULL"); |
| 258 | goto exit; |
| 259 | } |
| 260 | token->source_first_character_length = length; |
| 261 | exit: |
| 262 | GRN_API_RETURN(ctx->rc); |
| 263 | } |
| 264 | |
| 265 | bool |
| 266 | grn_token_have_overlap(grn_ctx *ctx, grn_token *token) |
| 267 | { |
| 268 | GRN_API_ENTER; |
| 269 | if (!token) { |
| 270 | ERR(GRN_INVALID_ARGUMENT, "[token][overlap][have] token must not be NULL"); |
| 271 | GRN_API_RETURN(false); |
| 272 | } |
| 273 | GRN_API_RETURN(token->have_overlap); |
| 274 | } |
| 275 | |
| 276 | grn_rc |
| 277 | grn_token_set_overlap(grn_ctx *ctx, grn_token *token, bool have_overlap) |
| 278 | { |
| 279 | GRN_API_ENTER; |
| 280 | if (!token) { |
| 281 | ERR(GRN_INVALID_ARGUMENT, "[token][overlap][set] token must not be NULL"); |
| 282 | goto exit; |
| 283 | } |
| 284 | token->have_overlap = have_overlap; |
| 285 | exit: |
| 286 | GRN_API_RETURN(ctx->rc); |
| 287 | } |
| 288 | |
| 289 | grn_obj * |
| 290 | grn_token_get_metadata(grn_ctx *ctx, grn_token *token) |
| 291 | { |
| 292 | GRN_API_ENTER; |
| 293 | if (!token) { |
| 294 | ERR(GRN_INVALID_ARGUMENT, "[token][metadata][get] token must not be NULL"); |
| 295 | GRN_API_RETURN(NULL); |
| 296 | } |
| 297 | GRN_API_RETURN(&(token->metadata)); |
| 298 | } |
| 299 | |
| 300 | bool |
| 301 | grn_token_get_force_prefix_search(grn_ctx *ctx, grn_token *token) |
| 302 | { |
| 303 | GRN_API_ENTER; |
| 304 | if (!token) { |
| 305 | ERR(GRN_INVALID_ARGUMENT, |
| 306 | "[token][force-prefix-search][get] token must not be NULL"); |
| 307 | GRN_API_RETURN(false); |
| 308 | } |
| 309 | GRN_API_RETURN(token->force_prefix_search); |
| 310 | } |
| 311 | |
| 312 | grn_rc |
| 313 | grn_token_set_force_prefix_search(grn_ctx *ctx, grn_token *token, bool force) |
| 314 | { |
| 315 | GRN_API_ENTER; |
| 316 | if (!token) { |
| 317 | ERR(GRN_INVALID_ARGUMENT, |
| 318 | "[token][force-prefix-search][set] token must not be NULL"); |
| 319 | GRN_API_RETURN(ctx->rc); |
| 320 | } |
| 321 | token->force_prefix_search = force; |
| 322 | GRN_API_RETURN(ctx->rc); |
| 323 | } |
| 324 | |
| 325 | uint32_t |
| 326 | grn_token_get_position(grn_ctx *ctx, grn_token *token) |
| 327 | { |
| 328 | GRN_API_ENTER; |
| 329 | if (!token) { |
| 330 | ERR(GRN_INVALID_ARGUMENT, "[token][position][get] token must not be NULL"); |
| 331 | GRN_API_RETURN(0); |
| 332 | } |
| 333 | GRN_API_RETURN(token->position); |
| 334 | } |
| 335 | |
| 336 | grn_rc |
| 337 | grn_token_set_position(grn_ctx *ctx, grn_token *token, uint32_t position) |
| 338 | { |
| 339 | GRN_API_ENTER; |
| 340 | if (!token) { |
| 341 | ERR(GRN_INVALID_ARGUMENT, "[token][position][set] token must not be NULL"); |
| 342 | GRN_API_RETURN(ctx->rc); |
| 343 | } |
| 344 | token->position = position; |
| 345 | GRN_API_RETURN(ctx->rc); |
| 346 | } |
| 347 | |
| 348 | float |
| 349 | grn_token_get_weight(grn_ctx *ctx, grn_token *token) |
| 350 | { |
| 351 | GRN_API_ENTER; |
| 352 | if (!token) { |
| 353 | ERR(GRN_INVALID_ARGUMENT, "[token][weight][get] token must not be NULL"); |
| 354 | GRN_API_RETURN(0.0); |
| 355 | } |
| 356 | GRN_API_RETURN(token->weight); |
| 357 | } |
| 358 | |
| 359 | grn_rc |
| 360 | grn_token_set_weight(grn_ctx *ctx, grn_token *token, float weight) |
| 361 | { |
| 362 | GRN_API_ENTER; |
| 363 | if (!token) { |
| 364 | ERR(GRN_INVALID_ARGUMENT, "[token][float][set] token must not be NULL"); |
| 365 | GRN_API_RETURN(ctx->rc); |
| 366 | } |
| 367 | token->weight = weight; |
| 368 | GRN_API_RETURN(ctx->rc); |
| 369 | } |
| 370 | |
| 371 | grn_rc |
| 372 | grn_token_reset(grn_ctx *ctx, grn_token *token) |
| 373 | { |
| 374 | GRN_API_ENTER; |
| 375 | if (!token) { |
| 376 | ERR(GRN_INVALID_ARGUMENT, "[token][reset] token must not be NULL"); |
| 377 | goto exit; |
| 378 | } |
| 379 | GRN_BULK_REWIND(&(token->data)); |
| 380 | token->status = GRN_TOKEN_CONTINUE; |
| 381 | token->source_offset = 0; |
| 382 | token->source_length = 0; |
| 383 | token->source_first_character_length = 0; |
| 384 | token->have_overlap = false; |
| 385 | grn_token_metadata_reset(ctx, &(token->metadata)); |
| 386 | token->force_prefix_search = false; |
| 387 | token->position = 0; |
| 388 | token->weight = 0.0; |
| 389 | exit: |
| 390 | GRN_API_RETURN(ctx->rc); |
| 391 | } |
| 392 | |
| 393 | grn_rc |
| 394 | grn_token_copy(grn_ctx *ctx, grn_token *token, grn_token *source) |
| 395 | { |
| 396 | GRN_API_ENTER; |
| 397 | if (!token) { |
| 398 | ERR(GRN_INVALID_ARGUMENT, "[token][copy] token must not be NULL"); |
| 399 | goto exit; |
| 400 | } |
| 401 | GRN_TEXT_SET(ctx, |
| 402 | &(token->data), |
| 403 | GRN_TEXT_VALUE(&(source->data)), |
| 404 | GRN_TEXT_LEN(&(source->data))); |
| 405 | token->status = source->status; |
| 406 | token->source_offset = source->source_offset; |
| 407 | token->source_length = source->source_length; |
| 408 | token->source_first_character_length = source->source_first_character_length; |
| 409 | token->have_overlap = source->have_overlap; |
| 410 | grn_token_metadata_reset(ctx, &(token->metadata)); |
| 411 | grn_token_metadata_copy(ctx, &(token->metadata), &(source->metadata)); |
| 412 | token->force_prefix_search = source->force_prefix_search; |
| 413 | token->position = source->position; |
| 414 | token->weight = source->weight; |
| 415 | exit: |
| 416 | GRN_API_RETURN(ctx->rc); |
| 417 | } |