C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Switch on strings

From: Lénárd Szolnoki <cpp_at_[hidden]>
Date: Mon, 27 Feb 2023 08:34:48 +0000
On 27 February 2023 07:39:32 GMT, Sebastian Wittmeier via Std-Proposals <std-proposals_at_[hidden]> wrote:
>Or
>
>
>(3) Depending on the possible values of the string variable use a fast hashing that has no collisions.
>
>
>For strings you probably want to compare not only for equalness, but at least
>
> * case insensitive
>
> * ignoring white sace
>
> * starts with prefix
>
> * Unicode aware (different Unicode variants that lead to the same result)
>
> * or even full blown regular expressions.

Apart from regex, all the optimisations are still available after normalisation.

Even with regex, there might be some optimisations available for some of the patterns.

I wouldn't say that all of these features are always desirable. If you are just doing keyword lookup for an interpreted language, then possibly you need none of these.

>Should all those be also included into switch?
>
>
>Then the advanced switch is more like a replacement for chained if else cascades.
>
>
>Best,
>
>Sebastian
>
>
>-----Ursprüngliche Nachricht-----
>Von:Frederick Virchanza Gotham via Std-Proposals <std-proposals_at_[hidden]>
>Gesendet:Mo 27.02.2023 01:04
>Betreff:Re: [std-proposals] Switch on strings
>An:std-proposals_at_[hidden];
>CC:Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>;
>On Sun, Feb 26, 2023 at 11:06 PM Breno Guimarães <brenorg_at_[hidden]> wrote:
>>
>> Oops: https://godbolt.org/z/r8Mvaaevf
>
>
>The solution to collisions is one of the following:
>
>(1) Use a better hash algorithm that won't collide
>(2) After the hash matches, use the equality operator to be sure
>
>But if you're going to use the equality operator at all then you could
>get rid of the hashing altogether -- unless you want to keep it in
>there for speed.

Received on 2023-02-27 08:34:53