Switch ignores case (?)

ketmar via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Nov 23 13:11:36 PST 2016


quickfix:


diff --git a/src/rt/switch_.d b/src/rt/switch_.d
index ec124e3..83572fe 100644
--- a/src/rt/switch_.d
+++ b/src/rt/switch_.d
@@ -27,6 +27,32 @@ private import core.stdc.string;

  extern (C):

+import core.stdc.wchar_ : wchar_t, wmemcmp;
+
+
+static if (wchar_t.sizeof == wchar.sizeof) {
+  alias memcmpw = wmemcmp;
+  int memcmpd (const(void)* s0, const(void)* s1, size_t len) {
+    while (len--) {
+      if (int r = *cast(const(int)*)s0-*cast(const(int)*)s1) 
return (r < 0 ? -1 : 1);
+      s0 += dchar.sizeof;
+      s1 += dchar.sizeof;
+    }
+    return 0;
+  }
+} else static if (wchar_t.sizeof == dchar.sizeof) {
+  int memcmpw (const(void)* s0, const(void)* s1, size_t len) {
+    while (len--) {
+      if (int r = 
*cast(const(ushort)*)s0-*cast(const(ushort)*)s1) return (r < 0 ? 
-1 : 1);
+      s0 += wchar.sizeof;
+      s1 += wchar.sizeof;
+    }
+    return 0;
+  }
+  alias memcmpd = wmemcmp;
+} else static assert(0, "wut?!");
+
+
  int _d_switch_string(char[][] table, char[] ca)
  in
  {
@@ -189,7 +215,7 @@ in
          {
              int c;

-            c = memcmp(table[j - 1].ptr, table[j].ptr, len1 * 
wchar.sizeof);
+            c = memcmpw(table[j - 1].ptr, table[j].ptr, len1);
              assert(c < 0);  // c==0 means a duplicate
          }
      }
@@ -205,7 +231,7 @@ out (result)
          for (auto i = 0u; i < table.length; i++)
          {
              if (table[i].length == ca.length)
-            {   c = memcmp(table[i].ptr, ca.ptr, ca.length * 
wchar.sizeof);
+            {   c = memcmpw(table[i].ptr, ca.ptr, ca.length);
                  assert(c != 0);
              }
          }
@@ -218,7 +244,7 @@ out (result)
              assert(i < table.length);
              if (table[i].length == ca.length)
              {
-                c = memcmp(table[i].ptr, ca.ptr, ca.length * 
wchar.sizeof);
+                c = memcmpw(table[i].ptr, ca.ptr, ca.length);
                  if (c == 0)
                  {
                      assert(i == result);
@@ -253,7 +279,7 @@ body
          auto c = cast(sizediff_t)(ca.length - pca.length);
          if (c == 0)
          {
-            c = memcmp(ca.ptr, pca.ptr, ca.length * 
wchar.sizeof);
+            c = memcmpw(ca.ptr, pca.ptr, ca.length);
              if (c == 0)
              {   //printf("found %d\n", mid);
                  return cast(int)mid;
@@ -317,7 +343,7 @@ in
          assert(len1 <= len2);
          if (len1 == len2)
          {
-            auto c = memcmp(table[j - 1].ptr, table[j].ptr, len1 
* dchar.sizeof);
+            auto c = memcmpd(table[j - 1].ptr, table[j].ptr, 
len1);
              assert(c < 0);  // c==0 means a duplicate
          }
      }
@@ -331,7 +357,7 @@ out (result)
          for (auto i = 0u; i < table.length; i++)
          {
              if (table[i].length == ca.length)
-            {   auto c = memcmp(table[i].ptr, ca.ptr, ca.length 
* dchar.sizeof);
+            {   auto c = memcmpd(table[i].ptr, ca.ptr, 
ca.length);
                  assert(c != 0);
              }
          }
@@ -344,7 +370,7 @@ out (result)
              assert(i < table.length);
              if (table[i].length == ca.length)
              {
-                auto c = memcmp(table[i].ptr, ca.ptr, ca.length 
* dchar.sizeof);
+                auto c = memcmpd(table[i].ptr, ca.ptr, 
ca.length);
                  if (c == 0)
                  {
                      assert(i == result);
@@ -379,7 +405,7 @@ body
          auto c = cast(sizediff_t)(ca.length - pca.length);
          if (c == 0)
          {
-            c = memcmp(ca.ptr, pca.ptr, ca.length * 
dchar.sizeof);
+            c = memcmpd(ca.ptr, pca.ptr, ca.length);
              if (c == 0)
              {   //printf("found %d\n", mid);
                  return cast(int)mid;
--
2.9.2


More information about the Digitalmars-d-learn mailing list