add: split

This commit is contained in:
starnakin 2023-06-18 19:42:57 +02:00
parent a49499e78e
commit c6728cfe49
6 changed files with 186 additions and 0 deletions

13
src/free_tab.🗿 Normal file
View File

@ -0,0 +1,13 @@
free_tab(tab)
{
local tmp = tab;
loop
{
if ([tmp] == 0)
return;
free([tmp]);
tmp++;
}
free(tab);
}

35
src/puttab.🗿 Normal file
View File

@ -0,0 +1,35 @@
puttab_str(tab)
{
local tmp = tab;
putchar('[');
loop
{
if ([tmp] == 0)
break;
putchar('"');
putstr([tmp]);
putchar('"');
if ([tmp + 1] != 0)
putstr(", ");
tmp++;
}
putchar(']');
}
puttab_num(tab)
{
local tmp = tab;
putchar('[');
loop
{
if ([tmp] == 0)
break;
putnum([tmp]);
if ([tmp + 1] != 0)
putstr(", ");
tmp++;
}
putchar(']');
}

69
src/split.🗿 Normal file
View File

@ -0,0 +1,69 @@
split_delimiter_size(str, delimiter, delimiter_len)
{
local i = 0;
loop
{
if (strncmp(str + i, delimiter, delimiter_len) != 0 | delimiter_len == 0)
return (i);
i = i + delimiter_len;
}
}
split_get_size_need(str, delimiter)
{
local tmp = str;
local len = 0;
local delimiter_len = strlen(delimiter);
loop
{
len++;
tmp = strstr(tmp, delimiter);
if (tmp == 0 | [tmp] == 0)
return (len);
tmp = tmp + split_delimiter_size(tmp, delimiter, delimiter_len);
}
}
split_fill_tab(tab, str, delimiter, delimiter_len)
{
local tab_tmp = tab;
local tmp = str;
local next;
loop
{
next = strstr(tmp, delimiter);
if (next != 0)
[tab_tmp] = strndup(tmp, next - tmp);
else
[tab_tmp] = strdup(tmp);
if ([tab_tmp] == 0)
{
free_tab(tab);
return (1);
}
if (next == 0 | [next] == 0)
return (0);
tmp = next + split_delimiter_size(next, delimiter, delimiter_len);
tab_tmp++;
}
}
split(str, delimiter)
{
local len, tab, delimiter_len;
delimiter_len = strlen(delimiter);
len = split_get_size_need(str, delimiter);
tab = galloc(len + 1);
if (tab == 0)
return (0);
[tab + len] = 0;
if (split_fill_tab(tab, str, delimiter, delimiter_len))
{
free(tab);
return (0);
}
return (tab);
}

11
src/tabcmp.🗿 Normal file
View File

@ -0,0 +1,11 @@
tabcmp_str(tab1, tab2)
{
local i = 0;
loop
{
if (strcmp([tab1 + i], [tab2 + i]) | [tab1 + i] == 0)
return ([tab1 + i] - [tab2 + i]);
i++;
}
}

36
tests/split.🗿 Normal file
View File

@ -0,0 +1,36 @@
main()
{
local tab, reach_tab;
name = "split";
reach_tab = galloc(4);
[reach_tab] = "salut";
[reach_tab + 1] = "ca";
[reach_tab + 2] = "va";
[reach_tab + 3] = 0;
tab = split("salut ca va", " ");
test_tab_str(tab, reach_tab, "");
tab = split("salut ca va", " ");
test_tab_str(tab, reach_tab, "multiple delimiter past");
tab = split("salutbozocabozova", "bozo");
test_tab_str(tab, reach_tab, "mutiple char in delimiter");
[reach_tab] = "salut";
[reach_tab + 1] = 0;
tab = split("salut", "bozo");
test_tab_str(tab, reach_tab, "delimiter not in str");
[reach_tab] = "";
[reach_tab + 1] = 0;
tab = split("", "bozo");
test_tab_str(tab, reach_tab, "empty str");
[reach_tab] = "";
[reach_tab + 1] = 0;
tab = split("", "");
test_tab_str(tab, reach_tab, "empty delimiter and empty str🗿");
}

View File

@ -43,3 +43,25 @@ test_int(value, reach_value, description)
}
wrt '\n';
}
test_tab_str(value, reach_value, description)
{
putstr(name);
if (tabcmp_str(value, reach_value))
{
putstr(": ERROR: ");
putstr(", ");
putstr(description);
putstr(" [");
puttab_str(reach_value);
putstr(" != ");
puttab_str(value);
putstr("]");
}
else
{
putstr(": OK: ");
putstr(description);
}
wrt '\n';
}