Path: csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c++
Subject: Re: Can anyone improve this ?
Date: Sun, 16 Jan 2022 12:09:13 -0800
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <86tue3tpcm.fsf@linuxsc.com>
References: <87ee5d6ny8.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="66ee21d996f56235c8cdb5e1ea47e716"; logging-data="1917"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+rrFX8H6HvDQAeFQUYfEllRk/zTVOnFuk="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:46qGo/07xfhXPJpcQPQUodaiHVk= sha1:dog6Zk806f4Uv/IDK6z+EmQe/rc=
Xref: csiph.com comp.lang.c++:82784
Ben Bacarisse writes:
> Bonita Montero writes:
>
>> sizes instead of size; corrected:
>>
>> size_t utf8Strlen( char const *str )
>> {
>> static size_t const sizes[8] = { 1, 0, 2, 3, 4, 0, 0, 0 };
>> size_t len = 0;
>> for( unsigned char c; (c = *str); )
>> {
>> size_t size = sizes[countl_zero( ~c )];
>> if( !size ) [[unlikely]]
>> return -1;
>> ++len;
>> for( char const *cpEnd = str + size; ++str != cpEnd; )
>> if( ((unsigned char)*str & 0x0C0) != 0x080 ) [[unlikely]]
>> return -1;
>> }
>> return len;
>> }
>
> You reject simpler solutions because they don't detect the one error
> you have decided to look for. There are other encoding errors that
> this code won't catch. Seems a bit arbitrary to me. [...]
I wouldn't say arbitrary. The function verifies that its input is
syntactically well-formed while ignoring the question of whether it
is semantically well-formed. That choice may not be an ideal choice but I
don't think it is an unreasonable one.