Add functions for splitting strings.

This commit is contained in:
Christoffer Lerno
2022-12-04 23:01:53 +01:00
parent 927ad2001f
commit c15fb7460c
2 changed files with 40 additions and 0 deletions

View File

@@ -15,6 +15,15 @@ macro tconcat(arr1, arr2)
return result;
}
macro index_of(array, element)
{
foreach (i, &e : array)
{
if (*e == element) return i;
}
return SearchResult.MISSING!;
}
macro concat(arr1, arr2)
{
var $Type = $typeof(arr1[0]);

View File

@@ -148,6 +148,37 @@ fn bool starts_with(char[] s, char[] needle)
return true;
}
fn char[][] tsplit(char[] s, char[] needle) = split(s, needle, mem::temp_allocator()) @inline;
fn char[][] split(char[] s, char[] needle, Allocator* allocator = mem::current_allocator())
{
usz capacity = 16;
usz i = 0;
char[]* holder = allocator.alloc(char[].sizeof * capacity)!!;
while (s.len)
{
usz! index = index_of(s, needle);
char[] res = void;
if (try index)
{
res = s[:index];
s = s[index + 1..];
}
else
{
res = s;
s = s[:0];
}
if (i == capacity)
{
capacity *= 2;
holder = allocator.realloc(holder, capacity)!!;
}
holder[i++] = res;
}
return holder[:i];
}
fn usz! index_of(char[] s, char[] needle)
{
usz match = 0;