From: Dirk Gouders <dirk@gouders.net>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org
Subject: Re: [PATCH v2] utf8: add display_mode_esc_sequence_len_counted()
Date: Tue, 17 Nov 2026 23:03:31 +0100 [thread overview]
Message-ID: <ghtslfkte4.fsf@gouders.net> (raw)
In-Reply-To: <xmqqbj7ncexk.fsf@gitster.g>
On Tue, Nov 17 2026, Junio C Hamano wrote:
> 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?
Meanwhile, I thought: even if the test would be a good idea, the
"\033"-test probably is the one that handles most cases.
Maybe I will look closer, but not now. Correctness is more important
and I need to work on v2.
>> 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?
That wouldn't be stealing, of course you can use it.
Regards,
Dirk
>> 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.
next prev parent reply other threads:[~2026-11-17 22:03 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
2026-11-17 22:03 ` Dirk Gouders [this message]
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=ghtslfkte4.fsf@gouders.net \
--to=dirk@gouders.net \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
/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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.