libluna/CString: Implement strspn and strcspn, and using those implement strtok

This commit is contained in:
apio 2023-03-10 21:32:18 +01:00
parent 6512e9549e
commit 5a9da55e05
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 72 additions and 0 deletions

View File

@ -13,6 +13,11 @@ extern "C"
int strcmp(const char* a, const char* b);
usize strspn(const char* str, const char* accept);
usize strcspn(const char* str, const char* reject);
char* strtok(char* str, const char* delim);
usize wcslen(const wchar_t* str);
char* strdup(const char* str);

View File

@ -136,4 +136,71 @@ extern "C"
dest[copy_len] = 0;
return src_len;
}
usize strcspn(const char* str, const char* reject)
{
const char* s = str;
while (*s)
{
const char* rp = reject;
while (*rp)
{
if (*s == *rp) return (usize)(s - str);
rp++;
}
s++;
}
return (usize)(s - str);
}
usize strspn(const char* str, const char* accept)
{
const char* s = str;
while (*s)
{
const char* ap = accept;
bool match = false;
while (*ap)
{
if (*s == *ap)
{
match = true;
break;
}
ap++;
}
if (!match) return (usize)(s - str);
s++;
}
return (usize)(s - str);
}
char* strtok(char* str, const char* delim)
{
static char* s = nullptr;
if (str) s = str;
if (!s) return nullptr;
if (*s)
{
usize skip = strspn(s, delim);
s += skip;
if (*s == 0) return nullptr;
usize use = strcspn(s, delim);
char* result = s;
if (s[use] != 0)
{
s[use] = 0;
s += (use + 1);
}
else { s = nullptr; }
return result;
}
return nullptr;
}
}