Merge remote-tracking branch 'refs/remotes/origin/main'

This commit is contained in:
FantasyPvP
2024-11-08 10:42:52 +00:00
4 changed files with 278 additions and 29 deletions
+33
View File
@@ -44,10 +44,13 @@ String_t str_from_slice(const char* string, int len) {
return s;
}
String_t str_new() {
return str_with_capacity(1);
}
int str_dealloc(String_t* self) {
free(self->data);
return 0;
@@ -70,6 +73,36 @@ int str_push(String_t* self, char c) {
return 0;
}
int str_push_str(String_t* self, char* c) {
if (self->size + strlen(c) >= self->capacity) {
self->capacity = ( self->size + strlen(c) ) * sizeof(char);
self->data = (char*)realloc(self->data, self->capacity);
for (int i = 0; i < strlen(c); i++) {
self->data[self->size + i] = c[i];
}
self->size = self->capacity;
}
}
String_t str_from_file(FILE* file) {
if (file == NULL) {
return str_new();
}
char buffer[64];
String_t string = str_new();
while (fgets(buffer, sizeof(buffer), file) != NULL) {
str_push_str(&string, buffer);
}
fclose(file);
return string;
}
char str_pop(String_t* self) {
if (self->size == 0) {
return '\0';