Skip to content

Commit

Permalink
adjustments as per request
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienDug committed Nov 27, 2023
1 parent 3f69824 commit 42daee9
Showing 1 changed file with 16 additions and 45 deletions.
61 changes: 16 additions & 45 deletions src/BigNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ abstract class BigNumber implements \Serializable, \JsonSerializable
private const PARSE_REGEXP_NUMERICAL =
'/^' .
'(?<sign>[\-\+])?' .
'(?<integral>[0-9]+)?' .
'(?<point>\.)?' .
'(?<fractional>[0-9]+)?' .
'(?:[eE](?<exponent>[\-\+]?[0-9]+))?' .
'(?<integral>[0-9]+)?' .
'(?<point>\.)?' .
'(?<fractional>[0-9]+)?' .
'(?:[eE](?<exponent>[\-\+]?[0-9]+))?' .
'$/';

/**
* The regular expression used to parse rational numbers.
*/
private const PARSE_REGEXP_RATIONAL =
'/^' .
'(?<sign>[\-\+])?' .
'(?<numerator>[0-9]+)' .
'\/?' .
'(?<denominator>[0-9]+)' .
'(?<sign>[\-\+])?' .
'(?<numerator>[0-9]+)' .
'\/?' .
'(?<denominator>[0-9]+)' .
'$/';
/**
* Creates a BigNumber of the given value.
Expand Down Expand Up @@ -82,12 +82,8 @@ public static function of(BigNumber|int|float|string $value) : BigNumber
assert($numerator !== null);
assert($denominator !== null);

if ($sign !== null) {
$numerator = $sign . $numerator;
}

$numerator = self::cleanUp($numerator);
$denominator = self::cleanUp($denominator);
$numerator = self::cleanUp($sign, $numerator);
$denominator = self::cleanUp(null, $denominator);

if ($denominator === '0') {
throw DivisionByZeroException::denominatorMustNotBeZero();
Expand Down Expand Up @@ -126,7 +122,7 @@ public static function of(BigNumber|int|float|string $value) : BigNumber
throw new NumberFormatException('Exponent too large.');
}

$unscaledValue = self::leanCleanUp(($sign ?? ''), $integral . $fractional);
$unscaledValue = self::cleanUp(($sign ?? ''), $integral . $fractional);

$scale = \strlen($fractional) - $exponent;

Expand All @@ -140,7 +136,7 @@ public static function of(BigNumber|int|float|string $value) : BigNumber
return new BigDecimal($unscaledValue, $scale);
}

$integral = self::leanCleanUp(($sign ?? ''), $integral);
$integral = self::cleanUp(($sign ?? ''), $integral);

return new BigInteger($integral);
}
Expand All @@ -150,7 +146,7 @@ public static function of(BigNumber|int|float|string $value) : BigNumber
* Throws a NumberFormatException for the given value.
* @psalm-pure
*/
protected static function throwException(string $value) : void {
private static function throwException(string $value) : void {
throw new NumberFormatException(\sprintf(
'The given value "%s" does not represent a valid number.',
$value
Expand Down Expand Up @@ -316,39 +312,14 @@ private static function add(BigNumber $a, BigNumber $b) : BigNumber
return $a->plus($b);
}

/**
* Removes optional leading zeros and + sign from the given number.
*
* @param string $number The number, validated as a non-empty string of digits with optional leading sign.
*
* @psalm-pure
*/
private static function cleanUp(string $number) : string
{
$firstChar = $number[0];

if ($firstChar === '+' || $firstChar === '-') {
$number = \substr($number, 1);
}

$number = \ltrim($number, '0');

if ($number === '') {
return '0';
}

return $firstChar === '-' ? '-' . $number : $number;
}

/**
* Removes optional leading zeros and applies sign if needed(- for negatives).
*
* @param string $number The number, validated as a non-empty string of digits
* @param string $sign The sign, + or -
*
* @param string|null $sign The sign, '+' or '-', optional.
* @param string $number The number, validated as a non-empty string of digits.
* @psalm-pure
*/
private static function leanCleanUp(string $sign, string $number) : string
private static function cleanUp(string|null $sign, string $number) : string
{
$number = \ltrim($number, '0');

Expand Down

0 comments on commit 42daee9

Please sign in to comment.