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: Mon, 17 Jan 2022 10:13:28 -0800
Organization: A noiseless patient Spider
Lines: 50
Message-ID: <86iluis01j.fsf@linuxsc.com>
References: <87ee5d6ny8.fsf@bsb.me.uk> <86pmortp6q.fsf@linuxsc.com> <4a8780c9-19a5-43af-935d-9ccf756014f4n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="15598caebf32ebbe97649a2f881af1fe"; logging-data="17642"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/RGiP7MormxD5mXsdZ+2wLtDFmXwbnKDQ="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:+T8cpc6LeDWnwcgzBKeqtk627p4= sha1:jvRBy8SLJsKrY0loN85YO+R5S4w=
Xref: csiph.com comp.lang.c++:82799
Tiib writes:
> On Sunday, 16 January 2022 at 22:13:01 UTC+2, Tim Rentsch wrote:
>
>> 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.
>
> With complicated syntax and semantics it makes sense
> as the issues are easier to reason about in separation and the
> expectations of users to diagnostics are also quite high.
>
> UTF-8 is usually produced by software. It takes not too lot of
> code to detect all issues in it. Usually the symbol ? is shown
> as "diagnostic" for any issue in Unicode. [...]
>
> So if some problem involving UTF-8 needs only half of error
> checking then it is fair to expect it been said in problem
> description.
Yes, I know you have no shortage of strong opinions.