[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);
}

View file

@ -231,6 +231,9 @@ extern bool ParseeIsAdmin(ParseeData *data, char *user);
/* Measures Parsee's overall uptime */
extern uint64_t ParseeUptime(void);
/* Turns a date into a nice "X minutes, Y seconds" string */
/** Turns a duration into a nice "X minutes, Y seconds" string
* ---------
* Returns: A human-readable string showing the duration[LA:HEAP]
* Modifies: NOTHING */
extern char * ParseeStringifyDate(uint64_t millis);
#endif

35
src/include/StringSplit.h Normal file
View file

@ -0,0 +1,35 @@
#ifndef PARSEE_STRINGSPLIT_H
#define PARSEE_STRINGSPLIT_H
#include <stdlib.h>
/* Represents a boundary in a linesplit string */
typedef struct StringRect {
char **source_lines;
/* NOTE: All of these values are inclusive */
/* The start line/character index */
size_t start_line;
size_t start_char;
/* The end line/character index */
size_t end_line;
size_t end_char;
} StringRect;
/** Splits a string into a set of NULL-terminated substrings, which is
* terminated by a NULL pointer.
* ---------
* Returns: A set of lines(excluding them) [LA:HEAP]
* Modifies: NOTHING
* Thrasher: StrFreeLines */
extern char ** StrSplitLines(char *text);
extern void StrFreeLines(char **split);
extern size_t StrLines(char **split);
/* Creates a full zone covering every part of the split */
extern StringRect StrFullRect(char **split);
#endif