Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ed: generic search function #808

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 10 additions & 50 deletions bin/ed
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,6 @@ sub edPrintLineNum {
edWarn(E_ARGEXT);
return;
}

my $adr = $adrs[1];
if (!defined($adr)) {
$adr = $adrs[0];
Expand All @@ -821,9 +820,6 @@ sub edPrintLineNum {
$adr = maxline();
}
print "$adr\n";

# v7 docs say this does not affect current line. GNU ed sets the line.
# We go with the v7 docs.
}

#
Expand Down Expand Up @@ -982,11 +978,7 @@ sub getAddr {
return A_NOPAT unless defined $SearchPat;
$re = $SearchPat;
}
if ($delim eq '/') {
$n = edSearchForward($re);
} else {
$n = edSearchBackward($re);
}
$n = edSearch($re, $delim eq '?');
$n = A_NOMATCH unless $n;
}
return $n;
Expand All @@ -1009,50 +1001,18 @@ sub getRe {
return $re;
}

#
# Search forward for a pattern...wrap if not found
#
# Inputs:
# pattern - via argument
# CurrentLineNum - global
# lines - global
#
# Return:
# 0 - not found
# >0 - line where first found
#

sub edSearchForward {
my($pattern) = @_;
sub edSearch {
my ($pattern, $backward) = @_;

$SearchPat = $pattern;
for my $line (($CurrentLineNum + 1) .. maxline(), 1 .. $CurrentLineNum) {
if ($lines[$line] =~ /$pattern/) {
return $line;
}
my $cur = $CurrentLineNum;
my @idx;
if ($backward) {
@idx = reverse ($cur .. maxline(), 1 .. ($cur - 1));
} else {
@idx = (($cur + 1) .. maxline(), 1 .. $cur);
}
return 0;
}

#
# Search backward for a pattern...wrap if not found
#
# Inputs:
# pattern - via argument
# CurrentLineNum - global
# lines - global
#
# Return:
# 0 - not found
# >0 - line where first found
#

sub edSearchBackward {
my($pattern) = @_;

$SearchPat = $pattern;
my @idx = ($CurrentLineNum .. maxline(), 1 .. ($CurrentLineNum - 1));
foreach my $line (reverse @idx) {
foreach my $line (@idx) {
if ($lines[$line] =~ /$pattern/) {
return $line;
}
Expand Down
Loading