Skip to content

Commit

Permalink
split: refactor get_count() (#555)
Browse files Browse the repository at this point in the history
* Rewrite validation routine to use only 1 regex, which extracts the number prefix and optional multiplier suffix
* Remove warnings for users
* The following are valid for -b argument: 1, 1k, 1K, 1m, 1M (not 0 and not empty string)
  • Loading branch information
mknos committed Apr 15, 2024
1 parent 8c2e2a5 commit 0ea12dc
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions bin/split
Expand Up @@ -21,7 +21,6 @@ License: perl
# Perl Power Tools -- http://language.perl.com/ppt/
#

$^W = 1; # -w
use strict;
use Getopt::Std;
use File::Basename;
Expand All @@ -31,18 +30,11 @@ my $me = basename($0);
## get_count expands byte/linecount 'k' and 'm' and checks sanity
sub get_count {
my $count = shift;

return undef unless $count =~ /^\d+[KkMm]?$/; # sane?

if ($count =~ /[Kk]$/) {
$count =~ s/[Kk]//g;
$count *= 1024;
} elsif ($count =~ /[Mm]$/) {
$count =~ s/[Mm]//g;
$count *= 1024 * 1024;
}

return $count;
my %mult = ('k' => 1024, 'm' => 1024 * 1024);
return if $count !~ m/\A([0-9]+)([KkMm]?)\Z/;
my $n = $1;
$n *= $mult{lc($2)} if $2;
return $n;
}

# nextfile creates the next file for output, and returns the
Expand Down

0 comments on commit 0ea12dc

Please sign in to comment.