[ADD/WIP] Start making string splitters

I want to go on an encore of XEP-0393, with _rectangles_ as boundaries,
this time.
This commit is contained in:
LDA 2024-07-16 19:33:05 +02:00
commit ed712a9f28
3 changed files with 104 additions and 1 deletions

65
src/StrSplit.c Normal file
View file

@ -0,0 +1,65 @@
#include <StringSplit.h>
#include <Cytoplasm/Memory.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
char **
StrSplitLines(char *text)
{
char **ret = NULL;
size_t lines = 0;
char *next = text, *ptr = text;
bool done = false;
if (!text)
{
return NULL;
}
while (!done)
{
size_t bytes;
char *line;
next = strchr(ptr, '\n');
if (!next)
{
next = text + strlen(text);
done = true;
}
/* Take the chunk from ptr to [text] */
bytes = next - ptr;
line = Malloc(bytes + 1);
memcpy(line, ptr, bytes);
line[bytes] = '\0';
ret = Realloc(ret, sizeof(char *) * ++lines);
ret[lines - 1] = line;
ptr = *next ? next + 1 : next;
}
ret = Realloc(ret, sizeof(char *) * ++lines);
ret[lines - 1] = NULL;
return ret;
}
void
StrFreeLines(char **split)
{
char *line, **orig;
if (!split)
{
return;
}
orig = split;
while ((line = *split++))
{
Free(line);
}
Free(orig);
}