StringView_Partition for One-Off Splits

This commit is contained in:
VegOwOtenks 2024-09-23 18:37:54 +02:00
parent 1a330b3c9b
commit 1b3d007ec2
3 changed files with 51 additions and 0 deletions

View file

@ -133,6 +133,22 @@ StringView StringView_Slice(StringView string, size_t start, size_t end)
return slice; return slice;
} }
bool StringView_Partition(StringView* left, StringView* right, StringView source, StringView delim)
{
if (source.length == 0) return false;
size_t offset = StringView_FindStringOffset(source, delim);
if (offset != SIZE_MAX) {
if (left != NULL) *left = StringView_Slice(source, 0, offset);
if (right != NULL) *right = StringView_Slice(source, offset + delim.length, source.length);
} else {
if (left != NULL) *left = source;
if (right != NULL) *right = STRINGVIEW_NONE;
}
return offset != SIZE_MAX;
}
bool StringView_NextSplit(StringView* dest, StringView* source, StringView delim) bool StringView_NextSplit(StringView* dest, StringView* source, StringView delim)
{ {
if (source->length == 0) return false; if (source->length == 0) return false;
@ -149,6 +165,23 @@ bool StringView_NextSplit(StringView* dest, StringView* source, StringView delim
return true; return true;
} }
bool StringView_LastSplit(StringView* dest, StringView* source, StringView delim)
{
StringView s = *source;
while (s.length != 0 && ! StringView_EndsWith(s, delim)) {
s.length -= 1;
}
if (s.length == 0) {
return false;
} else {
*dest = StringView_Slice(*source, s.length, source->length);
s.length -= delim.length;
*source = s;
return true;
}
}
StringView StringView_StripLeft(StringView sv, StringView strip) StringView StringView_StripLeft(StringView sv, StringView strip)
{ {
while (StringView_StartsWith(sv, strip)) { while (StringView_StartsWith(sv, strip)) {

View file

@ -46,7 +46,9 @@ size_t StringView_FindStringOffset(StringView haystack, StringView needle);
StringView StringView_FindString(StringView haystack, StringView needle); StringView StringView_FindString(StringView haystack, StringView needle);
StringView StringView_Slice(StringView string, size_t start, size_t end); // start and end are offsets StringView StringView_Slice(StringView string, size_t start, size_t end); // start and end are offsets
bool StringView_Partition(StringView* left, StringView* right, StringView source, StringView delim);
bool StringView_NextSplit(StringView* dest, StringView* source, StringView delim); bool StringView_NextSplit(StringView* dest, StringView* source, StringView delim);
bool StringView_LastSplit(StringView* dest, StringView* source, StringView delim);
StringView StringView_StripLeft(StringView sv, StringView strip); StringView StringView_StripLeft(StringView sv, StringView strip);
StringView StringView_StripRight(StringView sv, StringView strip); StringView StringView_StripRight(StringView sv, StringView strip);

View file

@ -29,8 +29,24 @@ void test_split(void)
return; return;
} }
void test_fext(void)
{
const char* filepath = "database.sqlite.old";
StringView sourceSV = StringView_FromString(filepath);
StringView delim = StringView_FromString(".");
StringView split;
assert(StringView_LastSplit(&split, &sourceSV, delim));
assert(StringView_Equal(split, StringView_FromString("old")));
assert(StringView_Equal(sourceSV, StringView_FromString("database.sqlite")));
return;
}
int main() int main()
{ {
test_split(); test_split();
test_fext();
return 0; return 0;
} }