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
BIN
View File
Binary file not shown.
+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;
// }
+14 -14
View File
@@ -5,32 +5,32 @@ typedef struct {
int size;
int capacity;
char* data;
} String;
} String_t;
String str_with_capacity(int capacity);
String_t str_with_capacity(int capacity);
String str_from_chars(char* string);
String_t str_from_chars(char* string);
String str_from_slice(char* string, int len);
String_t str_from_slice(char* string, int len);
String str_new();
String_t str_new();
String* str_lines(String* self, int* numlines);
String_t* str_lines(String_t* self, int* numlines);
// String* str_split(String* self, int* res_len, char c);
// String_t* str_split(String_t* self, int* res_len, char c);
void str_push(String* s, char c);
void str_push(String_t* s, char c);
char str_pop(String* s);
char str_pop(String_t* s);
void str_insert(String* s, int index, char c);
void str_insert(String_t* s, int index, char c);
void str_remove(String* s, int index);
void str_remove(String_t* s, int index);
int str_dealloc(String* s);
int str_dealloc(String_t* s);
char* to_chars(String* s);
char* to_chars(String_t* s);
int str_len(String* s);
int str_len(String_t* s);
#endif
+19 -6
View File
@@ -9,7 +9,7 @@ typedef struct {
int screen_line;
int screen_col;
bool editmode;
String* buffer;
String_t* buffer;
} Editor;
Editor new_editor() {
@@ -18,7 +18,13 @@ Editor new_editor() {
e.screen_line = 0;
e.screen_col = 0;
e.editmode = false;
e.buffer = NULL;
String_t lines = str_from_chars("");
e.buffer = str_lines(&lines, &e.lines);
str_dealloc(&lines);
addstr("");
return e;
}
@@ -29,12 +35,18 @@ Editor editor_from(char* input_string) {
e.screen_line = 0;
e.screen_col = 0;
e.editmode = false;
e.buffer = NULL;
String_t lines = str_from_chars(input_string);
e.buffer = str_lines(&lines, &e.lines);
str_dealloc(&lines);
addstr(input_string);
return e;
}
void move_cursor(Editor* self, int x, int y) {
if (x != 0
if (y != 0
&& self->screen_line + y >= 0
&& self->screen_line + y <= self->lines)
{
@@ -45,6 +57,7 @@ void move_cursor(Editor* self, int x, int y) {
}
} else if (self->screen_col + x < 0) {
if (self->screen_line - 1 >= 0) {
self->screen_line -= 1;
self->screen_col = str_len(&self->buffer[self->screen_line]);
}
} else if (self->screen_col + x > str_len(&self->buffer[self->screen_line])) {
@@ -70,13 +83,13 @@ void addchar(Editor* self, char c) {
if (self->screen_line == self->lines) {
// reallocate self->buffer to be 1 larger
self->lines++;
self->buffer = realloc(self->buffer, sizeof(String) * (self->lines + 1));
self->buffer = realloc(self->buffer, sizeof(String_t) * (self->lines + 1));
}
str_insert(&self->buffer[self->screen_line], self->screen_col, c);
move_cursor(self, 1, 0);
}
String* to_string(Editor* self) {
String_t* to_string(Editor* self) {
}
+2 -2
View File
@@ -4,7 +4,7 @@ typedef struct {
int screen_line;
int screen_col;
bool editmode;
String* buffer;
String_t* buffer;
} Editor;
Editor new_editor();
@@ -15,4 +15,4 @@ void move_cursor(Editor* self, int x, int y);
void delchar(Editor* self);
void addchar(Editor* self, char c);
String* to_string(Editor* self);
String_t* to_string(Editor* self);
+19 -2
View File
@@ -24,7 +24,7 @@ int open_editor() {
move(0, 0);
Editor editor = new_editor();
Editor editor = editor_from("this is some text to edit\nthis is the second line\nand this is the third lol");
while (true) {
refresh();
@@ -35,10 +35,26 @@ int open_editor() {
editor.editmode = false;
break;
case KEY_BACKSPACE:
delchar(&editor);
move_cursor(&editor, -1, 0);
break;
case KEY_DC:
delchar(&editor);
break;
case KEY_ENTER:
editor.editmode = false;
//TODO: next line
break;
case KEY_UP:
move_cursor(&editor, 0, -1);
break;
case KEY_DOWN:
move_cursor(&editor, 0, 1);
break;
case KEY_LEFT:
move_cursor(&editor, -1, 0);
break;
case KEY_RIGHT:
move_cursor(&editor, 1, 0);
break;
default:
addchar(&editor, c);
@@ -47,6 +63,7 @@ int open_editor() {
} else {
switch (c) {
case 'q':
endwin();
return 0;
case 'i':
editor.editmode = true;
+4
View File
@@ -0,0 +1,4 @@