Przeglądaj źródła

ncd: modules: explode: Pass through external strings (avoid copy).

Ambroz Bizjak 11 lat temu
rodzic
commit
7bb72a96db
2 zmienionych plików z 14 dodań i 1 usunięć
  1. 9 1
      ncd/modules/explode.c
  2. 5 0
      ncd/tests/explode.ncd

+ 9 - 1
ncd/modules/explode.c

@@ -76,6 +76,7 @@ struct compile_search_instance {
 
 struct instance {
     NCDModuleInst *i;
+    NCDValRef input;
     struct ExpArray arr;
     size_t num;
 };
@@ -176,6 +177,7 @@ static void func_new_common (void *vo, NCDModuleInst *i, const struct NCDModuleI
         ModuleLog(i, BLOG_ERROR, "wrong type");
         goto fail1;
     }
+    o->input = input_arg;
     
     size_t limit = SIZE_MAX;
     if (!NCDVal_IsInvalid(limit_arg)) {
@@ -268,9 +270,15 @@ static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValR
         if (NCDVal_IsInvalid(*out)) {
             goto fail;
         }
+        int is_external = NCDVal_IsExternalString(o->input);
         for (size_t j = 0; j < o->num; j++) {
             MemRef elem = ((MemRef *)o->arr.v)[j];
-            NCDValRef str = NCDVal_NewStringBinMr(mem, elem);
+            NCDValRef str;
+            if (is_external) {
+                str = NCDVal_NewExternalString(mem, elem.ptr, elem.len, NCDVal_ExternalStringTarget(o->input));
+            } else {
+                str = NCDVal_NewStringBinMr(mem, elem);
+            }
             if (NCDVal_IsInvalid(str)) {
                 goto fail;
             }

+ 5 - 0
ncd/tests/explode.ncd

@@ -23,6 +23,11 @@ process main {
     explode("participate in parachute", "parachute in participation of participate in parachuteparparticipate in parachute participate in parachut") l;
     val_equal(l, {"parachute in participation of ", "par", " participate in parachut"}) a;
     assert(a);
+    
+    concat("aaaFOObbbFOOcccFOOddd") e;
+    explode("FOO", e) l;
+    val_equal(l, {"aaa", "bbb", "ccc", "ddd"}) a;
+    assert(a);
 
     exit("0");
 }