Skip to content

Commit

Permalink
fix: handle case when entire input is padding characters
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyBogdanov committed Dec 10, 2021
1 parent 7bc24ac commit c8d7329
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .phpunit.cache/test-results

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion src/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,11 @@ public static function convert( string $input, string $inputAlphabet, string $ou
$ob = count( $os );

$carry = preg_match( '/^' . preg_quote( $is[0], '/' ) . '+/', $input, $match ) ? strlen( $match[0] ) : 0;
$value = '0';
if ( $carry === strlen( $input ) ) {
return str_repeat( $os[0], $carry );
}

$value = '0';
foreach ( array_reverse( str_split( substr( $input, $carry ) ) ) as $index => $symbol ) {
$value = bcadd( $value, bcmul( $isf[ $symbol ], bcpow( $ib, $index ) ) );
}
Expand Down
9 changes: 9 additions & 0 deletions tests/ConvertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public function testEmptyInput(): void {
$this->assertSame( '', Converter::convert( '', 'ab', 'ab' ) );
}

/**
* @return void
* @throws InvalidAlphabetException
* @throws InvalidInputException
*/
public function testOnlyPaddingInput(): void {
$this->assertSame( 'qqq', Converter::convert( 'aaa', 'ab', 'qw' ) );
}

/**
* @return void
* @throws InvalidAlphabetException
Expand Down

0 comments on commit c8d7329

Please sign in to comment.