Re: [PATCH v2] utf8: add display_mode_esc_sequence_len_counted()
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Nov 17, 2026, 21:43 UTC
- Message-ID
- <xmqqbj7ncexk.fsf@gitster.g>
- In-Reply-To
- <ghy0arkv39.fsf@gouders.net>
Dirk Gouders <dirk@gouders.net> writes:
Show 6 quoted lines
> You have better knowledge of the users of this function: would an early > return perhaps handle a significant amount of cases? > > /* The shortest SGR sequence is "\033[m". */ > if (len < 3) > return 0;
Do you have any benchmarks? I don't.
I find it of dubious performance merit to run this comparison for a long string as it is eaten one UTF-8 characters at a time by the caller, and when the length is actually shorter than 3, then runnning the remainder of the function for a few steps should not be 10x more expensive than the above comparison, no?
> And, why not add some tests (the untested function I showed had > an off-by-one error):
Sounds good. Should I just steal what you sent out?
Show 44 quoted lines
> diff --git a/Makefile b/Makefile
> index 4c84eea6e2..3c8dc9ca40 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -1564,6 +1564,7 @@ CLAR_TEST_SUITES += u-strvec
> CLAR_TEST_SUITES += u-trailer
> CLAR_TEST_SUITES += u-urlmatch-normalization
> CLAR_TEST_SUITES += u-utf8-width
> +CLAR_TEST_SUITES += u-utf8-ansi
> CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
> CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
> CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/clar/clar.o
> diff --git a/t/meson.build b/t/meson.build
> index 1cf1d95f70..e224df58c8 100644
> --- a/t/meson.build
> +++ b/t/meson.build
> @@ -27,6 +27,7 @@ clar_test_suites = [
> 'unit-tests/u-trailer.c',
> 'unit-tests/u-urlmatch-normalization.c',
> 'unit-tests/u-utf8-width.c',
> + 'unit-tests/u-utf8-ansi.c',
> ]
>
> clar_sources = [
> diff --git a/t/unit-tests/u-utf8-ansi.c b/t/unit-tests/u-utf8-ansi.c
> new file mode 100644
> index 0000000000..c19d8bce78
> --- /dev/null
> +++ b/t/unit-tests/u-utf8-ansi.c
> @@ -0,0 +1,14 @@
> +#include "unit-test.h"
> +#include "utf8.h"
> +#include "strbuf.h"
> +
> +/*
> + * Test len limited functions for ANSI SGR lengths.
> + */
> +void test_utf8_ansi__sgr_len(void)
> +{
> + cl_assert_equal_i(0, display_mode_esc_sequence_len_counted("\033[m", 2));
> + cl_assert_equal_i(3, display_mode_esc_sequence_len_counted("\033[m", 3));
> + cl_assert_equal_i(5, display_mode_esc_sequence_len_counted("\033[31m", 5));
> + cl_assert_equal_i(7, display_mode_esc_sequence_len_counted("\033[0;31m", 7));
> +}Thanks.