fixed some bugs in c versiosn

This commit is contained in:
FantasyPvP
2024-11-05 09:32:36 +00:00
parent 83a71ae0a1
commit b40ecc5bb1
9 changed files with 136 additions and 76 deletions
+38 -50
View File
@@ -5,15 +5,15 @@
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
typedef struct {
int size;
int capacity;
uint32_t size, capacity;
char* data;
} String;
} String_t;
String str_with_capacity(int capacity) {
String s;
String_t str_with_capacity(int capacity) {
String_t s;
/// allocate memory for 'capacity' chars
s.data = (char*)calloc(capacity, sizeof(char));
@@ -24,8 +24,8 @@ String str_with_capacity(int capacity) {
return s;
}
String str_from_chars(char* string) {
String s;
String_t str_from_chars(const char* string) {
String_t s;
s.data = (char*)calloc(strlen(string), sizeof(char));
strcpy(s.data, string);
@@ -34,8 +34,8 @@ String str_from_chars(char* string) {
return s;
}
String str_from_slice(char* string, int len) {
String s;
String_t str_from_slice(const char* string, int len) {
String_t s;
s.data = (char*)calloc(len, sizeof(char));
strncpy(s.data, string, len);
@@ -44,22 +44,21 @@ String str_from_slice(char* string, int len) {
return s;
}
String str_new() {
String_t str_new() {
return str_with_capacity(1);
}
int str_dealloc(String* self) {
int str_dealloc(String_t* self) {
free(self->data);
return 0;
}
void str_push(String* self, char c) {
int str_push(String_t* self, char c) {
// check size < capacity
if (self->size < self->capacity) {
self->data[self->size] = c;
self->size++;
return;
return 0;
}
// reallocate to add capacity for an extra char
@@ -68,10 +67,10 @@ void str_push(String* self, char c) {
self->data[self->size] = c;
self->size++;
self->capacity = newcap;
return;
return 0;
}
char str_pop(String* self) {
char str_pop(String_t* self) {
if (self->size == 0) {
return '\0';
}
@@ -80,31 +79,33 @@ char str_pop(String* self) {
return c;
}
void str_insert(String* self, int index, char c) {
int str_insert(String_t* self, int index, char c) {
if (index > self->size) {
return;
return -1;
}
self->size++;
for (int i = self->size - 1; i > index; i--) {
self->data[i] = self->data[i - 1];
}
self->data[index] = c;
return 0;
}
void str_remove(String* self, int index) {
int str_remove(String_t* self, int index) {
if (index >= self->size) {
return;
return -1;
}
self->size--;
for (int i = index; i < self->size; i++) {
self->data[i] = self->data[i + 1];
}
return 0;
}
String* str_lines(String* self, int* numlines) {
String_t* str_lines(String_t* self, int* numlines) {
char* string = self->data;
String* lines = NULL;
String_t* lines = NULL;
// find the number of lines in the file
*numlines = 0;
@@ -121,7 +122,7 @@ String* str_lines(String* self, int* numlines) {
}
// allocate memory for an array of pointers to each line
lines = (String*)malloc((*numlines + 1) * sizeof(String));
lines = (String_t*)malloc((*numlines + 1) * sizeof(String_t));
int i = 0;
char* start = string;
@@ -139,15 +140,15 @@ String* str_lines(String* self, int* numlines) {
end++;
}
}
// returns an array of String
// returns an array of String_t
return lines;
}
/// splits a string into an array of strings based on a delimiter
String* str_split(String* self, int* res_len, char c) {
String_t* str_split(String_t* self, int* res_len, char c) {
char* string = self->data;
String* elements = NULL;
String_t* elements = NULL;
// find the number of lines in the file
@@ -168,7 +169,7 @@ String* str_split(String* self, int* res_len, char c) {
}
// allocate memory for an array of pointers to each line
elements = (String*)malloc((*res_len) * sizeof(String));
elements = (String_t*)malloc((*res_len) * sizeof(String_t));
int i = 0;
flag = false;
@@ -193,40 +194,27 @@ String* str_split(String* self, int* res_len, char c) {
}
// returns an array of String
// returns an array of String_t
return elements;
}
int str_len(String* s) {
int str_len(String_t* s) {
return s->size;
}
char* to_chars(String* s) {
char* to_chars(String_t* s) {
return s->data;
}
// int main() {
// String s = str_from_chars("hello\nworld\neeeee\notherline\n\0");
// String_t s = str_from_chars("hello\nworld\neeeee\notherline\n\0");
// str_remove(&s, 10);
// str_insert(&s, 10, 'h');
// printf("%s\n", to_chars(&s));
// int numlines = 0;
// String* lns = str_lines(&s, &numlines);
// str_dealloc(&s);
// for (int i = 0; i < numlines; i++) {
// printf("%s\n", lns[i].data);
// str_dealloc(&lns[i]);
// }
// String s2 = str_from_chars("$this$needs$to$be$split");
// int elements = 0;
// String* split = str_split(&s2, &elements, '$');
// str_dealloc(&s2);
// printf("%d elements\n", elements);
// for (int i = 0; i < elements; i++) {
// printf("%s\n", split[i].data);
// str_dealloc(&split[i]);
// printf("(dealloced)\n");
// }
// return 0;
// }