From c6728cfe496e5a8ff0f70d76faaed1869a966fe9 Mon Sep 17 00:00:00 2001 From: starnakin Date: Sun, 18 Jun 2023 19:42:57 +0200 Subject: [PATCH] add: split --- src/free_tab.๐Ÿ—ฟ | 13 ++++++++++ src/puttab.๐Ÿ—ฟ | 35 +++++++++++++++++++++++++ src/split.๐Ÿ—ฟ | 69 +++++++++++++++++++++++++++++++++++++++++++++++++ src/tabcmp.๐Ÿ—ฟ | 11 ++++++++ tests/split.๐Ÿ—ฟ | 36 ++++++++++++++++++++++++++ tests/test.๐Ÿ—ฟ | 22 ++++++++++++++++ 6 files changed, 186 insertions(+) create mode 100644 src/free_tab.๐Ÿ—ฟ create mode 100644 src/puttab.๐Ÿ—ฟ create mode 100644 src/split.๐Ÿ—ฟ create mode 100644 src/tabcmp.๐Ÿ—ฟ create mode 100644 tests/split.๐Ÿ—ฟ diff --git a/src/free_tab.๐Ÿ—ฟ b/src/free_tab.๐Ÿ—ฟ new file mode 100644 index 0000000..95ca485 --- /dev/null +++ b/src/free_tab.๐Ÿ—ฟ @@ -0,0 +1,13 @@ +free_tab(tab) +{ + local tmp = tab; + + loop + { + if ([tmp] == 0) + return; + free([tmp]); + tmp++; + } + free(tab); +} diff --git a/src/puttab.๐Ÿ—ฟ b/src/puttab.๐Ÿ—ฟ new file mode 100644 index 0000000..31ef74d --- /dev/null +++ b/src/puttab.๐Ÿ—ฟ @@ -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(']'); +} diff --git a/src/split.๐Ÿ—ฟ b/src/split.๐Ÿ—ฟ new file mode 100644 index 0000000..a75e92b --- /dev/null +++ b/src/split.๐Ÿ—ฟ @@ -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); +} diff --git a/src/tabcmp.๐Ÿ—ฟ b/src/tabcmp.๐Ÿ—ฟ new file mode 100644 index 0000000..2207b89 --- /dev/null +++ b/src/tabcmp.๐Ÿ—ฟ @@ -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++; + } +} diff --git a/tests/split.๐Ÿ—ฟ b/tests/split.๐Ÿ—ฟ new file mode 100644 index 0000000..7a662d3 --- /dev/null +++ b/tests/split.๐Ÿ—ฟ @@ -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๐Ÿ—ฟ"); +} diff --git a/tests/test.๐Ÿ—ฟ b/tests/test.๐Ÿ—ฟ index 3650595..7788236 100644 --- a/tests/test.๐Ÿ—ฟ +++ b/tests/test.๐Ÿ—ฟ @@ -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'; +}