diff options
-rw-r--r-- | 05/main.c | 16 | ||||
-rw-r--r-- | 05/string.h | 120 |
2 files changed, 128 insertions, 8 deletions
@@ -15,9 +15,19 @@ int compar(const void *a, const void *b) { int main(int argc, char **argv) { char buf[36]; - memset(buf, 'a', sizeof buf); - strncpy(buf, "hello, world!\n",36); - printf("%d\n",strcmp(buf, "hello, world!\n")); + strcpy(buf, "Hello there there!"); +/* buf[36]='b'; */ + 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; } diff --git a/05/string.h b/05/string.h index 9b59637..02216b5 100644 --- a/05/string.h +++ b/05/string.h @@ -59,17 +59,127 @@ int memcmp(const void *s1, const void *s2, size_t n) { int strcmp(const char *s1, const char *s2) { char *p = s1, *q = s2; - for (; *p && *q; ++p, ++q) { + for (; ; ++p, ++q) { if (*p > *q) return 1; if (*p < *q) return -1; + if (!*p) break; } - if (*p > *q) - return 1; - if (*p < *q) - return -1; 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 |