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

Re: [PATCH v2 1/4] utf8: add display_mode_esc_sequence_len_counted()

From
Dirk Gouders <dirk@gouders.net>
Date
Nov 19, 2026, 22:59 UTC
Message-ID
<gh7bi81l8f.fsf@gouders.net>
In-Reply-To
<ghbj7k1mg5.fsf@gouders.net>
On Thu, Nov 19 2026, Dirk Gouders wrote:
Show 23 quoted lines
> 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;
 }
 
Previous: Dirk GoudersNext: Junio C Hamano
Message 17 of 26 in “utf8: two minor fixes”
  1. 0/2 utf8: two minor fixesDirk Gouders, Nov 16, 2026
  2. 1/2 utf8: respect bounds in utf8_strnwidth()Dirk Gouders, Nov 16, 2026
  3. Junio C HamanoNov 17, 2026
  4. Dirk GoudersNov 17, 2026
  5. Junio C HamanoNov 17, 2026
  6. Dirk GoudersNov 17, 2026
  7. Junio C HamanoNov 17, 2026
  8. 2/2 utf8: fix comment for utf8_strnwidth()Dirk Gouders, Nov 16, 2026
  9. 0/4 utf8: bounds-related fixes and a doc fixDirk Gouders, Nov 18, 2026
  10. 1/4 utf8: add display_mode_esc_sequence_len_counted()Dirk Gouders, Nov 18, 2026
  11. Andy KoppeNov 19, 2026
  12. Dirk GoudersNov 19, 2026
  13. Andy KoppeNov 19, 2026
  14. Andy KoppeNov 19, 2026
  15. Dirk GoudersNov 19, 2026
  16. Dirk GoudersNov 19, 2026
  17. Dirk GoudersNov 19, 2026
  18. Junio C HamanoNov 19, 2026
  19. Dirk GoudersNov 19, 2026
  20. 2/4 utf8: fix comment for utf8_strnwidth()Dirk Gouders, Nov 18, 2026
  21. Andy KoppeNov 18, 2026
  22. Dirk GoudersNov 19, 2026
  23. 3/4 utf8: respect string boundaries after skipping ANSI escape sequencesDirk Gouders, Nov 18, 2026
  24. Junio C HamanoNov 18, 2026
  25. 4/4 utf8: respect boundaries in utf8_strnwidth()Dirk Gouders, Nov 18, 2026
  26. Junio C HamanoNov 18, 2026

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.