Fixed realloc, and fixed right as well

master
Aadhavan Srinivasan 2 years ago
parent 0d001b550e
commit f19eb3becc

@ -26,19 +26,21 @@ Buffer* new_buffer(int size) {
} }
void buffer_grow(Buffer* buffer) { void buffer_grow(Buffer* buffer) {
int old_size = buffer->size;
int start_offset = buffer->start - buffer->text; int start_offset = buffer->start - buffer->text;
buffer->size += 10; buffer->size += 10;
buffer->text = realloc(buffer->text,buffer->size); buffer->text = realloc(buffer->text,buffer->size);
buffer->start = buffer->text + start_offset; buffer->start = buffer->text + start_offset;
buffer->gap_size = 10; buffer->gap_size = 10;
// for (int i=strlen(buffer->text)-1; i > (buffer->start - buffer->text); i--) { for (int i=0; i < (old_size - start_offset); i++) {
for (int i=0; i < (strlen(buffer->text) - start_offset); i++) {
*(buffer->start + i + buffer->gap_size) = *(buffer->start + i); *(buffer->start + i + buffer->gap_size) = *(buffer->start + i);
*(buffer->start + i) = 0; *(buffer->start + i) = 0;
} }
buffer->end = buffer->start + buffer->gap_size; buffer->end = buffer->start + buffer->gap_size;
} }
void buffer_insert(char ch, Buffer* buffer) { void buffer_insert(char ch, Buffer* buffer) {
@ -59,8 +61,10 @@ void buffer_delete(Buffer* buffer) {
void buffer_right(Buffer* buffer) { void buffer_right(Buffer* buffer) {
if (buffer->end != buffer->text + buffer->size) { if (buffer->end != buffer->text + buffer->size) {
char c = *(buffer->end);
buffer->start++; buffer->start++;
buffer->end++; buffer->end++;
*(buffer->start -1) = c;
} }
} }
@ -70,6 +74,7 @@ void buffer_left(Buffer* buffer) {
buffer->start--; buffer->start--;
buffer->end--; buffer->end--;
*(buffer->end) = c; *(buffer->end) = c;
*(buffer->start) = 0;
} }
} }

Loading…
Cancel
Save