Changed libc structure, shouldn't've added anything new

This commit is contained in:
2024-10-19 15:11:00 +02:00
parent e2e24d3853
commit 64b4e47d73
38 changed files with 264 additions and 276 deletions
+13
View File
@@ -0,0 +1,13 @@
#include <string.h>
unsigned int atoi(const char* str) {
unsigned int res = 0;
// TODO: Add error checking
while (*str >= '0' && *str <= '9') {
res = res * 10 + (*str++ - '0');
}
return res;
}
+20
View File
@@ -0,0 +1,20 @@
#include <string.h>
// Source: http://www.strudel.org.uk/itoa/
char* itoa(size_t val, int base) {
static char buf[32] = { 0 };
if (base == 0 || base > 16) { return NULL; }
if (val == 0) {
buf[0] = '0';
return buf;
}
int i;
for (i = 30; val && i; i--, val /= base) {
buf[i] = "0123456789abcdef"[val % base];
}
return &buf[i + 1];
}
+17
View File
@@ -0,0 +1,17 @@
#include <string.h>
#include <stdint.h>
void* memcpy(void* src, void* dst, uint32_t length) {
for (uint32_t i = 0; i < length; i++) {
*((uint8_t*)dst + i) = *((uint8_t*)src + i);
}
return dst;
}
void* memcpy32(void* src, void* dst, uint32_t length) {
for (uint32_t i = 0; i * 4 < length; i++) {
*((uint32_t*)dst + i) = *((uint32_t*)src + i);
}
return dst;
}
+18
View File
@@ -0,0 +1,18 @@
#include <string.h>
#include <stdint.h>
#include <stddef.h>
void* memset(void* dst, uint8_t value, size_t length) {
for (size_t i = 0; i < length; i++) {
*(uint8_t*)(dst + i) = value;
}
return dst;
}
void* memset32(void* dst, uint32_t value, size_t length) {
for (size_t i = 0; i < length; i++) {
*(uint32_t*)(dst + i) = value;
}
return dst;
}
+15
View File
@@ -0,0 +1,15 @@
#include <string.h>
char* strcat(char* str1, const char* str2) {
char* s = str1;
while (*s != '\0') { s++; }
strcpy(s, str2);
return str1;
}
char* strncat(char* str1, const char* str2, size_t n) {
char* s = str1;
while (*s != '\0') { s++; }
strncpy(s, str2, n);
return str1;
}
+10
View File
@@ -0,0 +1,10 @@
#include <string.h>
char* strchr(const char* str, char c) {
const char* s = str;
while (*s != '\0' && *s != c) { s++; }
return (*s == c) ? (char*)s : NULL;
}
char* strrchr(const char* str, char c) { return NULL; } // TODO
+27
View File
@@ -0,0 +1,27 @@
#include <string.h>
int strcmp(const char* str1, const char* str2) {
while (*str1 != '\0' && *str1 == *str2) {
str1++;
str2++;
}
uint8_t c1 = (*(uint8_t*)str1);
uint8_t c2 = (*(uint8_t*)str2);
return ((c1 < c2) ? -1 : (c1 > c2));
}
int strncmp(const char* str1, const char* str2, size_t n) {
if (n == 0) { return 0; }
while (n-- > 0 && *str1 == *str2) {
if (n == 0 || *str1 == '\0') { return 0; }
str1++;
str2++;
}
uint8_t c1 = (*(uint8_t*)str1);
uint8_t c2 = (*(uint8_t*)str2);
return ((c1 < c2) ? -1 : (c1 > c2));
}
+22
View File
@@ -0,0 +1,22 @@
#include <string.h>
char* strcpy(char* str1, const char* str2) {
char* dst = str1;
const char* src = str2;
while ((*dst++ = *src++) != '\0');
return str1;
}
char* strncpy(char* str1, const char* str2, size_t n) {
char* dst = str1;
const char* src = str2;
while (n > 0) {
n--;
if ((*dst++ = *src++) == '\0') {
memset(dst, '\0', n);
break;
}
}
return str1;
}
+8
View File
@@ -0,0 +1,8 @@
#include <string.h>
#include <stddef.h>
size_t strlen(const char* str) {
const char* p = str;
while (*++p != '\0');
return (size_t)(p - str);
}