[Groonga-commit] groonga/groonga at b52ac4c [master] plugin string: add string_substring()

アーカイブの一覧に戻る

Naoya Murakami null+****@clear*****
Tue Jun 28 14:04:42 JST 2016


Naoya Murakami	2016-06-28 14:04:42 +0900 (Tue, 28 Jun 2016)

  New Revision: b52ac4c2629380a91c962e014464dcd8ce3ced8c
  https://github.com/groonga/groonga/commit/b52ac4c2629380a91c962e014464dcd8ce3ced8c

  Merged a3bee2f: Merge pull request #564 from naoa/string-subtract

  Message:
    plugin string: add string_substring()
    
    string_substring(string, from, [length])

  Added files:
    test/command/suite/select/function/string/string_substring/ascii.expected
    test/command/suite/select/function/string/string_substring/ascii.test
    test/command/suite/select/function/string/string_substring/multibyte.expected
    test/command/suite/select/function/string/string_substring/multibyte.test
    test/command/suite/select/function/string/string_substring/no_length.expected
    test/command/suite/select/function/string/string_substring/no_length.test
  Modified files:
    plugins/functions/string.c

  Modified: plugins/functions/string.c (+73 -0)
===================================================================
--- plugins/functions/string.c    2016-06-25 22:50:51 +0900 (b19ef6a)
+++ plugins/functions/string.c    2016-06-28 14:04:42 +0900 (27af0d2)
@@ -80,6 +80,76 @@ func_string_length(grn_ctx *ctx, int n_args, grn_obj **args,
   return grn_length;
 }
 
+static grn_obj *
+func_string_substring(grn_ctx *ctx, int n_args, grn_obj **args,
+                     grn_user_data *user_data)
+{
+  grn_obj *target;
+  size_t string_length = 0;
+  unsigned long long from = 0;
+  unsigned long long length = 0;
+  const char *start;
+  const char *end;
+  grn_obj *grn_text;
+
+  if (n_args < 2) {
+    GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT,
+                     "string_substring(): wrong number of arguments (%d for 2..)",
+                     n_args);
+    return NULL;
+  }
+
+  target = args[0];
+  from = GRN_UINT64_VALUE(args[1]);
+
+  if (n_args == 3) {
+    length = GRN_UINT64_VALUE(args[2]);
+  }
+
+  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_substring(): 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 *p;
+    unsigned int cl = 0;
+    start = GRN_TEXT_VALUE(target);
+    end = GRN_BULK_CURR(target);
+    for (p = start; p < end && (cl = grn_charlen(ctx, p, end)); p += cl) {
+      if (string_length == from) {
+        start = p;
+      }
+      if (length > 0 && string_length - from == length) {
+        end = p;
+        break;
+      }
+      string_length++;
+    }
+  }
+
+  grn_text = grn_plugin_proc_alloc(ctx, user_data, target->header.domain, 0);
+  if (!grn_text) {
+    return NULL;
+  }
+
+  GRN_TEXT_SET(ctx, grn_text, start, end - start);
+
+  return grn_text;
+}
+
 grn_rc
 GRN_PLUGIN_INIT(grn_ctx *ctx)
 {
@@ -94,6 +164,9 @@ GRN_PLUGIN_REGISTER(grn_ctx *ctx)
   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);
+
   return rc;
 }
 

  Added: test/command/suite/select/function/string/string_substring/ascii.expected (+38 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/string/string_substring/ascii.expected    2016-06-28 14:04:42 +0900 (1ebf7f8)
@@ -0,0 +1,38 @@
+plugin_register functions/string
+[[0,0.0,0.0],true]
+table_create Memos TABLE_HASH_KEY ShortText
+[[0,0.0,0.0],true]
+load --table Memos
+[
+{"_key": "Groonga"}
+]
+[[0,0.0,0.0],1]
+select Memos   --output_columns '_key, string_substring(_key, 0, 2)'
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        1
+      ],
+      [
+        [
+          "_key",
+          "ShortText"
+        ],
+        [
+          "string_substring",
+          null
+        ]
+      ],
+      [
+        "Groonga",
+        "Gr"
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/function/string/string_substring/ascii.test (+11 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/string/string_substring/ascii.test    2016-06-28 14:04:42 +0900 (b30360f)
@@ -0,0 +1,11 @@
+plugin_register functions/string
+
+table_create Memos TABLE_HASH_KEY ShortText
+
+load --table Memos
+[
+{"_key": "Groonga"}
+]
+
+select Memos \
+  --output_columns '_key, string_substring(_key, 0, 2)'

  Added: test/command/suite/select/function/string/string_substring/multibyte.expected (+38 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/string/string_substring/multibyte.expected    2016-06-28 14:04:42 +0900 (07d186e)
@@ -0,0 +1,38 @@
+plugin_register functions/string
+[[0,0.0,0.0],true]
+table_create Memos TABLE_HASH_KEY ShortText
+[[0,0.0,0.0],true]
+load --table Memos
+[
+{"_key": "ぐるんが"}
+]
+[[0,0.0,0.0],1]
+select Memos   --output_columns '_key, string_substring(_key, 2, 3)'
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        1
+      ],
+      [
+        [
+          "_key",
+          "ShortText"
+        ],
+        [
+          "string_substring",
+          null
+        ]
+      ],
+      [
+        "ぐるんが",
+        "んが"
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/function/string/string_substring/multibyte.test (+11 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/string/string_substring/multibyte.test    2016-06-28 14:04:42 +0900 (4359f61)
@@ -0,0 +1,11 @@
+plugin_register functions/string
+
+table_create Memos TABLE_HASH_KEY ShortText
+
+load --table Memos
+[
+{"_key": "ぐるんが"}
+]
+
+select Memos \
+  --output_columns '_key, string_substring(_key, 2, 3)'

  Added: test/command/suite/select/function/string/string_substring/no_length.expected (+38 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/string/string_substring/no_length.expected    2016-06-28 14:04:42 +0900 (be49356)
@@ -0,0 +1,38 @@
+plugin_register functions/string
+[[0,0.0,0.0],true]
+table_create Memos TABLE_HASH_KEY ShortText
+[[0,0.0,0.0],true]
+load --table Memos
+[
+{"_key": "Groonga"}
+]
+[[0,0.0,0.0],1]
+select Memos   --output_columns '_key, string_substring(_key, 2)'
+[
+  [
+    0,
+    0.0,
+    0.0
+  ],
+  [
+    [
+      [
+        1
+      ],
+      [
+        [
+          "_key",
+          "ShortText"
+        ],
+        [
+          "string_substring",
+          null
+        ]
+      ],
+      [
+        "Groonga",
+        "oonga"
+      ]
+    ]
+  ]
+]

  Added: test/command/suite/select/function/string/string_substring/no_length.test (+11 -0) 100644
===================================================================
--- /dev/null
+++ test/command/suite/select/function/string/string_substring/no_length.test    2016-06-28 14:04:42 +0900 (bd1b442)
@@ -0,0 +1,11 @@
+plugin_register functions/string
+
+table_create Memos TABLE_HASH_KEY ShortText
+
+load --table Memos
+[
+{"_key": "Groonga"}
+]
+
+select Memos \
+  --output_columns '_key, string_substring(_key, 2)'
-------------- next part --------------
HTML����������������������������...
ダウンロード 



More information about the Groonga-commit mailing list
アーカイブの一覧に戻る