Skip to content

Commit f85b49f

Browse files
LorenzoPegorarigitster
authored andcommitted
diff: improve scaling of filenames in diffstat to handle UTF-8 chars
The `show_stats()` function tries to scale the filenames in the diffstat to ensure they don't exceed the given `name-width`. It does so by calculating the "display width" of the characters to be dropped, but then advances the filename pointer by that number of bytes. However, the "display width" of a character is not always equal to its byte count. The result is that sometimes, when displaying UTF-8 characters, filenames exceed the given `name-width`, and frequently the bytes of the UTF-8 characters are truncated. The following is an example of the issue, where the 2 files are "HelloHi" and "Hello你好", and `name-width=6`: ...oHi | 0 ...<BD><A0>好 | 0 Make the filename pointer move by the actual number of bytes of the characters to drop from the filename, rather than their display width, using the `utf8_width()` function. Force `len` to not be less than 0 (this happens if the given `name-width` is 2 or less), otherwise an infinite loop is entered. Signed-off-by: LorenzoPegorari <lorenzo.pegorari2002@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 9a2fb14 commit f85b49f

1 file changed

Lines changed: 6 additions & 11 deletions

File tree

diff.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2823,17 +2823,12 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
28232823
char *slash;
28242824
prefix = "...";
28252825
len -= 3;
2826-
/*
2827-
* NEEDSWORK: (name_len - len) counts the display
2828-
* width, which would be shorter than the byte
2829-
* length of the corresponding substring.
2830-
* Advancing "name" by that number of bytes does
2831-
* *NOT* skip over that many columns, so it is
2832-
* very likely that chomping the pathname at the
2833-
* slash we will find starting from "name" will
2834-
* leave the resulting string still too long.
2835-
*/
2836-
name += name_len - len;
2826+
if (len < 0)
2827+
len = 0;
2828+
2829+
while (name_len > len)
2830+
name_len -= utf8_width((const char**)&name, NULL);
2831+
28372832
slash = strchr(name, '/');
28382833
if (slash)
28392834
name = slash;

0 commit comments

Comments
 (0)