Decodes the input string for an integer value. There is an optional sign: - or +. Then the number may be prefixed by: 0x, 0X, or # for hexadecimal, 0 for octal. If there is no prefix then the number is treated as decimal. @param __s The input string
(String __s)
| 182 | * @since 2018/11/11 |
| 183 | */ |
| 184 | public static Integer decode(String __s) |
| 185 | throws NullPointerException, NumberFormatException |
| 186 | { |
| 187 | if (__s == null) |
| 188 | throw new NullPointerException("NARG"); |
| 189 | |
| 190 | // {@squirreljme.error ZZ2o Cannot decode an empty string.} |
| 191 | if (__s.isEmpty()) |
| 192 | throw new NumberFormatException("ZZ2o"); |
| 193 | |
| 194 | // It may be changed! |
| 195 | String orig = __s; |
| 196 | |
| 197 | // Check for sign, assume positive otherwise |
| 198 | char sign = __s.charAt(0); |
| 199 | if (sign != '-' && sign != '+') |
| 200 | sign = '+'; |
| 201 | |
| 202 | // Remove the sign |
| 203 | else |
| 204 | __s = __s.substring(1); |
| 205 | |
| 206 | // Which number format? |
| 207 | int radix; |
| 208 | if (__s.startsWith("0x") || __s.startsWith("0X")) |
| 209 | { |
| 210 | radix = 16; |
| 211 | __s = __s.substring(2); |
| 212 | } |
| 213 | else if (__s.startsWith("#")) |
| 214 | { |
| 215 | radix = 16; |
| 216 | __s = __s.substring(1); |
| 217 | } |
| 218 | else if (__s.startsWith("0")) |
| 219 | { |
| 220 | radix = 8; |
| 221 | __s = __s.substring(1); |
| 222 | } |
| 223 | else |
| 224 | radix = 10; |
| 225 | |
| 226 | // {@squirreljme.error ZZ2o Misplaced sign. (The input string)} |
| 227 | if (__s.startsWith("-") || __s.startsWith("+")) |
| 228 | throw new NumberFormatException("ZZ2p " + orig); |
| 229 | |
| 230 | // Decode value with radix |
| 231 | try |
| 232 | { |
| 233 | return Integer.parseInt(sign + __s, radix); |
| 234 | } |
| 235 | |
| 236 | // {@squirreljme.error ZZ2q Could not parse number. (The input string)} |
| 237 | catch (NumberFormatException e) |
| 238 | { |
| 239 | RuntimeException t = new NumberFormatException("ZZ2q " + orig); |
| 240 | t.initCause(e); |
| 241 | throw t; |