Some string functions (untested)

This commit is contained in:
bumbread 2022-06-06 12:30:01 +11:00
parent 67e15fbb4e
commit 8c6bda878a
8 changed files with 197 additions and 28 deletions

View File

@ -1,8 +1,17 @@
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
// TODO: rewrite memmove to not allocate anything
typedef unsigned char byte;
void *memcpy(void *restrict s1, const void *restrict s2, size_t n) {
const unsigned char *restrict c2 = s2;
unsigned char *restrict c1 = s1;
const byte *restrict c2 = s2;
byte *restrict c1 = s1;
while (n--) {
*c1++ = *c2++;
@ -10,35 +19,173 @@ void *memcpy(void *restrict s1, const void *restrict s2, size_t n) {
return s1;
}
int memcmp(const void *s1, const void *s2, size_t n) {
const unsigned char *u1 = s1;
const unsigned char *u2 = s2;
for (; n--; u1++, u2++) {
if (*u1 != *u2) return *u1 - *u2;
}
return 0;
void *memmove(void *s1, const void *s2, size_t n)
{
byte *u1 = s1;
byte const *u2 = s2;
void *buffer = malloc(n);
strcpy(buffer, s2);
strcpy(s1, buffer);
free(buffer);
return s1;
}
void *memset(void *s, int c, size_t n) {
unsigned char *restrict buf = s;
byte *restrict buf = s;
while (n--) {
*buf++ = c;
}
return s;
}
int strcmp(const char *s1, const char *s2) {
while (*s1 != 0 && *s2 != 0) {
if (*s1 != *s2) break;
char *strcpy(char *restrict s1, const char *restrict s2)
{
while(*s2 != 0) {
*s1++ = *s2++;
}
*s1 = 0;
return s1;
}
if(*s1 > *s2) return 1;
if(*s1 < *s2) return -1;
char *strncpy(char *restrict s1, const char *restrict s2, size_t n)
{
while(n-->0 && *s2 != 0) {
*s1++ = *s2++;
}
while(n-->0) {
*s1++ = 0;
}
return s1;
}
char *strncat(char *restrict s1, const char *restrict s2, size_t n)
{
int start = strlen(s1);
for(int i = 0; i != n && *s2 != 0; ++i) {
s1[start+i] = s2[i];
}
s1[start+n] = 0;
return s1;
}
int memcmp(const void *s1, const void *s2, size_t n) {
const byte *u1 = s1;
const byte *u2 = s2;
for (; n--; u1++, u2++) {
if (*u1 != *u2) return *u1 - *u2;
}
return 0;
}
int strcmp(const char *s1, const char *s2) {
int diff;
do {
diff = *s1 - *s2;
} while(diff != 0 && *s1 != 0 && *s2 != 0);
return diff;
}
int strncmp(const char *s1, const char *s2, size_t n)
{
int diff;
int i = 0;
if(n != 0) do {
diff = *s1 - *s2;
} while(++i < n && diff != 0 && *s1 != 0 && *s2 != 0);
return diff;
}
// fuck locales
int strcoll(const char *s1, const char *s2)
{
return strcmp(s1, s2);
}
// fuck bad implementation of strcoll
size_t strxfrm(char *restrict s1, const char *restrict s2, size_t n)
{
size_t len = strlen(s2);
if(s1 != NULL && n != 0) {
for(int i = 0; i != n; ++i) {
*s1 = *s2;
}
}
return len;
}
void *memchr(const void *ptr, int c, size_t n)
{
const byte *s = ptr;
int i = 0;
for(; i != n && *s != c; ++i, ++s) {
++s;
}
if(i == n) return NULL;
return (void *)s; // fuck clang
}
char *strchr(const char *s, int c)
{
while(*s && *s != c) ++s;
if(*s != c) return NULL;
return (void *)s; // fuck clang
}
size_t strcspn(const char *s1, const char *s2)
{
size_t i = 0;
while(*s1) {
if(strchr(s2, *s1) != NULL) {
break;
}
++s1;
++i;
}
return i;
}
char *strpbrk(const char *s1, const char *s2)
{
while(*s1) {
if(strchr(s2, *s1) == NULL) {
break;
}
++s1;
}
return (char *)s1;
}
char *strstr(const char *s1, const char *s2)
{
if(!*s2) return (char *)s1;
size_t len = strlen(s2);
while(*s1 != 0) {
bool match = true;
for(size_t i = 0; i != len; ++i) {
if(!*s1 || s1[i] != s2[i]) {
match = false;
break;
}
}
++s1;
if(match) return (char *)s1;
}
return NULL;
}
char *strerror(int errnum)
{
switch(errnum) {
case 0: return "No errors";
case EDOM: return "Value is out of domain of the function";
case EILSEQ: return "Illegal byte sequence";
case ERANGE: return "Value is out of range";
}
return "Unkown error";
}
size_t strlen(const char *s) {
size_t i = 0;
while (s[i]) {
@ -47,7 +194,8 @@ size_t strlen(const char *s) {
return i;
}
// __STDC_WANT_LIB_EXT1__
#if __STDC_WANT_LIB_EXT1__ == 1
size_t strnlen_s(const char *s, size_t maxsize) {
if (s == NULL) return 0;
@ -57,3 +205,5 @@ size_t strnlen_s(const char *s, size_t maxsize) {
}
return i;
}
#endif

View File

@ -1,9 +1,15 @@
#include "_platform.h"
#pragma once
#define _COL_IGNORE_SPACE 0x1
#define _COL_IGNORE_SYMBOL 0x2
struct lconv {
// LC_NUMERIC
char *decimal_point; // "."
char *thousands_sep; // ""
char *grouping; // ""
// LC_MONETARY
char *mon_decimal_point; // ""
char *mon_thousands_sep; // ""
char *mon_grouping; // ""

View File

@ -3,9 +3,9 @@
typedef int sig_atomic_t;
// TODO: implement this
#define SIG_DFL (-1)
#define SIG_ERR (-1)
#define SIG_IGN (-1)
#define SIG_DFL 0
#define SIG_ERR 1
#define SIG_IGN 2
// not sure why but windows picked these, we can change it later
#define SIGINT 2

View File

@ -1,6 +1,11 @@
#include "_platform.h"
int _wcsicmp(const wchar_t *string1, const wchar_t *string2);
#pragma once
#if !defined(NULL)
#define NULL ((void *)0)
#endif
// int _wcsicmp(const wchar_t *string1, const wchar_t *string2);
void *memcpy(void * restrict s1, const void * restrict s2, size_t n);
void *memmove(void *s1, const void *s2, size_t n);
@ -25,7 +30,7 @@ void *memset(void *s, int c, size_t n);
char *strerror(int errnum);
size_t strlen(const char *s);
#if __STDC_WANT_LIB_EXT1__
#if __STDC_WANT_LIB_EXT1__ == 1
errno_t memcpy_s(void * restrict s1, rsize_t s1max, const void * restrict s2, rsize_t n);
errno_t memmove_s(void *s1, rsize_t s1max, const void *s2, rsize_t n);
errno_t strcpy_s(char * restrict s1, rsize_t s1max, const char * restrict s2);

View File

@ -1,4 +1,5 @@
#include "_platform.h"
#include <time.h>
#define thread_local _Thread_local
#define ONCE_FLAG_INIT 1

View File

@ -1,4 +1,9 @@
#include "_platform.h"
#pragma once
#if !defined(NULL)
#define NULL ((void *)0)
#endif
// The number of clock ticks per second
#define CLOCKS_PER_SEC ((clock_t)1000)

2
platform/win32/env.c Normal file
View File

@ -0,0 +1,2 @@
#include <stdlib.h>