Merge pull request #4168 from TouyamaRie/v5-dev-1128

Fix issue 4167
This commit is contained in:
Golden Looly 2025-11-30 16:47:40 +08:00 committed by GitHub
commit af97ba4084
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 78 additions and 23 deletions

View File

@ -71,29 +71,28 @@ public class SplitIter extends ComputeIter<String> implements Serializable {
return text.substring(offset);
}
final int start = finder.start(offset);
// 无分隔符结束
if (start < 0) {
// 如果不再有分隔符但是遗留了字符则单独作为一个段
if (offset <= text.length()) {
final String result = text.substring(offset);
if (false == ignoreEmpty || false == result.isEmpty()) {
// 返回非空串
offset = Integer.MAX_VALUE;
return result;
String result = null;
int start;
do {
start = finder.start(offset);
// 无分隔符结束
if (start < 0) {
// 如果不再有分隔符但是遗留了字符则单独作为一个段
if (offset <= text.length()) {
result = text.substring(offset);
if (!ignoreEmpty || !result.isEmpty()) {
// 返回非空串
offset = Integer.MAX_VALUE;
return result;
}
}
return null;
}
return null;
}
// 找到新的分隔符位置
final String result = text.substring(offset, start);
offset = finder.end(start);
if (ignoreEmpty && result.isEmpty()) {
// 发现空串且需要忽略时跳过之
return computeNext();
}
// 找到新的分隔符位置
result = text.substring(offset, start);
offset = finder.end(start);
} while (ignoreEmpty && result.isEmpty()); // 空串则继续循环
count++;
return result;

View File

@ -379,15 +379,24 @@ public class HexUtil {
* @return 格式化后的字符串
*/
public static String format(final String hexStr, String prefix) {
if (StrUtil.isEmpty(hexStr)) {
return StrUtil.EMPTY;
}
if (null == prefix) {
prefix = StrUtil.EMPTY;
}
final int length = hexStr.length();
final StringBuilder builder = StrUtil.builder(length + length / 2 + (length / 2 * prefix.length()));
builder.append(prefix).append(hexStr.charAt(0)).append(hexStr.charAt(1));
for (int i = 2; i < length - 1; i += 2) {
builder.append(CharUtil.SPACE).append(prefix).append(hexStr.charAt(i)).append(hexStr.charAt(i + 1));
for (int i = 0; i < length; i++) {
if (i % 2 == 0) {
if (i != 0) {
builder.append(CharUtil.SPACE);
}
builder.append(prefix);
}
builder.append(hexStr.charAt(i));
}
return builder.toString();
}

View File

@ -6,6 +6,8 @@ import cn.hutool.core.text.finder.PatternFinder;
import cn.hutool.core.text.finder.StrFinder;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;
@ -153,4 +155,18 @@ public class SplitIterTest {
assertEquals(1, strings.size());
});
}
@Test
public void issue4169Test() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 20000; i++) { // 1万次连续分隔符模拟递归深度风险场景
sb.append(",");
}
sb.append("test");
SplitIter iter = new SplitIter(sb.toString(), new StrFinder(",",false), 0, true);
List<String> result = iter.toList(false);
assertEquals(Collections.singletonList("test"), result);
}
}

View File

@ -116,4 +116,35 @@ public class HexUtilTest {
final String hex3 = "#FF";
assertEquals(new BigInteger("FF", 16), HexUtil.toBigInteger(hex3));
}
@Test
public void testFormatEmpty() {
String result = HexUtil.format("");
assertEquals("", result);
}
@Test
public void testFormatSingleChar() {
String result = HexUtil.format("1");
assertEquals("1", result);
}
@Test
public void testFormatOddLength() {
String result = HexUtil.format("123");
assertEquals("12 3", result);
}
@Test
public void testFormatWithPrefixSingleChar() {
String result = HexUtil.format("1", "0x");
assertEquals("0x1", result);
}
@Test
public void testFormatWithPrefixOddLength() {
String result = HexUtil.format("123", "0x");
assertEquals("0x12 0x3", result);
}
}