diff --git a/editor.c b/editor.c index d64bf0f..267cda5 100644 --- a/editor.c +++ b/editor.c @@ -38,7 +38,7 @@ void buffer_grow(Buffer* buffer) { buffer->start = buffer->text + start_offset; buffer->gap_size = 10; - for (int i=0; i < (old_size - start_offset); i++) { + for (int i= (old_size - start_offset) - 1; i >= 0; i--) { *(buffer->start + i + buffer->gap_size) = *(buffer->start + i); *(buffer->start + i) = 0; } @@ -133,13 +133,16 @@ int main(int argc, char** argv) { gap starts at the first index of the string, and continues till the end */ while (i < buffer->size) { - addch(*(buffer->text + i)); - i++; if ((buffer->start - buffer->text) == i) { /* If we have encountered the start of the gap */ getyx(stdscr,y,x); i += buffer->gap_size; } + if (i >= buffer->size) { + break; + } + addch(*(buffer->text + i)); + i++; }