Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
55 / 55 |
|
100.00% |
1 / 1 |
CRAP | n/a |
0 / 0 |
|
Wikimedia\base_convert | |
100.00% |
55 / 55 |
|
100.00% |
1 / 1 |
23 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | namespace Wikimedia; |
22 | |
23 | /** |
24 | * Convert an arbitrarily-long string from one numeric base to |
25 | * another, optionally zero-padding to a minimum column width. |
26 | * |
27 | * Supports base 2 through 36; digit values 10-36 are represented |
28 | * as lowercase letters a-z. Input is case-insensitive. |
29 | * |
30 | * @param string $input Input number |
31 | * @param int $sourceBase Base of the input number |
32 | * @param int $destBase Desired base of the output |
33 | * @param int $pad Minimum number of digits in the output (pad with zeroes) |
34 | * @param bool $lowercase Whether to output in lowercase or uppercase |
35 | * @param string $engine Either "gmp", "bcmath", "php" or "auto" (default). |
36 | * In the case of "auto", the other engines ("gmp" and "bcmath") are used in |
37 | * the listed order in terms of preference if that PHP extension is actually loaded. |
38 | * @return string|false The output number as a string, or false on error |
39 | */ |
40 | function base_convert( $input, $sourceBase, $destBase, $pad = 1, |
41 | $lowercase = true, $engine = 'auto' |
42 | ) { |
43 | $input = (string)$input; |
44 | if ( |
45 | $sourceBase < 2 || |
46 | $sourceBase > 36 || |
47 | $destBase < 2 || |
48 | $destBase > 36 || |
49 | $sourceBase !== (int)$sourceBase || |
50 | $destBase !== (int)$destBase || |
51 | $pad !== (int)$pad || |
52 | !preg_match( |
53 | "/^[" . substr( '0123456789abcdefghijklmnopqrstuvwxyz', 0, $sourceBase ) . "]+$/i", |
54 | $input |
55 | ) |
56 | ) { |
57 | return false; |
58 | } |
59 | |
60 | static $baseChars = [ |
61 | 10 => 'a', 11 => 'b', 12 => 'c', 13 => 'd', 14 => 'e', 15 => 'f', |
62 | 16 => 'g', 17 => 'h', 18 => 'i', 19 => 'j', 20 => 'k', 21 => 'l', |
63 | 22 => 'm', 23 => 'n', 24 => 'o', 25 => 'p', 26 => 'q', 27 => 'r', |
64 | 28 => 's', 29 => 't', 30 => 'u', 31 => 'v', 32 => 'w', 33 => 'x', |
65 | 34 => 'y', 35 => 'z', |
66 | |
67 | '0' => 0, '1' => 1, '2' => 2, '3' => 3, '4' => 4, '5' => 5, |
68 | '6' => 6, '7' => 7, '8' => 8, '9' => 9, 'a' => 10, 'b' => 11, |
69 | 'c' => 12, 'd' => 13, 'e' => 14, 'f' => 15, 'g' => 16, 'h' => 17, |
70 | 'i' => 18, 'j' => 19, 'k' => 20, 'l' => 21, 'm' => 22, 'n' => 23, |
71 | 'o' => 24, 'p' => 25, 'q' => 26, 'r' => 27, 's' => 28, 't' => 29, |
72 | 'u' => 30, 'v' => 31, 'w' => 32, 'x' => 33, 'y' => 34, 'z' => 35 |
73 | ]; |
74 | |
75 | if ( extension_loaded( 'gmp' ) && ( $engine === 'auto' || $engine === 'gmp' ) ) { |