creating new lines works now, fixed several segfaults and bugs

This commit is contained in:
FantasyPvP
2024-11-07 14:50:17 +00:00
parent 214dfc66a5
commit bef5547d50
6 changed files with 54 additions and 86 deletions
+5 -43
View File
@@ -102,48 +102,6 @@ int str_remove(String_t* self, int index) {
return 0;
}
String_t* str_lines(String_t* self, int* numlines) {
char* string = self->data;
String_t* lines = NULL;
// find the number of lines in the file
*numlines = 0;
for (int i = 0; i < strlen(string); i++) {
if (string[i] == '\n') {
(*numlines)++;
}
}
// add one if the last char is not a newline
// in this case there is one more line than newline symbols
if (self->data[strlen(self->data) - 1] != '\n') {
(*numlines)++;
}
// allocate memory for an array of pointers to each line
lines = (String_t*)malloc((*numlines + 1) * sizeof(String_t));
int i = 0;
char* start = string;
char* end = string;
while (*end != '\0') {
if (*end == '\n') {
lines[i] = str_from_slice(start, end - start);
end++;
start = end;
i++;
} else {
end++;
}
}
// returns an array of String_t
return lines;
}
/// splits a string into an array of strings based on a delimiter
String_t* str_split(String_t* self, int* res_len, char c) {
@@ -198,6 +156,10 @@ String_t* str_split(String_t* self, int* res_len, char c) {
return elements;
}
String_t* str_lines(String_t* self, int* numlines) {
return str_split(self, numlines, '\n');
}
int str_len(String_t* s) {
return s->size;
}
@@ -207,7 +169,7 @@ char* to_chars(String_t* s) {
}
// int main() {
// String_t s = str_from_chars("hello\nworld\neeeee\notherline\n\0");
// String_t s = str_from_chars("hello\nworld\neeeee\notherline\n");
// str_remove(&s, 10);
// str_insert(&s, 10, 'h');