| 1 | /* |
| 2 | Copyright (C) 2016 Brazil |
| 3 | Copyright (C) 2020-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 | #ifdef GRN_EMBEDDED |
| 21 | # define GRN_PLUGIN_FUNCTION_TAG functions_string |
| 22 | #endif |
| 23 | |
| 24 | #include <grn_onigmo.h> |
| 25 | |
| 26 | #include <groonga/plugin.h> |
| 27 | |
| 28 | static int64_t |
| 29 | string_n_chars(grn_ctx *ctx, const char *string, size_t string_size) |
| 30 | { |
| 31 | const char *p = string; |
| 32 | const char *end = string + string_size; |
| 33 | int64_t n_chars = 0; |
| 34 | int char_size = 0; |
| 35 | |
| 36 | for (; p < end && (char_size = grn_charlen(ctx, p, end)); p += char_size) { |
| 37 | n_chars++; |
| 38 | } |
| 39 | |
| 40 | return n_chars; |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | * func_string_length() returns the number of characters in a string. |
| 45 | * If the string contains an invalid byte sequence, this function returns the |
| 46 | * number of characters before the invalid byte sequence. |
| 47 | */ |
| 48 | static grn_obj * |
| 49 | func_string_length(grn_ctx *ctx, |
| 50 | int n_args, |
| 51 | grn_obj **args, |
| 52 | grn_user_data *user_data) |
| 53 | { |
| 54 | grn_obj *target; |
| 55 | unsigned int length = 0; |
| 56 | grn_obj *grn_length; |
| 57 | |
| 58 | if (n_args != 1) { |
| 59 | GRN_PLUGIN_ERROR(ctx, |
| 60 | GRN_INVALID_ARGUMENT, |
| 61 | "string_length(): wrong number of arguments (%d for 1)", |
| 62 | n_args); |
| 63 | return NULL; |
| 64 | } |
| 65 | |
| 66 | target = args[0]; |
| 67 | if (!(target->header.type == GRN_BULK && |
| 68 | ((target->header.domain == GRN_DB_SHORT_TEXT) || |
| 69 | (target->header.domain == GRN_DB_TEXT) || |
| 70 | (target->header.domain == GRN_DB_LONG_TEXT)))) { |
| 71 | grn_obj inspected; |
| 72 | |
| 73 | GRN_TEXT_INIT(&inspected, 0); |
| 74 | grn_inspect(ctx, &inspected, target); |
| 75 | GRN_PLUGIN_ERROR(ctx, |
| 76 | GRN_INVALID_ARGUMENT, |
| 77 | "string_length(): target object must be a text bulk: " |
| 78 | "<%.*s>", |
| 79 | (int)GRN_TEXT_LEN(&inspected), |
| 80 | GRN_TEXT_VALUE(&inspected)); |
| 81 | GRN_OBJ_FIN(ctx, &inspected); |
| 82 | return NULL; |
| 83 | } |
| 84 | |
| 85 | length = string_n_chars(ctx, GRN_TEXT_VALUE(target), GRN_TEXT_LEN(target)); |
| 86 | grn_length = grn_plugin_proc_alloc(ctx, user_data, GRN_DB_UINT32, 0); |
| 87 | if (!grn_length) { |
| 88 | return NULL; |
| 89 | } |
| 90 | |
| 91 | GRN_UINT32_SET(ctx, grn_length, length); |
| 92 | |
| 93 | return grn_length; |
| 94 | } |
| 95 | |
| 96 | static grn_obj * |
| 97 | func_string_substring(grn_ctx *ctx, |
| 98 | int n_args, |
| 99 | grn_obj **args, |
| 100 | grn_user_data *user_data) |
| 101 | { |
| 102 | #define string_substring_tag "[string_substring]" |
| 103 | |
| 104 | grn_obj *target; |
| 105 | grn_obj *from_raw; |
| 106 | grn_obj *length_raw = NULL; |
| 107 | grn_obj *default_value = NULL; |
| 108 | grn_obj *options = NULL; |
| 109 | int64_t from = 0; |
| 110 | int64_t length = -1; |
| 111 | const char *start = NULL; |
| 112 | const char *end = NULL; |
| 113 | grn_obj *substring = NULL; |
| 114 | |
| 115 | if (n_args < 2 || n_args > 4) { |
| 116 | GRN_PLUGIN_ERROR(ctx, |
| 117 | GRN_INVALID_ARGUMENT, |
| 118 | "%s " |
| 119 | "wrong number of arguments (%d for 2..4)", |
| 120 | string_substring_tag, |
| 121 | n_args); |
| 122 | return NULL; |
| 123 | } |
| 124 | |
| 125 | target = args[0]; |
| 126 | from_raw = args[1]; |
| 127 | |
| 128 | if (n_args >= 3) { |
| 129 | grn_obj *length_or_options = args[2]; |
| 130 | if (grn_obj_is_number_family_bulk(ctx, length_or_options)) { |
| 131 | length_raw = length_or_options; |
| 132 | } else if (length_or_options->header.type == GRN_TABLE_HASH_KEY) { |
| 133 | options = length_or_options; |
| 134 | } else { |
| 135 | grn_obj inspected; |
| 136 | |
| 137 | GRN_TEXT_INIT(&inspected, 0); |
| 138 | grn_inspect(ctx, &inspected, length_or_options); |
| 139 | GRN_PLUGIN_ERROR(ctx, |
| 140 | GRN_INVALID_ARGUMENT, |
| 141 | "%s " |
| 142 | "3rd argument must be a long or a hash table: %.*s", |
| 143 | string_substring_tag, |
| 144 | (int)GRN_TEXT_LEN(&inspected), |
| 145 | GRN_TEXT_VALUE(&inspected)); |
| 146 | GRN_OBJ_FIN(ctx, &inspected); |
| 147 | return NULL; |
| 148 | } |
| 149 | } |
| 150 | if (n_args == 4) { |
| 151 | // options type will be checked in grn_proc_options_parse |
| 152 | options = args[3]; |
| 153 | } |
| 154 | |
| 155 | if (options) { |
| 156 | grn_rc rc = grn_proc_options_parse(ctx, |
| 157 | options, |
| 158 | string_substring_tag, |
| 159 | "default_value", |
| 160 | GRN_PROC_OPTION_VALUE_RAW, |
| 161 | &default_value, |
| 162 | NULL); |
| 163 | |
| 164 | if (rc != GRN_SUCCESS) { |
| 165 | return NULL; |
| 166 | } |
| 167 | |
| 168 | if (default_value && !grn_obj_is_text_family_bulk(ctx, default_value)) { |
| 169 | grn_obj inspected; |
| 170 | |
| 171 | GRN_TEXT_INIT(&inspected, 0); |
| 172 | grn_inspect(ctx, &inspected, default_value); |
| 173 | GRN_PLUGIN_ERROR(ctx, |
| 174 | GRN_INVALID_ARGUMENT, |
| 175 | "%s[default_value] must be a text bulk: <%.*s>", |
| 176 | string_substring_tag, |
| 177 | (int)GRN_TEXT_LEN(&inspected), |
| 178 | GRN_TEXT_VALUE(&inspected)); |
| 179 | GRN_OBJ_FIN(ctx, &inspected); |
| 180 | return NULL; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | if (!grn_obj_is_text_family_bulk(ctx, target)) { |
| 185 | grn_obj inspected; |
| 186 | |
| 187 | GRN_TEXT_INIT(&inspected, 0); |
| 188 | grn_inspect(ctx, &inspected, target); |
| 189 | GRN_PLUGIN_ERROR(ctx, |
| 190 | GRN_INVALID_ARGUMENT, |
| 191 | "%s[target] must be a text bulk: <%.*s>", |
| 192 | string_substring_tag, |
| 193 | (int)GRN_TEXT_LEN(&inspected), |
| 194 | GRN_TEXT_VALUE(&inspected)); |
| 195 | GRN_OBJ_FIN(ctx, &inspected); |
| 196 | return NULL; |
| 197 | } |
| 198 | |
| 199 | from = grn_plugin_proc_get_value_int64(ctx, |
| 200 | from_raw, |
| 201 | 0, |
| 202 | string_substring_tag "[from]"); |
| 203 | length = grn_plugin_proc_get_value_int64(ctx, |
| 204 | length_raw, |
| 205 | -1, |
| 206 | string_substring_tag "[length]"); |
| 207 | |
| 208 | if (GRN_TEXT_LEN(target) == 0) { |
| 209 | goto exit; |
| 210 | } |
| 211 | if (length == 0) { |
| 212 | return grn_plugin_proc_alloc(ctx, user_data, target->header.domain, 0); |
| 213 | } |
| 214 | |
| 215 | while (from < 0) { |
| 216 | from += (int64_t)GRN_TEXT_LEN(target); |
| 217 | } |
| 218 | |
| 219 | { |
| 220 | const char *p; |
| 221 | |
| 222 | start = NULL; |
| 223 | p = GRN_TEXT_VALUE(target); |
| 224 | end = p + GRN_TEXT_LEN(target); |
| 225 | |
| 226 | if (from == 0) { |
| 227 | start = p; |
| 228 | } else { |
| 229 | int char_length = 0; |
| 230 | size_t n_chars = 0; |
| 231 | |
| 232 | for (; p < end && (char_length = grn_charlen(ctx, p, end)); |
| 233 | p += char_length, n_chars++) { |
| 234 | if (n_chars == (size_t)from) { |
| 235 | start = p; |
| 236 | break; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | if (start && length > 0) { |
| 242 | int char_length = 0; |
| 243 | size_t n_chars = 0; |
| 244 | |
| 245 | for (; p < end && (char_length = grn_charlen(ctx, p, end)); |
| 246 | p += char_length, n_chars++) { |
| 247 | if (n_chars == (size_t)length) { |
| 248 | end = p; |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | if (start) { |
| 256 | substring = grn_plugin_proc_alloc(ctx, user_data, target->header.domain, 0); |
| 257 | if (!substring) { |
| 258 | return NULL; |
| 259 | } |
| 260 | GRN_TEXT_SET(ctx, substring, start, end - start); |
| 261 | } |
| 262 | |
| 263 | exit: |
| 264 | |
| 265 | if (!substring) { |
| 266 | if (!default_value) { |
| 267 | default_value = |
| 268 | grn_plugin_proc_alloc(ctx, user_data, target->header.domain, 0); |
| 269 | if (!default_value) { |
| 270 | return NULL; |
| 271 | } |
| 272 | } |
| 273 | substring = default_value; |
| 274 | } else if (GRN_TEXT_LEN(substring) == 0 && default_value) { |
| 275 | substring = default_value; |
| 276 | } |
| 277 | |
| 278 | return substring; |
| 279 | #undef string_substring_tag |
| 280 | } |
| 281 | |
| 282 | static bool |
| 283 | string_is_over_n_chars(grn_ctx *ctx, |
| 284 | const char *string, |
| 285 | size_t string_size, |
| 286 | int64_t max_n_chars, |
| 287 | int64_t n_keep_chars, |
| 288 | const char **kept_end) |
| 289 | { |
| 290 | const char *p = string; |
| 291 | const char *end = string + string_size; |
| 292 | int64_t n_chars = 0; |
| 293 | int char_size = 0; |
| 294 | |
| 295 | *kept_end = string; |
| 296 | for (; p < end && (char_size = grn_charlen(ctx, p, end)); p += char_size) { |
| 297 | n_chars++; |
| 298 | if (n_chars == n_keep_chars) { |
| 299 | *kept_end = p + char_size; |
| 300 | } |
| 301 | if (n_chars > max_n_chars) { |
| 302 | return true; |
| 303 | } |
| 304 | } |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | /* |
| 309 | * func_string_truncate() truncates a string to at most `length` characters. |
| 310 | * If truncation occurs, the tail of the kept portion is replaced so that the |
| 311 | * result (kept portion + omission) is `length` characters long, in the same |
| 312 | * way as Ruby on Rails' String#truncate. |
| 313 | * |
| 314 | * Note: |
| 315 | * If `omission` itself is longer than `length`, the result is `omission` alone. |
| 316 | * This is the same as Ruby on Rails' String#truncate. |
| 317 | */ |
| 318 | static grn_obj * |
| 319 | func_string_truncate(grn_ctx *ctx, |
| 320 | int n_args, |
| 321 | grn_obj **args, |
| 322 | grn_user_data *user_data) |
| 323 | { |
| 324 | #define string_truncate_tag "[string_truncate]" |
| 325 | |
| 326 | if (n_args < 2 || n_args > 3) { |
| 327 | GRN_PLUGIN_ERROR(ctx, |
| 328 | GRN_INVALID_ARGUMENT, |
| 329 | "%s " |
| 330 | "wrong number of arguments (%d for 2..3)", |
| 331 | string_truncate_tag, |
| 332 | n_args); |
| 333 | return NULL; |
| 334 | } |
| 335 | |
| 336 | grn_obj *target = args[0]; |
| 337 | grn_obj *options = NULL; |
| 338 | if (n_args == 3) { |
| 339 | options = args[2]; |
| 340 | } |
| 341 | |
| 342 | if (!grn_obj_is_text_family_bulk(ctx, target)) { |
| 343 | grn_obj inspected; |
| 344 | GRN_TEXT_INIT(&inspected, 0); |
| 345 | grn_inspect(ctx, &inspected, target); |
| 346 | GRN_PLUGIN_ERROR(ctx, |
| 347 | GRN_INVALID_ARGUMENT, |
| 348 | "%s[target] must be a text bulk: <%.*s>", |
| 349 | string_truncate_tag, |
| 350 | (int)GRN_TEXT_LEN(&inspected), |
| 351 | GRN_TEXT_VALUE(&inspected)); |
| 352 | GRN_OBJ_FIN(ctx, &inspected); |
| 353 | return NULL; |
| 354 | } |
| 355 | |
| 356 | grn_obj *omission = NULL; |
| 357 | if (options) { |
| 358 | grn_rc rc = grn_proc_options_parse(ctx, |
| 359 | options, |
| 360 | string_truncate_tag, |
| 361 | "omission", |
| 362 | GRN_PROC_OPTION_VALUE_RAW, |
| 363 | &omission, |
| 364 | NULL); |
| 365 | |
| 366 | if (rc != GRN_SUCCESS) { |
| 367 | return NULL; |
| 368 | } |
| 369 | |
| 370 | if (omission && !grn_obj_is_text_family_bulk(ctx, omission)) { |
| 371 | grn_obj inspected; |
| 372 | GRN_TEXT_INIT(&inspected, 0); |
| 373 | grn_inspect(ctx, &inspected, omission); |
| 374 | GRN_PLUGIN_ERROR(ctx, |
| 375 | GRN_INVALID_ARGUMENT, |
| 376 | "%s[omission] must be a text bulk: <%.*s>", |
| 377 | string_truncate_tag, |
| 378 | (int)GRN_TEXT_LEN(&inspected), |
| 379 | GRN_TEXT_VALUE(&inspected)); |
| 380 | GRN_OBJ_FIN(ctx, &inspected); |
| 381 | return NULL; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | int64_t max_n_chars = |
| 386 | grn_plugin_proc_get_value_int64(ctx, |
| 387 | args[1], |
| 388 | 0, |
| 389 | string_truncate_tag "[length]"); |
| 390 | if (ctx->rc != GRN_SUCCESS) { |
| 391 | return NULL; |
| 392 | } |
| 393 | |
| 394 | const char *omission_value = "..."; |
| 395 | size_t omission_size = 3; |
| 396 | int64_t n_omission_chars = 3; |
| 397 | if (omission) { |
| 398 | omission_value = GRN_TEXT_VALUE(omission); |
| 399 | omission_size = GRN_TEXT_LEN(omission); |
| 400 | n_omission_chars = string_n_chars(ctx, omission_value, omission_size); |
| 401 | } |
| 402 | |
| 403 | grn_obj *result = |
| 404 | grn_plugin_proc_alloc(ctx, user_data, target->header.domain, 0); |
| 405 | if (!result) { |
| 406 | return NULL; |
| 407 | } |
| 408 | if (max_n_chars < 0) { |
| 409 | GRN_TEXT_SET(ctx, result, omission_value, omission_size); |
| 410 | return result; |
| 411 | } |
| 412 | |
| 413 | int64_t n_keep_chars = max_n_chars - n_omission_chars; |
| 414 | if (n_keep_chars < 0) { |
| 415 | n_keep_chars = 0; |
| 416 | } |
| 417 | const char *kept_end = NULL; |
| 418 | bool is_over = string_is_over_n_chars(ctx, |
| 419 | GRN_TEXT_VALUE(target), |
| 420 | GRN_TEXT_LEN(target), |
| 421 | max_n_chars, |
| 422 | n_keep_chars, |
| 423 | &kept_end); |
| 424 | if (!is_over) { |
| 425 | GRN_TEXT_SET(ctx, result, GRN_TEXT_VALUE(target), GRN_TEXT_LEN(target)); |
| 426 | return result; |
| 427 | } |
| 428 | |
| 429 | const char *kept_start = GRN_TEXT_VALUE(target); |
| 430 | GRN_TEXT_SET(ctx, result, kept_start, kept_end - kept_start); |
| 431 | GRN_TEXT_PUT(ctx, result, omission_value, omission_size); |
| 432 | return result; |
| 433 | #undef string_truncate_tag |
| 434 | } |
| 435 | |
| 436 | static grn_obj * |
| 437 | func_string_tokenize(grn_ctx *ctx, |
| 438 | int n_args, |
| 439 | grn_obj **args, |
| 440 | grn_user_data *user_data) |
| 441 | { |
| 442 | grn_obj *target; |
| 443 | grn_obj *lexicon; |
| 444 | grn_obj *options = NULL; |
| 445 | |
| 446 | if (!(n_args == 2 || n_args == 3)) { |
| 447 | GRN_PLUGIN_ERROR( |
| 448 | ctx, |
| 449 | GRN_INVALID_ARGUMENT, |
| 450 | "[string_tokenize] wrong number of arguments (%d for 2..3)", |
| 451 | n_args); |
| 452 | return NULL; |
| 453 | } |
| 454 | |
| 455 | target = args[0]; |
| 456 | lexicon = args[1]; |
| 457 | if (n_args == 3) { |
| 458 | options = args[2]; |
| 459 | } |
| 460 | |
| 461 | if (!grn_obj_is_text_family_bulk(ctx, target)) { |
| 462 | grn_obj inspected; |
| 463 | |
| 464 | GRN_TEXT_INIT(&inspected, 0); |
| 465 | grn_inspect(ctx, &inspected, target); |
| 466 | GRN_PLUGIN_ERROR(ctx, |
| 467 | GRN_INVALID_ARGUMENT, |
| 468 | "[string_tokenize][target] must be a text bulk: %.*s", |
| 469 | (int)GRN_TEXT_LEN(&inspected), |
| 470 | GRN_TEXT_VALUE(&inspected)); |
| 471 | GRN_OBJ_FIN(ctx, &inspected); |
| 472 | return NULL; |
| 473 | } |
| 474 | |
| 475 | if (!grn_obj_is_table_with_key(ctx, lexicon)) { |
| 476 | grn_obj inspected; |
| 477 | |
| 478 | GRN_TEXT_INIT(&inspected, 0); |
| 479 | grn_inspect(ctx, &inspected, lexicon); |
| 480 | GRN_PLUGIN_ERROR( |
| 481 | ctx, |
| 482 | GRN_INVALID_ARGUMENT, |
| 483 | "[string_tokenize][lexicon] must be a table with key: %.*s", |
| 484 | (int)GRN_TEXT_LEN(&inspected), |
| 485 | GRN_TEXT_VALUE(&inspected)); |
| 486 | GRN_OBJ_FIN(ctx, &inspected); |
| 487 | return NULL; |
| 488 | } |
| 489 | |
| 490 | grn_tokenize_mode mode = GRN_TOKEN_GET; |
| 491 | uint32_t flags = 0; |
| 492 | if (options) { |
| 493 | grn_rc rc = grn_proc_options_parse(ctx, |
| 494 | options, |
| 495 | "[string_tokenize]", |
| 496 | "mode", |
| 497 | GRN_PROC_OPTION_VALUE_TOKENIZE_MODE, |
| 498 | &mode, |
| 499 | "flags", |
| 500 | GRN_PROC_OPTION_VALUE_TOKEN_CURSOR_FLAGS, |
| 501 | &flags, |
| 502 | NULL); |
| 503 | if (rc != GRN_SUCCESS) { |
| 504 | return NULL; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | grn_obj *tokens = grn_plugin_proc_alloc(ctx, |
| 509 | user_data, |
| 510 | grn_obj_id(ctx, lexicon), |
| 511 | GRN_OBJ_VECTOR); |
| 512 | if (!tokens) { |
| 513 | return NULL; |
| 514 | } |
| 515 | tokens->header.flags |= GRN_OBJ_WITH_WEIGHT; |
| 516 | |
| 517 | grn_token_cursor *token_cursor; |
| 518 | token_cursor = grn_token_cursor_open(ctx, |
| 519 | lexicon, |
| 520 | GRN_TEXT_VALUE(target), |
| 521 | GRN_TEXT_LEN(target), |
| 522 | mode, |
| 523 | flags); |
| 524 | if (!token_cursor) { |
| 525 | return tokens; |
| 526 | } |
| 527 | while (grn_token_cursor_get_status(ctx, token_cursor) == |
| 528 | GRN_TOKEN_CURSOR_DOING) { |
| 529 | grn_id token_id = grn_token_cursor_next(ctx, token_cursor); |
| 530 | |
| 531 | if (token_id == GRN_ID_NIL) { |
| 532 | continue; |
| 533 | } |
| 534 | |
| 535 | grn_token *token = grn_token_cursor_get_token(ctx, token_cursor); |
| 536 | float weight = grn_token_get_weight(ctx, token); |
| 537 | grn_uvector_add_element_record(ctx, tokens, token_id, weight); |
| 538 | } |
| 539 | grn_token_cursor_close(ctx, token_cursor); |
| 540 | |
| 541 | return tokens; |
| 542 | } |
| 543 | |
| 544 | static grn_obj * |
| 545 | string_regexp_slice(grn_ctx *ctx, |
| 546 | int n_args, |
| 547 | grn_obj **args, |
| 548 | grn_user_data *user_data) |
| 549 | { |
| 550 | #ifdef GRN_SUPPORT_REGEXP |
| 551 | # define string_regexp_slice_tag "[string_slice]" |
| 552 | |
| 553 | grn_obj *target_raw, *pattern, *nth_or_name, *default_value = NULL, |
| 554 | *result = NULL; |
| 555 | |
| 556 | if (!(n_args == 3 || n_args == 4)) { |
| 557 | GRN_PLUGIN_ERROR(ctx, |
| 558 | GRN_INVALID_ARGUMENT, |
| 559 | "%s wrong number of arguments (%d for 3...4)", |
| 560 | string_regexp_slice_tag, |
| 561 | n_args); |
| 562 | return NULL; |
| 563 | } |
| 564 | |
| 565 | target_raw = args[0]; |
| 566 | pattern = args[1]; |
| 567 | nth_or_name = args[2]; |
| 568 | |
| 569 | if (n_args == 4) { |
| 570 | grn_obj *options = args[3]; |
| 571 | grn_rc rc = grn_proc_options_parse(ctx, |
| 572 | options, |
| 573 | string_regexp_slice_tag, |
| 574 | "default_value", |
| 575 | GRN_PROC_OPTION_VALUE_RAW, |
| 576 | &default_value, |
| 577 | NULL); |
| 578 | |
| 579 | if (rc != GRN_SUCCESS) { |
| 580 | return NULL; |
| 581 | } |
| 582 | |
| 583 | if (default_value && !grn_obj_is_text_family_bulk(ctx, default_value)) { |
| 584 | grn_obj inspected; |
| 585 | |
| 586 | GRN_TEXT_INIT(&inspected, 0); |
| 587 | grn_inspect(ctx, &inspected, default_value); |
| 588 | GRN_PLUGIN_ERROR(ctx, |
| 589 | GRN_INVALID_ARGUMENT, |
| 590 | "%s[default_value] must be a text bulk: <%.*s>", |
| 591 | string_regexp_slice_tag, |
| 592 | (int)GRN_TEXT_LEN(&inspected), |
| 593 | GRN_TEXT_VALUE(&inspected)); |
| 594 | GRN_OBJ_FIN(ctx, &inspected); |
| 595 | return NULL; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | if (!grn_obj_is_text_family_bulk(ctx, nth_or_name) && |
| 600 | !grn_obj_is_number_family_bulk(ctx, nth_or_name)) { |
| 601 | grn_obj inspected; |
| 602 | |
| 603 | GRN_TEXT_INIT(&inspected, 0); |
| 604 | grn_inspect(ctx, &inspected, nth_or_name); |
| 605 | GRN_PLUGIN_ERROR(ctx, |
| 606 | GRN_INVALID_ARGUMENT, |
| 607 | "%s[nth_or_name] must be a text or number bulk: %.*s", |
| 608 | string_regexp_slice_tag, |
| 609 | (int)GRN_TEXT_LEN(&inspected), |
| 610 | GRN_TEXT_VALUE(&inspected)); |
| 611 | GRN_OBJ_FIN(ctx, &inspected); |
| 612 | return NULL; |
| 613 | } |
| 614 | |
| 615 | // TODO: should cache |
| 616 | OnigRegex regexp = grn_onigmo_new(ctx, |
| 617 | GRN_TEXT_VALUE(pattern), |
| 618 | GRN_TEXT_LEN(pattern), |
| 619 | GRN_ONIGMO_OPTION_DEFAULT, |
| 620 | GRN_ONIGMO_SYNTAX_DEFAULT, |
| 621 | string_regexp_slice_tag "[regexp]"); |
| 622 | |
| 623 | if (!regexp) { |
| 624 | return NULL; |
| 625 | } |
| 626 | |
| 627 | const char *target = GRN_TEXT_VALUE(target_raw); |
| 628 | size_t target_length = GRN_TEXT_LEN(target_raw); |
| 629 | |
| 630 | OnigRegion region; |
| 631 | onig_region_init(®ion); |
| 632 | |
| 633 | // Cannot use normalized string. |
| 634 | // The matching parts of the original string cannot be inferred from the |
| 635 | // matching parts of the normalized string. |
| 636 | OnigPosition position = onig_search(regexp, |
| 637 | target, |
| 638 | target + target_length, |
| 639 | target, |
| 640 | target + target_length, |
| 641 | ®ion, |
| 642 | ONIG_OPTION_NONE); |
| 643 | |
| 644 | if (position != ONIG_MISMATCH) { |
| 645 | int64_t nth = -1; |
| 646 | |
| 647 | if (grn_obj_is_text_family_bulk(ctx, nth_or_name)) { |
| 648 | const char *name = GRN_TEXT_VALUE(nth_or_name); |
| 649 | size_t name_length = GRN_TEXT_LEN(nth_or_name); |
| 650 | |
| 651 | nth = |
| 652 | onig_name_to_backref_number(regexp, name, name + name_length, ®ion); |
| 653 | |
| 654 | } else if (grn_obj_is_number_family_bulk(ctx, nth_or_name)) { |
| 655 | nth = grn_plugin_proc_get_value_int64(ctx, |
| 656 | nth_or_name, |
| 657 | 0, |
| 658 | string_regexp_slice_tag "[nth]"); |
| 659 | } |
| 660 | |
| 661 | if (nth >= 0 && nth < region.num_regs) { |
| 662 | OnigPosition start = region.beg[nth]; |
| 663 | OnigPosition end = region.end[nth]; |
| 664 | |
| 665 | result = |
| 666 | grn_plugin_proc_alloc(ctx, user_data, target_raw->header.domain, 0); |
| 667 | if (!result) { |
| 668 | goto exit; |
| 669 | } |
| 670 | GRN_TEXT_SET(ctx, result, target + start, end - start); |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | if (!result) { |
| 675 | if (!default_value) { |
| 676 | default_value = |
| 677 | grn_plugin_proc_alloc(ctx, user_data, target_raw->header.domain, 0); |
| 678 | if (!default_value) { |
| 679 | goto exit; |
| 680 | } |
| 681 | } |
| 682 | result = default_value; |
| 683 | } |
| 684 | |
| 685 | exit: |
| 686 | |
| 687 | onig_region_free(®ion, 0); |
| 688 | onig_free(regexp); |
| 689 | |
| 690 | return result; |
| 691 | # undef string_regexp_slice_tag |
| 692 | #else // GRN_SUPPORT_REGEXP |
| 693 | return NULL; |
| 694 | #endif // GRN_SUPPORT_REGEXP |
| 695 | } |
| 696 | |
| 697 | static grn_obj * |
| 698 | func_string_slice(grn_ctx *ctx, |
| 699 | int n_args, |
| 700 | grn_obj **args, |
| 701 | grn_user_data *user_data) |
| 702 | { |
| 703 | const char *tag = "[string_slice]"; |
| 704 | |
| 705 | if (n_args < 2 || n_args > 4) { |
| 706 | GRN_PLUGIN_ERROR(ctx, |
| 707 | GRN_INVALID_ARGUMENT, |
| 708 | "%s " |
| 709 | "wrong number of arguments (%d for 2..4)", |
| 710 | tag, |
| 711 | n_args); |
| 712 | return NULL; |
| 713 | } |
| 714 | |
| 715 | if (grn_obj_is_number_family_bulk(ctx, args[1])) { |
| 716 | if (n_args >= 3) { |
| 717 | return func_string_substring(ctx, n_args, args, user_data); |
| 718 | } |
| 719 | |
| 720 | grn_obj *new_args[3]; |
| 721 | grn_obj third_arg; |
| 722 | grn_obj *result; |
| 723 | |
| 724 | GRN_INT64_INIT(&third_arg, 0); |
| 725 | GRN_INT64_SET(ctx, &third_arg, 1); |
| 726 | |
| 727 | new_args[0] = args[0]; |
| 728 | new_args[1] = args[1]; |
| 729 | new_args[2] = &third_arg; |
| 730 | |
| 731 | result = func_string_substring(ctx, 3, new_args, user_data); |
| 732 | |
| 733 | GRN_OBJ_FIN(ctx, &third_arg); |
| 734 | |
| 735 | return result; |
| 736 | } else if (grn_obj_is_text_family_bulk(ctx, args[1])) { |
| 737 | return string_regexp_slice(ctx, n_args, args, user_data); |
| 738 | } else { |
| 739 | grn_obj inspected; |
| 740 | |
| 741 | GRN_TEXT_INIT(&inspected, 0); |
| 742 | grn_inspect(ctx, &inspected, args[1]); |
| 743 | GRN_PLUGIN_ERROR(ctx, |
| 744 | GRN_INVALID_ARGUMENT, |
| 745 | "%s " |
| 746 | "2nd argument must be a text or number bulk: %.*s", |
| 747 | tag, |
| 748 | (int)GRN_TEXT_LEN(&inspected), |
| 749 | GRN_TEXT_VALUE(&inspected)); |
| 750 | GRN_OBJ_FIN(ctx, &inspected); |
| 751 | |
| 752 | return NULL; |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | grn_rc |
| 757 | GRN_PLUGIN_INIT(grn_ctx *ctx) |
| 758 | { |
| 759 | return ctx->rc; |
| 760 | } |
| 761 | |
| 762 | grn_rc |
| 763 | GRN_PLUGIN_REGISTER(grn_ctx *ctx) |
| 764 | { |
| 765 | grn_rc rc = GRN_SUCCESS; |
| 766 | |
| 767 | grn_proc_create(ctx, |
| 768 | "string_length", |
| 769 | -1, |
| 770 | GRN_PROC_FUNCTION, |
| 771 | func_string_length, |
| 772 | NULL, |
| 773 | NULL, |
| 774 | 0, |
| 775 | NULL); |
| 776 | |
| 777 | grn_proc_create(ctx, |
| 778 | "string_substring", |
| 779 | -1, |
| 780 | GRN_PROC_FUNCTION, |
| 781 | func_string_substring, |
| 782 | NULL, |
| 783 | NULL, |
| 784 | 0, |
| 785 | NULL); |
| 786 | |
| 787 | grn_proc_create(ctx, |
| 788 | "string_truncate", |
| 789 | -1, |
| 790 | GRN_PROC_FUNCTION, |
| 791 | func_string_truncate, |
| 792 | NULL, |
| 793 | NULL, |
| 794 | 0, |
| 795 | NULL); |
| 796 | |
| 797 | grn_proc_create(ctx, |
| 798 | "string_tokenize", |
| 799 | -1, |
| 800 | GRN_PROC_FUNCTION, |
| 801 | func_string_tokenize, |
| 802 | NULL, |
| 803 | NULL, |
| 804 | 0, |
| 805 | NULL); |
| 806 | |
| 807 | grn_proc_create(ctx, |
| 808 | "string_slice", |
| 809 | -1, |
| 810 | GRN_PROC_FUNCTION, |
| 811 | func_string_slice, |
| 812 | NULL, |
| 813 | NULL, |
| 814 | 0, |
| 815 | NULL); |
| 816 | |
| 817 | return rc; |
| 818 | } |
| 819 | |
| 820 | grn_rc |
| 821 | GRN_PLUGIN_FIN(grn_ctx *ctx) |
| 822 | { |
| 823 | return GRN_SUCCESS; |
| 824 | } |