From: "Hardik Kumar" <hardikxk@gmail.com>
To: "Pablo Sabater" <pabloosabaterr@gmail.com>,
"Hardik Kumar" <hardikxk@gmail.com>, <git@vger.kernel.org>
Cc: <l.s.r@web.de>, <gitster@pobox.com>
Subject: Re: [PATCH v2] utf8: use size_t for string width methods and callee sites.
Date: Mon, 27 Jul 2026 12:10:05 +0530 [thread overview]
Message-ID: <DK95C0SCPDX3.28ORSCO088KJ9@gmail.com> (raw)
In-Reply-To: <DK8Y8F4650AW.1XN921ROZW70F@gmail.com>
On Mon Jul 27, 2026 at 6:36 AM IST, Pablo Sabater wrote:
>> printf(" (%s%*s %10s",
>> name, pad, "",
>> format_time(ci.author_time,
>> @@ -668,7 +668,7 @@ static void find_alignment(struct blame_scoreboard *sb, int *option)
>>
>> for (e = sb->ent; e; e = e->next) {
>> struct blame_origin *suspect = e->suspect;
>> - int num;
>> + size_t num;
>
> Looking at how num is used, it is reused for multiple things:
> - strlen()
> - utf8_strwidth()
> - line-number sums
>
> The longest_* variables we compare num against are still int.
>
> Can we split num into different variables?
>
I think it would be better to just cast the return of `utf8_strwidth` to
int instead when assigning it num.
>>
>> skip_prefix(it->refname, "refs/heads/", &desc);
>> skip_prefix(it->refname, "refs/remotes/", &desc);
>> diff --git a/builtin/repo.c b/builtin/repo.c
>> index 84e012f..47b9191 100644
>> --- a/builtin/repo.c
>> +++ b/builtin/repo.c
>> @@ -367,7 +367,7 @@ static void stats_table_vaddf(struct stats_table *table,
>> struct strbuf buf = STRBUF_INIT;
>> struct string_list_item *item;
>> char *formatted_name;
>> - int name_width;
>> + size_t name_width;
> Same as above:
>
> if (name_width > table->name_col_width)
>
> I think that these three fields can be promoted safely
>
> struct stats_table {
> [snip]
>
> int name_col_width;
> int value_col_width;
> int unit_col_width;
> };
>
> but check every use of them afterwards for code that still expects an
> int.
Changes to the struct field types might generate more signed unsigned
warnings leading to changes to fix things which might just not be
necessary for this. There probably won't be a use case requiring a very
high number for col_width.
>>
>> strbuf_vaddf(&buf, format, ap);
>> formatted_name = strbuf_detach(&buf, NULL);
>> @@ -387,12 +387,12 @@ static void stats_table_vaddf(struct stats_table *table,
>> string_list_append_nodup(&table->annotations, strbuf_detach(&buf, NULL));
>> }
>> if (entry->value) {
>> - int value_width = utf8_strwidth(entry->value);
>> + size_t value_width = utf8_strwidth(entry->value);
>
> I feel this one is partially my fault, I wrote these as example output
> of the grep I sent last reroll. But they still need to be checked:
>
>> if (value_width > table->value_col_width)
>
> We are comparing size_t > int.
>
>> table->value_col_width = value_width;
>
> We are narrowing size_t to int.
>
>> }
>> if (entry->unit) {
>> - int unit_width = utf8_strwidth(entry->unit);
>> + size_t unit_width = utf8_strwidth(entry->unit);
>> if (unit_width > table->unit_col_width)
>> table->unit_col_width = unit_width;
>> }
>> @@ -582,8 +582,8 @@ static void stats_table_print_structure(const struct stats_table *table)
>> {
>> const char *name_col_title = _("Repository structure");
>> const char *value_col_title = _("Value");
>> - int title_name_width = utf8_strwidth(name_col_title);
>> - int title_value_width = utf8_strwidth(value_col_title);
>> + size_t title_name_width = utf8_strwidth(name_col_title);
>> + size_t title_value_width = utf8_strwidth(value_col_title);
>
> Same problem, these are compared against int *_col_width locals,
> and:
> value_col_width = title_value_width - unit_col_width
>
> below the context now mixes size_t and int. Promoting the struct fields
> as suggested above fixes all of this at once.
Otherwise keeping these as int like before and casting the returns where
necessary also works in this case.
>> else
>> width = options->stat_width ? options->stat_width : 80;
>> number_width = decimal_width(max_change) > number_width ?
>> @@ -3123,7 +3124,7 @@ static void show_stats(struct diffstat_t *data, struct diff_options *options)
>> if (slash)
>> name = slash;
>> }
>> - padding = len - utf8_strwidth(name);
>> + padding = len - cast_size_t_to_int(utf8_strwidth(name));
>> if (padding < 0)
>> padding = 0;
>
> The cast doesn't work here because len is also size_t. We could do this
> to be sure that there will be no problems:
>
> size_t name_disp = utf8_strwidth(name);
> if (name_disp > len)
> padding = 0;
> else
> padding = cast_size_t_to_int(len - name_disp);
>
Changing len back to int resolves this as well as the previous one.
>>
>> diff --git a/gettext.c b/gettext.c
>> index 8d08a61..4d5d05e 100644
>> --- a/gettext.c
>> +++ b/gettext.c
>> @@ -129,7 +129,7 @@ void git_setup_gettext(void)
>> }
>>
>> /* return the number of columns of string 's' in current locale */
>> -int gettext_width(const char *s)
>> +size_t gettext_width(const char *s)
>> {
>> static int is_utf8 = -1;
>> if (is_utf8 == -1)
>> diff --git a/gettext.h b/gettext.h
>> index 484cafa..f161a21 100644
>> --- a/gettext.h
>> +++ b/gettext.h
>> @@ -31,7 +31,7 @@
>> #ifndef NO_GETTEXT
>> extern int git_gettext_enabled;
>> void git_setup_gettext(void);
>> -int gettext_width(const char *s);
>> +size_t gettext_width(const char *s);
>
> Careful, this is inside an #ifndef, if we change the signature here,
> the other branch must follow.
The implmentations returns either a `utf8_strwidth()` or `strlent()`
both of which would return a `size_t`. The function is only called in a
single place so I suppose casting it back to int where its called would
be better.
Thanks,
Hardik.
next prev parent reply other threads:[~2026-07-27 6:40 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-07-26 12:34 [PATCH] change utf8_strwidth() return type to size_t Hardik Kumar
2026-07-26 13:41 ` René Scharfe
2026-07-26 15:50 ` Hardik Kumar
2026-07-26 14:52 ` Pablo Sabater
2026-07-26 15:52 ` Hardik Kumar
2026-07-26 19:57 ` [PATCH v2] utf8: use size_t for string width methods and callee sites Hardik Kumar
2026-07-27 0:06 ` Junio C Hamano
2026-07-27 4:02 ` Junio C Hamano
2026-07-27 6:42 ` Hardik Kumar
2026-07-27 1:06 ` Pablo Sabater
2026-07-27 6:40 ` Hardik Kumar [this message]
2026-07-27 6:59 ` [PATCH v3] utf8: make utf8_strwidth() and utf8_strnwidth() return size_t Hardik Kumar
2026-07-27 7:04 ` Hardik Kumar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DK95C0SCPDX3.28ORSCO088KJ9@gmail.com \
--to=hardikxk@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=l.s.r@web.de \
--cc=pabloosabaterr@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox