[pacman-dev] [PATCH v2] util.c: table_print_line: properly align texts involving CJK
For printf in C, width is counted as bytes rather than Unicode width. [1]
If the precision is specified, no more than that many bytes are written.
[1] Section 7.21.6, N2176, final draft for ISO/IEC 9899:2017 (C18) Thanks Andrew Gregory for suggesting a simpler approach. Fixes FS#59229 Signed-off-by: Chih-Hsuan Yen <yan12125@gmail.com> --- src/pacman/util.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/pacman/util.c b/src/pacman/util.c index e9187529..0c6a0c09 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -522,7 +522,9 @@ static void table_print_line(const alpm_list_t *line, short col_padding, continue; } - cell_width = (cell->mode & CELL_RIGHT_ALIGN ? (int)widths[i] : -(int)widths[i]); + /* calculate cell width, adjusting for multi-byte character strings */ + cell_width = (int)widths[i] - string_length(str) + strlen(str); + cell_width = cell->mode & CELL_RIGHT_ALIGN ? cell_width : -cell_width; if(need_padding) { printf("%*s", col_padding, ""); -- 2.28.0
On 13/9/20 6:41 pm, Chih-Hsuan Yen wrote:
For printf in C, width is counted as bytes rather than Unicode width. [1]
If the precision is specified, no more than that many bytes are written.
[1] Section 7.21.6, N2176, final draft for ISO/IEC 9899:2017 (C18)
Thanks Andrew Gregory for suggesting a simpler approach.
Fixes FS#59229
Signed-off-by: Chih-Hsuan Yen <yan12125@gmail.com> ---
Thanks - looks good to me.
src/pacman/util.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/src/pacman/util.c b/src/pacman/util.c index e9187529..0c6a0c09 100644 --- a/src/pacman/util.c +++ b/src/pacman/util.c @@ -522,7 +522,9 @@ static void table_print_line(const alpm_list_t *line, short col_padding, continue; }
- cell_width = (cell->mode & CELL_RIGHT_ALIGN ? (int)widths[i] : -(int)widths[i]); + /* calculate cell width, adjusting for multi-byte character strings */ + cell_width = (int)widths[i] - string_length(str) + strlen(str); + cell_width = cell->mode & CELL_RIGHT_ALIGN ? cell_width : -cell_width;
if(need_padding) { printf("%*s", col_padding, "");
participants (2)
-
Allan McRae
-
Chih-Hsuan Yen