Subject: | Incorrect length calc in Pod::Text::Termcap::wrap |
Environment:
This is perl, v5.8.0 built for i386-linux-thread-multi
$Pod::Text::Termcap::VERSION is 1.10
Summary: when computing the length of the remaining text in Pod::Text::Termcap::wrap, the characters that make up terminal control sequences (e.g., $$self{BOLD}) are included in the total. These sequences are "invisible" for the purpose of word wrapping, so they should NOT be included in the calculated length.
The attached patch fixes the problem via a helper function named visible_length() -- which returns the length of its first arg less the length of all terminal control sequences.
I'm using v1.10, but I've verified that this problem exists in v1.11 as well.
*** /usr/lib/perl5/5.8.0/Pod/Text/Termcap.pm 2003-08-13 09:17:12.000000000 -0700
--- Termcap.pm 2004-06-21 14:39:20.000000000 -0700
***************
*** 97,123 ****
$self->output ($$self{BOLD} . $code . $$self{NORM});
}
! # Override the wrapping code to igore the special sequences.
sub wrap {
my $self = shift;
local $_ = shift;
my $output = '';
my $spaces = ' ' x $$self{MARGIN};
my $width = $$self{width} - $$self{MARGIN};
! my $code = "(?:\Q$$self{BOLD}\E|\Q$$self{UNDL}\E|\Q$$self{NORM}\E)";
! while (length > $width) {
! if (s/^((?:$code?[^\n]){0,$width})\s+//
! || s/^((?:$code?[^\n]){$width})//) {
! $output .= $spaces . $1 . "\n";
! } else {
! last;
! }
}
$output .= $spaces . $_;
$output =~ s/\s+$/\n\n/;
$output;
}
##############################################################################
# Module return value and documentation
--- 97,133 ----
$self->output ($$self{BOLD} . $code . $$self{NORM});
}
! # Override the wrapping code to ignore the special sequences.
sub wrap {
my $self = shift;
local $_ = shift;
my $output = '';
my $spaces = ' ' x $$self{MARGIN};
my $width = $$self{width} - $$self{MARGIN};
! my $invisible = "\Q$$self{BOLD}\E|\Q$$self{UNDL}\E|\Q$$self{NORM}\E";
! my $code = "(?:$invisible)";
! my $length = visible_length($_, $invisible);
! while ($length > $width) {
! if (s/^((?:$code?[^\n]){0,$width})\s+//
! || s/^((?:$code?[^\n]){$width})//) {
! $output .= $spaces . $1 . "\n";
! $length = visible_length($_, $invisible);
! } else {
! last;
! }
}
$output .= $spaces . $_;
$output =~ s/\s+$/\n\n/;
$output;
}
+ sub visible_length {
+ my ($text, $invisible) = @_;
+ my $sum = 0;
+ while ($text =~ /$invisible/g) { $sum += length($&); }
+ length $text - $sum;
+ }
+
##############################################################################
# Module return value and documentation