ed: fix line insertion before current line. Closes 15081

When text is inserted by insertLine() the lines following the
insertion are moved down and the insertion point is made the new
current line.  To avoid too much scanning of the linked list of
lines setCurNum() may use the position of the old current line to
determine the location of the new current line.

If the insertion point is before the old current line in the file
the latter will have been moved down, so its line pointer needs to
be adjusted.

function                                             old     new   delta
insertLine                                           162     180     +18
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 18/0)               Total: 18 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Ron Yorston 2024-07-18 14:59:30 +01:00 committed by Denys Vlasenko
parent 480a07bd68
commit 26895db35d
2 changed files with 23 additions and 0 deletions

View File

@ -345,6 +345,8 @@ static int insertLine(int num, const char *data, int len)
lp->prev->next = newLp;
lp->prev = newLp;
if (num <= curNum)
curLine = curLine->prev;
lastNum++;
dirty = TRUE;
return setCurNum(num);

21
testsuite/ed.tests Executable file
View File

@ -0,0 +1,21 @@
#!/bin/sh
. ./testing.sh
# testing "test name" "command" "expected result" "file input" "stdin"
testing "ed insert text before current line" \
"ed input" "8\n1\ntext\n2\n3\n4\n13\n" "1\n2\n3\n4\n" '2i
text
.
,p
w
q
'
testing "ed read text before current line" \
"ed input" "8\n8\n1\n2\n1\n2\n3\n4\n3\n4\n16\n" "1\n2\n3\n4\n" '2r input
,p
w
q
'