dChar Error

Salih Dincer salihdb at hotmail.com
Sat Dec 31 01:22:27 UTC 2022


On Saturday, 31 December 2022 at 00:42:50 UTC, Salih Dincer wrote:
> ... it possible to infer

Let me save you the torment of code duplication 😀

Thanks everyone.  Yes, I guess there is no other way but to 
overload.  This is both the safest and the fastest.  It's also 
short enough like this:

```d
   // D 2.0.83 or higher

   import std.stdio : writeln;
   import std.conv  : to;

auto Fun(string str)() {
   auto result = Values!(char, str.length)();
   result.data = str.to!(char[]);
   return result;
}

auto Fun(wstring str)() {
   auto result = Values!(wchar, str.length)();
   result.data = str.to!(wchar[]);
   return result;
}

auto Fun(dstring str)() {
   auto result = Values!(dchar, str.length)();
   result.data = str.to!(dchar[]);
   return result;
}

struct Values(T, size_t len) {
   union {
     T[len] data;
     ubyte[T.sizeof * len] bytes;
   }
   string toString() {
     import std.format;
     return format("%s: %(%02X-%)", data, bytes);
   }
}

void main()
{
   Fun!"β€Ş".writeln;  // β€Ş: CE-B2-E2-82-AC-C5-9E
   Fun!"β€Ş"w.writeln; // β€Ş: B2-03-AC-20-5E-01
   Fun!"β€Ş"d.writeln; // B2-03-00-00-AC-20-00-00-5E-01-00-00
}
```

SDB at 79




More information about the Digitalmars-d-learn mailing list