diff options
Diffstat (limited to 'src/common/libc')
-rw-r--r-- | src/common/libc/ctype.c | 50 | ||||
-rw-r--r-- | src/common/libc/string.c | 48 |
2 files changed, 97 insertions, 1 deletions
diff --git a/src/common/libc/ctype.c b/src/common/libc/ctype.c index 56daa6b..e54a24c 100644 --- a/src/common/libc/ctype.c +++ b/src/common/libc/ctype.c @@ -1,3 +1,51 @@ #include <ctype.h> -// TODO + +int isalnum(int c) { + return isalpha(c) || isdigit(c); +} +int isalpha(int c) { + return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); +} +int isdigit(int c) { + return (c >= '0' && c <= '9'); +} +int isxdigit(int c) { + return isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); +} +int isspace(int c) { + return c == ' ' || c == '\t' || c == '\r' || c == '\n'; +} +int isprint(int c) { + return (c >= ' ' && c < 256) || isspace(c); +} +int isupper(int c) { + return c >= 'A' && c <= 'Z'; +} +int islower(int c) { + return c >= 'a' && c <= 'z'; +} +int ispunct(int c) { + return isprint(c) && !isspace(c); +} +int isgraph(int c) { + return c > ' ' && c < 256; +} +int iscntrl(int c) { + return c > 0 && c < ' '; +} + +int toupper(int c) { + if (islower(c)) + return c + 'A' - 'a'; + else + return c; +} +int tolower(int c) { + if (isupper(c)) + return c + 'a' - 'A'; + else + return c; +} + +/* vim: set sts=0 ts=4 sw=4 tw=0 noet :*/ diff --git a/src/common/libc/string.c b/src/common/libc/string.c index e1ed21e..4b99dda 100644 --- a/src/common/libc/string.c +++ b/src/common/libc/string.c @@ -1,3 +1,4 @@ +#include <stdbool.h> #include <string.h> #include <kogata/malloc.h> @@ -149,4 +150,51 @@ int strcoll(const char *s1, const char *s2) { return strcmp(s1, s2); } +void *memchr(const void *s, int c, size_t n) { + unsigned char *p = (unsigned char*)s; + for (size_t i = 0; i < n; i++) { + if (p[i] == (unsigned char)c) + return &p[i]; + } + return NULL; +} + +size_t strspn(const char *s, const char *accept) { + size_t l = 0; + while (s[l] != 0) { + bool ok = false; + for (const char* p = accept; *p != 0; p++) { + if (s[l] == *p) { + ok = true; + break; + } + } + if (!ok) break; + l++; + } + return l; +} + +const char *strstr(const char *haystack, const char *needle) { + for (const char* p = haystack; *p != 0; p++) { + if (!strcmp(p, needle)) return p; + } + return NULL; +} + +char* strerror(int errnum) { + // TODO + return "(unspecified error)"; +} + +const char *strpbrk(const char *s, const char *accept) { + while (*s) { + for (const char *p = accept; *p != 0; p++) { + if (*s == *p) return s; + } + s++; + } + return NULL; +} + /* vim: set ts=4 sw=4 tw=0 noet :*/ |