Added a bunch of stuff

This commit is contained in:
Gitea 2020-12-01 17:40:03 -06:00
parent 66350160b0
commit 77c2abd247
66 changed files with 1808 additions and 0 deletions

36
string/strcat.c Normal file
View file

@ -0,0 +1,36 @@
#include <string.h>
/*
Is this...okay? Like, from what I can tell, it should be,
since s1 and s2 are restricted. s1 and s2 shouldn't be the
same object. But, like, I'm worried about what happens if
someone passes in the same pointer for s1 and s2. You'd get
an infinite loop of adding stuff that just eats up all
memory from there! But, like, I think musl does the same
thing? I really can't tell. For now, I'm gonna assume I've
done this acceptably.
For now.
-Kat
*/
/*
I'm pretty sure musl does the same thing, for the most part.
-Kat
*/
/*
Does weird stuff. Maybe don't use until we figure out what
the fuck is wrong with it.
-Kat
*/
char * strcat(char * restrict s1, const char * restrict s2) {
int i = 0, j;
for(i = 0; s1[i] != '\0'; i++){}
for(j = 0; s2[j] != '\0'; i++, j++) {
s1[i] = s2[j];
}
s1[i] = '\0';
return s1;
}

12
string/strcmp.c Normal file
View file

@ -0,0 +1,12 @@
#include <string.h>
int strcmp(const char * s1, const char * s2) {
size_t i = 0;
while(s1[i] != '\0' && s2[i] != '\0') {
if(s1[i] != s2[i]) {
return s1[i] - s2[i];
}
i++;
}
return s1[i] - s2[i];
}

22
string/strcspn.c Normal file
View file

@ -0,0 +1,22 @@
#include <string.h>
/*
This might need to be rewritten. It does grow as
O(m*n), where m and n are the number of characters
in s2 and s1. If I do, musl's implementation is
kinda cool. I could do something like that. But,
I think it'll do for now.
-Kat
*/
size_t strcspn(const char * s1, const char * s2) {
int i = 0;
for(i = 0; s1[i] != '\0'; i++) {
for(int j = 0; s2[j] != '\0'; j++) {
if(s1[i] == s2[j]) {
return i;
}
}
}
return i;
}

30
string/strtok.c Normal file
View file

@ -0,0 +1,30 @@
#include <string.h>
char * strtok(char * restrict str, char * restrict sep) {
static char * state;
char * search_str;
int i = 0, sep_size = strlen(sep);
if(str != NULL) {
search_str = str;
}
else {
search_str = state;
}
while(*search_str != '\0' && strcmp(search_str, sep) == 0) {
search_str += sep_size;
}
if(*search_str == '\0') {
return NULL;
}
while(search_str[i] != '\0' && strcmp(&(search_str[i]), sep) != 0) {
i++;
}
search_str[i] = '\0';
state = &(search_str[i + 1]);
return search_str;
}