mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Add functions for splitting strings.
This commit is contained in:
@@ -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]);
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user