Thread View: linux.kernel.git
1 messages
1 total messages
Started by Junio C Hamano
Mon, 23 Nov 2026 17:04
Re: [PATCH v5 1/4] utf8: add display_mode_esc_sequence_len_counted()
Author: Junio C Hamano
Date: Mon, 23 Nov 2026 17:04
Date: Mon, 23 Nov 2026 17:04
183 lines
18944 bytes
18944 bytes
Dirk Gouders <dirk@gouders.net> writes:
> From: Junio C Hamano <gitster@pobox.com>
>
> display_mode_esc_sequence_len() scans for an ANSI display mode
> escape sequence (e.g. "ESC [ 1 ; 31 m") at the start of a string,
> relying on NUL termination to stop. Some callers, however, work on
> a counted buffer: utf8_strnwidth() takes <string, len> and must not
> scan past it, while strbuf_utf8_replace() and
> strbuf_add_wrapped_text() know (or can cheaply learn) the end.
>
> Add display_mode_esc_sequence_len_counted() taking <ptr, len>,
> which never reads at or beyond ptr[len], and make
> display_mode_esc_sequence_len() a thin wrapper for NUL-terminated
> strings.
Good.
> Use the counted variant in utf8_strnwidth() for correctness, and in
> strbuf_utf8_replace() and strbuf_add_wrapped_text() (computing the
> end once upfront in the latter) to avoid measuring the remaining
> text in each iteration.
OK, but the latter needs a slight fix (see below).
> [Dirk Gouders: allow SIZE_MAX to be passed for NUL-terminated strings
> to avoid O(n²) degradation as suggested by Andy Koppe. Also add
> tests for these functions.]
>
> Helped-by: Andy Koppe <andy.koppe@gmail.com>
> Signed-off-by: Junio C Hamano <gitster@pobox.com>
> Signed-off-by: Dirk Gouders <dirk@gouders.net>
Minor nit. These ought to be a chronological record of custodial chain.
The original author's sign-off comes first, then Helped-by and [note on
the help in square brackets], and finally the sign-off of person who is
relaying the final package.
> diff --git a/t/unit-tests/u-utf8-ansi.c b/t/unit-tests/u-utf8-ansi.c
> new file mode 100644
> index 0000000000..d969737000
> --- /dev/null
> +++ b/t/unit-tests/u-utf8-ansi.c
> @@ -0,0 +1,86 @@
> +#include "unit-test.h"
> +#include "utf8.h"
> +#include "strbuf.h"
Do we use <strbuf.h> at this stage? I do not think so (and
<unit-test.h> includes it anyway).
> +
> +/*
> + * Test display_mode_esc_sequence_len_counted() for NUL-terminated strings.
> + */
> +void test_utf8_ansi__sgr_len_counted(void)
> +{
> + cl_assert_equal_i(0, display_mode_esc_sequence_len_counted("", SIZE_MAX));
> +
> + cl_assert_equal_i(0, display_mode_esc_sequence_len_counted("\033", SIZE_MAX));
> +...
> + cl_assert_equal_i(7, display_mode_esc_sequence_len_counted("\033[31;0ma", SIZE_MAX));
It is sad that these lines have to become this long.
As the values returned from display_mode_esc_sequence_len_counted() and
friends are all size_t, not signed, cl_assert_equal_u() would be a much
more appropriate assertion here.
See how t/unit-tests/u-odb-inmemory.c:cl_assert_object_info() nearby
uses it to check values of type size_t.
> +/*
> + * Test display_mode_esc_sequence_len_counted() with end pointers.
Probably with "length limits" instead of "end pointers" in this
iteration.
> diff --git a/utf8.c b/utf8.c
> index 96460cc414..503edb8edc 100644
> --- a/utf8.c
> +++ b/utf8.c
> @@ -16,18 +16,31 @@ struct interval {
> ucs_char_t last;
> };
>
> -size_t display_mode_esc_sequence_len(const char *s)
> +/*
> + * Return the length of the ANSI display mode escape sequence (e.g.,
> + * "\033[1;31m") that appears at the beginning of the "len" bytes
> + * 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)
> {
> - const char *p = s;
> - if (*p++ != '\033')
> + size_t i = 0;
> +
> + if (i >= len || s[i++] != '\033')
> return 0;
> - if (*p++ != '[')
> + if (i >= len || s[i++] != '[')
> return 0;
> - while (isdigit(*p) || *p == ';')
> - p++;
> - if (*p++ != 'm')
> + while (i < len && (isdigit(s[i]) || s[i] == ';'))
> + i++;
> + if (i >= len || s[i++] != 'm')
> return 0;
Among 4 comparisons between i and len, only one of them follow the
"textual order reflects actual order" principle. Can we revert it back
to the version I gave you to compare (len <= i) instead, if we allow the
commit to claim to be from me, as I wouldn't write (i >= len) in these
places if I were writing this patch myself.
> + return i;
> +}
> /* auxiliary function for binary search in interval table */
> @@ -218,7 +231,8 @@ int utf8_strnwidth(const char *string, size_t len, int skip_ansi)
> size_t skip;
>
> while (skip_ansi &&
> - (skip = display_mode_esc_sequence_len(string)) != 0)
> + (skip = display_mode_esc_sequence_len_counted(string,
> + orig + len - string)) != 0)
> string += skip;
>
> glyph_width = utf8_width(&string, NULL);
> @@ -279,6 +293,7 @@ void strbuf_add_wrapped_text(struct strbuf *buf,
> {
> int indent, w, assume_utf8 = 1;
> const char *bol, *space, *start = text;
> + const char *end = text + strlen(text);
This strlen() call is unconditionally made, even before the check to see
if we divert to strbuf_add_indented_text() with "if (width <= 0)";
measuring the length of text in that code path is wasted.
> size_t orig_len = buf->len;
>
> if (width <= 0) {
> @@ -299,7 +314,8 @@ void strbuf_add_wrapped_text(struct strbuf *buf,
> char c;
> size_t skip;
>
> - while ((skip = display_mode_esc_sequence_len(text)))
> + while ((skip = display_mode_esc_sequence_len_counted(text,
> + end - text)))
> text += skip;
OK, I guess.
It is a bit sad that we cannot use the trick similar to passing SIZE_MAX
to avoid having to prescan to find "end", though.
> c = *text;
> @@ -378,7 +394,8 @@ void strbuf_utf8_replace(struct strbuf *sb_src, int pos, int width,
> int glyph_width;
> size_t n;
>
> - while ((n = display_mode_esc_sequence_len(src))) {
> + while ((n = display_mode_esc_sequence_len_counted(src,
> + end - src))) {
> strbuf_add(&dst, src, n);
> src += n;
> }
> diff --git a/utf8.h b/utf8.h
> index cf8ecb0f21..57ca2ab270 100644
> --- a/utf8.h
> +++ b/utf8.h
> @@ -6,6 +6,7 @@ struct strbuf;
> typedef unsigned int ucs_char_t; /* assuming 32bit int */
>
> size_t display_mode_esc_sequence_len(const char *s);
> +size_t display_mode_esc_sequence_len_counted(const char *s, size_t len);
> 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);
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads