String.[ch]: memset, memcpy, memmove, memcmph and strlen, strnlen
This commit is contained in:
parent
be50854251
commit
521217f2b5
2 changed files with 93 additions and 0 deletions
60
lib/string.c
60
lib/string.c
|
@ -1,3 +1,5 @@
|
|||
#include <string.h>
|
||||
|
||||
char *itoa(int value, char *str, int base) {
|
||||
char *p = str;
|
||||
char *p1, *p2;
|
||||
|
@ -37,3 +39,61 @@ char *itoa(int value, char *str, int base) {
|
|||
|
||||
return str;
|
||||
}
|
||||
|
||||
void *memset(void *dst, int c, size_t length) {
|
||||
u8 *ptr = (u8 *)dst;
|
||||
const u8 value = (u8)c;
|
||||
|
||||
while (length--) *(ptr++) = value;
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
void *memcpy(void *dst, const void *src, size_t len) {
|
||||
u8 *d = (u8 *)dst;
|
||||
const u8 *s = (const u8 *)src;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
d[i] = s[i];
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
void *memmove(void *dst, const void *src, size_t len) {
|
||||
u8 *d = (u8 *)dst;
|
||||
const u8 *s = (const u8 *)src;
|
||||
if (d < s) {
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
d[i] = s[i];
|
||||
}
|
||||
} else if (d > s) {
|
||||
for (size_t i = len; i > 0; i--) {
|
||||
d[i - 1] = s[i - 1];
|
||||
}
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
int memcmp(const void *s1, const void *s2, size_t len) {
|
||||
const u8 *a = (const u8 *)s1;
|
||||
const u8 *b = (const u8 *)s2;
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if (a[i] != b[i]) {
|
||||
return (int)a[i] - (int)b[i];
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t strlen(const char *s) {
|
||||
const char *p = s;
|
||||
while (*p) ++p;
|
||||
return (size_t)(p - s);
|
||||
}
|
||||
|
||||
size_t strnlen(const char *s, size_t maxlen) {
|
||||
size_t len = 0;
|
||||
while (len < maxlen && s[len] != '\0') {
|
||||
len++;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
|
33
lib/string.h
33
lib/string.h
|
@ -1,7 +1,40 @@
|
|||
#ifndef KERNEL_STRING_H
|
||||
#define KERNEL_STRING_H
|
||||
|
||||
#include <types.h>
|
||||
|
||||
/** Integer to ascii */
|
||||
char *itoa(int value, char *str, int base);
|
||||
|
||||
/** Fill memory with constant byte */
|
||||
void *memset(void *dst, int c, size_t len);
|
||||
|
||||
/** Copy `len` bytes from `src` to `dst`. Undefined if regions overlap. */
|
||||
void *memcpy(void *dst, const void *src, size_t len);
|
||||
|
||||
/** Copy `len` bytes from `src` to `dst`, safe for overlapping regions. */
|
||||
void *memmove(void *dst, const void *src, size_t len);
|
||||
|
||||
/** Compare `len` bytes of `s1` and `s2`.
|
||||
* Returns 0 if equal, <0 if s1 < s2, >0 if s1 > s2. */
|
||||
int memcmp(const void *s1, const void *s2, size_t len);
|
||||
|
||||
/** Returns the length of a null-terminated string */
|
||||
size_t strlen(const char *s);
|
||||
|
||||
/** Return length of string `s`, up to a max of `maxlen` bytes */
|
||||
size_t strnlen(const char *s, size_t maxlen);
|
||||
|
||||
// TODO: These:
|
||||
/*
|
||||
int strcmp(const char *s1, const char *s2);
|
||||
int strncmp(const char *s1, const char *s2, size_t n);
|
||||
|
||||
char *strcpy(char *dst, const char *src);
|
||||
char *strncpy(char *dst, const char *src, size_t n);
|
||||
|
||||
char *strchr(const char *s, int c);
|
||||
char *strrchr(const char *s, int c);
|
||||
*/
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Reference in a new issue