[MOD/WIP] Mess a bit with the XEP-0393 parser

It took a comical amount of time for me to do that LMAO
This commit is contained in:
LDA 2024-08-01 10:31:59 +02:00
commit cb0e77e7a4
5 changed files with 345 additions and 148 deletions

View file

@ -1,9 +1,13 @@
#include <StringSplit.h>
#include <Cytoplasm/Memory.h>
#include <Cytoplasm/Str.h>
#include <Cytoplasm/Log.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
char **
StrSplitLines(char *text)
@ -109,3 +113,131 @@ StrFullRect(char **split)
.source_lines = split
});
}
char
StrGet(StringRect rect, int line, int col)
{
int actual_line, actual_col;
char *linep;
if (!rect.source_lines)
{
return '\0';
}
actual_line = rect.start_line + line;
actual_col = rect.start_char + col;
if (actual_line > rect.end_line)
{
return '\0';
}
if (!(linep = rect.source_lines[actual_line]))
{
return '\0';
}
if (actual_col > strlen(linep))
{
return '\0';
}
return linep[actual_col];
}
size_t
StrViewChars(StringRect rect, int line)
{
int actual_line;
char *linep;
if (!rect.source_lines)
{
return 0;
}
actual_line = rect.start_line + line;
if (actual_line > rect.end_line)
{
return 0;
}
if (!(linep = rect.source_lines[actual_line]))
{
return 0;
}
return rect.end_char - rect.start_char;
}
StringRect
StrGetl(StringRect rect, int line, bool extend)
{
int actual_line;
if (!rect.source_lines)
{
return StrFullRect(NULL);
}
actual_line = rect.start_line + line;
if (actual_line > rect.end_line)
{
return StrFullRect(NULL);
}
rect.start_line = actual_line;
if (!extend)
{
rect.end_line = actual_line;
}
return rect;
}
StringRect
StrShift(StringRect rect, int n)
{
int new = rect.start_char + n;
if (new > rect.end_char)
{
new = rect.end_char;
}
rect.start_char = new;
return rect;
}
size_t
StrViewLines(StringRect view)
{
if (view.start_line > view.end_line)
{
return 0;
}
return view.end_line - view.start_line + 1;
}
void
PrintRect(StringRect rect)
{
size_t i;
if (!rect.source_lines)
{
return;
}
for (i = 0; i < StrViewLines(rect); i++)
{
char *line = NULL, *tmp;
char cbuf[2] = { 0, '\0' };
size_t chi = 0;
while ((*cbuf = StrGet(rect, i, chi)) != '\0' &&
chi++ <= StrViewChars(rect, i))
{
tmp = line;
line = StrConcat(2, line, cbuf);
Free(tmp);
}
Log(LOG_INFO, line);
Free(line);
}
}