String switch is odd using betterC

Simen Kjærås simen.kjaras at gmail.com
Wed Feb 26 09:26:08 UTC 2020


On Wednesday, 26 February 2020 at 08:32:50 UTC, Abby wrote:
> On Wednesday, 26 February 2020 at 08:25:00 UTC, Abby wrote:
>> Any idea why?
>
> Ok so this is enough to produce the same result, it seems that 
> there is a problem in string switch when there is more the 6 
> cases.
>
> extern(C) void main()
> {
>     auto s = "F";
>     final switch(s)
>     {
>         case "A": break;
>         case "B": break;
>         case "C": break;
>         case "D": break;
>         case "E": break;
>         case "F": break;
>         case "G": break;
>     }
> }

The explanation can be found in 
druntime/import/core/internal/switch_.d: the __switch template 
does a simple binary search for less than 7 cases, but calls 
.idup on each case label for >= 7 cases.

There's a comment there about why it's being done, but it seems 
to be a far more complicated fix than necessary - static 
immutable cases = [caseLabels]; works just as well, it seems.

Anyway, the current code was added in this commit: 
https://github.com/dlang/druntime/commit/fa665f6618af7dbc09ed5ba1333f385017b7ece8.

Anyways, reported here: 
https://issues.dlang.org/show_bug.cgi?id=20613

--
   Simen


More information about the Digitalmars-d-learn mailing list