优化测试
This commit is contained in:
parent
cc4a8a628e
commit
32d51fdd61
|
|
@ -117,12 +117,12 @@ public class SapMetaPrintTest {
|
|||
// 计算每列最大宽度
|
||||
int[] maxWidths = new int[headers.size()];
|
||||
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 (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("|");
|
||||
for (int i = 0; i < widths.length; 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();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue