a3a2d8ab5e85 — Leonard Ritter 24 days ago
* `String`: `'append` now supports appending whole strings
2 files changed, 30 insertions(+), 1 deletions(-)

M lib/scopes/String.sc
M testing/test_string.sc
M lib/scopes/String.sc +25 -1
@@ 239,7 239,31 @@ typedef+ StringBase
     fn append (self value)
         let dest = (append-slots self 1:usize)
         assign (imply value ((typeof self) . ElementType)) dest
-        dest
+        ;
+
+    inline... append (self, value : (typematch T < StringBase))
+        let cls = (typeof self)
+        static-assert (cls == (typeof value))
+        let count = (countof value)
+        let ptr = (append-slots self (countof value))
+        llvm.memcpy.p0i8.p0i8.i64
+            bitcast (& ptr) (mutable rawstring)
+            & (value @ 0)
+            (count * (sizeof cls.ElementType)) as i64
+            false
+        ;
+    case (self, value : string)
+        let cls = (typeof self)
+        static-assert (cls.ElementType == i8)
+        let count = (countof value)
+        let ptr = (append-slots self (countof value))
+        llvm.memcpy.p0i8.p0i8.i64
+            bitcast (& ptr) (mutable rawstring)
+            value as rawstring
+            (count * (sizeof cls.ElementType)) as i64
+            false
+        ;
+    case using append
 
     """"Construct a new element with arguments `args...` directly in a newly
         assigned slot of string `self`. When the string is of `GrowingString`

          
M testing/test_string.sc +5 -0
@@ 89,6 89,11 @@ do
     local q : String = "init"
     test (q == "init")
     test ((String "test") == "test")
+    'append q "tini"
+    test (q == "inittini")
+    'append q (String "init")
+    test (q == "inittiniinit")
+    ;
 
 # testing proper globalization
 local s = (String "test")