tweaks: when undoing an addition or redoing a deletion, do not reallocate

Especially when undoing an addition, reallocating the line data is a
waste of time, because most likely other text will be added instead.

(This also removes a coding error in the redo code for a deletion:
it allocated too many bytes for the new line data: twice the amount
of the deletion too much.)
This commit is contained in:
Benno Schulenberg 2020-02-27 14:14:18 +01:00
parent 7cf08b93ff
commit 4f67f364bc

View File

@ -534,11 +534,8 @@ void do_undo(void)
undidmsg = _("addition"); undidmsg = _("addition");
if ((u->xflags & WAS_FINAL_LINE) && !ISSET(NO_NEWLINES)) if ((u->xflags & WAS_FINAL_LINE) && !ISSET(NO_NEWLINES))
remove_magicline(); remove_magicline();
data = charalloc(strlen(line->data) - strlen(u->strdata) + 1); memmove(line->data + u->head_x, line->data + u->head_x + strlen(u->strdata),
strncpy(data, line->data, u->head_x); strlen(line->data + u->head_x) - strlen(u->strdata) + 1);
strcpy(&data[u->head_x], &line->data[u->head_x + strlen(u->strdata)]);
free(line->data);
line->data = data;
goto_line_posx(u->head_lineno, u->head_x); goto_line_posx(u->head_lineno, u->head_x);
break; break;
case ENTER: case ENTER:
@ -723,11 +720,8 @@ void do_redo(void)
case BACK: case BACK:
case DEL: case DEL:
redidmsg = _("deletion"); redidmsg = _("deletion");
data = charalloc(strlen(line->data) + strlen(u->strdata) + 1); memmove(line->data + u->head_x, line->data + u->head_x + strlen(u->strdata),
strncpy(data, line->data, u->head_x); strlen(line->data + u->head_x) - strlen(u->strdata) + 1);
strcpy(&data[u->head_x], &line->data[u->head_x + strlen(u->strdata)]);
free(line->data);
line->data = data;
goto_line_posx(u->head_lineno, u->head_x); goto_line_posx(u->head_lineno, u->head_x);
break; break;
case JOIN: case JOIN: