git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2] utf8: add display_mode_esc_sequence_len_counted()

From
Dirk Gouders <dirk@gouders.net>
Date
Nov 17, 2026, 22:03 UTC
Message-ID
<ghtslfkte4.fsf@gouders.net>
In-Reply-To
<xmqqbj7ncexk.fsf@gitster.g>
On Tue, Nov 17 2026, Junio C Hamano wrote:
Show 16 quoted lines
> 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
Show 46 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.
Previous: Junio C HamanoNext: Junio C Hamano
Message 5 of 15 in “utf8: make display_mode_esc_sequence_len() take a length”
  1. utf8: make display_mode_esc_sequence_len() take a lengthJunio C Hamano, Nov 17, 2026
  2. utf8: add display_mode_esc_sequence_len_counted()Junio C Hamano, Nov 17, 2026
  3. Dirk GoudersNov 17, 2026
  4. Junio C HamanoNov 17, 2026
  5. Dirk GoudersNov 17, 2026
  6. Junio C HamanoNov 18, 2026
  7. Dirk GoudersNov 18, 2026
  8. Andy KoppeNov 17, 2026
  9. Junio C HamanoNov 17, 2026
  10. Andy KoppeNov 18, 2026
  11. Dirk GoudersNov 18, 2026
  12. Andy KoppeNov 18, 2026
  13. Dirk GoudersNov 18, 2026
  14. Andy KoppeNov 18, 2026
  15. Dirk GoudersNov 18, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.