Bug 5381 – [regression 2.051] switch fails for wstring and dstring
Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P2
Component
druntime
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2010-12-28T00:04:00Z
Last change time
2010-12-28T12:30:06Z
Keywords
patch, wrong-code
Assigned to
braddr
Creator
r.sagitario
Comments
Comment #0 by r.sagitario — 2010-12-28T00:04:25Z
With dmd 2.051, switch statement on strings other than utf8 may fail:
import std.stdio;
int testswitch(wstring ws)
{
switch(ws)
{
case "unittest": return -1;
case "D_Version2": return 1;
case "none": return -1;
case "all": return 1;
default: return 0;
}
}
void main()
{
wstring ws = "none";
writeln("testswitch = ", testswitch(ws));
}
outputs 0, but -1 is expected.
Here's the patch, that just copies the corresponding line for the string switches into the implementation for wstring and dstring:
Index: switch_.d
===================================================================
--- switch_.d (revision 455)
+++ switch_.d (working copy)
@@ -241,7 +241,7 @@
{
auto mid = (low + high) >> 1;
auto pca = table[mid];
- auto c = ca.length - pca.length;
+ auto c = cast(sizediff_t)(ca.length - pca.length);
if (c == 0)
{
c = memcmp(ca.ptr, pca.ptr, ca.length * wchar.sizeof);
@@ -353,7 +353,7 @@
{
auto mid = (low + high) >> 1;
auto pca = table[mid];
- auto c = ca.length - pca.length;
+ auto c = cast(sizediff_t)(ca.length - pca.length);
if (c == 0)
{
c = memcmp(ca.ptr, pca.ptr, ca.length * dchar.sizeof);