-
Notifications
You must be signed in to change notification settings - Fork 0
string.h
zliang7 edited this page Jan 25, 2016
·
1 revision
- void* memset(void *b, int c, size_t len);
- void* memccpy(void *dest, const void *src, int c, size_t n);
- void* memcpy(void *dest, const void *src, size_t n);
- void* memmove(void *dest, const void *src, size_t len);
- void* memchr(const void *s, int c, size_t n);
- int memcmp(const void *s1, const void *s2, size_t n);
- char* strchr(const char *s, int c)
- char* strrchr(const char *s, int c);
- int strncmp(const char *s1, const char *s2, size_t n);
size_t strnlen(const char* s, size_t maxlen) {
return memchr(s, 0, maxlen) - s;
}
size_t strlen(const char* s) {
return strnlen(s, SIZE_MAX);
}
int strcmp(const char* s1, const char* s2) {
return strncmp(s1, s2, SIZE_MAX);
}
char* stpncpy(char* dest, const char* src, size_t n) {
char* e = memccpy(dest, src, 0, n);
if (!e) return dest + n;
memset(e, 0, n - (e - dest));
return e - 1;
}
char* stpcpy(char* dest, const char* src) {
return stpncpy(dest, src, strlen(src) + 1);
}
char* strncpy(char* dest, const char* src, size_t n) {
char* e = memccpy(dest, src, 0, n);
if (e) memset(e, 0, n - (e - dest));;
return dest;
}
char* strcpy(char* dest, const char* src) {
return strncpy(dest, src, strlen(src) + 1);
}
char *strncat(char* dest, const char* src, size_t n) {
*(stpncpy(memchr(dest, 0, SIZE_MAX), src, n)) = 0;
return dest;
}
char *strcat(char* dest, const char* src) {
return strncat(dest, src, strlen(src) + 1);
}
char *strdup(const char* s) {
size_t len = strlen(s) + 1;
char* buf = malloc(len);
return memcpy(buf, s, len);
}