All of lore.kernel.org
 help / color / mirror / Atom feed
From: Hardik Kumar <hardikxk@gmail.com>
To: git@vger.kernel.org
Cc: Hardik Kumar <hardikxk@gmail.com>
Subject: [PATCH] change utf8_strwidth() return type to size_t
Date: Sun, 26 Jul 2026 18:04:27 +0530	[thread overview]
Message-ID: <20260726123427.173877-1-hardikxk@gmail.com> (raw)

The patch changes the return types of `utf8_strwidth()` and
`utf8_strnwidth()` to `size_t` (implementing a //TODO). Both functions
have been updated in the header file also.

Signed-off-by: Hardik Kumar <hardikxk@gmail.com>
---
 utf8.c | 13 ++++---------
 utf8.h |  4 ++--
 2 files changed, 6 insertions(+), 11 deletions(-)

diff --git a/utf8.c b/utf8.c
index 96460cc..1081573 100644
--- a/utf8.c
+++ b/utf8.c
@@ -208,7 +208,7 @@ int utf8_width(const char **start, size_t *remainder_p)
  * string, assuming that the string is utf8.  Returns strlen() instead
  * if the string does not look like a valid utf8 string.
  */
-int utf8_strnwidth(const char *string, size_t len, int skip_ansi)
+size_t utf8_strnwidth(const char *string, size_t len, int skip_ansi)
 {
 	const char *orig = string;
 	size_t width = 0;
@@ -225,15 +225,10 @@ int utf8_strnwidth(const char *string, size_t len, int skip_ansi)
 		if (glyph_width > 0)
 			width += glyph_width;
 	}
-
-	/*
-	 * TODO: fix the interface of this function and `utf8_strwidth()` to
-	 * return `size_t` instead of `int`.
-	 */
-	return cast_size_t_to_int(string ? width : len);
+	return (string) ? width : len;
 }
 
-int utf8_strwidth(const char *string)
+size_t utf8_strwidth(const char *string)
 {
 	return utf8_strnwidth(string, strlen(string), 0);
 }
@@ -821,7 +816,7 @@ void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int wid
 		       const char *s)
 {
 	size_t slen = strlen(s);
-	int display_len = utf8_strnwidth(s, slen, 0);
+	size_t display_len = utf8_strnwidth(s, slen, 0);
 	int utf8_compensation = slen - display_len;
 
 	if (display_len >= width) {
diff --git a/utf8.h b/utf8.h
index cf8ecb0..531e968 100644
--- a/utf8.h
+++ b/utf8.h
@@ -7,8 +7,8 @@ typedef unsigned int ucs_char_t;  /* assuming 32bit int */
 
 size_t display_mode_esc_sequence_len(const char *s);
 int utf8_width(const char **start, size_t *remainder_p);
-int utf8_strnwidth(const char *string, size_t len, int skip_ansi);
-int utf8_strwidth(const char *string);
+size_t utf8_strnwidth(const char *string, size_t len, int skip_ansi);
+size_t utf8_strwidth(const char *string);
 int is_utf8(const char *text);
 int is_encoding_utf8(const char *name);
 int same_encoding(const char *, const char *);

base-commit: 9a0c4701dcd5725c4184599322b52933ff5005ca
-- 
2.55.0


             reply	other threads:[~2026-07-26 12:34 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-26 12:34 Hardik Kumar [this message]
2026-07-26 13:41 ` [PATCH] change utf8_strwidth() return type to size_t 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
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=20260726123427.173877-1-hardikxk@gmail.com \
    --to=hardikxk@gmail.com \
    --cc=git@vger.kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.