From: Junio C Hamano Date: Sat, 23 Feb 2008 22:04:05 GMT Subject: Re: [PATCH] gitweb: Better chopping in commit search results Message-ID: <7vd4qn1ga2.fsf@gitster.siamese.dyndns.org> In-Reply-To: <200802222014.13205.jnareb@gmail.com> Jakub Narebski writes: > # regexps: ending and beginning with word part up to $add_len > my $endre = qr/.{0,$len}[^ \/\-_:\.@]{0,$add_len}/; > my $begre = qr/[^ \/\-_:\.@]{0,$add_len}.{0,$len}/; I have no idea what these line noise characters inside [] are. Did you mean something like "\w"? I have a suspicion that it may be easier to read and could be even more efficient to split an overlong line at word boundaries and to remove elements from the end you are removing from until it fits. sub chop_whence { my ($line, $max, $slop, $where) = @_; my $len = length($line); if ($len < $max + $slop) { return $line; } # Cut at word boundaries my @split = split(/\b/, $line); my $filler = "..."; while ((2 < @split)) { my $removed; my $splice_at; if ($where eq 'left') { $removed = shift @split; } elsif ($where eq 'right') { $removed = pop @split; } else { my $splice_at = int($#split / 2); $removed = splice(@split, $splice_at, 1); } $len -= length($removed) + length($filler); if ($len < $max + $slop) { if ($where eq 'left') { unshift @split, $filler; } elsif ($where eq 'right') { push @split, $filler; } else { my $splice_at = int($#split / 2); splice(@split, $splice_at, 0, $filler); } return join('', @split); } } # give up return $line; }