模仿C库中的字符串切割
作者:
小小蒟蒻
,
2020-10-12 13:02:38
,
所有人可见
,
阅读 485
typedef unsigned int size_t;
typedef unsigned char uchar;
size_t Strlen(const char* str) {
int i = 0;
for (const char* p = str; p && *p; p++, i++);
return i;
}
// first element that in { c | c = str - (str ∩ strSet) }
size_t Strspn(const char* str, const char* strSet) {
for (const char* p = str; p && *p; p++)
for (const char* q = strSet; q; q++)
{
if (!*q) return p - str;
else if (*q == *p) break;
else continue;
}
return 0;
}
// first element that in { c | c = str ∩ strSet }
size_t Strcspn(const char* str, const char* strSet)
{
for (const char* p = str; p && *p; p++)
for (const char* q = strSet; *q && q; q++)
if (*q == *p) return p - str;
return 0;
}
char* Strtok(char* token, const char* delimit)
{
const char* empty = "";
static char* last;
char* e = (char*)delimit;
char* s;
token ? s = token : s = last;
s += Strspn(s, e);
if (!*s) {
last = (char*)empty;
return NULL;
}
e = s + Strcspn(s, e);
if (*e) *e++ = '\0';
last = e;
return s;
}