File: string.c

package info (click to toggle)
groonga 15.2.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 171,500 kB
  • sloc: ansic: 772,536; cpp: 51,530; ruby: 40,538; javascript: 10,250; yacc: 7,045; sh: 5,622; python: 2,821; makefile: 1,677
file content (605 lines) | stat: -rw-r--r-- 18,010 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/*
  Copyright (C) 2016  Brazil
  Copyright (C) 2020-2022  Sutou Kouhei <kou@clear-code.com>

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#ifdef GRN_EMBEDDED
#  define GRN_PLUGIN_FUNCTION_TAG functions_string
#endif

#include <grn_onigmo.h>

#include <groonga/plugin.h>

/*
 * func_string_length() returns the number of characters in a string.
 * If the string contains an invalid byte sequence, this function returns the
 * number of characters before the invalid byte sequence.
 */
static grn_obj *
func_string_length(grn_ctx *ctx, int n_args, grn_obj **args,
                   grn_user_data *user_data)
{
  grn_obj *target;
  unsigned int length = 0;
  grn_obj *grn_length;

  if (n_args != 1) {
    GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
                     "string_length(): wrong number of arguments (%d for 1)",
                     n_args);
    return NULL;
  }

  target = args[0];
  if (!(target->header.type == GRN_BULK &&
        ((target->header.domain == GRN_DB_SHORT_TEXT) ||
         (target->header.domain == GRN_DB_TEXT) ||
         (target->header.domain == GRN_DB_LONG_TEXT)))) {
    grn_obj inspected;

    GRN_TEXT_INIT(&inspected, 0);
    grn_inspect(ctx, &inspected, target);
    GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
                     "string_length(): target object must be a text bulk: "
                     "<%.*s>",
                     (int)GRN_TEXT_LEN(&inspected),
                     GRN_TEXT_VALUE(&inspected));
    GRN_OBJ_FIN(ctx, &inspected);
    return NULL;
  }

  {
    const char *s = GRN_TEXT_VALUE(target);
    const char *e = GRN_TEXT_VALUE(target) + GRN_TEXT_LEN(target);
    const char *p;
    int cl = 0;
    for (p = s; p < e && (cl = grn_charlen(ctx, p, e)); p += cl) {
      length++;
    }
  }

  grn_length = grn_plugin_proc_alloc(ctx, user_data, GRN_DB_UINT32, 0);
  if (!grn_length) {
    return NULL;
  }

  GRN_UINT32_SET(ctx, grn_length, length);

  return grn_length;
}

static grn_obj *
func_string_substring(grn_ctx *ctx, int n_args, grn_obj **args,
                      grn_user_data *user_data)
{
#define string_substring_tag "[string_substring]"

  grn_obj *target;
  grn_obj *from_raw;
  grn_obj *length_raw = NULL;
  grn_obj *default_value = NULL;
  grn_obj *options = NULL;
  int64_t from = 0;
  int64_t length = -1;
  const char *start = NULL;
  const char *end = NULL;
  grn_obj *substring = NULL;

  if (n_args < 2 || n_args > 4) {
    GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
                     "%s "
                     "wrong number of arguments (%d for 2..4)",
                     string_substring_tag,
                     n_args);
    return NULL;
  }

  target = args[0];
  from_raw = args[1];

  if (n_args >= 3) {
    grn_obj *length_or_options = args[2];
    if (grn_obj_is_number_family_bulk(ctx, length_or_options)) {
      length_raw = length_or_options;
    } else if (length_or_options->header.type == GRN_TABLE_HASH_KEY) {
      options = length_or_options;
    } else {
      grn_obj inspected;

      GRN_TEXT_INIT(&inspected, 0);
      grn_inspect(ctx, &inspected, length_or_options);
      GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
                       "%s "
                       "3rd argument must be a long or a hash table: %.*s",
                       string_substring_tag,
                       (int)GRN_TEXT_LEN(&inspected),
                       GRN_TEXT_VALUE(&inspected));
      GRN_OBJ_FIN(ctx, &inspected);
      return NULL;
    }
  }
  if (n_args == 4) {
    //options type will be checked in grn_proc_options_parse
    options = args[3];
  }

  if (options) {
    grn_rc rc = grn_proc_options_parse(ctx,
                                       options,
                                       string_substring_tag,
                                       "default_value",
                                       GRN_PROC_OPTION_VALUE_RAW,
                                       &default_value,
                                       NULL);

    if (rc != GRN_SUCCESS) {
      return NULL;
    }

    if (default_value && !grn_obj_is_text_family_bulk(ctx, default_value)) {
      grn_obj inspected;

      GRN_TEXT_INIT(&inspected, 0);
      grn_inspect(ctx, &inspected, default_value);
      GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
                       "%s[default_value] must be a text bulk: <%.*s>",
                       string_substring_tag,
                       (int)GRN_TEXT_LEN(&inspected),
                       GRN_TEXT_VALUE(&inspected));
      GRN_OBJ_FIN(ctx, &inspected);
      return NULL;
    }
  }

  if (!grn_obj_is_text_family_bulk(ctx, target)) {
    grn_obj inspected;

    GRN_TEXT_INIT(&inspected, 0);
    grn_inspect(ctx, &inspected, target);
    GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
                     "%s[target] must be a text bulk: <%.*s>",
                     string_substring_tag,
                     (int)GRN_TEXT_LEN(&inspected),
                     GRN_TEXT_VALUE(&inspected));
    GRN_OBJ_FIN(ctx, &inspected);
    return NULL;
  }

  from = grn_plugin_proc_get_value_int64(ctx,
                                         from_raw,
                                         0,
                                         string_substring_tag "[from]");
  length = grn_plugin_proc_get_value_int64(ctx,
                                           length_raw,
                                           -1,
                                           string_substring_tag "[length]");

  if (GRN_TEXT_LEN(target) == 0) {
    goto exit;
  }
  if (length == 0) {
    return grn_plugin_proc_alloc(ctx, user_data, target->header.domain, 0);
  }

  while (from < 0) {
    from += (int64_t)GRN_TEXT_LEN(target);
  }

  {
    const char *p;

    start = NULL;
    p = GRN_TEXT_VALUE(target);
    end = p + GRN_TEXT_LEN(target);

    if (from == 0) {
      start = p;
    } else {
      int char_length = 0;
      size_t n_chars = 0;

      for (;
           p < end && (char_length = grn_charlen(ctx, p, end));
           p += char_length, n_chars++) {
        if (n_chars == (size_t)from) {
          start = p;
          break;
        }
      }
    }

    if (start && length > 0) {
      int char_length = 0;
      size_t n_chars = 0;

      for (;
           p < end && (char_length = grn_charlen(ctx, p, end));
           p += char_length, n_chars++) {
        if (n_chars == (size_t)length) {
          end = p;
          break;
        }
      }
    }
  }

  if (start) {
    substring = grn_plugin_proc_alloc(ctx, user_data, target->header.domain, 0);
    if (!substring) {
      return NULL;
    }
    GRN_TEXT_SET(ctx, substring, start, end - start);
  }

exit:

  if (!substring) {
    if (!default_value) {
      default_value = grn_plugin_proc_alloc(ctx, user_data, target->header.domain, 0);
      if (!default_value) {
        return NULL;
      }
    }
    substring = default_value;
  } else if (GRN_TEXT_LEN(substring) == 0 && default_value) {
    substring = default_value;
  }

  return substring;
#undef string_substring_tag
}

static grn_obj *
func_string_tokenize(grn_ctx *ctx, int n_args, grn_obj **args,
                     grn_user_data *user_data)
{
  grn_obj *target;
  grn_obj *lexicon;
  grn_obj *options = NULL;

  if (!(n_args == 2 || n_args == 3)) {
    GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
                     "[string_tokenize] wrong number of arguments (%d for 2..3)",
                     n_args);
    return NULL;
  }

  target = args[0];
  lexicon = args[1];
  if (n_args == 3) {
    options = args[2];
  }

  if (!grn_obj_is_text_family_bulk(ctx, target)) {
    grn_obj inspected;

    GRN_TEXT_INIT(&inspected, 0);
    grn_inspect(ctx, &inspected, target);
    GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
                     "[string_tokenize][target] must be a text bulk: %.*s",
                     (int)GRN_TEXT_LEN(&inspected),
                     GRN_TEXT_VALUE(&inspected));
    GRN_OBJ_FIN(ctx, &inspected);
    return NULL;
  }

  if (!grn_obj_is_table_with_key(ctx, lexicon)) {
    grn_obj inspected;

    GRN_TEXT_INIT(&inspected, 0);
    grn_inspect(ctx, &inspected, lexicon);
    GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
                     "[string_tokenize][lexicon] must be a table with key: %.*s",
                     (int)GRN_TEXT_LEN(&inspected),
                     GRN_TEXT_VALUE(&inspected));
    GRN_OBJ_FIN(ctx, &inspected);
    return NULL;
  }

  grn_tokenize_mode mode = GRN_TOKEN_GET;
  uint32_t flags = 0;
  if (options) {
    grn_rc rc = grn_proc_options_parse(ctx,
                                       options,
                                       "[string_tokenize]",
                                       "mode",
                                       GRN_PROC_OPTION_VALUE_TOKENIZE_MODE,
                                       &mode,
                                       "flags",
                                       GRN_PROC_OPTION_VALUE_TOKEN_CURSOR_FLAGS,
                                       &flags,
                                       NULL);
    if (rc != GRN_SUCCESS) {
      return NULL;
    }
  }

  grn_obj *tokens = grn_plugin_proc_alloc(ctx,
                                          user_data,
                                          grn_obj_id(ctx, lexicon),
                                          GRN_OBJ_VECTOR);
  if (!tokens) {
    return NULL;
  }
  tokens->header.flags |= GRN_OBJ_WITH_WEIGHT;

  grn_token_cursor *token_cursor;
  token_cursor = grn_token_cursor_open(ctx,
                                       lexicon,
                                       GRN_TEXT_VALUE(target),
                                       GRN_TEXT_LEN(target),
                                       mode,
                                       flags);
  if (!token_cursor) {
    return tokens;
  }
  while (grn_token_cursor_get_status(ctx, token_cursor) ==
         GRN_TOKEN_CURSOR_DOING) {
    grn_id token_id = grn_token_cursor_next(ctx, token_cursor);

    if (token_id == GRN_ID_NIL) {
      continue;
    }

    grn_token *token = grn_token_cursor_get_token(ctx, token_cursor);
    float weight = grn_token_get_weight(ctx, token);
    grn_uvector_add_element_record(ctx, tokens, token_id, weight);
  }
  grn_token_cursor_close(ctx, token_cursor);

  return tokens;
}

static grn_obj *
string_regexp_slice(grn_ctx *ctx, int n_args, grn_obj **args, grn_user_data *user_data)
{
#ifdef GRN_SUPPORT_REGEXP
#define string_regexp_slice_tag "[string_slice]"

  grn_obj *target_raw, *pattern, *nth_or_name, *default_value = NULL, *result = NULL;

  if (!(n_args == 3 || n_args == 4)) {
    GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
                     "%s wrong number of arguments (%d for 3...4)",
                     string_regexp_slice_tag,
                     n_args);
    return NULL;
  }

  target_raw = args[0];
  pattern = args[1];
  nth_or_name = args[2];

  if (n_args == 4) {
    grn_obj *options = args[3];
    grn_rc rc = grn_proc_options_parse(ctx,
                                       options,
                                       string_regexp_slice_tag,
                                       "default_value",
                                       GRN_PROC_OPTION_VALUE_RAW,
                                       &default_value,
                                       NULL);

    if (rc != GRN_SUCCESS) {
      return NULL;
    }

    if (default_value && !grn_obj_is_text_family_bulk(ctx, default_value)) {
      grn_obj inspected;

      GRN_TEXT_INIT(&inspected, 0);
      grn_inspect(ctx, &inspected, default_value);
      GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
                       "%s[default_value] must be a text bulk: <%.*s>",
                       string_regexp_slice_tag,
                       (int)GRN_TEXT_LEN(&inspected),
                       GRN_TEXT_VALUE(&inspected));
      GRN_OBJ_FIN(ctx, &inspected);
      return NULL;
    }
  }

  if (!grn_obj_is_text_family_bulk(ctx, nth_or_name) && !grn_obj_is_number_family_bulk(ctx, nth_or_name)) {
    grn_obj inspected;

    GRN_TEXT_INIT(&inspected, 0);
    grn_inspect(ctx, &inspected, nth_or_name);
    GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
                     "%s[nth_or_name] must be a text or number bulk: %.*s",
                     string_regexp_slice_tag,
                     (int)GRN_TEXT_LEN(&inspected),
                     GRN_TEXT_VALUE(&inspected));
    GRN_OBJ_FIN(ctx, &inspected);
    return NULL;
  }

  //TODO: should cache
  OnigRegex regexp = grn_onigmo_new(ctx,
                                    GRN_TEXT_VALUE(pattern),
                                    GRN_TEXT_LEN(pattern),
                                    GRN_ONIGMO_OPTION_DEFAULT,
                                    GRN_ONIGMO_SYNTAX_DEFAULT,
                                    string_regexp_slice_tag "[regexp]");

  if (!regexp) {
    return NULL;
  }

  const char *target = GRN_TEXT_VALUE(target_raw);
  size_t target_length = GRN_TEXT_LEN(target_raw);

  OnigRegion region;
  onig_region_init(&region);

  //Cannot use normalized string.
  //The matching parts of the original string cannot be inferred from the matching parts of the normalized string.
  OnigPosition position = onig_search(regexp,
                                      target,
                                      target + target_length,
                                      target,
                                      target + target_length,
                                      &region,
                                      ONIG_OPTION_NONE);

  if (position != ONIG_MISMATCH) {
    int64_t nth = -1;

    if (grn_obj_is_text_family_bulk(ctx, nth_or_name)) {
      const char *name = GRN_TEXT_VALUE(nth_or_name);
      size_t name_length = GRN_TEXT_LEN(nth_or_name);

      nth = onig_name_to_backref_number(regexp,
                                        name,
                                        name + name_length,
                                        &region);

    } else if (grn_obj_is_number_family_bulk(ctx, nth_or_name)) {
      nth = grn_plugin_proc_get_value_int64(ctx,
                                            nth_or_name,
                                            0,
                                            string_regexp_slice_tag "[nth]");
    }

    if (nth >= 0 && nth < region.num_regs) {
      OnigPosition start = region.beg[nth];
      OnigPosition end = region.end[nth];

      result = grn_plugin_proc_alloc(ctx, user_data, target_raw->header.domain, 0);
      if (!result) {
        goto exit;
      }
      GRN_TEXT_SET(ctx, result, target + start, end - start);
    }
  }

  if (!result) {
    if (!default_value) {
      default_value = grn_plugin_proc_alloc(ctx, user_data, target_raw->header.domain, 0);
      if (!default_value) {
        goto exit;
      }
    }
    result = default_value;
  }

exit:

  onig_region_free(&region, 0);
  onig_free(regexp);

  return result;
#undef string_regexp_slice_tag
#else //GRN_SUPPORT_REGEXP
  return NULL;
#endif //GRN_SUPPORT_REGEXP
}

static grn_obj *
func_string_slice(grn_ctx *ctx, int n_args, grn_obj **args,
                  grn_user_data *user_data)
{
  const char *tag = "[string_slice]";

  if (n_args < 2 || n_args > 4) {
    GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
                     "%s "
                     "wrong number of arguments (%d for 2..4)",
                     tag,
                     n_args);
    return NULL;
  }

  if (grn_obj_is_number_family_bulk(ctx, args[1])) {
    if (n_args >= 3) {
      return func_string_substring(ctx, n_args, args, user_data);
    }

    grn_obj *new_args[3];
    grn_obj third_arg;
    grn_obj *result;

    GRN_INT64_INIT(&third_arg, 0);
    GRN_INT64_SET(ctx, &third_arg, 1);

    new_args[0] = args[0];
    new_args[1] = args[1];
    new_args[2] = &third_arg;

    result = func_string_substring(ctx, 3, new_args, user_data);

    GRN_OBJ_FIN(ctx, &third_arg);

    return result;
  } else if (grn_obj_is_text_family_bulk(ctx, args[1])) {
    return string_regexp_slice(ctx, n_args, args, user_data);
  } else {
    grn_obj inspected;

    GRN_TEXT_INIT(&inspected, 0);
    grn_inspect(ctx, &inspected, args[1]);
    GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
                     "%s "
                     "2nd argument must be a text or number bulk: %.*s",
                     tag,
                     (int)GRN_TEXT_LEN(&inspected),
                     GRN_TEXT_VALUE(&inspected));
    GRN_OBJ_FIN(ctx, &inspected);

    return NULL;
  }
}

grn_rc
GRN_PLUGIN_INIT(grn_ctx *ctx)
{
  return ctx->rc;
}

grn_rc
GRN_PLUGIN_REGISTER(grn_ctx *ctx)
{
  grn_rc rc = GRN_SUCCESS;

  grn_proc_create(ctx, "string_length", -1,
                  GRN_PROC_FUNCTION,
                  func_string_length,
                  NULL, NULL, 0, NULL);

  grn_proc_create(ctx, "string_substring", -1,
                  GRN_PROC_FUNCTION,
                  func_string_substring,
                  NULL, NULL, 0, NULL);

  grn_proc_create(ctx, "string_tokenize", -1,
                  GRN_PROC_FUNCTION,
                  func_string_tokenize,
                  NULL, NULL, 0, NULL);

  grn_proc_create(ctx, "string_slice", -1,
                  GRN_PROC_FUNCTION,
                  func_string_slice,
                  NULL, NULL, 0, NULL);

  return rc;
}

grn_rc
GRN_PLUGIN_FIN(grn_ctx *ctx)
{
  return GRN_SUCCESS;
}