From: Dirk Gouders Date: Thu, 19 Nov 2026 22:32:58 GMT Subject: Re: [PATCH v2 1/4] utf8: add display_mode_esc_sequence_len_counted() Message-ID: In-Reply-To: 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. Dirk > Therefore I think that if the original implementation isn't going to be > kept, it would be better not to provide the wrapper, and thereby make > callers think about what they want to pass as the length. > > Uses on strbufs can of course compute it with `sb.buf + sb.len - s`, > but they don't actually need this safety net, because strbufs are > guaranteed to be NUL-terminated and the function stops at NULs. > And the length calculation itself adds a source of errors.