Permalink
Browse files

Reverse paragraphs before fixing up trailing whitespace

This avoids the regex engine having to scan the entire string.
  • Loading branch information...
1 parent b4b7cba commit b3e64af8bd2b8f1fb047138cd7d49dfe9d37027d @ilmari committed Jan 26, 2013
Showing with 6 additions and 2 deletions.
  1. +6 −2 lib/Pod/Man.pm
View
@@ -970,7 +970,9 @@ sub cmd_para {
# Force exactly one newline at the end and strip unwanted trailing
# whitespace at the end, but leave "\ " backslashed space from an S< >
# at the end of a line.
- $text =~ s/((?:\\ )*)\s*$/$1\n/;
+ # Reverse the text first, to avoid having to scan the entire paragraph.
+ ($text = reverse $text) =~ s/\A\s*?(?= \\|\S|\z)/\n/;
+ $text = reverse $text;
# Output the paragraph.
$self->output ($self->protect ($self->textmapfonts ($text)));
@@ -990,7 +992,9 @@ sub cmd_verbatim {
# Force exactly one newline at the end and strip unwanted trailing
# whitespace at the end.
- $text =~ s/\s*$/\n/;
+ # Reverse the text first, to avoid having to scan the entire paragraph.
+ ($text = reverse $text) =~ s/\A\s*/\n/;
+ $text = reverse $text;
# Get a count of the number of lines before the first blank line, which
# we'll pass to .Vb as its parameter. This tells *roff to keep that many

0 comments on commit b3e64af

Please sign in to comment.