mirror of
https://github.com/nikic/PHP-Parser.git
synced 2025-01-17 23:28:15 +01:00
Remove internal LNumber::parse() method
Combine it into ::fromString(), as they both do the same checks.
This commit is contained in:
parent
f493219c7d
commit
fc36239be5
@ -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') {
|
||||
return new LNumber((int) $str, $attributes);
|
||||
}
|
||||
|
||||
if ('x' === $str[1] || 'X' === $str[1]) {
|
||||
$attributes['kind'] = LNumber::KIND_HEX;
|
||||
} elseif ($str[1] === 'b' || $str[1] === 'B') {
|
||||
return new LNumber(hexdec($str), $attributes);
|
||||
}
|
||||
|
||||
if ('b' === $str[1] || 'B' === $str[1]) {
|
||||
$attributes['kind'] = LNumber::KIND_BIN;
|
||||
} else {
|
||||
$attributes['kind'] = LNumber::KIND_OCT;
|
||||
}
|
||||
return new self(self::parse($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;
|
||||
return new LNumber(bindec($str), $attributes);
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
// bin
|
||||
if ('b' === $str[1] || 'B' === $str[1]) {
|
||||
return bindec($str);
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user