Skip to content

Commit c26901a

Browse files
committed
Merge branch 'gt/maint-1.6.1-utf8-width' into maint-1.6.1
* gt/maint-1.6.1-utf8-width: builtin-blame.c: Use utf8_strwidth for author's names utf8: add utf8_strwidth()
2 parents 2f5bfa7 + ffaf9cc commit c26901a

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

builtin-blame.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "string-list.h"
2020
#include "mailmap.h"
2121
#include "parse-options.h"
22+
#include "utf8.h"
2223

2324
static char blame_usage[] = "git blame [options] [rev-opts] [rev] [--] file";
2425

@@ -1618,13 +1619,14 @@ static void emit_other(struct scoreboard *sb, struct blame_entry *ent, int opt)
16181619
printf(" %*d", max_orig_digits,
16191620
ent->s_lno + 1 + cnt);
16201621

1621-
if (!(opt & OUTPUT_NO_AUTHOR))
1622-
printf(" (%-*.*s %10s",
1623-
longest_author, longest_author,
1624-
ci.author,
1622+
if (!(opt & OUTPUT_NO_AUTHOR)) {
1623+
int pad = longest_author - utf8_strwidth(ci.author);
1624+
printf(" (%s%*s %10s",
1625+
ci.author, pad, "",
16251626
format_time(ci.author_time,
16261627
ci.author_tz,
16271628
show_raw_time));
1629+
}
16281630
printf(" %*d) ",
16291631
max_digits, ent->lno + 1 + cnt);
16301632
}
@@ -1755,7 +1757,7 @@ static void find_alignment(struct scoreboard *sb, int *option)
17551757
if (!(suspect->commit->object.flags & METAINFO_SHOWN)) {
17561758
suspect->commit->object.flags |= METAINFO_SHOWN;
17571759
get_commit_info(suspect->commit, &ci, 1);
1758-
num = strlen(ci.author);
1760+
num = utf8_strwidth(ci.author);
17591761
if (longest_author < num)
17601762
longest_author = num;
17611763
}

utf8.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,25 @@ int utf8_width(const char **start, size_t *remainder_p)
246246
return git_wcwidth(ch);
247247
}
248248

249+
/*
250+
* Returns the total number of columns required by a null-terminated
251+
* string, assuming that the string is utf8. Returns strlen() instead
252+
* if the string does not look like a valid utf8 string.
253+
*/
254+
int utf8_strwidth(const char *string)
255+
{
256+
int width = 0;
257+
const char *orig = string;
258+
259+
while (1) {
260+
if (!string)
261+
return strlen(orig);
262+
if (!*string)
263+
return width;
264+
width += utf8_width(&string, NULL);
265+
}
266+
}
267+
249268
int is_utf8(const char *text)
250269
{
251270
while (*text) {

utf8.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ typedef unsigned int ucs_char_t; /* assuming 32bit int */
55

66
ucs_char_t pick_one_utf8_char(const char **start, size_t *remainder_p);
77
int utf8_width(const char **start, size_t *remainder_p);
8+
int utf8_strwidth(const char *string);
89
int is_utf8(const char *text);
910
int is_encoding_utf8(const char *name);
1011

0 commit comments

Comments
 (0)