diff --git a/include/string.h b/include/string.h new file mode 100644 index 0000000..8cfa98a --- /dev/null +++ b/include/string.h @@ -0,0 +1,14 @@ +#ifndef FROSTBYTE_STRING_H +#define FROSTBYTE_STRING_H + +#include "types.h" + +void* memset(void* dest, int value, usize count); +void* memcpy(void* dest, const void* src, usize count); +void* memmove(void* dest, const void* src, usize count); +int memcmp(const void* a, const void* b, usize count); +usize strlen(const char* str); +int strcmp(const char* a, const char* b); +int strncmp(const char* a, const char* b, usize count); + +#endif diff --git a/include/types.h b/include/types.h new file mode 100644 index 0000000..25e0267 --- /dev/null +++ b/include/types.h @@ -0,0 +1,21 @@ +#ifndef FROSTBYTE_TYPES_H +#define FROSTBYTE_TYPES_H + +typedef unsigned char uint8; +typedef signed char int8; +typedef unsigned short uint16; +typedef signed short int16; +typedef unsigned int uint32; +typedef signed int int32; +typedef unsigned long uint64; +typedef signed long int64; + +typedef unsigned long usize; +typedef signed long isize; +typedef unsigned long uintptr; + +typedef enum { false = 0, true = 1 } bool; + +#define nullptr ((void*)0) + +#endif diff --git a/src/string.c b/src/string.c new file mode 100644 index 0000000..2e59e0b --- /dev/null +++ b/src/string.c @@ -0,0 +1,69 @@ +#include "string.h" + +void* memset(void* dest, int value, usize count) +{ + uint8* target = (uint8*)dest; + for (usize i = 0; i < count; i++) + target[i] = (uint8)value; + return dest; +} + +void* memcpy(void* dest, const void* src, usize count) +{ + uint8* target = (uint8*)dest; + const uint8* source = (const uint8*)src; + for (usize i = 0; i < count; i++) + target[i] = source[i]; + return dest; +} + +void* memmove(void* dest, const void* src, usize count) +{ + uint8* target = (uint8*)dest; + const uint8* source = (const uint8*)src; + if (target < source) { + for (usize i = 0; i < count; i++) + target[i] = source[i]; + } else { + for (usize i = count; i > 0; i--) + target[i - 1] = source[i - 1]; + } + return dest; +} + +int memcmp(const void* a, const void* b, usize count) +{ + const uint8* left = (const uint8*)a; + const uint8* right = (const uint8*)b; + for (usize i = 0; i < count; i++) { + if (left[i] != right[i]) + return (int)left[i] - (int)right[i]; + } + return 0; +} + +usize strlen(const char* str) +{ + usize length = 0; + while (str[length]) + length++; + return length; +} + +int strcmp(const char* a, const char* b) +{ + while (*a && (*a == *b)) { + a++; + b++; + } + return (int)(uint8)*a - (int)(uint8)*b; +} + +int strncmp(const char* a, const char* b, usize count) +{ + for (usize i = 0; i < count; i++) { + if (a[i] != b[i] || a[i] == 0) + return (int)(uint8)a[i] - (int)(uint8)b[i]; + } + return 0; +}