From: Steve Hancock Date: Wed, 23 Aug 2023 15:29:44 +0000 (-0700) Subject: clean up CR/LF coding X-Git-Tag: 20230701.03~4 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=ce8c00bd7eaf7ee13746d6248a012a0f2ce3c691;p=perltidy.git clean up CR/LF coding --- diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index 4f51151d..7db75d87 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -1709,6 +1709,56 @@ EOM ); } ## end sub get_decoded_string_buffer +{ #<<< + +my $LF; +my $CR; +my $CRLF; + +BEGIN { + $LF = chr(10); + $CR = chr(13); + $CRLF = $CR . $LF; +} + +sub get_line_separator_default { + + my ( $rOpts, $input_file ) = @_; + + # Get the line separator that will apply unless overridden by a + # --preserve-line-endings flag for a specific file + + my $line_separator_default = "\n"; + + my $ole = $rOpts->{'output-line-ending'}; + if ($ole) { + my %endings = ( + dos => $CRLF, + win => $CRLF, + mac => $CR, + unix => $LF, + ); + + $line_separator_default = $endings{ lc $ole }; + + if ( !$line_separator_default ) { + my $str = join SPACE, keys %endings; + Die(<{'preserve-line-endings'} ) { + Warn("Ignoring -ple; conflicts with -ole\n"); + $rOpts->{'preserve-line-endings'} = undef; + } + } + + return $line_separator_default; + +} ## end sub get_line_separator_default + sub set_line_separator { my ( $self, $rinput_string ) = @_; @@ -1728,19 +1778,24 @@ sub set_line_separator { my $str = substr( ${$rinput_string}, 0, 1024 ); if ($str) { - if ( $str =~ /([\012\015]+)/ ) { + if ( $str =~ m/(($CR|$LF)+)/ ) { + my $test = $1; # dos - if ( $test =~ /^(\015\012)+$/ ) { - $input_line_separator = "\015\012"; + if ( $test =~ /^($CRLF)+$/ ) { + $input_line_separator = $CRLF; } # mac - elsif ( $test =~ /^\015+$/ ) { $input_line_separator = "\015" } + elsif ( $test =~ /^($CR)+$/ && $test !~ /$LF/ ) { + $input_line_separator = $CR; + } # unix - elsif ( $test =~ /^\012+$/ ) { $input_line_separator = "\012" } + elsif ( $test =~ /^($LF)+$/ && $test !~ /$CR/ ) { + $input_line_separator = $LF; + } # unknown else { } @@ -1758,15 +1813,14 @@ sub set_line_separator { } # patch to read raw mac files under unix, dos - if ( $input_line_separator ne "\n" && $input_line_separator eq "\015" ) - { + if ( $input_line_separator ne "\n" && $input_line_separator eq $CR ) { # if this file is currently a single line .. my @lines = split /^/, ${$rinput_string}; if ( @lines == 1 ) { # and becomes multiple lines with the change .. - @lines = map { $_ . "\n" } split /\015/, ${$rinput_string}; + @lines = map { $_ . "\n" } split /$CR/, ${$rinput_string}; if ( @lines > 1 ) { # then make the change @@ -1780,6 +1834,7 @@ sub set_line_separator { $self->[_line_separator_] = $line_separator; return $rinput_string; } ## end sub set_line_separator +} sub process_all_files { @@ -4612,44 +4667,6 @@ EOM return; } ## end sub check_options -sub get_line_separator_default { - - my ( $rOpts, $input_file ) = @_; - - # Get the line separator that will apply unless overridden by a - # --preserve-line-endings flag for a specific file - - my $line_separator_default = "\n"; - - my $ole = $rOpts->{'output-line-ending'}; - if ($ole) { - my %endings = ( - dos => "\015\012", - win => "\015\012", - mac => "\015", - unix => "\012", - ); - - $line_separator_default = $endings{ lc $ole }; - - if ( !$line_separator_default ) { - my $str = join SPACE, keys %endings; - Die(<{'preserve-line-endings'} ) { - Warn("Ignoring -ple; conflicts with -ole\n"); - $rOpts->{'preserve-line-endings'} = undef; - } - } - - return $line_separator_default; - -} ## end sub get_line_separator_default - sub find_file_upwards { my ( $search_dir, $search_file ) = @_;