diff --git a/core/includes/common.inc b/core/includes/common.inc index 46a7b8fc82..f32e581b00 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -251,9 +251,9 @@ function check_url($uri) { * A translated string representation of the size. */ function format_size($size, $langcode = NULL) { - if ($size < Bytes::KILOBYTE) { + if (abs($size) < Bytes::KILOBYTE) { if ($size == -1) { - return \Drupal::translation()->t('-1 byte', [], ['langcode' => $langcode]); + return \Drupal::translation()->translate('-1 byte', [], ['langcode' => $langcode]); } else { return \Drupal::translation()->formatPlural($size, '1 byte', '@count bytes', [], ['langcode' => $langcode]); @@ -261,13 +261,15 @@ function format_size($size, $langcode = NULL) { } else { // Convert bytes to kilobytes. + $positive = $size >= 0; + $temp_size = abs($size); foreach (['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] as $unit) { - if (round($size, 2) < Bytes::KILOBYTE) { + if (round($temp_size, 2) < Bytes::KILOBYTE) { break; } - $size = $size / Bytes::KILOBYTE; + $temp_size = $temp_size / Bytes::KILOBYTE; } - $args = ['@size' => round($size, 2)]; + $args = ['@size' => $positive ? round($temp_size, 2) : -round($temp_size, 2)]; $options = ['langcode' => $langcode]; switch ($unit) { case 'KB': diff --git a/core/lib/Drupal/Component/Utility/Bytes.php b/core/lib/Drupal/Component/Utility/Bytes.php index d625b12364..2a7102a4b0 100644 --- a/core/lib/Drupal/Component/Utility/Bytes.php +++ b/core/lib/Drupal/Component/Utility/Bytes.php @@ -28,7 +28,7 @@ public static function toInt($size) { // Remove the non-unit characters from the size. $unit = preg_replace('/[^bkmgtpezy]/i', '', $size); // Remove the non-numeric characters from the size. - $size = preg_replace('/[^0-9\.]/', '', $size); + $size = preg_replace('/[^0-9\.\-]/', '', $size); if ($unit) { // Find the position of the unit in the ordered string which is the power // of magnitude to multiply a kilobyte by. diff --git a/core/tests/Drupal/KernelTests/Core/Common/SizeTest.php b/core/tests/Drupal/KernelTests/Core/Common/SizeTest.php index e821cb01fa..333cf62552 100644 --- a/core/tests/Drupal/KernelTests/Core/Common/SizeTest.php +++ b/core/tests/Drupal/KernelTests/Core/Common/SizeTest.php @@ -36,6 +36,7 @@ protected function setUp() { '-2 bytes' => -2, // Rounded to 1 MB (not 1000 or 1024 kilobyte!). '1 MB' => ($kb * $kb) - 1, + '-1 MB' => -(($kb * $kb) - 1), '3.77 GB' => 4053371676, // Megabytes. round(3623651 / ($this->exactTestCases['1 MB']), 2) . ' MB' => 3623651, @@ -52,7 +53,7 @@ protected function setUp() { public function testCommonFormatSize() { foreach ([$this->exactTestCases, $this->roundedTestCases] as $test_cases) { foreach ($test_cases as $expected => $input) { - $result = format_size($input, NULL); + $result = (string) format_size($input, NULL); $this->assertSame($expected, $result, $expected . ' == ' . $result . ' (' . $input . ' bytes)'); } } @@ -63,7 +64,7 @@ public function testCommonFormatSize() { */ public function testCommonParseSizeFormatSize() { foreach ($this->exactTestCases as $size) { - $parsed_size = Bytes::toInt($string = format_size($size, NULL)); + $parsed_size = (int) Bytes::toInt($string = format_size($size, NULL)); $this->assertSame($size, $parsed_size, $size . ' == ' . $parsed_size . ' (' . $string . ')'); } }