sv: add a few more functions

* alisp.org: reset state of Unit tests to TODO

* alisp.org: take sv_t tasks out of the backlog and set to WIP

* sv: add prototypes for sv_chop_{left,right} and sv_substr

* sv: implement sv_chop_{left, right} and sv_substr

* sv: sv_till and sv_while

* sv: add sv_truncate

When you just want to decrease to a specific size, sv_chop_right is a
bit cumbersome.

* alisp.org: Update and adjust
This commit is contained in:
2026-02-06 07:02:10 +00:00
committed by GitHub
parent a65964e2f7
commit a6d3d861b7
3 changed files with 74 additions and 11 deletions

View File

@@ -22,6 +22,62 @@ sv_t sv_copy(sv_t old)
return SV(newstr, old.size);
}
sv_t sv_chop_left(sv_t sv, u64 size)
{
if (sv.size <= size)
return SV(NULL, 0);
return SV(sv.data + size, sv.size - size);
}
sv_t sv_chop_right(sv_t sv, u64 size)
{
if (sv.size <= size)
return SV(NULL, 0);
return SV(sv.data, sv.size - size);
}
sv_t sv_substr(sv_t sv, u64 position, u64 size)
{
return sv_chop_right(sv_chop_left(sv, position), size);
}
sv_t sv_truncate(sv_t sv, u64 newsize)
{
if (newsize >= sv.size)
return sv;
return SV(sv.data, newsize);
}
sv_t sv_till(sv_t sv, const char *reject)
{
if (sv.size == 0 || !sv.data)
return SV(NULL, 0);
u64 offset;
for (offset = 0; offset < sv.size && strchr(reject, sv.data[offset]) == NULL;
++offset)
continue;
if (offset == sv.size)
return sv;
return sv_chop_right(sv, sv.size - offset);
}
sv_t sv_while(sv_t sv, const char *accept)
{
if (sv.size == 0 || !sv.data)
return SV(NULL, 0);
u64 offset;
for (offset = 0; offset < sv.size && strchr(accept, sv.data[offset]) != NULL;
++offset)
continue;
if (offset == sv.size)
return sv;
return sv_chop_right(sv, sv.size - offset);
}
/* Copyright (C) 2025, 2026 Aryadev Chavali
* This program is distributed in the hope that it will be useful, but WITHOUT