From 6df8efd433b22c4bcb1ae1e7afc24fad389538c1 Mon Sep 17 00:00:00 2001 From: Michael Mikonos <127171689+mknos@users.noreply.github.com> Date: Wed, 24 Jan 2024 19:25:46 +0800 Subject: [PATCH 1/2] ed: simplify backwards search * In edSearchBackwards(), init $line index directly instead of copying value from $start * I found this by accident when reading the code * Test jumps to line 60, finds "perl" patten backwards on line 9 then again on 7 %perl ed -p '>>> ' awk 4595 >>> 60 open(TMPOUT, '+>', ($tmpout = "/tmp/a2pout.$$")) || >>> ?perl License: perl >>> n 9 License: perl >>> ?perl Author: Tom Christiansen, tchrist@perl.com >>> n 7 Author: Tom Christiansen, tchrist@perl.com >>> q --- bin/ed | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/bin/ed b/bin/ed index 91c7a01c..482e129b 100755 --- a/bin/ed +++ b/bin/ed @@ -1111,13 +1111,8 @@ sub edSearchForward { sub edSearchBackward { my($pattern) = @_; - my($line,$start); - # brute force search... - - $start = $CurrentLineNum > 1 ? $CurrentLineNum - 1 : maxline(); - - $line = $start; + my $line = $CurrentLineNum > 1 ? $CurrentLineNum - 1 : maxline(); while ($line > 0) { if ($lines[$line] =~ /$pattern/) { From 56cd01904d0a0cf4e19f36603029db9178e56524 Mon Sep 17 00:00:00 2001 From: Michael Mikonos <127171689+mknos@users.noreply.github.com> Date: Wed, 24 Jan 2024 21:46:37 +0800 Subject: [PATCH 2/2] merge loops --- bin/ed | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/bin/ed b/bin/ed index 482e129b..392b852b 100755 --- a/bin/ed +++ b/bin/ed @@ -1112,23 +1112,12 @@ sub edSearchForward { sub edSearchBackward { my($pattern) = @_; - my $line = $CurrentLineNum > 1 ? $CurrentLineNum - 1 : maxline(); - - while ($line > 0) { + my @idx = ($CurrentLineNum .. maxline(), 1 .. ($CurrentLineNum - 1)); + foreach my $line (reverse @idx) { if ($lines[$line] =~ /$pattern/) { return $line; } - $line--; } - - $line = maxline(); - while ($line >= $CurrentLineNum) { - if ($lines[$line] =~ /$pattern/) { - return $line; - } - $line--; - } - return 0; }