From a7700c7de50d39341389dcea864eb2a165b5984e Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Sun, 20 Nov 2022 23:21:40 -0600 Subject: [PATCH] added 'string.join'; made 'array.concat' faster --- core/container/array.onyx | 5 ++++- core/string/string.onyx | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/core/container/array.onyx b/core/container/array.onyx index 3bbd3b00..be954731 100644 --- a/core/container/array.onyx +++ b/core/container/array.onyx @@ -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) { diff --git a/core/string/string.onyx b/core/string/string.onyx index 1e37901f..668a020f 100644 --- a/core/string/string.onyx +++ b/core/string/string.onyx @@ -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 -- 2.25.1