git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/2] utf8: make utf8_strnwidth & utf8_strwidth return size_t instead of int

From
Mohit Marathe via GitGitGadget <gitgitgadget@gmail.com>
Date
Mar 17, 2024, 08:27 UTC
Message-ID
<615481efd70fbd7e8950bea3edca12e43a024cd8.1710664071.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1690.git.1710664071.gitgitgadget@gmail.com>
From: Mohit Marathe <mohitmarathe@proton.me>

This patch addresses the TODO comment of changing the return types of these functions from int to size_t.

Signed-off-by: Mohit Marathe <mohitmarathe@proton.me>
---
 utf8.c | 10 +++-------
 utf8.h |  4 ++--
 2 files changed, 5 insertions(+), 9 deletions(-)
diff --git a/utf8.c b/utf8.c
index 8ccdf684e07..050fc8b3cdf 100644
--- a/utf8.c
+++ b/utf8.c
@@ -206,7 +206,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;
@@ -224,14 +224,10 @@ int utf8_strnwidth(const char *string, size_t len, int skip_ansi)
 			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);
 }
diff --git a/utf8.h b/utf8.h
index 35df76086a6..cae10d5ac1f 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 *);
-- 
gitgitgadget
Previous: Patrick SteinhardtNext: Junio C Hamano
Message 4 of 6 in “utf8: change return type of some helpers from int to size_t”
  1. 0/2 utf8: change return type of some helpers from int to size_tMohit Marathe via GitGitGadget, Mar 17, 2024
  2. 1/2 utf8: change type from int to size_tMohit Marathe via GitGitGadget, Mar 17, 2024
  3. Patrick SteinhardtMar 21, 2024
  4. 2/2 utf8: make utf8_strnwidth & utf8_strwidth return size_t instead of intMohit Marathe via GitGitGadget, Mar 17, 2024
  5. Junio C HamanoMar 17, 2024
  6. Patrick SteinhardtMar 21, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.