Skip to content

Commit

Permalink
Revert "Remove deprecated BigInteger::parse()"
Browse files Browse the repository at this point in the history
This reverts commit 6c37468.
Re-introducing - this should not have been removed in the 0.8 release cycle.
  • Loading branch information
BenMorel committed Aug 18, 2020
1 parent 90c46f0 commit edffaf9
Show file tree
Hide file tree
Showing 2 changed files with 231 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/BigInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,30 @@ public static function of($value) : BigNumber
return parent::of($value)->toBigInteger();
}

/**
* Parses a string containing an integer in an arbitrary base.
*
* @deprecated will be removed in version 0.9 - use fromBase() instead
*
* The string can optionally be prefixed with the `+` or `-` sign.
* For bases greater than 10, both uppercase and lowercase letters are allowed.
*
* @param string $number The number to parse.
* @param int $base The base of the number, between 2 and 36.
*
* @return BigInteger
*
* @throws \InvalidArgumentException If the number is invalid or the base is out of range.
*/
public static function parse(string $number, int $base = 10) : BigInteger
{
try {
return self::fromBase($number, $base);
} catch (NumberFormatException $e) {
throw new \InvalidArgumentException($e->getMessage(), 0, $e);
}
}

/**
* Creates a number from a string in a given base.
*
Expand Down
207 changes: 207 additions & 0 deletions tests/BigIntegerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,213 @@ public function providerOfNonConvertibleValueThrowsException() : array
];
}

/**
* @dataProvider providerParse
*
* @param string $number The number to create.
* @param int $base The base of the number.
* @param string $expected The expected result in base 10.
*/
public function testParse($number, $base, $expected)
{
$this->assertBigIntegerEquals($expected, BigInteger::parse($number, $base));
}

/**
* @return array
*/
public function providerParse()
{
return [
['0', 10, '0'],
['-0', 10, '0'],
['+0', 10, '0'],
['00', 16, '0'],
['-00', 16, '0'],
['+00', 16, '0'],

['1', 10, '1'],
['-1', 10, '-1'],
['+1', 10, '1'],
['01', 8, '1'],
['-01', 8, '-1'],
['+01', 8, '1'],

['123', 10, '123'],
['+456', 10, '456'],
['-789', 10, '-789'],
['0123', 10, '123'],
['+0456', 10, '456'],
['-0789', 10, '-789'],

['123', 11, '146'],
['+456', 11, '545'],
['-789', 11, '-944'],
['0123', 11, '146'],
['+0456', 11, '545'],
['-0789', 11, '-944'],

['110011001100110011001111', 36, '640998479760579495168036691627608949'],
['110011001100110011001111', 35, '335582856048758779730579523833856636'],
['110011001100110011001111', 34, '172426711023004493064981145981549295'],
['110011001100110011001111', 33, '86853227285668653965326574185738990'],
['110011001100110011001111', 32, '42836489934972583913564073319498785'],
['110011001100110011001111', 31, '20658924711984480538771889603666144'],
['110011001100110011001111', 30, '9728140488839986222205212599027931'],
['110011001100110011001111', 29, '4465579470019956787945275674107410'],
['110011001100110011001111', 28, '1994689924537781753408144504465645'],
['110011001100110011001111', 27, '865289950909412968716094193925700'],
['110011001100110011001111', 26, '363729369583879309352831568000039'],
['110011001100110011001111', 25, '147793267388865354156500488297526'],
['110011001100110011001111', 24, '57888012016107577099138793486425'],
['110011001100110011001111', 23, '21788392294523974761749372677800'],
['110011001100110011001111', 22, '7852874701996329566765721637715'],
['110011001100110011001111', 21, '2699289081943123258094476428634'],
['110011001100110011001111', 20, '880809345058406615041344008421'],
['110011001100110011001111', 19, '271401690926468032718781859340'],
['110011001100110011001111', 18, '78478889737009209699633503455'],
['110011001100110011001111', 17, '21142384915931646646976872830'],
['110011001100110011001111', 16, '5261325448418072742917574929'],
['110011001100110011001111', 15, '1197116069565850925807253616'],
['110011001100110011001111', 14, '245991074299834917455374155'],
['110011001100110011001111', 13, '44967318722190498361960610'],
['110011001100110011001111', 12, '7177144825886069940574045'],
['110011001100110011001111', 11, '976899716207148313491924'],
['110011001100110011001111', 10, '110011001100110011001111'],
['110011001100110011001111', 9, '9849210196991880028870'],
['110011001100110011001111', 8, '664244955832213832265'],
['110011001100110011001111', 7, '31291601125492514360'],
['110011001100110011001111', 6, '922063395565287619'],
['110011001100110011001111', 5, '14328039609468906'],
['110011001100110011001111', 4, '88305875046485'],
['110011001100110011001111', 3, '127093291420'],
['110011001100110011001111', 2, '13421775'],

['ZyXwVuTsRqPoNmLkJiHgFeDcBa9876543210', 36, '106300512100105327644605138221229898724869759421181854980'],
['YxWvUtSrQpOnMlKjIhGfEdCbA9876543210', 35, '1101553773143634726491620528194292510495517905608180485'],
['XwVuTsRqPoNmLkJiHgFeDcBa9876543210', 34, '11745843093701610854378775891116314824081102660800418'],
['WvUtSrQpOnMlKjIhGfEdCbA9876543210', 33, '128983956064237823710866404905431464703849549412368'],
['VuTsRqPoNmLkJiHgFeDcBa9876543210', 32, '1459980823972598128486511383358617792788444579872'],
['UtSrQpOnMlKjIhGfEdCbA9876543210', 31, '17050208381689099029767742314582582184093573615'],
['TsRqPoNmLkJiHgFeDcBa9876543210', 30, '205646315052919334126040428061831153388822830'],
['SrQpOnMlKjIhGfEdCbA9876543210', 29, '2564411043271974895869785066497940850811934'],
['RqPoNmLkJiHgFeDcBa9876543210', 28, '33100056003358651440264672384704297711484'],
['QpOnMlKjIhGfEdCbA9876543210', 27, '442770531899482980347734468443677777577'],
['PoNmLkJiHgFeDcBa9876543210', 26, '6146269788878825859099399609538763450'],
['OnMlKjIhGfEdCbA9876543210', 25, '88663644327703473714387251271141900'],
['NmLkJiHgFeDcBa9876543210', 24, '1331214537196502869015340298036888'],
['MlKjIhGfEdCbA9876543210', 23, '20837326537038308910317109288851'],
['LkJiHgFeDcBa9876543210', 22, '340653664490377789692799452102'],
['KjIhGfEdCbA9876543210', 21, '5827980550840017565077671610'],
['JiHgFeDcBa9876543210', 20, '104567135734072022160664820'],
['IhGfEdCbA9876543210', 19, '1972313422155189164466189'],
['HgFeDcBa9876543210', 18, '39210261334551566857170'],
['GfEdCbA9876543210', 17, '824008854613343261192'],
['FeDcBa9876543210', 16, '18364758544493064720'],
['EdCbA9876543210', 15, '435659737878916215'],
['DcBa9876543210', 14, '11046255305880158'],
['CbA9876543210', 13, '300771807240918'],
['Ba9876543210', 12, '8842413667692'],
['A9876543210', 11, '282458553905'],
['9876543210', 10, '9876543210'],
['876543210', 9, '381367044'],
['76543210', 8, '16434824'],
['6543210', 7, '800667'],
['543210', 6, '44790'],
['43210', 5, '2930'],
['3210', 4, '228'],
['210', 3, '21'],
['10', 2, '2'],
];
}

/**
* @dataProvider providerParseInvalidValueThrowsException
* @expectedException \InvalidArgumentException
*
* @param string $value
* @param int $base
*/
public function testParseInvalidValueThrowsException($value, $base)
{
BigInteger::parse($value, $base);
}

/**
* @return array
*/
public function providerParseInvalidValueThrowsException()
{
return [
['', 10],
[' ', 10],
['+', 10],
['-', 10],
['1 ', 10],
[' 1', 10],

['Z', 35],
['y', 34],
['X', 33],
['w', 32],
['V', 31],
['u', 30],
['T', 29],
['s', 28],
['R', 27],
['q', 26],
['P', 25],
['o', 24],
['N', 23],
['m', 22],
['L', 21],
['k', 20],
['J', 19],
['i', 18],
['H', 17],
['g', 16],
['F', 15],
['e', 14],
['D', 13],
['c', 12],
['B', 11],
['a', 10],
['9', 9],
['8', 8],
['7', 7],
['6', 6],
['5', 5],
['4', 4],
['3', 3],
['2', 2]
];
}

/**
* @dataProvider providerParseWithInvalidBaseThrowsException
* @expectedException \InvalidArgumentException
*
* @param int $base
*/
public function testParseWithInvalidBaseThrowsException($base)
{
BigInteger::parse('0', $base);
}

/**
* @return array
*/
public function providerParseWithInvalidBaseThrowsException()
{
return [
[-2],
[-1],
[0],
[1],
[37]
];
}

/**
* @dataProvider providerFromBase
*
Expand Down

0 comments on commit edffaf9

Please sign in to comment.