Switch ignores case (?)

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Nov 23 13:31:08 PST 2016


On 11/23/16 2:30 PM, ketmar wrote:
> On Wednesday, 23 November 2016 at 19:07:49 UTC, Chris wrote:
>> It has something to do with the smart quote, e.g.:
>
> it is wrong binary search in `_d_switch_string()`.
>
> strings for switch are lexically sorted, and compiler calls
> `_d_switch_string()` to select one. the thing is that comparison in
> `_d_switch_string()` is done with `memcmp()`. still not clear? ok, let's
> see how cases are encoded:
>
> body _d_switch_dstring()
>        'U0027' (ca)
> table[0] = 1, 'U0027'
> table[1] = 1, 'U2019'
>
> or, in memory:
>
> table[0] = 1, 0x27, 0x00
> table[1] = 1, 0x19, 0x20
>
> so, memcmp for `table[1]` returns... 1! 'cause 0x27 is greater than
> 0x19. and binsearch is broken from here on. the same is true for
> `_d_switch_ustring()`, of course.
>
> this can be fixed either by using slow char-by-char comparisons in
> druntime, or by fixing codegen, so it would sort strings as byte arrays.

Oh wow, so this is really an endian issue. On a big endian machine, the 
code would work. Interesting!

I think it makes the most sense to remove the memcmp, and do binary 
search based on actual char values.

Thanks for finding this.

-Steve



More information about the Digitalmars-d-learn mailing list