Remove internal LNumber::parse() method

Combine it into ::fromString(), as they both do the same checks.
This commit is contained in:
Nikita Popov 2016-03-10 13:01:42 +01:00
parent f493219c7d
commit fc36239be5

View File

@ -39,50 +39,23 @@ class LNumber extends Scalar
* @return LNumber The constructed LNumber, including kind attribute
*/
public static function fromString($str, array $attributes = array()) {
if ($str === '0' || $str[0] !== '0') {
if ('0' !== $str[0] || '0' === $str) {
$attributes['kind'] = LNumber::KIND_DEC;
} elseif ($str[1] === 'x' || $str[1] === 'X') {
$attributes['kind'] = LNumber::KIND_HEX;
} elseif ($str[1] === 'b' || $str[1] === 'B') {
$attributes['kind'] = LNumber::KIND_BIN;
} else {
$attributes['kind'] = LNumber::KIND_OCT;
}
return new self(self::parse($str), $attributes);
return new LNumber((int) $str, $attributes);
}
/**
* @internal
*
* Parses an LNUMBER token (dec, hex, oct and bin notations) like PHP would.
*
* @param string $str A string number
*
* @return int The parsed number
*/
public static function parse($str) {
// handle plain 0 specially
if ('0' === $str) {
return 0;
}
// if first char is 0 (and number isn't 0) it's a special syntax
if ('0' === $str[0]) {
// hex
if ('x' === $str[1] || 'X' === $str[1]) {
return hexdec($str);
$attributes['kind'] = LNumber::KIND_HEX;
return new LNumber(hexdec($str), $attributes);
}
// bin
if ('b' === $str[1] || 'B' === $str[1]) {
return bindec($str);
$attributes['kind'] = LNumber::KIND_BIN;
return new LNumber(bindec($str), $attributes);
}
// oct (intval instead of octdec to get proper cutting behavior with malformed numbers)
return intval($str, 8);
}
// dec
return (int) $str;
// use intval instead of octdec to get proper cutting behavior with malformed numbers
$attributes['kind'] = LNumber::KIND_OCT;
return new LNumber(intval($str, 8), $attributes);
}
}