فهرست منبع

ncd: modules: value: implement append() to list value. Not even I could remember that insert(x) appends to a list.

ambrop7 13 سال پیش
والد
کامیت
9e3d14bfad
2فایلهای تغییر یافته به همراه25 افزوده شده و 6 حذف شده
  1. 19 4
      ncd/modules/value.c
  2. 6 2
      ncd/tests/value.ncd

+ 19 - 4
ncd/modules/value.c

@@ -140,11 +140,14 @@
  *   undo operation.
  * 
  * Synopsis:
- *   value::append(string data)
+ *   value::append(append_val)
  * 
  * Description:
- *   Appends the given data to a string value. The value this is called on must
- *   be a string value.
+ *   Only defined when the existing value object is a string or list. If it is a string,
+ *   appends the string 'append_val' to this string value; 'append_val' must be a string.
+ *   If is is a list, inserts 'append_val' to the end of this list value. Unlike insert(),
+ *   the resulting append() object is not itself a value object (which in case of insert()
+ *   serves as a reference to the new value).
  */
 
 #include <stdlib.h>
@@ -1259,8 +1262,20 @@ static int value_append (NCDModuleInst *i, struct value *v, NCDValRef data)
             value_string_set_allocd(v, new_string, new_length);
         } break;
         
+        case NCDVAL_LIST: {
+            struct value *nv = value_init_fromvalue(i, data);
+            if (!nv) {
+                return 0;
+            }
+            
+            if (!value_list_insert(i, v, nv, value_list_len(v))) {
+                value_cleanup(nv);
+                return 0;
+            }
+        } break;
+        
         default:
-            ModuleLog(i, BLOG_ERROR, "cannot append to non-string");
+            ModuleLog(i, BLOG_ERROR, "append is only defined for strings and lists");
             return 0;
     }
     

+ 6 - 2
ncd/tests/value.ncd

@@ -177,11 +177,15 @@ process main {
     assert(a);
     
     value({}) v;
-    v->insert("1");
+    v->insert("1") ins_v;
     v->insert("2");
     v->insert("3");
     v->insert("4");
-    val_equal(v, {"1", "2", "3", "4"})  a;
+    v->append("5");
+    v->append("6");
+    val_equal(v, {"1", "2", "3", "4", "5", "6"})  a;
+    assert(a);
+    val_equal(ins_v, "1") a;
     assert(a);
     
     value({}) v;