Skip to content

Commit

Permalink
[Locale] fixed bug on the parsing of TYPE_INT64 integers in 32 bit an…
Browse files Browse the repository at this point in the history
…d 64 bit environments, caused by PHP bug fix :) (closes #4718)
  • Loading branch information
eriksencosta committed Jul 9, 2012
1 parent c1fea1d commit 28f002d
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 12 deletions.
28 changes: 26 additions & 2 deletions src/Symfony/Component/Locale/Stub/StubNumberFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,8 @@ private function getInt32Value($value)
* @param mixed $value The value to be converted
*
* @return int|float The converted value
*
* @see https://bugs.php.net/bug.php?id=59597 Bug #59597
*/
private function getInt64Value($value)
{
Expand All @@ -827,11 +829,33 @@ private function getInt64Value($value)
}

if (PHP_INT_SIZE !== 8 && ($value > self::$int32Range['positive'] || $value <= self::$int32Range['negative'])) {
// Bug #59597 was fixed on PHP 5.3.14 and 5.4.4
// The negative PHP_INT_MAX was being converted to float
if (
$value == self::$int32Range['negative'] &&
(
(version_compare(PHP_VERSION, '5.4.0', '<') && version_compare(PHP_VERSION, '5.3.14', '>=')) ||
version_compare(PHP_VERSION, '5.4.4', '>=')
)
) {
return (int) $value;
}

return (float) $value;
}

if (PHP_INT_SIZE === 8 && ($value > self::$int32Range['positive'] || $value < self::$int32Range['negative'])) {
$value = (-2147483648 - ($value % -2147483648)) * ($value / abs($value));
if (PHP_INT_SIZE === 8) {
// Bug #59597 was fixed on PHP 5.3.14 and 5.4.4
// A 32 bit integer was being generated instead of a 64 bit integer
if (
($value > self::$int32Range['positive'] || $value < self::$int32Range['negative']) &&
(
(version_compare(PHP_VERSION, '5.3.14', '<')) ||
(version_compare(PHP_VERSION, '5.4.0', '>=') && version_compare(PHP_VERSION, '5.4.4', '<'))
)
) {
$value = (-2147483648 - ($value % -2147483648)) * ($value / abs($value));
}
}

return (int) $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -806,10 +806,20 @@ public function testParseTypeInt64StubWith32BitIntegerInPhp32Bit()
$this->assertInternalType('integer', $parsedValue);
$this->assertEquals(2147483647, $parsedValue);

// Look that the parsing of '-2,147,483,648' results in a float like the literal -2147483648
$parsedValue = $formatter->parse('-2,147,483,648', \NumberFormatter::TYPE_INT64);
$this->assertInternalType('float', $parsedValue);
$this->assertEquals(((float) -2147483647 - 1), $parsedValue);

// Bug #59597 was fixed on PHP 5.3.14 and 5.4.4
// The negative PHP_INT_MAX was being converted to float
if (
(version_compare(PHP_VERSION, '5.4.0', '<') && version_compare(PHP_VERSION, '5.3.14', '>=')) ||
version_compare(PHP_VERSION, '5.4.4', '>=')
) {
$this->assertInternalType('int', $parsedValue);
} else {
$this->assertInternalType('float', $parsedValue);
}

$this->assertEquals(-2147483648, $parsedValue);
}

public function testParseTypeInt64StubWith32BitIntegerInPhp64Bit()
Expand Down Expand Up @@ -857,11 +867,31 @@ public function testParseTypeInt64StubWith64BitIntegerInPhp64Bit()

$parsedValue = $formatter->parse('2,147,483,648', \NumberFormatter::TYPE_INT64);
$this->assertInternalType('integer', $parsedValue);
$this->assertEquals(-2147483647 - 1, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');

// Bug #59597 was fixed on PHP 5.3.14 and 5.4.4
// A 32 bit integer was being generated instead of a 64 bit integer
if (
(version_compare(PHP_VERSION, '5.3.14', '<')) ||
(version_compare(PHP_VERSION, '5.4.0', '>=') && version_compare(PHP_VERSION, '5.4.4', '<'))
) {
$this->assertEquals(-2147483648, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range (PHP < 5.3.14 and PHP < 5.4.4).');
} else {
$this->assertEquals(2147483648, $parsedValue, '->parse() TYPE_INT64 uses true 64 bit integers (PHP >= 5.3.14 and PHP >= 5.4.4).');
}

$parsedValue = $formatter->parse('-2,147,483,649', \NumberFormatter::TYPE_INT64);
$this->assertInternalType('integer', $parsedValue);
$this->assertEquals(2147483647, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');

// Bug #59597 was fixed on PHP 5.3.14 and 5.4.4
// A 32 bit integer was being generated instead of a 64 bit integer
if (
(version_compare(PHP_VERSION, '5.3.14', '<')) ||
(version_compare(PHP_VERSION, '5.4.0', '>=') && version_compare(PHP_VERSION, '5.4.4', '<'))
) {
$this->assertEquals(2147483647, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range (PHP < 5.3.14 and PHP < 5.4.4).');
} else {
$this->assertEquals(-2147483649, $parsedValue, '->parse() TYPE_INT64 uses true 64 bit integers (PHP >= 5.3.14 and PHP >= 5.4.4).');
}
}

// Intl Tests
Expand All @@ -877,10 +907,20 @@ public function testParseTypeInt64IntlWith32BitIntegerInPhp32Bit()
$this->assertInternalType('integer', $parsedValue);
$this->assertEquals(2147483647, $parsedValue);

// Look that the parsing of '-2,147,483,648' results in a float like the literal -2147483648
$parsedValue = $formatter->parse('-2,147,483,648', \NumberFormatter::TYPE_INT64);
$this->assertInternalType('float', $parsedValue);
$this->assertEquals(((float) -2147483647 - 1), $parsedValue);

// Bug #59597 was fixed on PHP 5.3.14 and 5.4.4
// The negative PHP_INT_MAX was being converted to float.
if (
(version_compare(PHP_VERSION, '5.4.0', '<') && version_compare(PHP_VERSION, '5.3.14', '>=')) ||
version_compare(PHP_VERSION, '5.4.4', '>=')
) {
$this->assertInternalType('int', $parsedValue);
} else {
$this->assertInternalType('float', $parsedValue);
}

$this->assertEquals(-2147483648, $parsedValue);
}

public function testParseTypeInt64IntlWith32BitIntegerInPhp64Bit()
Expand Down Expand Up @@ -931,11 +971,31 @@ public function testParseTypeInt64IntlWith64BitIntegerInPhp64Bit()

$parsedValue = $formatter->parse('2,147,483,648', \NumberFormatter::TYPE_INT64);
$this->assertInternalType('integer', $parsedValue);
$this->assertEquals(-2147483647 - 1, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');

// Bug #59597 was fixed on PHP 5.3.14 and 5.4.4
// A 32 bit integer was being generated instead of a 64 bit integer
if (
(version_compare(PHP_VERSION, '5.3.14', '<')) ||
(version_compare(PHP_VERSION, '5.4.0', '>=') && version_compare(PHP_VERSION, '5.4.4', '<'))
) {
$this->assertEquals(-2147483648, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range (PHP < 5.3.14 and PHP < 5.4.4).');
} else {
$this->assertEquals(2147483648, $parsedValue, '->parse() TYPE_INT64 uses true 64 bit integers (PHP >= 5.3.14 and PHP >= 5.4.4).');
}

$parsedValue = $formatter->parse('-2,147,483,649', \NumberFormatter::TYPE_INT64);
$this->assertInternalType('integer', $parsedValue);
$this->assertEquals(2147483647, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range.');

// Bug #59597 was fixed on PHP 5.3.14 and 5.4.4
// A 32 bit integer was being generated instead of a 64 bit integer
if (
(version_compare(PHP_VERSION, '5.3.14', '<')) ||
(version_compare(PHP_VERSION, '5.4.0', '>=') && version_compare(PHP_VERSION, '5.4.4', '<'))
) {
$this->assertEquals(2147483647, $parsedValue, '->parse() TYPE_INT64 does not use true 64 bit integers, using only the 32 bit range (PHP < 5.3.14 and PHP < 5.4.4).');
} else {
$this->assertEquals(-2147483649, $parsedValue, '->parse() TYPE_INT64 uses true 64 bit integers (PHP >= 5.3.14 and PHP >= 5.4.4).');
}
}

/**
Expand Down

0 comments on commit 28f002d

Please sign in to comment.