CharUtil.toCloseByNumber增加下边界检查(pr#1421@Gitee)

This commit is contained in:
Looly
2025-12-26 16:17:15 +08:00
parent 205067fd22
commit c988de9bc8
2 changed files with 16 additions and 9 deletions

View File

@@ -419,7 +419,7 @@ public class CharUtil implements CharPool {
* @since 5.6.2
*/
public static char toCloseByNumber(final int number) {
if (number > 20) {
if (number < 1 || number > 20) {
throw new IllegalArgumentException("Number must be [1-20]");
}
return (char) ('①' + number - 1);

View File

@@ -19,7 +19,7 @@ package cn.hutool.v7.core.text;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
public class CharUtilTest {
@@ -27,7 +27,7 @@ public class CharUtilTest {
public void trimTest() {
//此字符串中的第一个字符为不可见字符: '\u202a'
final String str = "C:/Users/maple/Desktop/tone.txt";
Assertions.assertEquals('\u202a', str.charAt(0));
assertEquals('\u202a', str.charAt(0));
Assertions.assertTrue(CharUtil.isBlankChar(str.charAt(0)));
}
@@ -66,16 +66,16 @@ public class CharUtilTest {
@Test
public void toCloseCharTest(){
Assertions.assertEquals('②', CharUtil.toCloseChar('2'));
Assertions.assertEquals('Ⓜ', CharUtil.toCloseChar('M'));
Assertions.assertEquals('ⓡ', CharUtil.toCloseChar('r'));
assertEquals('②', CharUtil.toCloseChar('2'));
assertEquals('Ⓜ', CharUtil.toCloseChar('M'));
assertEquals('ⓡ', CharUtil.toCloseChar('r'));
}
@Test
public void toCloseByNumberTest(){
Assertions.assertEquals('②', CharUtil.toCloseByNumber(2));
Assertions.assertEquals('⑫', CharUtil.toCloseByNumber(12));
Assertions.assertEquals('⑳', CharUtil.toCloseByNumber(20));
assertEquals('②', CharUtil.toCloseByNumber(2));
assertEquals('⑫', CharUtil.toCloseByNumber(12));
assertEquals('⑳', CharUtil.toCloseByNumber(20));
}
@SuppressWarnings("UnnecessaryUnicodeEscape")
@@ -87,4 +87,11 @@ public class CharUtilTest {
c = '\u2800';
Assertions.assertTrue(CharUtil.isBlankChar(c));
}
@Test
public void issueIDFNHETest(){
//Console.log(CharUtil.toCloseByNumber(0)); //此时会打印"⑟"
assertThrows(IllegalArgumentException.class, () -> CharUtil.toCloseByNumber(0));
assertThrows(IllegalArgumentException.class, () -> CharUtil.toCloseByNumber(-1));
}
}