From e52bbde39b46172e6f8535c66db2b0d924f94ad0 Mon Sep 17 00:00:00 2001 From: jrchamp Date: Fri, 9 Dec 2016 14:27:25 -0500 Subject: [PATCH] PMA_getRealSize - Performance and Readability The "upstream" Moodle code was updated in MDL-39524; rather than looping through the options, preg_match allows us to directly select the correct multiplication factor with a single function call. Note that the default behavior (integer coercion) handles both bare integers and numbers of bytes (factor 1). Signed-off-by: Jonathan Champ --- libraries/core.lib.php | 38 ++++++++++++-------------------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/libraries/core.lib.php b/libraries/core.lib.php index a52d3ca775..99e713a866 100644 --- a/libraries/core.lib.php +++ b/libraries/core.lib.php @@ -363,36 +363,22 @@ function PMA_getRealSize($size = 0) return 0; } - $scan = array( - 'gb' => 1073741824, //1024 * 1024 * 1024, - 'g' => 1073741824, //1024 * 1024 * 1024, - 'mb' => 1048576, - 'm' => 1048576, - 'kb' => 1024, - 'k' => 1024, - 'b' => 1, + $binaryprefixes = array( + 'T' => 1099511627776, + 't' => 1099511627776, + 'G' => 1073741824, + 'g' => 1073741824, + 'M' => 1048576, + 'm' => 1048576, + 'K' => 1024, + 'k' => 1024, ); - foreach ($scan as $unit => $factor) { - $sizeLength = strlen($size); - $unitLength = strlen($unit); - if ($sizeLength > $unitLength - && strtolower( - substr( - $size, - $sizeLength - $unitLength - ) - ) == $unit - ) { - return substr( - $size, - 0, - $sizeLength - $unitLength - ) * $factor; - } + if (preg_match('/^([0-9]+)([KMGT])/i', $size, $matches)) { + return $matches[1] * $binaryprefixes[$matches[2]]; } - return $size; + return (int) $size; } // end function PMA_getRealSize() /**