t0021/rot13-filter: refactor checking final lf
authorChristian Couder <christian.couder@gmail.com>
Sun, 5 Nov 2017 21:38:34 +0000 (22:38 +0100)
committerJunio C Hamano <gitster@pobox.com>
Tue, 7 Nov 2017 00:54:41 +0000 (09:54 +0900)
As checking for a lf character at the end of a buffer
will be useful in another function, let's refactor this
functionality into a small remove_final_lf_or_die()
helper function.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/t0021/rot13-filter.pl
index 2f74ab2e45c169f8fe3f984296ea33c2371e9dc5..d47b7f5666a590cc09c26f71d3965f5c838f402f 100644 (file)
@@ -93,12 +93,20 @@ sub packet_bin_read {
        }
 }
 
-sub packet_txt_read {
-       my ( $res, $buf ) = packet_bin_read();
-       unless ( $res == -1 or $buf eq '' or $buf =~ s/\n$// ) {
+sub remove_final_lf_or_die {
+       my $buf = shift;
+       unless ( $buf =~ s/\n$// ) {
                die "A non-binary line MUST be terminated by an LF.\n"
                    . "Received: '$buf'";
        }
+       return $buf;
+}
+
+sub packet_txt_read {
+       my ( $res, $buf ) = packet_bin_read();
+       unless ( $res == -1 or $buf eq '' ) {
+               $buf = remove_final_lf_or_die($buf);
+       }
        return ( $res, $buf );
 }