| 1 | /* |
| 2 | Copyright (C) 2009-2018 Brazil |
| 3 | Copyright (C) 2018-2022 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 "grn_string.h" |
| 23 | #include "grn_normalizer.h" |
| 24 | #include "grn_str.h" |
| 25 | #include "grn_util.h" |
| 26 | |
| 27 | #include <groonga/tokenizer.h> |
| 28 | |
| 29 | const char * |
| 30 | grn_char_type_to_string(grn_char_type type) |
| 31 | { |
| 32 | const char *string = "unknown"; |
| 33 | |
| 34 | #define CHAR_TYPE_STRING_WITH_BLANK(type_string) do { \ |
| 35 | if (GRN_CHAR_IS_BLANK(type)) { \ |
| 36 | string = type_string "|blank"; \ |
| 37 | } else { \ |
| 38 | string = type_string; \ |
| 39 | } \ |
| 40 | } while (false) |
| 41 | |
| 42 | switch (GRN_CHAR_TYPE(type)) { |
| 43 | case GRN_CHAR_NULL : |
| 44 | CHAR_TYPE_STRING_WITH_BLANK("null"); |
| 45 | break; |
| 46 | case GRN_CHAR_ALPHA : |
| 47 | CHAR_TYPE_STRING_WITH_BLANK("alpha"); |
| 48 | break; |
| 49 | case GRN_CHAR_DIGIT : |
| 50 | CHAR_TYPE_STRING_WITH_BLANK("digit"); |
| 51 | break; |
| 52 | case GRN_CHAR_SYMBOL : |
| 53 | CHAR_TYPE_STRING_WITH_BLANK("symbol"); |
| 54 | break; |
| 55 | case GRN_CHAR_HIRAGANA : |
| 56 | CHAR_TYPE_STRING_WITH_BLANK("hiragana"); |
| 57 | break; |
| 58 | case GRN_CHAR_KATAKANA : |
| 59 | CHAR_TYPE_STRING_WITH_BLANK("katakana"); |
| 60 | break; |
| 61 | case GRN_CHAR_KANJI : |
| 62 | CHAR_TYPE_STRING_WITH_BLANK("kanji"); |
| 63 | break; |
| 64 | case GRN_CHAR_OTHERS : |
| 65 | CHAR_TYPE_STRING_WITH_BLANK("others"); |
| 66 | break; |
| 67 | case GRN_CHAR_EMOJI : |
| 68 | CHAR_TYPE_STRING_WITH_BLANK("emoji"); |
| 69 | break; |
| 70 | default : |
| 71 | CHAR_TYPE_STRING_WITH_BLANK("unknown"); |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | #undef CHAR_TYPE_STRING_WITH_BLANK |
| 76 | |
| 77 | return string; |
| 78 | } |
| 79 | |
| 80 | static grn_string * |
| 81 | grn_fake_string_open(grn_ctx *ctx, grn_string *string) |
| 82 | { |
| 83 | /* TODO: support GRN_STRING_REMOVE_BLANK flag and ctypes */ |
| 84 | grn_string *nstr = string; |
| 85 | const char *str; |
| 86 | unsigned int str_len; |
| 87 | |
| 88 | str = nstr->original; |
| 89 | str_len = nstr->original_length_in_bytes; |
| 90 | |
| 91 | if (!(nstr->normalized = GRN_MALLOC(str_len + 1))) { |
| 92 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 93 | "[strinig][fake] failed to allocate normalized text space"); |
| 94 | grn_string_close(ctx, (grn_obj *)nstr); |
| 95 | return NULL; |
| 96 | } |
| 97 | |
| 98 | if (nstr->flags & GRN_STRING_REMOVE_TOKENIZED_DELIMITER && |
| 99 | ctx->encoding == GRN_ENC_UTF8) { |
| 100 | int char_length; |
| 101 | const char *source_current = str; |
| 102 | const char *source_end = str + str_len; |
| 103 | char *destination = nstr->normalized; |
| 104 | uint32_t destination_length = 0; |
| 105 | while ((char_length = grn_charlen(ctx, source_current, source_end)) > 0) { |
| 106 | if (!grn_tokenizer_is_tokenized_delimiter(ctx, |
| 107 | source_current, |
| 108 | (unsigned int)char_length, |
| 109 | ctx->encoding)) { |
| 110 | grn_memcpy(destination, source_current, (size_t)char_length); |
| 111 | destination += char_length; |
| 112 | destination_length += (uint32_t)char_length; |
| 113 | } |
| 114 | source_current += char_length; |
| 115 | } |
| 116 | nstr->normalized[destination_length] = '\0'; |
| 117 | nstr->normalized_length_in_bytes = destination_length; |
| 118 | } else { |
| 119 | grn_memcpy(nstr->normalized, str, str_len); |
| 120 | nstr->normalized[str_len] = '\0'; |
| 121 | nstr->normalized_length_in_bytes = str_len; |
| 122 | } |
| 123 | |
| 124 | if (nstr->flags & GRN_STRING_WITH_CHECKS) { |
| 125 | int16_t f = 0; |
| 126 | unsigned char c; |
| 127 | size_t i; |
| 128 | if (!(nstr->checks = (int16_t *) GRN_MALLOC(sizeof(int16_t) * str_len))) { |
| 129 | grn_string_close(ctx, (grn_obj *)nstr); |
| 130 | ERR(GRN_NO_MEMORY_AVAILABLE, |
| 131 | "[strinig][fake] failed to allocate checks space"); |
| 132 | return NULL; |
| 133 | } |
| 134 | switch (nstr->encoding) { |
| 135 | case GRN_ENC_EUC_JP: |
| 136 | for (i = 0; i < str_len; i++) { |
| 137 | if (!f) { |
| 138 | c = (unsigned char) str[i]; |
| 139 | f = ((c >= 0xa1U && c <= 0xfeU) || c == 0x8eU ? 2 : (c == 0x8fU ? 3 : 1) |
| 140 | ); |
| 141 | nstr->checks[i] = f; |
| 142 | } else { |
| 143 | nstr->checks[i] = 0; |
| 144 | } |
| 145 | f--; |
| 146 | } |
| 147 | break; |
| 148 | case GRN_ENC_SJIS: |
| 149 | for (i = 0; i < str_len; i++) { |
| 150 | if (!f) { |
| 151 | c = (unsigned char) str[i]; |
| 152 | f = (c >= 0x81U && ((c <= 0x9fU) || (c >= 0xe0U && c <= 0xfcU)) ? 2 : 1); |
| 153 | nstr->checks[i] = f; |
| 154 | } else { |
| 155 | nstr->checks[i] = 0; |
| 156 | } |
| 157 | f--; |
| 158 | } |
| 159 | break; |
| 160 | case GRN_ENC_UTF8: |
| 161 | for (i = 0; i < str_len; i++) { |
| 162 | if (!f) { |
| 163 | c = (unsigned char) str[i]; |
| 164 | f = (c & 0x80U ? (c & 0x20U ? (c & 0x10U ? 4 : 3) |
| 165 | : 2) |
| 166 | : 1); |
| 167 | nstr->checks[i] = f; |
| 168 | } else { |
| 169 | nstr->checks[i] = 0; |
| 170 | } |
| 171 | f--; |
| 172 | } |
| 173 | break; |
| 174 | default: |
| 175 | for (i = 0; i < str_len; i++) { |
| 176 | nstr->checks[i] = 1; |
| 177 | } |
| 178 | break; |
| 179 | } |
| 180 | } |
| 181 | return nstr; |
| 182 | } |
| 183 | |
| 184 | grn_rc |
| 185 | grn_string_init(grn_ctx *ctx, |
| 186 | grn_obj *string, |
| 187 | grn_obj *lexicon_or_normalizer, |
| 188 | int flags, |
| 189 | grn_encoding encoding) |
| 190 | { |
| 191 | grn_string *string_ = (grn_string *)string; |
| 192 | |
| 193 | GRN_OBJ_INIT(string, GRN_STRING, 0, GRN_ID_NIL); |
| 194 | string_->original = NULL; |
| 195 | string_->original_length_in_bytes = 0; |
| 196 | string_->normalized = NULL; |
| 197 | string_->normalized_length_in_bytes = 0; |
| 198 | string_->n_characters = 0; |
| 199 | string_->checks = NULL; |
| 200 | string_->ctypes = NULL; |
| 201 | string_->offsets = NULL; |
| 202 | string_->encoding = encoding; |
| 203 | string_->flags = flags; |
| 204 | string_->lexicon = NULL; |
| 205 | string_->normalizer_index = 0; |
| 206 | |
| 207 | if (lexicon_or_normalizer && |
| 208 | lexicon_or_normalizer != GRN_NORMALIZER_AUTO && |
| 209 | grn_obj_is_table(ctx, lexicon_or_normalizer)) { |
| 210 | string_->lexicon = lexicon_or_normalizer; |
| 211 | } |
| 212 | |
| 213 | return ctx->rc; |
| 214 | } |
| 215 | |
| 216 | grn_obj * |
| 217 | grn_string_open_(grn_ctx *ctx, |
| 218 | const char *str, |
| 219 | unsigned int str_len, |
| 220 | grn_obj *lexicon_or_normalizer, |
| 221 | int flags, |
| 222 | grn_encoding encoding) |
| 223 | { |
| 224 | grn_string *string_; |
| 225 | grn_obj *string; |
| 226 | |
| 227 | if (!str || !str_len) { |
| 228 | return NULL; |
| 229 | } |
| 230 | |
| 231 | string_ = GRN_CALLOC(sizeof(grn_string)); |
| 232 | if (!string_) { |
| 233 | GRN_LOG(ctx, GRN_LOG_ALERT, |
| 234 | "[string][open] failed to allocate memory"); |
| 235 | return NULL; |
| 236 | } |
| 237 | |
| 238 | string = (grn_obj *)string_; |
| 239 | grn_string_init(ctx, string, lexicon_or_normalizer, flags, encoding); |
| 240 | string->header.impl_flags |= GRN_OBJ_ALLOCATED; |
| 241 | string_->original = str; |
| 242 | string_->original_length_in_bytes = str_len; |
| 243 | |
| 244 | if (!lexicon_or_normalizer) { |
| 245 | return (grn_obj *)grn_fake_string_open(ctx, string_); |
| 246 | } |
| 247 | |
| 248 | { |
| 249 | grn_obj normalizers; |
| 250 | GRN_PTR_INIT(&normalizers, GRN_OBJ_VECTOR, GRN_ID_NIL); |
| 251 | if (string_->lexicon) { |
| 252 | grn_obj_get_info(ctx, |
| 253 | string_->lexicon, |
| 254 | GRN_INFO_NORMALIZERS, |
| 255 | &normalizers); |
| 256 | } else { |
| 257 | bool is_normalizer_auto = (lexicon_or_normalizer == GRN_NORMALIZER_AUTO); |
| 258 | if (is_normalizer_auto) { |
| 259 | grn_obj *normalizer = grn_ctx_get(ctx, GRN_NORMALIZER_AUTO_NAME, -1); |
| 260 | if (!normalizer) { |
| 261 | grn_obj_close(ctx, string); |
| 262 | ERR(GRN_INVALID_ARGUMENT, |
| 263 | "[string][open] NormalizerAuto normalizer isn't available"); |
| 264 | return NULL; |
| 265 | } |
| 266 | GRN_PTR_PUT(ctx, &normalizers, normalizer); |
| 267 | } else { |
| 268 | grn_obj *normalizer = lexicon_or_normalizer; |
| 269 | GRN_PTR_PUT(ctx, &normalizers, normalizer); |
| 270 | } |
| 271 | } |
| 272 | size_t n = GRN_PTR_VECTOR_SIZE(&normalizers); |
| 273 | if (n == 0) { |
| 274 | GRN_OBJ_FIN(ctx, &normalizers); |
| 275 | return (grn_obj *)grn_fake_string_open(ctx, string_); |
| 276 | } |
| 277 | char *previous_normalized = NULL; |
| 278 | unsigned int previous_normalized_length_in_bytes = 0; |
| 279 | int16_t *previous_checks = NULL; |
| 280 | uint8_t *previous_types = NULL; |
| 281 | uint64_t *previous_offsets = NULL; |
| 282 | unsigned int previous_n_characters = 0; |
| 283 | size_t i; |
| 284 | for (i = 0; i < n; i++) { |
| 285 | grn_obj *normalizer = GRN_PTR_VALUE_AT(&normalizers, i); |
| 286 | string_->normalizer_index = (uint32_t)i; |
| 287 | if (i > 0) { |
| 288 | previous_normalized = string_->normalized; |
| 289 | previous_normalized_length_in_bytes = |
| 290 | string_->normalized_length_in_bytes; |
| 291 | previous_n_characters = string_->n_characters; |
| 292 | previous_checks = string_->checks; |
| 293 | previous_types = string_->ctypes; |
| 294 | previous_offsets = string_->offsets; |
| 295 | string_->original = previous_normalized; |
| 296 | string_->original_length_in_bytes = previous_normalized_length_in_bytes; |
| 297 | } |
| 298 | grn_normalizer_normalize(ctx, normalizer, string); |
| 299 | if (i > 0) { |
| 300 | if (ctx->rc != GRN_SUCCESS) { |
| 301 | break; |
| 302 | } |
| 303 | if (previous_checks) { |
| 304 | if (string_->checks) { |
| 305 | unsigned int previous_i = 0; |
| 306 | unsigned int current_i = 0; |
| 307 | for (current_i = 0; |
| 308 | current_i < string_->normalized_length_in_bytes; |
| 309 | current_i++) { |
| 310 | int16_t current_check = string_->checks[current_i]; |
| 311 | if (current_check > 0) { |
| 312 | int16_t previous_check = previous_checks[previous_i]; |
| 313 | /* TODO: How to merge check? This logic may be |
| 314 | * wrong. We need many cases. We have only a few cases |
| 315 | * in |
| 316 | * test/command/suite/normalizers/multiple/options/ .*/ |
| 317 | if (previous_check > current_check) { |
| 318 | string_->checks[current_i] = previous_check; |
| 319 | } |
| 320 | previous_i += (unsigned int)current_check; |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | GRN_FREE(previous_checks); |
| 325 | } |
| 326 | if (previous_types) { |
| 327 | /* |
| 328 | * Currently, ctypes are merged only in limited cases. |
| 329 | * Specifically, we merge only GRN_CHAR_BLANK when the |
| 330 | * number of characters in the first normalized result is equal to |
| 331 | * that in the latter normalized result, and previous_types has |
| 332 | * GRN_CHAR_BLANK set. |
| 333 | * |
| 334 | * In other words, this works only when NormalizerTable performs |
| 335 | * normalization without changing the number of characters. |
| 336 | * |
| 337 | * The latter normalizer is NormalizerTable, and users can define any |
| 338 | * normalization pattern in NormalizerTable. |
| 339 | * |
| 340 | * It is difficult to handle all such cases with a single |
| 341 | * implementation here. |
| 342 | * Therefore, we currently support only this simple case. |
| 343 | */ |
| 344 | if (string_->ctypes && |
| 345 | string_->n_characters == previous_n_characters) { |
| 346 | unsigned int character_i = 0; |
| 347 | for (character_i = 0; character_i < previous_n_characters; |
| 348 | character_i++) { |
| 349 | if (previous_types[character_i] & GRN_CHAR_BLANK) { |
| 350 | string_->ctypes[character_i] |= GRN_CHAR_BLANK; |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | GRN_FREE(previous_types); |
| 355 | } |
| 356 | if (previous_offsets) { |
| 357 | if (string_->offsets) { |
| 358 | uint64_t previous_offset = 0; |
| 359 | const char *previous_start = string_->original; |
| 360 | const char *previous_end = |
| 361 | previous_start + string_->original_length_in_bytes; |
| 362 | unsigned int previous_i = 0; |
| 363 | unsigned int current_i = 0; |
| 364 | for (current_i = 0; |
| 365 | current_i <= string_->n_characters; |
| 366 | current_i++) { |
| 367 | while (string_->offsets[current_i] > previous_offset) { |
| 368 | int previous_character_length = |
| 369 | grn_charlen(ctx, previous_start, previous_end); |
| 370 | if (previous_character_length == 0) { |
| 371 | ERR(GRN_INVALID_ARGUMENT, |
| 372 | "[string][open] invalid character in normalized string: " |
| 373 | "<%.*s>", |
| 374 | (int)(previous_end - previous_start), |
| 375 | previous_start); |
| 376 | break; |
| 377 | } |
| 378 | previous_start += previous_character_length; |
| 379 | previous_offset += (uint64_t)previous_character_length; |
| 380 | previous_i++; |
| 381 | } |
| 382 | if (ctx->rc != GRN_SUCCESS) { |
| 383 | break; |
| 384 | } |
| 385 | string_->offsets[current_i] = previous_offsets[previous_i]; |
| 386 | } |
| 387 | } |
| 388 | GRN_FREE(previous_offsets); |
| 389 | } |
| 390 | GRN_FREE(previous_normalized); |
| 391 | } |
| 392 | if (ctx->rc != GRN_SUCCESS) { |
| 393 | break; |
| 394 | } |
| 395 | } |
| 396 | string_->original = str; |
| 397 | string_->original_length_in_bytes = str_len; |
| 398 | string_->normalizer_index = 0; |
| 399 | GRN_OBJ_FIN(ctx, &normalizers); |
| 400 | } |
| 401 | if (ctx->rc != GRN_SUCCESS) { |
| 402 | grn_obj_close(ctx, string); |
| 403 | string = NULL; |
| 404 | } |
| 405 | |
| 406 | return string; |
| 407 | } |
| 408 | |
| 409 | grn_obj * |
| 410 | grn_string_open(grn_ctx *ctx, |
| 411 | const char *str, |
| 412 | unsigned int str_len, |
| 413 | grn_obj *lexicon_or_normalizer, |
| 414 | int flags) |
| 415 | { |
| 416 | return grn_string_open_(ctx, |
| 417 | str, |
| 418 | str_len, |
| 419 | lexicon_or_normalizer, |
| 420 | flags, |
| 421 | ctx->encoding); |
| 422 | } |
| 423 | |
| 424 | grn_rc |
| 425 | grn_string_get_original(grn_ctx *ctx, grn_obj *string, |
| 426 | const char **original, |
| 427 | unsigned int *length_in_bytes) |
| 428 | { |
| 429 | grn_rc rc; |
| 430 | grn_string *string_ = (grn_string *)string; |
| 431 | GRN_API_ENTER; |
| 432 | if (string_) { |
| 433 | if (original) { *original = string_->original; } |
| 434 | if (length_in_bytes) { |
| 435 | *length_in_bytes = string_->original_length_in_bytes; |
| 436 | } |
| 437 | rc = GRN_SUCCESS; |
| 438 | } else { |
| 439 | rc = GRN_INVALID_ARGUMENT; |
| 440 | } |
| 441 | GRN_API_RETURN(rc); |
| 442 | } |
| 443 | |
| 444 | grn_rc |
| 445 | grn_string_set_original(grn_ctx *ctx, |
| 446 | grn_obj *string, |
| 447 | const char *original, |
| 448 | unsigned int length_in_bytes) |
| 449 | { |
| 450 | grn_rc rc; |
| 451 | grn_string *string_ = (grn_string *)string; |
| 452 | GRN_API_ENTER; |
| 453 | if (string_) { |
| 454 | grn_string_fin(ctx, string); |
| 455 | string_->original = original; |
| 456 | string_->original_length_in_bytes = length_in_bytes; |
| 457 | rc = GRN_SUCCESS; |
| 458 | } else { |
| 459 | rc = GRN_INVALID_ARGUMENT; |
| 460 | } |
| 461 | GRN_API_RETURN(rc); |
| 462 | } |
| 463 | |
| 464 | int |
| 465 | grn_string_get_flags(grn_ctx *ctx, grn_obj *string) |
| 466 | { |
| 467 | int flags = 0; |
| 468 | grn_string *string_ = (grn_string *)string; |
| 469 | GRN_API_ENTER; |
| 470 | if (string_) { |
| 471 | flags = string_->flags; |
| 472 | } |
| 473 | GRN_API_RETURN(flags); |
| 474 | } |
| 475 | |
| 476 | grn_rc |
| 477 | grn_string_get_normalized(grn_ctx *ctx, grn_obj *string, |
| 478 | const char **normalized, |
| 479 | unsigned int *length_in_bytes, |
| 480 | unsigned int *n_characters) |
| 481 | { |
| 482 | grn_rc rc; |
| 483 | grn_string *string_ = (grn_string *)string; |
| 484 | GRN_API_ENTER; |
| 485 | if (string_) { |
| 486 | if (normalized) { *normalized = string_->normalized; } |
| 487 | if (length_in_bytes) { |
| 488 | *length_in_bytes = string_->normalized_length_in_bytes; |
| 489 | } |
| 490 | if (n_characters) { *n_characters = string_->n_characters; } |
| 491 | rc = GRN_SUCCESS; |
| 492 | } else { |
| 493 | if (normalized) { *normalized = NULL; } |
| 494 | if (length_in_bytes) { *length_in_bytes = 0; } |
| 495 | if (n_characters) { *n_characters = 0; } |
| 496 | rc = GRN_INVALID_ARGUMENT; |
| 497 | } |
| 498 | GRN_API_RETURN(rc); |
| 499 | } |
| 500 | |
| 501 | grn_rc |
| 502 | grn_string_set_normalized(grn_ctx *ctx, grn_obj *string, |
| 503 | char *normalized, unsigned int length_in_bytes, |
| 504 | unsigned int n_characters) |
| 505 | { |
| 506 | grn_rc rc; |
| 507 | grn_string *string_ = (grn_string *)string; |
| 508 | GRN_API_ENTER; |
| 509 | if (string_) { |
| 510 | if (string_->normalized) { GRN_FREE(string_->normalized); } |
| 511 | string_->normalized = normalized; |
| 512 | string_->normalized_length_in_bytes = length_in_bytes; |
| 513 | string_->n_characters = n_characters; |
| 514 | rc = GRN_SUCCESS; |
| 515 | } else { |
| 516 | rc = GRN_INVALID_ARGUMENT; |
| 517 | } |
| 518 | GRN_API_RETURN(rc); |
| 519 | } |
| 520 | |
| 521 | const int16_t * |
| 522 | grn_string_get_checks(grn_ctx *ctx, grn_obj *string) |
| 523 | { |
| 524 | int16_t *checks = NULL; |
| 525 | grn_string *string_ = (grn_string *)string; |
| 526 | GRN_API_ENTER; |
| 527 | if (string_) { |
| 528 | checks = string_->checks; |
| 529 | } else { |
| 530 | checks = NULL; |
| 531 | } |
| 532 | GRN_API_RETURN(checks); |
| 533 | } |
| 534 | |
| 535 | grn_rc |
| 536 | grn_string_set_checks(grn_ctx *ctx, grn_obj *string, int16_t *checks) |
| 537 | { |
| 538 | grn_rc rc; |
| 539 | grn_string *string_ = (grn_string *)string; |
| 540 | GRN_API_ENTER; |
| 541 | if (string_) { |
| 542 | if (string_->checks) { GRN_FREE(string_->checks); } |
| 543 | string_->checks = checks; |
| 544 | rc = GRN_SUCCESS; |
| 545 | } else { |
| 546 | rc = GRN_INVALID_ARGUMENT; |
| 547 | } |
| 548 | GRN_API_RETURN(rc); |
| 549 | } |
| 550 | |
| 551 | const uint8_t * |
| 552 | grn_string_get_types(grn_ctx *ctx, grn_obj *string) |
| 553 | { |
| 554 | uint8_t *types = NULL; |
| 555 | grn_string *string_ = (grn_string *)string; |
| 556 | GRN_API_ENTER; |
| 557 | if (string_) { |
| 558 | types = string_->ctypes; |
| 559 | } else { |
| 560 | types = NULL; |
| 561 | } |
| 562 | GRN_API_RETURN(types); |
| 563 | } |
| 564 | |
| 565 | grn_rc |
| 566 | grn_string_set_types(grn_ctx *ctx, grn_obj *string, uint8_t *types) |
| 567 | { |
| 568 | grn_rc rc; |
| 569 | grn_string *string_ = (grn_string *)string; |
| 570 | GRN_API_ENTER; |
| 571 | if (string_) { |
| 572 | if (string_->ctypes) { GRN_FREE(string_->ctypes); } |
| 573 | string_->ctypes = types; |
| 574 | rc = GRN_SUCCESS; |
| 575 | } else { |
| 576 | rc = GRN_INVALID_ARGUMENT; |
| 577 | } |
| 578 | GRN_API_RETURN(rc); |
| 579 | } |
| 580 | |
| 581 | const uint64_t * |
| 582 | grn_string_get_offsets(grn_ctx *ctx, grn_obj *string) |
| 583 | { |
| 584 | uint64_t *offsets = NULL; |
| 585 | grn_string *string_ = (grn_string *)string; |
| 586 | GRN_API_ENTER; |
| 587 | if (string_) { |
| 588 | offsets = string_->offsets; |
| 589 | } else { |
| 590 | offsets = NULL; |
| 591 | } |
| 592 | GRN_API_RETURN(offsets); |
| 593 | } |
| 594 | |
| 595 | grn_rc |
| 596 | grn_string_set_offsets(grn_ctx *ctx, grn_obj *string, uint64_t *offsets) |
| 597 | { |
| 598 | grn_rc rc; |
| 599 | grn_string *string_ = (grn_string *)string; |
| 600 | GRN_API_ENTER; |
| 601 | if (string_) { |
| 602 | if (string_->offsets) { GRN_FREE(string_->offsets); } |
| 603 | string_->offsets = offsets; |
| 604 | rc = GRN_SUCCESS; |
| 605 | } else { |
| 606 | rc = GRN_INVALID_ARGUMENT; |
| 607 | } |
| 608 | GRN_API_RETURN(rc); |
| 609 | } |
| 610 | |
| 611 | grn_encoding |
| 612 | grn_string_get_encoding(grn_ctx *ctx, grn_obj *string) |
| 613 | { |
| 614 | grn_encoding encoding = GRN_ENC_NONE; |
| 615 | grn_string *string_ = (grn_string *)string; |
| 616 | GRN_API_ENTER; |
| 617 | if (string_) { |
| 618 | encoding = string_->encoding; |
| 619 | } |
| 620 | GRN_API_RETURN(encoding); |
| 621 | } |
| 622 | |
| 623 | grn_obj * |
| 624 | grn_string_get_table(grn_ctx *ctx, grn_obj *string) |
| 625 | { |
| 626 | grn_obj *table = NULL; |
| 627 | grn_string *string_ = (grn_string *)string; |
| 628 | GRN_API_ENTER; |
| 629 | if (string_) { |
| 630 | table = string_->lexicon; |
| 631 | } |
| 632 | GRN_API_RETURN(table); |
| 633 | } |
| 634 | |
| 635 | grn_rc |
| 636 | grn_string_inspect(grn_ctx *ctx, grn_obj *buffer, grn_obj *string) |
| 637 | { |
| 638 | grn_string *string_ = (grn_string *)string; |
| 639 | |
| 640 | GRN_TEXT_PUTS(ctx, buffer, "#<string:"); |
| 641 | |
| 642 | GRN_TEXT_PUTS(ctx, buffer, " original:<"); |
| 643 | GRN_TEXT_PUT(ctx, buffer, |
| 644 | string_->original, |
| 645 | string_->original_length_in_bytes); |
| 646 | GRN_TEXT_PUTS(ctx, buffer, ">"); |
| 647 | GRN_TEXT_PUTS(ctx, buffer, "("); |
| 648 | grn_text_ulltoa(ctx, buffer, string_->original_length_in_bytes); |
| 649 | GRN_TEXT_PUTS(ctx, buffer, ")"); |
| 650 | |
| 651 | GRN_TEXT_PUTS(ctx, buffer, " normalized:<"); |
| 652 | GRN_TEXT_PUT(ctx, buffer, |
| 653 | string_->normalized, |
| 654 | string_->normalized_length_in_bytes); |
| 655 | GRN_TEXT_PUTS(ctx, buffer, ">"); |
| 656 | GRN_TEXT_PUTS(ctx, buffer, "("); |
| 657 | grn_text_ulltoa(ctx, buffer, string_->normalized_length_in_bytes); |
| 658 | GRN_TEXT_PUTS(ctx, buffer, ")"); |
| 659 | |
| 660 | GRN_TEXT_PUTS(ctx, buffer, " n_characters:"); |
| 661 | grn_text_ulltoa(ctx, buffer, string_->n_characters); |
| 662 | |
| 663 | GRN_TEXT_PUTS(ctx, buffer, " encoding:"); |
| 664 | grn_inspect_encoding(ctx, buffer, string_->encoding); |
| 665 | |
| 666 | GRN_TEXT_PUTS(ctx, buffer, " flags:"); |
| 667 | if (string_->flags & GRN_STRING_REMOVE_BLANK) { |
| 668 | GRN_TEXT_PUTS(ctx, buffer, "REMOVE_BLANK|"); |
| 669 | } |
| 670 | if (string_->flags & GRN_STRING_WITH_TYPES) { |
| 671 | GRN_TEXT_PUTS(ctx, buffer, "WITH_TYPES|"); |
| 672 | } |
| 673 | if (string_->flags & GRN_STRING_WITH_CHECKS) { |
| 674 | GRN_TEXT_PUTS(ctx, buffer, "WITH_CHECKS|"); |
| 675 | } |
| 676 | if (string_->flags & GRN_STRING_REMOVE_TOKENIZED_DELIMITER) { |
| 677 | GRN_TEXT_PUTS(ctx, buffer, "REMOVE_TOKENIZED_DELIMITER|"); |
| 678 | } |
| 679 | if (GRN_TEXT_VALUE(buffer)[GRN_TEXT_LEN(buffer) - 1] == '|') { |
| 680 | grn_bulk_truncate(ctx, buffer, GRN_TEXT_LEN(buffer) - 1); |
| 681 | } |
| 682 | |
| 683 | GRN_TEXT_PUTS(ctx, buffer, ">"); |
| 684 | |
| 685 | return GRN_SUCCESS; |
| 686 | } |
| 687 | |
| 688 | uint32_t |
| 689 | grn_string_get_normalizer_index(grn_ctx *ctx, grn_obj *string) |
| 690 | { |
| 691 | uint32_t index = 0; |
| 692 | grn_string *string_ = (grn_string *)string; |
| 693 | GRN_API_ENTER; |
| 694 | if (string_) { |
| 695 | index = string_->normalizer_index; |
| 696 | } |
| 697 | GRN_API_RETURN(index); |
| 698 | } |
| 699 | |
| 700 | grn_rc |
| 701 | grn_string_fin(grn_ctx *ctx, grn_obj *string) |
| 702 | { |
| 703 | grn_rc rc; |
| 704 | grn_string *string_ = (grn_string *)string; |
| 705 | if (string_) { |
| 706 | if (string_->normalized) { |
| 707 | GRN_FREE(string_->normalized); |
| 708 | string_->normalized = NULL; |
| 709 | string_->normalized_length_in_bytes = 0; |
| 710 | string_->n_characters = 0; |
| 711 | } |
| 712 | if (string_->checks) { |
| 713 | GRN_FREE(string_->checks); |
| 714 | string_->checks = NULL; |
| 715 | } |
| 716 | if (string_->ctypes) { |
| 717 | GRN_FREE(string_->ctypes); |
| 718 | string_->ctypes = NULL; |
| 719 | } |
| 720 | if (string_->offsets) { |
| 721 | GRN_FREE(string_->offsets); |
| 722 | string_->ctypes = NULL; |
| 723 | } |
| 724 | rc = GRN_SUCCESS; |
| 725 | } else { |
| 726 | rc = GRN_INVALID_ARGUMENT; |
| 727 | } |
| 728 | return rc; |
| 729 | } |
| 730 | |
| 731 | grn_rc |
| 732 | grn_string_close(grn_ctx *ctx, grn_obj *string) |
| 733 | { |
| 734 | grn_rc rc; |
| 735 | grn_string *string_ = (grn_string *)string; |
| 736 | if (string_) { |
| 737 | grn_string_fin(ctx, string); |
| 738 | GRN_FREE(string); |
| 739 | rc = GRN_SUCCESS; |
| 740 | } else { |
| 741 | rc = GRN_INVALID_ARGUMENT; |
| 742 | } |
| 743 | return rc; |
| 744 | } |