修复HexUtil.isHexNumber()对"-"的判断问题

This commit is contained in:
Looly 2023-01-15 11:01:28 +08:00
parent 09a2dc3807
commit a0cba2fd55
3 changed files with 11 additions and 2 deletions

View File

@ -3,10 +3,11 @@
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------
# 5.8.12.M1 (2022-12-27) # 5.8.12.M1 (2023-01-15)
### 🐣新特性 ### 🐣新特性
### 🐞Bug修复 ### 🐞Bug修复
* 【core 】 修复HexUtil.isHexNumber()对"-"的判断问题issue#2857@Github
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------

View File

@ -27,7 +27,11 @@ public class HexUtil {
* @return 是否为16进制 * @return 是否为16进制
*/ */
public static boolean isHexNumber(String value) { public static boolean isHexNumber(String value) {
int index = (value.startsWith("-") ? 1 : 0); if(StrUtil.startWith(value, '-')){
// issue#2875
return false;
}
int index = 0;
if (value.startsWith("0x", index) || value.startsWith("0X", index)) { if (value.startsWith("0x", index) || value.startsWith("0X", index)) {
index += 2; index += 2;
} else if (value.startsWith("#", index)) { } else if (value.startsWith("#", index)) {

View File

@ -49,6 +49,10 @@ public class HexUtilTest {
// 错误的 // 错误的
a = "0x0000001000T00001158e460913d00000"; a = "0x0000001000T00001158e460913d00000";
Assert.assertFalse(HexUtil.isHexNumber(a)); Assert.assertFalse(HexUtil.isHexNumber(a));
// 错误的,https://github.com/dromara/hutool/issues/2857
a = "-1";
Assert.assertFalse(HexUtil.isHexNumber(a));
} }
@Test @Test