I scan the entire thread. I figured it's done seldom enough that speed isn't crucial. It's not THAT slow, either, at least not with my data storage setup (keeping posts on separate single lines of a HTML file).
Here's the relevant code (@page contains the lines of the current thread page, $num is the argument to q, @posts is filled with a list of which posts to display, $replyrange_re is a regexp to match the >> syntax, in_range() checks if a certain post number is contained in a >> reference.
push @posts,$num;
OUTER: foreach my $post (1..$total)
{
next if($post eq $num);
while($page[$post+1]=~/>>($replyrange_re)/g)
{
if(in_range($num,$1)) { push @posts,$post; next OUTER; }
}
}
Here's in_range():
sub in_range($$)
{
my ($num,$ranges)=@_;
foreach my $range (split /(,|,)/,$ranges)
{
if($range=~/^([0-9]*)-([0-9]*)$/)
{
my $start=($1 or 1);
my $end=($2 or 1000000); # arbitary large number
($start,$end)=($end,$start) if($start>$end);
return 1 if($num>=$start and $num<=$end);
}
elsif($range=~/^([0-9]+)$/)
{
return 1 if($num==$1);
}
#elsif($range=~/^l([0-9]+)$/i) {} # l ranges never match
#elsif($range=~/^r([0-9]*)$/i) {} # r ranges never match
#elsif($range=~/^q([0-9]+)$/i) {} # q ranges never match
}
return 0;
}