git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Dirk Gouders <dirk@gouders.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 1/2] utf8: respect bounds in utf8_strnwidth()
Date: Tue, 17 Nov 2026 18:57:08 +0100	[thread overview]
Message-ID: <ghbj7nmjd7.fsf@gouders.net> (raw)
In-Reply-To: <xmqqse0zfn2n.fsf@gitster.g>

On Tue, Nov 17 2026, Junio C Hamano wrote:

> Junio C Hamano <gitster@pobox.com> writes:
>
>> The problem is well spotted, but isn't the blame slightly misplaced?
>> ...
>> Even if display_mode_esc_sequence_len() were updated to take the
>> remaining number of bytes in string (which I think *is* a good
>> change, by the way), ...
>
> An update to display_mode_esc_sequence_len() to take the length
> limit, including its fallout, is shown below, and it does not look
> too bad.  Not even compile-tested, but it should be sufficient as an
> illustration of the idea.
>
> This does not make your "after display_mode_esc_sequence_len()
> skips, make sure if we still have remaining string to grab one UTF-8
> character" change unnecessary, as I said.

My v2 would have introduced two new functions (see attached) to replace
display_mode_esc_sequence_len():

• size_t ansi_sgr_nlen(const char *str, size_t len);
• size_t ansi_sgr_nlen_multi(const char *s, size_t len);

The latter one to help (most of all) users who want to eat *all* leading
SGR sequences so that they don't need loops.

But I saw you already posted a patch and that is also fine, of course.

Dirk

diff --git a/utf8.c b/utf8.c
index 96460cc414..1e3887480f 100644
--- a/utf8.c
+++ b/utf8.c
@@ -30,6 +30,44 @@ size_t display_mode_esc_sequence_len(const char *s)
 	return p - s;
 }
 
+size_t ansi_sgr_nlen(const char *str, size_t len)
+{
+	const char *p = str;
+	const char *p_max = str + len;
+
+	if (len < 3)
+		return 0;
+
+	if (*p++ != '\033')
+		return 0;
+	if (*p++ != '[')
+		return 0;
+
+	while (p < p_max && (isdigit(*p) || *p == ';'))
+		p++;
+
+	if (p == p_max || *p != 'm')
+		return 0;
+
+	return p - str;
+}
+
+size_t ansi_sgr_nlen_multi(const char *s, size_t len)
+{
+	size_t sgr_len = 0;
+
+	while (sgr_len < len) {
+		size_t count = ansi_sgr_nlen(s + sgr_len, len - sgr_len);
+
+		if (!count)
+			break;
+
+		sgr_len += count;
+	}
+
+	return sgr_len;
+}
+
 /* auxiliary function for binary search in interval table */
 static int bisearch(ucs_char_t ucs, const struct interval *table, int max)
 {

  reply	other threads:[~2026-11-17 17:57 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 18:56 [PATCH 0/2] utf8: two minor fixes Dirk Gouders
2026-11-16 18:57 ` [PATCH 1/2] utf8: respect bounds in utf8_strnwidth() Dirk Gouders
2026-11-17  0:02   ` Junio C Hamano
2026-11-17 15:15     ` Dirk Gouders
2026-11-17 16:18     ` Junio C Hamano
2026-11-17 17:57       ` Dirk Gouders [this message]
2026-11-17 18:11         ` Junio C Hamano
2026-11-16 18:57 ` [PATCH 2/2] utf8: fix comment for utf8_strnwidth() Dirk Gouders

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=ghbj7nmjd7.fsf@gouders.net \
    --to=dirk@gouders.net \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.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;
as well as URLs for NNTP newsgroup(s).