The following commit has been merged in the master branch: commit f399c98ee24cfc18afd72e38af032fb10b977a8b Author: Niels Thykier <ni...@thykier.net> Date: Fri Mar 29 13:14:12 2013 +0100
L::Util: Introduce "{,l,r}strip" ... ... to replace uses of s/^\s++//; or/and s/\s++$//;. In void context they modify their input inplace and in other contexts they copy the input. Thus they can replace the "chomp" like: while ( my $line = <>) { while (<>) { strip ($line); strip; # do something ... # do something... } } For cases where the original input might be useful, they can also return a copy: while ( my $orig = <>) { my $stripped = strip ($orig); } Signed-off-by: Niels Thykier <ni...@thykier.net> diff --git a/checks/binaries b/checks/binaries index e2a2bc4..717eec6 100644 --- a/checks/binaries +++ b/checks/binaries @@ -28,7 +28,7 @@ use Lintian::Data; use Lintian::Relation qw(:constants); use Lintian::Tags qw(tag); use Lintian::Output qw(debug_msg); -use Lintian::Util qw(fail slurp_entire_file); +use Lintian::Util qw(fail slurp_entire_file strip); use File::Spec; @@ -45,8 +45,7 @@ sub _embedded_libs { $regex = $opts; $opts = ''; } else { - $opts=~ s/^\s++//o; - $opts=~ s/^\s++//o; + strip ($opts); foreach my $optstr (split m/\s++/, $opts) { my ($opt, $val) = split m/=/, $optstr, 2; if ($opt eq 'source' or $opt eq 'libname') { diff --git a/checks/debhelper b/checks/debhelper index 18451f2..9cd47da 100644 --- a/checks/debhelper +++ b/checks/debhelper @@ -25,7 +25,7 @@ use warnings; use Lintian::Data; use Lintian::Relation; use Lintian::Tags qw(tag); -use Lintian::Util qw(fail slurp_entire_file); +use Lintian::Util qw(fail slurp_entire_file strip); # If compat is less than or equal to this, then a missing version # for this level is only a pedantic issue. @@ -523,16 +523,13 @@ sub _shebang_cmd { if (read $fd, $magic, 2) { if ($magic eq '#!') { $cmd = <$fd>; - chomp $cmd; # It is beyond me why anyone would place a lincity data # file here... but if they do, we will handle it # correctly. $cmd = '' if $cmd =~ m/^#!/o; - # Strip whitespace if any - $cmd =~ s/^\s++//o; - $cmd =~ s/\s++$//o; + strip ($cmd); } } close $fd; diff --git a/checks/filename-length b/checks/filename-length index 28581e3..3d58574 100644 --- a/checks/filename-length +++ b/checks/filename-length @@ -23,6 +23,7 @@ use strict; use warnings; use Lintian::Tags qw(tag); +use Lintian::Util qw(strip); use constant FILENAME_LENGTH_LIMIT => 80; @@ -72,7 +73,7 @@ $len = 0; foreach my $entry (split m/\n/o, $info->field ('files', '')){ my $filename; my $flen; - $entry =~ s/^\s++//o; + strip ($entry); next unless $entry; (undef, undef, $filename) = split m/\s++/o, $entry; next unless $filename; diff --git a/checks/rules b/checks/rules index 4aad759..91192aa 100644 --- a/checks/rules +++ b/checks/rules @@ -19,7 +19,7 @@ use warnings; use Lintian::Data; use Lintian::Tags qw(tag); -use Lintian::Util qw(fail); +use Lintian::Util qw(fail rstrip); our $PYTHON_DEPEND = 'python | python-dev | python-all | python-all-dev'; our $PYTHON3_DEPEND = 'python3 | python3-dev | python3-all | python3-all-dev'; @@ -247,8 +247,7 @@ while (<RULES>) { # we think we know what it will expand to - note # we ought to "delay" it was a "=" variable rather # than ":=" or "+=". - $val =~ s/\s++$//o; - for (split m/\s++/o, $val) { + for (split m/\s++/o, rstrip ($val)) { $seen{$_}++ if $required{$_}; $seen{$_}++ if $recommended{$_}; } @@ -288,9 +287,8 @@ while (<RULES>) { # we think we know what it will expand to - note # we ought to "delay" it was a "=" variable rather # than ":=" or "+=". - $val =~ s/\s++$//o; local $_; - for (split m/\s++/o, $val) { + for (split m/\s++/o, rstrip ($val)) { $seen{$_}++ if $required{$_}; $seen{$_}++ if $recommended{$_}; } diff --git a/collection/java-info b/collection/java-info index 14b78d9..cfe4d3a 100755 --- a/collection/java-info +++ b/collection/java-info @@ -30,7 +30,7 @@ use FileHandle; use lib "$ENV{'LINTIAN_ROOT'}/lib/"; use Lintian::Collect; use Lintian::Command qw(spawn reap); -use Lintian::Util qw(fail); +use Lintian::Util qw(fail rstrip); sub collect { my ($pkg, $type, $dir) = @_; @@ -61,7 +61,7 @@ my $open_java_info = sub { my $errorhandler = sub { my ($err) = @_; $err =~ s/\r?\n/ /g; - $err =~ s/\s++$//; + rstrip ($err); print {$opts{pipe_in}} "-- ERROR: $err\n"; }; diff --git a/frontend/lintian b/frontend/lintian index 401237d..debfda8 100755 --- a/frontend/lintian +++ b/frontend/lintian @@ -418,8 +418,7 @@ sub cfg_display_level { } return if @display_level; - $val =~ s/^\s++//; - $val =~ s/\s++$//; + strip ($val); foreach my $dl (split m/\s++/, $val) { record_display_level('display-level', $dl); } @@ -666,7 +665,7 @@ require Lintian::Tags; import Lintian::Tags qw(tag); require Lintian::Unpacker; require Lintian::Util; -import Lintian::Util qw(fail parse_boolean); +import Lintian::Util qw(fail parse_boolean strip); if (defined $experimental_output_opts) { my %opts = map { split(/=/) } split( /,/, $experimental_output_opts ); diff --git a/lib/Lintian/Data.pm b/lib/Lintian/Data.pm index 7000ccc..d2179d7 100644 --- a/lib/Lintian/Data.pm +++ b/lib/Lintian/Data.pm @@ -22,6 +22,8 @@ use warnings; use Carp qw(croak); +use Lintian::Util qw(strip); + sub new { my ($class, @args) = @_; my $type = $args[0]; @@ -133,8 +135,7 @@ sub _parse_file { $filename = $vendors->[$vno] . '/' . $type if $vno < scalar @$vendors; local ($_, $.); while (<$fd>) { - chomp; - s/^\s++//; + strip; next if /^\#/ or $_ eq ''; if (s/^\@//) { my ($op, $value) = split m/\s++/; diff --git a/lib/Lintian/Lab/Entry.pm b/lib/Lintian/Lab/Entry.pm index 11a1151..e0a4143 100644 --- a/lib/Lintian/Lab/Entry.pm +++ b/lib/Lintian/Lab/Entry.pm @@ -68,7 +68,7 @@ use File::Spec; use Scalar::Util qw(refaddr); use Lintian::Lab; -use Lintian::Util qw(delete_dir read_dpkg_control get_dsc_info); +use Lintian::Util qw(delete_dir read_dpkg_control get_dsc_info strip); # This is the entry format version - this changes whenever the layout of # entries changes. This differs from LAB_FORMAT in that LAB_FORMAT @@ -307,7 +307,7 @@ sub create { # collection for the symlinking. my (undef, $dir, undef) = File::Spec->splitpath($pkg_path); for my $fs (split(m/\n/o, $self->info->field ('files'))) { - $fs =~ s/^\s*//o; + strip ($fs); next if $fs eq ''; my @t = split(/\s+/o,$fs); next if ($t[2] =~ m,/,o); @@ -456,11 +456,7 @@ sub _init { $coll = $head->{'collections'}//''; $coll =~ s/\n/ /go; - # Strip leading and trailing space to avoid "interesting" issues - # with the "first" collection having leading spaces. - $coll =~ s/^\s++//go; - $coll =~ s/\s++$//go; - foreach my $c (split m/\s*,\s*+/o, $coll) { + foreach my $c (split m/\s*,\s*+/o, strip ($coll)) { my ($cname, $cver) = split m/\s*=\s*/, $c; $self->_mark_coll_finished ($cname, $cver); } diff --git a/lib/Lintian/Processable.pm b/lib/Lintian/Processable.pm index 0909e92..99e6ae7 100644 --- a/lib/Lintian/Processable.pm +++ b/lib/Lintian/Processable.pm @@ -26,6 +26,8 @@ use warnings; use Carp qw(croak); +use Lintian::Util qw(strip); + =head1 NAME Lintian::Processable -- An (abstract) object that Lintian can process @@ -124,7 +126,7 @@ sub new_from_metadata { $dir .= '/' if defined $dir; $dir //= ''; foreach my $f (split m/\n/, $fn) { - $f =~ s/^\s++//o; + strip ($f); next unless $f && $f =~ m/\.dsc$/; my (undef, undef, $file) = split m/\s++/, $f; # $dir should end with a slash if it is non-empty. diff --git a/lib/Lintian/ProcessableGroup.pm b/lib/Lintian/ProcessableGroup.pm index 14a74e6..6d42d5d 100644 --- a/lib/Lintian/ProcessableGroup.pm +++ b/lib/Lintian/ProcessableGroup.pm @@ -24,7 +24,7 @@ use warnings; use Lintian::Collect::Group; use Lintian::Processable; -use Lintian::Util qw(fail get_dsc_info); +use Lintian::Util qw(fail get_dsc_info strip); =head1 NAME @@ -103,8 +103,7 @@ sub _init_group_from_changes { foreach my $line (split (/\n/o, $cinfo->{'files'}//'')) { my ($file); next unless defined $line; - chomp($line); - $line =~ s/^\s++//o; + strip ($line); next if $line eq ''; # Ignore files that may lead to path traversal issues. diff --git a/lib/Lintian/Profile.pm b/lib/Lintian/Profile.pm index e55c7f9..14a3ea9 100644 --- a/lib/Lintian/Profile.pm +++ b/lib/Lintian/Profile.pm @@ -30,7 +30,7 @@ use Dpkg::Vendor qw(get_current_vendor get_vendor_info); use Lintian::CheckScript; use Lintian::Tags; -use Lintian::Util qw(parse_boolean read_dpkg_control); +use Lintian::Util qw(parse_boolean read_dpkg_control strip); =head1 NAME @@ -490,10 +490,7 @@ sub _parse_boolean { sub _split_comma_sep_field { my ($self, $data) = @_; return () unless defined $data; - # remove trailing and leading white-space - $data =~ s/^\s++//o; - $data =~ s/\s++$//o; - return split m/\s*,\s*/o, $data; + return split m/\s*,\s*/o, strip ($data); } # $self->_check_for_invalid_fields($para, $known, $pname, $paraname) diff --git a/lib/Lintian/Util.pm b/lib/Lintian/Util.pm index 5b57f14..9efde3f 100644 --- a/lib/Lintian/Util.pm +++ b/lib/Lintian/Util.pm @@ -58,6 +58,9 @@ BEGIN { slurp_entire_file file_is_encoded_in_non_utf8 fail + strip + lstrip + rstrip system_env delete_dir copy_dir @@ -501,7 +504,7 @@ sub visit_dpkg_paragraph { # Policy: Horizontal whitespace (spaces and tabs) may occur # immediately before or after the value and is ignored there. my ($tag,$value) = (lc $1,$2); - $value =~ s/\s+$//; + rstrip ($value); if (exists $section->{$tag}) { # Policy: A paragraph must not contain more than one instance # of a particular field name. @@ -519,8 +522,7 @@ sub visit_dpkg_paragraph { # each continuation line must start with a space or a tab. Any # trailing spaces or tabs at the end of individual lines of a # field value are ignored. - my $value = $1; - $value =~ s/\s+$//; + my $value = rstrip ($1); $section->{$last_tag} .= "\n" . $value; } # None of the above => syntax error @@ -979,6 +981,76 @@ sub fail { croak $str; } +=item strip ([LINE]) + +Strips whitespace from the beginning and the end of LINE and returns +it. If LINE is omitted, C<$_> will be used instead. Example + + @lines = map { strip } <$fd>; + +In void context, the input argument will be modified so it can be +used as a replacement for chomp in some cases: + + while ( my $line = <$fd> ) { + strip ($line); + # $line no longer has any leading or trailing whitespace + } + +Otherwise, a copy of the string is returned: + + while ( my $orig = <$fd> ) { + my $stripped = strip ($orig); + if ($stripped ne $orig) { + # $orig had leadning or/and trailing whitespace + } + } + +=item lstrip ([LINE]) + +Like L<strip|/strip ([LINE])> but only strip leading whitespace. + +=item rstrip ([LINE]) + +Like L<strip|/strip ([LINE])> but only strip trailing whitespace. + +=cut + +# prototype for default to $_ +sub strip (_) { + if (defined wantarray) { + # perl 5.14 s///r would have been useful here. + my ($arg) = @_; + $arg =~ s/^\s++|\s++$//g; + return $arg; + } else { + $_[0] =~ s/^\s++|\s++$//g; + } +} + +# prototype for default to $_ +sub lstrip (_) { + if (defined wantarray) { + # perl 5.14 s///r would have been useful here. + my ($arg) = @_; + $arg =~ s/^\s++//; + return $arg; + } else { + $_[0] =~ s/^\s++//; + } +} + +# prototype for default to $_ +sub rstrip (_) { + if (defined wantarray) { + # perl 5.14 s///r would have been useful here. + my ($arg) = @_; + $arg =~ s/\s++$//g; + return $arg; + } else { + $_[0] =~ s/\s++$//; + } +} + =item check_path (CMD) Returns 1 if CMD can be found in PATH (i.e. $ENV{PATH}) and is diff --git a/private/generate-profiles.pl b/private/generate-profiles.pl index 50caa98..df7ef18 100755 --- a/private/generate-profiles.pl +++ b/private/generate-profiles.pl @@ -23,7 +23,7 @@ BEGIN { } use lib "$ENV{LINTIAN_ROOT}/lib"; -use Lintian::Util qw(fail read_dpkg_control); +use Lintian::Util qw(fail read_dpkg_control strip); my $root = $ENV{LINTIAN_ROOT}; my @dirs = ('profiles/debian'); @@ -125,10 +125,8 @@ sub read_tags { my @tags = (); open my $fd, '<', $file or die "$file: $!"; while (<$fd>) { - chomp; - s/^\s++//; + strip; next if /^#/ or $_ eq ''; - s/\s++$//; push @tags, $_; } close $fd; diff --git a/reporting/harness b/reporting/harness index 9aab497..d9c229b 100755 --- a/reporting/harness +++ b/reporting/harness @@ -115,7 +115,7 @@ require Lintian::Processable; require Lintian::Relation::Version; import Lintian::Relation::Version qw(versions_lt); require Lintian::Util; -import Lintian::Util qw(visit_dpkg_paragraph); +import Lintian::Util qw(strip visit_dpkg_paragraph); # turn file buffering off $| = 1; @@ -490,9 +490,7 @@ sub Die { sub _trim_split { my ($val) = @_; return () unless $val; - $val =~ s/^\s++//o; - $val =~ s/\s++$//o; - return split m/\s*+,\s*+/o, $val; + return split m/\s*+,\s*+/o, strip ($val); } # local_mirror_manifests ($mirdir, $dists, $areas, $archs) @@ -612,7 +610,7 @@ sub _parse_srcs_pg { return unless $active_srcs->{$s}; $dir .= '/' if $dir; foreach my $f (split m/\n/, $data->{'files'}) { - $f =~ s/^\s++//o; + strip ($f); next unless $f && $f =~ m/\.dsc$/; my (undef, undef, $file) = split m/\s++/, $f; # $dir should end with a slash if it is non-empty. -- Debian package checker -- To UNSUBSCRIBE, email to debian-lint-maint-requ...@lists.debian.org with a subject of "unsubscribe". Trouble? Contact listmas...@lists.debian.org Archive: http://lists.debian.org/e1ulc9l-0006aj...@vasks.debian.org