diff options
Diffstat (limited to 'includes')
-rw-r--r-- | includes/bootstrap.inc | 8 | ||||
-rw-r--r-- | includes/common.inc | 30 |
2 files changed, 23 insertions, 15 deletions
diff --git a/includes/bootstrap.inc b/includes/bootstrap.inc index 495dd0bbb..a08bff5ef 100644 --- a/includes/bootstrap.inc +++ b/includes/bootstrap.inc @@ -162,6 +162,14 @@ define('DRUPAL_ANONYMOUS_RID', 1); define('DRUPAL_AUTHENTICATED_RID', 2); /** + * The number of bytes in a kilobyte. The scientific standard for kilo is 1000, + * but used commonly in the field of computers to represent 1024 bits. For more + * information on the different standards please visit: + * http://en.wikipedia.org/wiki/Kilobyte. + */ +define('DRUPAL_KILOBYTE', 1024); + +/** * No language negotiation. The default language is used. */ define('LANGUAGE_NEGOTIATION_NONE', 0); diff --git a/includes/common.inc b/includes/common.inc index 8e7860619..0b6faa35a 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -1210,20 +1210,20 @@ function format_plural($count, $singular, $plural, $args = array(), $langcode = * Parse a given byte count. * * @param $size - * A size expressed as a number of bytes with optional SI size and unit - * suffix (e.g. 2, 3K, 5MB, 10G). + * A size expressed as a number of bytes with optional SI or IEC binary unit + * prefix (e.g. 2, 3K, 5MB, 10G, 6GiB, 8 bytes, 9mbytes). * @return - * An integer representation of the size. + * An integer representation of the size in bytes. */ function parse_size($size) { - $suffixes = array( - '' => 1, - 'k' => 1024, - 'm' => 1048576, // 1024 * 1024 - 'g' => 1073741824, // 1024 * 1024 * 1024 - ); - if (preg_match('/([0-9]+)\s*(k|m|g)?(b?(ytes?)?)/i', $size, $match)) { - return $match[1] * $suffixes[drupal_strtolower($match[2])]; + $unit = preg_replace('/[^bkmgtpezy]/i', '', $size); // Remove the non-unit characters from the size. + $size = preg_replace('/[^0-9\.]/', '', $size); // Remove the non-numeric characters from the size. + if ($unit) { + // Find the position of the unit in the ordered string which is the power of magnitude to multiply a kilobyte by. + return round($size * pow(DRUPAL_KILOBYTE, stripos('bkmgtpezy', $unit[0]))); + } + else { + return round($size); } } @@ -1239,11 +1239,11 @@ function parse_size($size) { * A translated string representation of the size. */ function format_size($size, $langcode = NULL) { - if ($size < 1000) { + if ($size < DRUPAL_KILOBYTE) { return format_plural($size, '1 byte', '@count bytes', array(), $langcode); } else { - $size = $size / 1000; // convert bytes to kilobytes (1000 bytes) + $size = $size / DRUPAL_KILOBYTE; // Convert bytes to kilobytes. $units = array( t('@size KB', array(), $langcode), t('@size MB', array(), $langcode), @@ -1255,8 +1255,8 @@ function format_size($size, $langcode = NULL) { t('@size YB', array(), $langcode), ); foreach ($units as $unit) { - if (round($size, 2) >= 1000) { - $size = $size / 1000; + if (round($size, 2) >= DRUPAL_KILOBYTE) { + $size = $size / DRUPAL_KILOBYTE; } else { break; |