summaryrefslogtreecommitdiff
path: root/includes/common.inc
diff options
context:
space:
mode:
authorDries Buytaert <dries@buytaert.net>2008-11-16 15:19:34 +0000
committerDries Buytaert <dries@buytaert.net>2008-11-16 15:19:34 +0000
commit5fbdca023f6aa66563ef362afd65908aeb2e3a14 (patch)
tree460bb0aeed33804afe120475d03fa179a768319d /includes/common.inc
parentdaf5005c2328dd6fc3bb6c1fe14f2a87736b9384 (diff)
downloadbrdo-5fbdca023f6aa66563ef362afd65908aeb2e3a14.tar.gz
brdo-5fbdca023f6aa66563ef362afd65908aeb2e3a14.tar.bz2
- Patch #267883 by MadHarold, boombatower: parse_size() and format_size() do not use the same kilo standard.
Diffstat (limited to 'includes/common.inc')
-rw-r--r--includes/common.inc30
1 files changed, 15 insertions, 15 deletions
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;