utilitiec/src/StringView/StringView.c

276 lines
6.4 KiB
C

/*
* This code is part of the programming language Ivy.
* Ivy comes with ABSOLUTELY NO WARRANTY and is licensed under AGPL-3.0 or later.
* Copyright (C) 2024 VegOwOtenks
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "StringView.h"
#include <ctype.h>
#include <string.h>
#include <stdint.h>
StringView StringView_FromString(const char* source)
{
return StringView_FromStringSized(source, strlen(source));
}
StringView StringView_FromStringSized(const char* source, size_t length)
{
StringView view = {
.source = source,
.length = length,
};
return view;
}
bool StringView_Equal(StringView first, StringView second)
{
return first.length == second.length
&&
strncmp(first.source, second.source, first.length) == 0;
}
bool StringView_StartsWith(StringView string, StringView start)
{
return strncmp(string.source, start.source, start.length) == 0;
}
bool StringView_EndsWith(StringView string, StringView end)
{
if (end.length > string.length) {
return false;
}
return StringView_Equal(
end,
StringView_Slice(
string,
string.length - end.length,
string.length
)
);
}
bool StringView_Contains(StringView string, StringView find)
{
return StringView_FindString(string, find).source != NULL;
}
size_t StringView_Count(StringView string, StringView find)
{
size_t count = 0;
while ((find = StringView_FindString(string, find)).source != NULL) {
size_t offset = (uintptr_t) (find.source) - (uintptr_t) (string.source);
string = (StringView) {string.source + offset, string.length - offset};
}
return count;
}
size_t StringView_FindStringOffset(StringView haystack, StringView needle)
{
size_t offset = 0;
size_t index = 0;
while (offset < haystack.length) {
while (index < needle.length && needle.source[index] == haystack.source[offset + index]) {
index++;
}
if (index == needle.length) {
return offset;
}
offset++;
}
return SIZE_MAX;
}
StringView StringView_FindString(StringView haystack, StringView needle)
{
size_t offset = 0;
size_t index = 0;
while (offset < haystack.length) {
while (index < needle.length && needle.source[index] == haystack.source[offset + index]) {
index++;
}
if (index == needle.length) {
return (StringView) {haystack.source + offset, needle.length};
}
offset++;
}
return (StringView) {NULL, 0};
}
StringView StringView_Drop(StringView string, size_t length)
{
return StringView_Slice(string, length, string.length);
}
StringView StringView_TakeWhile(StringView string, StringViewContinue function)
{
size_t length = 0;
for (length = 0; string.length != length && function(string.source[length]); length++);
return StringView_Take(string, length);
}
StringView StringView_SpanWhile(StringView* source, StringViewContinue function)
{
StringView result = StringView_TakeWhile(*source, function);
*source = StringView_Slice(*source, result.length, source->length);
return result;
}
StringView StringView_Take(StringView string, size_t length)
{
return StringView_Slice(string, 0, length);
}
StringView StringView_Slice(StringView string, size_t start, size_t end)
{
if (end > string.length || start > string.length) {
return (StringView) {NULL, 0};
}
StringView slice = {
.length = end - start,
.source = string.source + start,
};
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)
{
if (source->length == 0) return false;
size_t offset = StringView_FindStringOffset(*source, delim);
if (offset == SIZE_MAX) {
// No more delimiters, return entire source
offset = source->length;
}
*dest = StringView_Slice(*source, 0, offset);
*source = StringView_Slice(*source, offset + delim.length, source->length);
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)
{
while (StringView_StartsWith(sv, strip)) {
sv = StringView_Slice(sv, strip.length, sv.length);
}
return sv;
}
StringView StringView_StripRight(StringView sv, StringView strip)
{
while (StringView_EndsWith(sv, strip)) {
sv = StringView_Slice(sv, 0, sv.length - strip.length);
}
return sv;
}
void* StringView_FoldLeft(StringView string, void* initial, StringViewFoldFunction function)
{
void* value = initial;
while (string.length != 0) {
value = function(string.source[0], value);
string = StringView_Drop(string, 1);
}
return value;
}
void StringView_Paste(char* destination, StringView source)
{
memcpy(destination, source.source, source.length);
}
int StringView_ParseInt(StringView source)
{
int value = 0;
int sign = 1;
switch (source.source[0]) {
case '+':
sign = 1;
source.source++;
source.length--;
break;
case '-':
sign = -1;
source.source++;
source.length--;
break;
default:
break;
}
while (source.length && isdigit(source.source[0])) {
int digit = source.source[0] - '0';
value *= 10;
value += digit;
source.source++;
source.length--;
}
return value * sign;
}