Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions hutool-core/src/main/java/cn/hutool/core/text/split/SplitIter.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,28 @@ protected String computeNext() {
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;
Expand Down
15 changes: 12 additions & 3 deletions hutool-core/src/main/java/cn/hutool/core/util/HexUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,15 +379,24 @@ public static String format(final String hexStr) {
* @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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
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;

Expand Down Expand Up @@ -153,4 +155,18 @@ public void splitByEmptyTest(){
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);
}
}
31 changes: 31 additions & 0 deletions hutool-core/src/test/java/cn/hutool/core/util/HexUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,35 @@ public void toBigIntegerTest() {
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);
}

}