feat(libc): add freestanding types and string helpers

This commit is contained in:
dualfroz committed 2026-08-23 19:48:16 +02:00
1 parent fbe59c8ffc
commit c111550d0e
3 files changed
+104

No files matched your search

+14
View File
@@ -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
+21
View File
@@ -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
+69
View File
@@ -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;
}