From: Steve Hancock Date: Thu, 5 Oct 2023 14:40:49 +0000 (-0700) Subject: check on trimming zero length input stream in tokenizer (c286) X-Git-Tag: 20230912.03~4 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=c00a0586670b8d1c0db06475be9674237a6683d8;p=perltidy.git check on trimming zero length input stream in tokenizer (c286) --- diff --git a/lib/Perl/Tidy/Tokenizer.pm b/lib/Perl/Tidy/Tokenizer.pm index 6b0ae8cb..98aa41ae 100644 --- a/lib/Perl/Tidy/Tokenizer.pm +++ b/lib/Perl/Tidy/Tokenizer.pm @@ -656,8 +656,12 @@ EOM # Get trimmed lines. It is much faster to strip leading whitespace from # the whole input file at once than line-by-line. - # Add a terminal newline if needed to keep line count unchanged (c283) - if ( $source_string && $source_string !~ /\n$/ ) { $source_string .= "\n" } + # Add a terminal newline if needed to keep line count unchanged: + # - avoids problem of losing a last line which is just \r and no \n (c283) + # - but check input line count to avoid adding line to an empty file (c286) + if ( @{$rinput_lines} && $source_string !~ /\n$/ ) { + $source_string .= "\n"; + } # Remove leading whitespace except newlines $source_string =~ s/^ [^\S\n]+ //gxm;