[Groonga-commit] groonga/groonga at 57b0145 [master] string_substring: optimize

Back to archive index

Kouhei Sutou null+****@clear*****
Tue Jul 5 20:33:57 JST 2016


Kouhei Sutou	2016-07-05 20:33:57 +0900 (Tue, 05 Jul 2016)

  New Revision: 57b014573732cb92bd898f1b0a591c4590d2bc24
  https://github.com/groonga/groonga/commit/57b014573732cb92bd898f1b0a591c4590d2bc24

  Message:
    string_substring: optimize

  Modified files:
    plugins/functions/string.c

  Modified: plugins/functions/string.c (+30 -12)
===================================================================
--- plugins/functions/string.c    2016-07-05 20:20:11 +0900 (b1d11ca)
+++ plugins/functions/string.c    2016-07-05 20:33:57 +0900 (aeef335)
@@ -87,7 +87,6 @@ func_string_substring(grn_ctx *ctx, int n_args, grn_obj **args,
   grn_obj *target;
   grn_obj *from_raw;
   grn_obj *length_raw = NULL;
-  size_t string_length = 0;
   int64_t from = 0;
   int64_t length = -1;
   const char *start = NULL;
@@ -230,20 +229,39 @@ func_string_substring(grn_ctx *ctx, int n_args, grn_obj **args,
 
   {
     const char *p;
-    unsigned int cl = 0;
+
     start = NULL;
-    end = GRN_TEXT_VALUE(target) + GRN_TEXT_LEN(target);
-    for (p = GRN_TEXT_VALUE(target);
-         p < end && (cl = grn_charlen(ctx, p, end));
-         p += cl) {
-      if (string_length == from) {
-        start = p;
+    p = GRN_TEXT_VALUE(target);
+    end = p + GRN_TEXT_LEN(target);
+
+    if (from == 0) {
+      start = p;
+    } else {
+      unsigned 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 == from) {
+          start = p;
+          break;
+        }
       }
-      if (length > 0 && string_length - from == length) {
-        end = p;
-        break;
+    }
+
+    if (start && length > 0) {
+      unsigned 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 == length) {
+          end = p;
+          break;
+        }
       }
-      string_length++;
     }
   }
 
-------------- next part --------------
HTML����������������������������...
下载 



More information about the Groonga-commit mailing list
Back to archive index