Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 2.67 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. perl way of getting a value expressed with the memory unit
  2. if( $current_value =~ /(d+)(MB)*/ ){
  3.         $current_value = $1 * 1024 * 1024;
  4.     }
  5.     elsif( $current_value =~ /(d+)(GB)*/ ){
  6.         $current_value = $1 * 1024 * 1024 * 1024;
  7.     }
  8.     elsif( $current_value =~ /(d+)(KB)*/ ){
  9.         $current_value = $1 * 1024;
  10.     }
  11.        
  12. my %FACTORS = ( 'KB' => 1024, 'MB' => 1024**2, 'GB' => 1024**3 );
  13.        
  14. if ( $current_value =~ /(d+)(KB|MB|GB)/ ) {
  15.     $current_value = $1 * $FACTORS{$2};
  16. }
  17.        
  18. use warnings;
  19. use strict;
  20.  
  21. use Number::Format qw(format_bytes);
  22. print format_bytes(1024), "n";
  23. print format_bytes(2535116549), "n";
  24.  
  25. __END__
  26.  
  27. 1K
  28. 2.36G
  29.        
  30. my $r;
  31.  
  32. $current_value =~ s/
  33.     (d+)(?:
  34.           Ki (?{ $r = $^N * 1024 })
  35.         | Mi (?{ $r = $^N * 1024 * 1024 })
  36.         | Gi (?{ $r = $^N * 1024 * 1024 * 1024 })
  37.     )/$r/xso;
  38.        
  39. use Test::More;
  40.  
  41. plan tests => 4;
  42.  
  43. ##
  44. # Convert a string denoting '50MB' into an amount in bytes.
  45. my %FACTORS = ( 'KB' => 1024, 'MB' => 1024*1024, 'GB' => 1024*1024*1024 );
  46. sub string_to_bytes {
  47.         my $current_value = shift;
  48.  
  49.         if ( $current_value =~ /(d+)(KB|MB|GB)/ ) {
  50.             $current_value = $1 * $FACTORS{$2};
  51.         }
  52.         return $current_value;
  53. }
  54.  
  55. my $tests = {
  56.         '50' => 50,
  57.         '52KB' => 52*1024,
  58.         '55MB' => 55*1024*1024,
  59.         '57GB' => 57*1024*1024*1024
  60. };
  61.  
  62. foreach(keys %$tests) {
  63.         is( string_to_bytes($_),$tests->{$_},
  64.             "Testing if $_ becomes $tests->{$_}");
  65. }
  66.        
  67. $ perl testz.pl
  68. 1..4
  69. ok 1 - Testing if 55MB becomes 57671680
  70. ok 2 - Testing if 50 becomes 50
  71. ok 3 - Testing if 52KB becomes 53248
  72. ok 4 - Testing if 57GB becomes 61203283968
  73.        
  74. use strict;
  75. use warnings;
  76. use Regexp::Common;
  77.  
  78. my %multiplier;
  79. my $multiplier_match;
  80. {
  81.  
  82.   # populate %multiplier
  83.   my %exponent = (
  84.     K => 1, # Kilo  Kibi
  85.     M => 2, # Mega  Mebi
  86.     G => 3, # Giga  Gibi
  87.     T => 4, # Tera  Tebi
  88.     P => 5, # Peta  Pebi
  89.     E => 6, # Exa   Exbi
  90.     Z => 7, # Zetta Zebi
  91.     Y => 8, # Yotta Yobi
  92.   );
  93.   while( my ($str,$exp) = each %exponent ){
  94.     @multiplier{ $str,      "${str}B"  } = (1000 ** $exp) x2; # K  KB
  95.     @multiplier{ "${str}i", "${str}iB" } = (1024 ** $exp) x2; # Ki KiB
  96.   }
  97.   # %multiplier now holds 32 pairs (8*4)
  98.  
  99.   # build $multiplier_match
  100.   local $" #" # fix broken highlighting
  101.     = '|';
  102.   my @keys = keys %multiplier;
  103.   $multiplier_match = qr(@keys);
  104.  
  105. }
  106.  
  107. sub remove_multiplier{
  108.   die unless @_ == 1;
  109.   local ($_) = @_;
  110.  
  111.   #  s/^($RE{num}{real})($multiplier_match)$/ $1 * $multiplier{$2} /e;
  112.   if( /^($RE{num}{real})($multiplier_match)$/ ){
  113.     return $1 * $multiplier{$2};
  114.   }
  115.  
  116.   return $_;
  117. }
  118.        
  119. # @multiplier{ $str, "${str}B"  } = (1000 ** $exp) x2; # K  KB
  120.   @multiplier{ $str, "${str}B"  } = (1024 ** $exp) x2; # K  KB