Skip to content

Commit

Permalink
Return integer from parse_bytes() function
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Dec 17, 2023
1 parent 9729ff9 commit 2851ac3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
30 changes: 14 additions & 16 deletions program/lib/Roundcube/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,35 +148,33 @@ function in_array_nocase($needle, $haystack)
*
* @param string $str Input string
*
* @return float Number of bytes
* @return int Number of bytes
*/
function parse_bytes($str)
{
if (is_numeric($str)) {
return floatval($str);
return intval($str);
}

$bytes = 0;

if (preg_match('/([0-9\.]+)\s*([a-z]*)/i', (string) $str, $regs)) {
if (preg_match('/([0-9\.]+)\s*([a-z])i?b?/i', (string) $str, $regs)) {
$bytes = floatval($regs[1]);
switch (strtolower($regs[2])) {
case 'g':
case 'gb':
$bytes *= 1073741824;
break;
case 'm':
case 'mb':
$bytes *= 1048576;
break;
case 'k':
case 'kb':
switch (strtoupper($regs[2])) {
case 'T':
$bytes *= 1024;
case 'G':
$bytes *= 1024;
case 'M':
$bytes *= 1024;
case 'K':
$bytes *= 1024;
break;
}

$bytes = (int) round($bytes);
}

return floatval($bytes);
return $bytes;
}

/**
Expand Down
22 changes: 13 additions & 9 deletions tests/Framework/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,29 @@ function test_in_array_nocase()
function test_parse_bytes()
{
$data = [
'0' => 0,
'1' => 1,
'1024' => 1024,

'2k' => 2 * 1024,
'2m' => 2 * 1024 * 1024,
'2g' => 2 * 1024 * 1024 * 1024,
'2t' => 2 * 1024 * 1024 * 1024 * 1024,

'2 k' => 2 * 1024,
'2kb' => 2 * 1024,
'2kB' => 2 * 1024,
'2m' => 2 * 1048576,
'2 m' => 2 * 1048576,
'2mb' => 2 * 1048576,
'2mB' => 2 * 1048576,
'2g' => 2 * 1024 * 1048576,
'2 g' => 2 * 1024 * 1048576,
'2gb' => 2 * 1024 * 1048576,
'2gB' => 2 * 1024 * 1048576,
'2KiB' => 2 * 1024,
'2 m' => 2 * 1024 * 1024,
'2TB' => 2 * 1024 * 1024 * 1024 * 1024,

'2.5k' => (int) round(2.5 * 1024),
'0.01 MiB' => (int) round(0.01 * 1024 * 1024),
];

foreach ($data as $value => $expected) {
$result = parse_bytes($value);
$this->assertEquals($expected, $result, "Invalid parse_bytes() result for $value");
$this->assertSame($expected, $result, "Invalid parse_bytes() result for $value");
}

$this->assertSame(0.0, parse_bytes(null));

Check failure on line 64 in tests/Framework/Bootstrap.php

View workflow job for this annotation

GitHub Actions / Linux / PHP 7.3

Failed asserting that 0 is identical to 0.0.

Check failure on line 64 in tests/Framework/Bootstrap.php

View workflow job for this annotation

GitHub Actions / Linux / PHP 7.4

Failed asserting that 0 is identical to 0.0.

Check failure on line 64 in tests/Framework/Bootstrap.php

View workflow job for this annotation

GitHub Actions / Linux / PHP 8.0

Failed asserting that 0 is identical to 0.0.

Check failure on line 64 in tests/Framework/Bootstrap.php

View workflow job for this annotation

GitHub Actions / Linux / PHP 8.1

Failed asserting that 0 is identical to 0.0.

Check failure on line 64 in tests/Framework/Bootstrap.php

View workflow job for this annotation

GitHub Actions / Linux / PHP 8.2

Failed asserting that 0 is identical to 0.0.

Check failure on line 64 in tests/Framework/Bootstrap.php

View workflow job for this annotation

GitHub Actions / Linux / PHP 8.3

Failed asserting that 0 is identical to 0.0.
Expand Down

0 comments on commit 2851ac3

Please sign in to comment.