Skip to content

Commit a5d5c50

Browse files
committed
Merge branch 'jx/repo-struct-utf8width-fix'
The "git repo structure" subcommand tried to align its output but mixed up byte count and display column width, which has been corrected. * jx/repo-struct-utf8width-fix: builtin/repo: fix table alignment for UTF-8 characters t/unit-tests: add UTF-8 width tests for CJK chars
2 parents 861312b + 7a03a10 commit a5d5c50

File tree

4 files changed

+153
-4
lines changed

4 files changed

+153
-4
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,7 @@ CLAR_TEST_SUITES += u-string-list
15251525
CLAR_TEST_SUITES += u-strvec
15261526
CLAR_TEST_SUITES += u-trailer
15271527
CLAR_TEST_SUITES += u-urlmatch-normalization
1528+
CLAR_TEST_SUITES += u-utf8-width
15281529
CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
15291530
CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
15301531
CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o

builtin/repo.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,14 +292,20 @@ static void stats_table_print_structure(const struct stats_table *table)
292292
int name_col_width = utf8_strwidth(name_col_title);
293293
int value_col_width = utf8_strwidth(value_col_title);
294294
struct string_list_item *item;
295+
struct strbuf buf = STRBUF_INIT;
295296

296297
if (table->name_col_width > name_col_width)
297298
name_col_width = table->name_col_width;
298299
if (table->value_col_width > value_col_width)
299300
value_col_width = table->value_col_width;
300301

301-
printf("| %-*s | %-*s |\n", name_col_width, name_col_title,
302-
value_col_width, value_col_title);
302+
strbuf_addstr(&buf, "| ");
303+
strbuf_utf8_align(&buf, ALIGN_LEFT, name_col_width, name_col_title);
304+
strbuf_addstr(&buf, " | ");
305+
strbuf_utf8_align(&buf, ALIGN_LEFT, value_col_width, value_col_title);
306+
strbuf_addstr(&buf, " |");
307+
printf("%s\n", buf.buf);
308+
303309
printf("| ");
304310
for (int i = 0; i < name_col_width; i++)
305311
putchar('-');
@@ -317,9 +323,16 @@ static void stats_table_print_structure(const struct stats_table *table)
317323
value = entry->value;
318324
}
319325

320-
printf("| %-*s | %*s |\n", name_col_width, item->string,
321-
value_col_width, value);
326+
strbuf_reset(&buf);
327+
strbuf_addstr(&buf, "| ");
328+
strbuf_utf8_align(&buf, ALIGN_LEFT, name_col_width, item->string);
329+
strbuf_addstr(&buf, " | ");
330+
strbuf_utf8_align(&buf, ALIGN_RIGHT, value_col_width, value);
331+
strbuf_addstr(&buf, " |");
332+
printf("%s\n", buf.buf);
322333
}
334+
335+
strbuf_release(&buf);
323336
}
324337

325338
static void stats_table_clear(struct stats_table *table)

t/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ clar_test_suites = [
2424
'unit-tests/u-strvec.c',
2525
'unit-tests/u-trailer.c',
2626
'unit-tests/u-urlmatch-normalization.c',
27+
'unit-tests/u-utf8-width.c',
2728
]
2829

2930
clar_sources = [

t/unit-tests/u-utf8-width.c

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#include "unit-test.h"
2+
#include "utf8.h"
3+
#include "strbuf.h"
4+
5+
/*
6+
* Test utf8_strnwidth with various Chinese strings
7+
* Chinese characters typically have a width of 2 columns when displayed
8+
*/
9+
void test_utf8_width__strnwidth_chinese(void)
10+
{
11+
const char *str;
12+
13+
/* Test basic ASCII - each character should have width 1 */
14+
cl_assert_equal_i(5, utf8_strnwidth("Hello", 5, 0));
15+
/* skip_ansi = 1 */
16+
cl_assert_equal_i(5, utf8_strnwidth("Hello", 5, 1));
17+
18+
/* Test simple Chinese characters - each should have width 2 */
19+
/* "你好" is 6 bytes (3 bytes per char in UTF-8), 4 display columns */
20+
cl_assert_equal_i(4, utf8_strnwidth("你好", 6, 0));
21+
22+
/* Test mixed ASCII and Chinese - ASCII = 1 column, Chinese = 2 columns */
23+
/* "h"(1) + "i"(1) + "你"(2) + "好"(2) = 6 */
24+
cl_assert_equal_i(6, utf8_strnwidth("Hi你好", 8, 0));
25+
26+
/* Test longer Chinese string */
27+
/* 5 Chinese chars = 10 display columns */
28+
cl_assert_equal_i(10, utf8_strnwidth("你好世界!", 15, 0));
29+
30+
/* Test individual Chinese character width */
31+
cl_assert_equal_i(2, utf8_strnwidth("中", 3, 0));
32+
33+
/* Test empty string */
34+
cl_assert_equal_i(0, utf8_strnwidth("", 0, 0));
35+
36+
/* Test length limiting */
37+
str = "你好世界";
38+
/* Only first char "你"(2 columns) within 3 bytes */
39+
cl_assert_equal_i(2, utf8_strnwidth(str, 3, 0));
40+
/* First two chars "你好"(4 columns) in 6 bytes */
41+
cl_assert_equal_i(4, utf8_strnwidth(str, 6, 0));
42+
}
43+
44+
/*
45+
* Tests for utf8_strwidth (simpler version without length limit)
46+
*/
47+
void test_utf8_width__strwidth_chinese(void)
48+
{
49+
/* Test basic ASCII */
50+
cl_assert_equal_i(5, utf8_strwidth("Hello"));
51+
52+
/* Test Chinese characters */
53+
/* 2 Chinese chars = 4 display columns */
54+
cl_assert_equal_i(4, utf8_strwidth("你好"));
55+
56+
/* Test longer Chinese string */
57+
/* 5 Chinese chars = 10 display columns */
58+
cl_assert_equal_i(10, utf8_strwidth("你好世界!"));
59+
60+
/* Test mixed ASCII and Chinese */
61+
/* 5 ASCII (5 cols) + 2 Chinese (4 cols) = 9 */
62+
cl_assert_equal_i(9, utf8_strwidth("Hello世界"));
63+
/* 2 ASCII (2 cols) + 2 Chinese (4 cols) + 1 ASCII (1 col) = 7 */
64+
cl_assert_equal_i(7, utf8_strwidth("Hi世界!"));
65+
}
66+
67+
/*
68+
* Additional tests with other East Asian characters
69+
*/
70+
void test_utf8_width__strnwidth_japanese_korean(void)
71+
{
72+
/* Japanese characters (should also be 2 columns each) */
73+
/* 5 Japanese chars x 2 cols each = 10 display columns */
74+
cl_assert_equal_i(10, utf8_strnwidth("こんにちは", 15, 0));
75+
76+
/* Korean characters (should also be 2 columns each) */
77+
/* 5 Korean chars x 2 cols each = 10 display columns */
78+
cl_assert_equal_i(10, utf8_strnwidth("안녕하세요", 15, 0));
79+
}
80+
81+
/*
82+
* Test utf8_strnwidth with CJK strings and ANSI sequences
83+
*/
84+
void test_utf8_width__strnwidth_cjk_with_ansi(void)
85+
{
86+
/* Test CJK with ANSI sequences */
87+
const char *ansi_test = "\033[1m你好\033[0m";
88+
int width = utf8_strnwidth(ansi_test, strlen(ansi_test), 1);
89+
/* Should skip ANSI sequences and count "你好" as 4 columns */
90+
cl_assert_equal_i(4, width);
91+
92+
/* Test mixed ASCII, CJK, and ANSI */
93+
ansi_test = "Hello\033[32m世界\033[0m!";
94+
width = utf8_strnwidth(ansi_test, strlen(ansi_test), 1);
95+
/* "Hello"(5) + "世界"(4) + "!"(1) = 10 */
96+
cl_assert_equal_i(10, width);
97+
}
98+
99+
/*
100+
* Test the strbuf_utf8_align function with CJK characters
101+
*/
102+
void test_utf8_width__strbuf_utf8_align(void)
103+
{
104+
struct strbuf buf = STRBUF_INIT;
105+
106+
/* Test left alignment with CJK */
107+
strbuf_utf8_align(&buf, ALIGN_LEFT, 10, "你好");
108+
/* Since "你好" is 4 display columns, we need 6 more spaces to reach 10 */
109+
cl_assert_equal_s("你好 ", buf.buf);
110+
strbuf_reset(&buf);
111+
112+
/* Test right alignment with CJK */
113+
strbuf_utf8_align(&buf, ALIGN_RIGHT, 8, "世界");
114+
/* "世界" is 4 display columns, so we need 4 leading spaces */
115+
cl_assert_equal_s(" 世界", buf.buf);
116+
strbuf_reset(&buf);
117+
118+
/* Test center alignment with CJK */
119+
strbuf_utf8_align(&buf, ALIGN_MIDDLE, 10, "中");
120+
/* "中" is 2 display columns, so (10-2)/2 = 4 spaces on left, 4 on right */
121+
cl_assert_equal_s(" 中 ", buf.buf);
122+
strbuf_reset(&buf);
123+
124+
strbuf_utf8_align(&buf, ALIGN_MIDDLE, 5, "中");
125+
/* "中" is 2 display columns, so (5-2)/2 = 1 spaces on left, 2 on right */
126+
cl_assert_equal_s(" 中 ", buf.buf);
127+
strbuf_reset(&buf);
128+
129+
/* Test alignment that is smaller than string width */
130+
strbuf_utf8_align(&buf, ALIGN_LEFT, 2, "你好");
131+
/* Since "你好" is 4 display columns, it should not be truncated */
132+
cl_assert_equal_s("你好", buf.buf);
133+
strbuf_release(&buf);
134+
}

0 commit comments

Comments
 (0)