优化测试

This commit is contained in:
曹鹏飞 2025-08-08 14:03:48 +08:00
parent cc4a8a628e
commit 32d51fdd61
1 changed files with 23 additions and 3 deletions

View File

@ -117,12 +117,12 @@ public class SapMetaPrintTest {
// 计算每列最大宽度 // 计算每列最大宽度
int[] maxWidths = new int[headers.size()]; int[] maxWidths = new int[headers.size()];
for (int i = 0; i < headers.size(); i++) { for (int i = 0; i < headers.size(); i++) {
maxWidths[i] = headers.get(i).length(); maxWidths[i] = getWeightedLength(headers.get(i));
} }
for (String[] row : rows) { for (String[] row : rows) {
for (int i = 0; i < row.length && i < maxWidths.length; i++) { for (int i = 0; i < row.length && i < maxWidths.length; i++) {
maxWidths[i] = Math.max(maxWidths[i], row[i].length()); maxWidths[i] = Math.max(maxWidths[i], getWeightedLength(row[i]));
} }
} }
@ -152,9 +152,29 @@ public class SapMetaPrintTest {
System.out.print("|"); System.out.print("|");
for (int i = 0; i < widths.length; i++) { for (int i = 0; i < widths.length; i++) {
String value = i < row.length ? row[i] : ""; String value = i < row.length ? row[i] : "";
System.out.printf(" %-" + widths[i] + "s |", value); System.out.printf(" %-" + (widths[i] - getChineseNum(value) / 2) + "s |", value);
} }
System.out.println(); System.out.println();
} }
private static final String CHINESE_REGEX = "[\\u4e00-\\u9fff]";
public static int getWeightedLength(String str) {
if (str == null || str.isEmpty()) {
return 0;
}
return str.chars()
.map(c -> String.valueOf((char) c).matches(CHINESE_REGEX) ? 2 : 1)
.sum();
}
public static int getChineseNum(String str) {
if (str == null || str.isEmpty()) {
return 0;
}
return str.chars()
.map(c -> String.valueOf((char) c).matches(CHINESE_REGEX) ? 1 : 0)
.sum();
}
} }
} }