Git development
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Dirk Gouders <dirk@gouders.net>
Cc: git@vger.kernel.org
Subject: Re: [PATCH v2] utf8: add display_mode_esc_sequence_len_counted()
Date: Tue, 17 Nov 2026 13:43:03 -0800	[thread overview]
Message-ID: <xmqqbj7ncexk.fsf@gitster.g> (raw)
In-Reply-To: <ghy0arkv39.fsf@gouders.net> (Dirk Gouders's message of "Tue, 17 Nov 2026 22:26:50 +0100")

Dirk Gouders <dirk@gouders.net> writes:

> 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?

> 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.

  reply	other threads:[~2026-11-17 21:43 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 16:50 [PATCH] utf8: make display_mode_esc_sequence_len() take a length Junio C Hamano
2026-11-17 20:06 ` [PATCH v2] utf8: add display_mode_esc_sequence_len_counted() Junio C Hamano
2026-11-17 21:26   ` Dirk Gouders
2026-11-17 21:43     ` Junio C Hamano [this message]
2026-11-17 22:03       ` Dirk Gouders
2026-11-18  1:33         ` Junio C Hamano
2026-11-18  7:37           ` Dirk Gouders
2026-11-17 23:08   ` Andy Koppe
2026-11-17 23:25     ` Junio C Hamano
2026-11-18  6:59       ` Andy Koppe
2026-11-18  7:43         ` Dirk Gouders

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=xmqqbj7ncexk.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=dirk@gouders.net \
    --cc=git@vger.kernel.org \
    /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