Skip to content

Commit 4bb643d

Browse files
authored
ed: save space in editor buffer
* The substitution s/\z/NEW/ produced the unwanted outcome of $lines[$x] containing two lines of text * Address this by not including trailing newlines in the editor buffer anymore * Introduce helper function get_terminated_line() which adds the newline * Now s/\z/NEW/ is interpreted the same as s/$/NEW/, i.e. append NEW to end of line * Bump version number in case of regressions * Removing the newlines from the editor buffer also reduces memory usage, but that is not the main intent of this patch
1 parent 85b3126 commit 4bb643d

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

bin/ed

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ my $UndoLine;
100100
my $PRINT_NUM = 1;
101101
my $PRINT_BIN = 2;
102102

103-
our $VERSION = '0.28';
103+
our $VERSION = '0.29';
104104

105105
my @ESC = (
106106
'\\000', '\\001', '\\002', '\\003', '\\004', '\\005', '\\006', '\\a',
@@ -380,7 +380,7 @@ sub edPrint {
380380
if ($do_bin) {
381381
print escape_line($i);
382382
} else {
383-
print $lines[$i];
383+
print get_terminated_line($i);
384384
}
385385
}
386386
$CurrentLineNum = $adrs[-1];
@@ -390,12 +390,17 @@ sub edPrint {
390390
sub escape_line {
391391
my $idx = shift;
392392

393-
my @chars = unpack 'C*', $lines[$idx];
393+
my @chars = unpack 'C*', get_terminated_line($idx);
394394
die 'internal error: unpack' unless @chars;
395395
my @s = map { $ESC[$_] } @chars;
396396
return join '', @s;
397397
}
398398

399+
sub get_terminated_line {
400+
my $i = shift;
401+
return $lines[$i] . "\n";
402+
}
403+
399404
# does not modify buffer
400405
sub edPipe {
401406
return E_ADDREXT if defined $adrs[0];
@@ -433,15 +438,12 @@ sub edJoin {
433438
if ($adrs[0] == $adrs[1]) { # nop
434439
return;
435440
}
436-
437-
my $buf = $lines[$adrs[0]];
438-
my $start = $adrs[0] + 1;
439-
for my $i ($start .. $adrs[1]) {
440-
chomp $buf;
441-
$buf .= $lines[$i];
442-
}
443-
$lines[$adrs[0]] = $buf;
444-
splice @lines, $start, $adrs[1] - $adrs[0];
441+
my $i = $adrs[0];
442+
my $j = $adrs[1];
443+
my $delcount = $j - $i;
444+
my @tmp = splice @lines, $i + 1, $delcount;
445+
my $buf = join '', @tmp;
446+
$lines[$i] .= $buf;
445447
$NeedToSave = 1;
446448
$UserHasBeenWarned = 0;
447449
$CurrentLineNum = $adrs[0];
@@ -669,7 +671,8 @@ sub edWrite {
669671
}
670672
}
671673
$chars = 0;
672-
for my $line (@lines[$adrs[0]..$adrs[1]]) {
674+
for my $i ($adrs[0] .. $adrs[1]) {
675+
my $line = get_terminated_line($i);
673676
print {$fh} $line;
674677
$chars += length($line);
675678
}
@@ -814,9 +817,9 @@ sub readin_lines {
814817
push @tmp, $_;
815818
}
816819
if (@tmp && substr($tmp[-1], -1, 1) ne "\n") {
817-
$tmp[-1] .= "\n";
818820
print "Newline appended\n";
819821
}
822+
chomp @tmp;
820823
return @tmp;
821824
}
822825

@@ -885,8 +888,8 @@ sub edPrintLineNum {
885888

886889
sub write_undo {
887890
my $fh = File::Temp->new;
888-
foreach (@lines) {
889-
print {$fh} $_;
891+
for my $i (1 .. maxline()) {
892+
print {$fh} get_terminated_line($i);
890893
}
891894
seek $fh, 0, 0;
892895
return $fh;
@@ -899,6 +902,7 @@ sub edUndo {
899902

900903
$CurrentLineNum = $UndoLine;
901904
@lines = <$UndoFile>;
905+
chomp @lines;
902906
unshift @lines, undef;
903907
$UserHasBeenWarned = 0;
904908
$NeedToSave = 1; # new tmpfile
@@ -950,7 +954,7 @@ sub edSetCurrentLine {
950954
$CurrentLineNum++;
951955
}
952956

953-
print $lines[$CurrentLineNum];
957+
print get_terminated_line($CurrentLineNum);
954958
return;
955959
}
956960

0 commit comments

Comments
 (0)