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:12:45 -0800
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <86pmortp6q.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/k9oy5BY+ehLESWvVnc+mhT7blK7aKask="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:qdtQZmiT5jWHhxzdu4VIAUFsgHY= sha1:Ru3mAwoSeq129hzgdKw3vgD5SQs=
Xref: csiph.com comp.lang.c++:82785
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. [...]
(Sorry, accidental send)
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 ideal but I
don't think it's totally unreasonable.