fix issue IDFPGR

This commit is contained in:
shad0wm00n
2025-12-25 10:42:51 +08:00
parent de93fa7670
commit 606d212de1
2 changed files with 26 additions and 1 deletions

View File

@@ -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;
}

View File

@@ -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);
});
}
}