This commit is contained in:
pommicket 2022-02-16 19:41:44 -05:00
parent c29bc36514
commit b1e837afb4
2 changed files with 128 additions and 8 deletions

View file

@ -15,9 +15,19 @@ int compar(const void *a, const void *b) {
int main(int argc, char **argv) { int main(int argc, char **argv) {
char buf[36]; char buf[36];
memset(buf, 'a', sizeof buf); strcpy(buf, "Hello there there!");
strncpy(buf, "hello, world!\n",36); /* buf[36]='b'; */
printf("%d\n",strcmp(buf, "hello, world!\n")); printf("%s\n",strstr(buf," ther"));
static char str[] = "?a???b,,,#c";
char *t;
printf("%s\n", strtok(str, "?")); /* t points to the token "a" */
printf("%s\n", strtok(NULL, ","));
printf("%s\n", strtok(NULL, "#,"));
printf("%s\n", strtok(NULL, "?"));
return 0; return 0;
} }

View file

@ -59,17 +59,127 @@ int memcmp(const void *s1, const void *s2, size_t n) {
int strcmp(const char *s1, const char *s2) { int strcmp(const char *s1, const char *s2) {
char *p = s1, *q = s2; char *p = s1, *q = s2;
for (; *p && *q; ++p, ++q) { for (; ; ++p, ++q) {
if (*p > *q) if (*p > *q)
return 1; return 1;
if (*p < *q) if (*p < *q)
return -1; return -1;
if (!*p) break;
} }
if (*p > *q)
return 1;
if (*p < *q)
return -1;
return 0; return 0;
} }
int strcoll(const char *s1, const char *s2) {
// we only support the C locale
return strcmp(s1, s2);
}
int strncmp(const char *s1, const char *s2, size_t n) {
char *p = s1, *q = s2;
size_t i;
for (i = 0; i < n; ++i, ++p, ++q) {
if (*p > *q)
return 1;
if (*p < *q)
return -1;
if (!*p) break;
}
return 0;
}
size_t strxfrm(char *s1, const char *s2, size_t n) {
// we only support the C locale
size_t l = strlen(s2);
if (l >= n) return l;
strcpy(s1, s2);
return l;
}
void *memchr(const void *s, int c, size_t n) {
char *p = s, *end = p + n;
while (p < end) {
if ((unsigned char)*p == c)
return p;
++p;
}
return NULL;
}
char *strchr(const char *s, int c) {
return memchr(s, c, strlen(s)+1);
}
size_t strcspn(const char *s1, const char *s2) {
const char *p, *q;
for (p = s1; *p; ++p) {
for (q = s2; *q; ++q) {
if (*p == *q)
goto ret;
}
}
ret:
return p - s1;
}
char *strpbrk(const char *s1, const char *s2) {
const char *p, *q;
for (p = s1; *p; ++p) {
for (q = s2; *q; ++q) {
if (*p == *q)
return p;
}
}
return NULL;
}
char *strrchr(const char *s, int c) {
char *p;
for (p = s + strlen(s); p >= s; --p) {
if (*p == c)
return p;
}
return NULL;
}
size_t strspn(const char *s1, const char *s2) {
const char *p, *q;
for (p = s1; *p; ++p) {
for (q = s2; *q; ++q) {
if (*p == *q) break;
}
if (!*q) break;
}
return p - s1;
}
char *strstr(const char *s1, const char *s2) {
char *p;
size_t l = strlen(s2);
for (p = s1; *p; ++p) {
if (memcmp(p, s2, l) == 0)
return p;
}
return NULL;
}
char *_strtok_str;
char *strtok(char *s1, const char *s2) {
if (s1) _strtok_str = s1;
if (!_strtok_str) return NULL;
char *p = _strtok_str + strspn(_strtok_str, s2);
if (!*p) {
_strtok_str = NULL;
return NULL;
}
char *q = strpbrk(p, s2);
if (q) {
*q = 0;
_strtok_str = q + 1;
} else {
_strtok_str = NULL;
}
return p;
}
#endif // _STRING_H #endif // _STRING_H