added 'string.join'; made 'array.concat' faster
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 21 Nov 2022 05:21:40 +0000 (23:21 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 21 Nov 2022 05:21:40 +0000 (23:21 -0600)
core/container/array.onyx
core/string/string.onyx

index 3bbd3b00ae20c356cf016cd939b7425c35b35267..be954731483213aa66dca02b3d042f06865398e5 100644 (file)
@@ -184,7 +184,10 @@ pop :: (arr: ^[..] $T) -> T {
 }
 
 concat :: (arr: ^[..] $T, other: [] T) {
-    for ^o: other do push(arr, *o);
+    if !ensure_capacity(arr, arr.count + other.count) do return;
+
+    core.memory.copy(arr.data + arr.count, other.data, other.count * sizeof T);
+    arr.count += other.count;
 }
 
 filter :: macro (arr: ^[..] $T, body: Code) {
index 1e37901ffa5413805deb798cc8d297fe6f4fbf28..668a020fa73a212e1d4e44fb9e63643772e562f9 100644 (file)
@@ -138,6 +138,27 @@ contains :: (s: str, substr: str) -> bool {
 }
 
 
+join :: (strs: [] str, sep: str, allocator := context.allocator) -> str {
+    if strs.count == 0 do return "";
+
+    len_sum := array.fold(strs, 0, #(acc + it.length));
+    out := make(str, len_sum + (strs.count - 1) * sep.count);
+
+    i := 0;
+    for strs {
+        if !#first {
+            core.memory.copy(^out.data[i], sep.data, sep.count);
+            i += sep.count;
+        }
+
+        core.memory.copy(^out.data[i], it.data, it.count);
+        i += it.count;
+    }
+
+    return out;
+}
+
+
 // @TODO
 // Check this for edge cases and other bugs. I'm not confident
 // it will work perfectly yet.                   - brendanfh 2020/12/21