将字符串转换为长整型数字 @param s 数字字符串 @param radix 进制数 @return
(String s, int radix)
| 111 | * @return |
| 112 | */ |
| 113 | private static long toNumber(String s, int radix) { |
| 114 | if (s == null) { |
| 115 | throw new NumberFormatException("null"); |
| 116 | } |
| 117 | |
| 118 | if (radix < MIN_RADIX) { |
| 119 | throw new NumberFormatException("radix " + radix |
| 120 | + " less than Numbers.MIN_RADIX"); |
| 121 | } |
| 122 | if (radix > MAX_RADIX) { |
| 123 | throw new NumberFormatException("radix " + radix |
| 124 | + " greater than Numbers.MAX_RADIX"); |
| 125 | } |
| 126 | |
| 127 | long result = 0; |
| 128 | boolean negative = false; |
| 129 | int i = 0, len = s.length(); |
| 130 | long limit = -Long.MAX_VALUE; |
| 131 | long multmin; |
| 132 | Integer digit; |
| 133 | |
| 134 | if (len > 0) { |
| 135 | char firstChar = s.charAt(0); |
| 136 | if (firstChar < '0') { |
| 137 | if (firstChar == '-') { |
| 138 | negative = true; |
| 139 | limit = Long.MIN_VALUE; |
| 140 | } else if (firstChar != '+') |
| 141 | throw forInputString(s); |
| 142 | |
| 143 | if (len == 1) { |
| 144 | throw forInputString(s); |
| 145 | } |
| 146 | i++; |
| 147 | } |
| 148 | multmin = limit / radix; |
| 149 | while (i < len) { |
| 150 | digit = digitMap.get(s.charAt(i++)); |
| 151 | if (digit == null) { |
| 152 | throw forInputString(s); |
| 153 | } |
| 154 | if (digit < 0) { |
| 155 | throw forInputString(s); |
| 156 | } |
| 157 | if (result < multmin) { |
| 158 | throw forInputString(s); |
| 159 | } |
| 160 | result *= radix; |
| 161 | if (result < limit + digit) { |
| 162 | throw forInputString(s); |
| 163 | } |
| 164 | result -= digit; |
| 165 | } |
| 166 | } else { |
| 167 | throw forInputString(s); |
| 168 | } |
| 169 | return negative ? result : -result; |
| 170 | } |
nothing calls this directly
no test coverage detected