Git development
 help / color / mirror / Atom feed
From: Dirk Gouders <dirk@gouders.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org, Andy Koppe <andy.koppe@gmail.com>
Subject: Re: [PATCH v2 1/4] utf8: add display_mode_esc_sequence_len_counted()
Date: Thu, 19 Nov 2026 23:59:12 +0100	[thread overview]
Message-ID: <gh7bi81l8f.fsf@gouders.net> (raw)
In-Reply-To: <ghbj7k1mg5.fsf@gouders.net>

On Thu, Nov 19 2026, Dirk Gouders wrote:

> On Thu, Nov 19 2026, Andy Koppe wrote:
>
>> Dirk Gouders wrote:
>>> +/* Same as above, but for a NUL-terminated string "s". */
>>> +size_t display_mode_esc_sequence_len(const char *s)
>>> +{
>>> +       return display_mode_esc_sequence_len_counted(s, strlen(s));
>>> +}
>>
>> This is a performance regression compared to the original function.
>> Calls that previously returned immediately because *s != '\033', or
>> after a few bytes of scanning, now scan the entire string with strlen.
>> It turns scanning a whole string for SGRs with repeated calls to the
>> function from linear to quadratic.
>
> How about creating a v3 for display_mode_esc_sequence_len() similar to
> utf8_width() to accept an optional remainder argument?
>
> If NULL is given, display_mode_esc_sequence_len() assumes a
> null-terminated string, otherwise the length is given in *remainder.
> The function can then update the remainder and return the length.
>
> Just an idea.

Something like this untested example (please ignore the _counted):

diff --git a/utf8.c b/utf8.c
index 8d4b159e1d..fe6f288179 100644
--- a/utf8.c
+++ b/utf8.c
@@ -22,18 +22,24 @@ struct interval {
  * of string "s", or 0 if the bytes do not begin with a complete
  * such sequence.  Never looks at s[len] or beyond.
  */
-size_t display_mode_esc_sequence_len_counted(const char *s, size_t len)
+size_t display_mode_esc_sequence_len_counted(const char *s, size_t *len)
 {
-	const char *p = s, *end = s + len;
+	const char *p = s, *end = NULL;
 
-	if (end <= p || *p++ != '\033')
+	if (len)
+		end = s + *len;
+
+	if ((end && end <= p) || *p++ != '\033')
 		return 0;
-	if (end <= p || *p++ != '[')
+	if ((end && end <= p) || *p++ != '[')
 		return 0;
-	while (p < end && (isdigit(*p) || *p == ';'))
+	while (((end && p < end) || (!end && *p)) && (isdigit(*p) || *p == ';'))
 		p++;
-	if (end <= p || *p++ != 'm')
+	if ((end && end <= p) || (!end && !*p) || *p++ != 'm')
 		return 0;
+
+	if (len)
+		*len -= p - s;
 	return p - s;
 }
 

  reply	other threads:[~2026-11-19 22:59 UTC|newest]

Thread overview: 26+ 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
2026-11-17 18:11         ` Junio C Hamano
2026-11-16 18:57 ` [PATCH 2/2] utf8: fix comment for utf8_strnwidth() Dirk Gouders
2026-11-18 21:08 ` [PATCH v2 0/4] utf8: bounds-related fixes and a doc fix Dirk Gouders
2026-11-18 21:08   ` [PATCH v2 1/4] utf8: add display_mode_esc_sequence_len_counted() Dirk Gouders
2026-11-19  5:54     ` Andy Koppe
2026-11-19  7:43       ` Dirk Gouders
2026-11-19 20:58         ` Andy Koppe
2026-11-19 21:04           ` Andy Koppe
2026-11-19 21:33             ` Dirk Gouders
2026-11-19 22:32       ` Dirk Gouders
2026-11-19 22:59         ` Dirk Gouders [this message]
2026-11-19 19:59     ` Junio C Hamano
2026-11-19 20:28       ` Dirk Gouders
2026-11-18 21:08   ` [PATCH v2 2/4] utf8: fix comment for utf8_strnwidth() Dirk Gouders
2026-11-18 22:40     ` Andy Koppe
2026-11-19  5:58       ` Dirk Gouders
2026-11-18 21:08   ` [PATCH v2 3/4] utf8: respect string boundaries after skipping ANSI escape sequences Dirk Gouders
2026-11-18 21:49     ` Junio C Hamano
2026-11-18 21:08   ` [PATCH v2 4/4] utf8: respect boundaries in utf8_strnwidth() Dirk Gouders
2026-11-18 21:50     ` Junio C Hamano

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=gh7bi81l8f.fsf@gouders.net \
    --to=dirk@gouders.net \
    --cc=andy.koppe@gmail.com \
    --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