Skip to content

Commit

Permalink
allow for lowercase as well, also strip the unit part off before pow()
Browse files Browse the repository at this point in the history
  • Loading branch information
ceeram committed Sep 18, 2012
1 parent 90c32ad commit ff676b5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/Cake/Test/Case/Utility/CakeNumberTest.php
Expand Up @@ -555,6 +555,7 @@ public function filesizes() {
array(array('size' => '1KB', 'default' => false), 1024),
array(array('size' => '1.5KB', 'default' => false), 1536),
array(array('size' => '1MB', 'default' => false), 1048576),
array(array('size' => '1mb', 'default' => false), 1048576),
array(array('size' => '1.5MB', 'default' => false), 1572864),
array(array('size' => '1GB', 'default' => false), 1073741824),
array(array('size' => '1.5GB', 'default' => false), 1610612736),
Expand Down
3 changes: 3 additions & 0 deletions lib/Cake/Utility/CakeNumber.php
Expand Up @@ -112,13 +112,16 @@ public static function fromReadableSize($size, $default = false) {
if (ctype_digit($size)) {
return $size * 1;
}
$size = strtoupper($size);

$i = array_search(substr($size, -2), array('KB', 'MB', 'GB', 'TB', 'PB'));
if ($i !== false) {
$size = substr($size, 0, strlen($size) -2);
return $size * pow(1024, $i + 1);
}

if (substr($size, -1) == 'B' && ctype_digit(substr($size, 0, strlen($size) - 1))) {
$size = substr($size, 0, strlen($size) - 1);
return $size * 1;
}

Expand Down

0 comments on commit ff676b5

Please sign in to comment.