mirror of
https://gitee.com/dromara/hutool.git
synced 2026-02-09 09:16:26 +08:00
fix issue IDFPGR
This commit is contained in:
@@ -92,12 +92,22 @@ public class RadixUtil {
|
||||
* @return long
|
||||
*/
|
||||
public static long decode(String radixs, String encodeStr) {
|
||||
if (radixs == null || radixs.length() < 2) {
|
||||
throw new IllegalArgumentException("radixs must contain at least 2 characters");
|
||||
}
|
||||
if (encodeStr == null || encodeStr.isEmpty()) {
|
||||
throw new IllegalArgumentException("encodeStr is null or empty");
|
||||
}
|
||||
//目标是多少进制
|
||||
int rl = radixs.length();
|
||||
long res = 0L;
|
||||
|
||||
for (char c : encodeStr.toCharArray()) {
|
||||
res = res * rl + radixs.indexOf(c);
|
||||
int idx = radixs.indexOf(c);
|
||||
if (idx < 0) {
|
||||
throw new IllegalArgumentException("Illegal character '" + c + "' for radixs");
|
||||
}
|
||||
res = res * rl + idx;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package cn.hutool.core.util;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
public class RadixUtilTest {
|
||||
@Test
|
||||
public void issueTest() {
|
||||
String radixs = "0123456789ABC"; // base 13
|
||||
String bad = "1X3"; // 'X' 不在 radix 中
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
RadixUtil.decode(radixs, bad);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user