dChar Error
Ali Çehreli
acehreli at yahoo.com
Sat Dec 31 02:15:56 UTC 2022
On 12/30/22 17:22, Salih Dincer wrote:
> I guess there is no other way but to overload.
Since the bodies of all three overloads are the same except some types,
they can easily be templatized.
> This is both the safest and the fastest.
I didn't think Values is fast with string copies that it makes. ;) I
think it was only for showing the byte values but you can do the same by
casting to ubyte[] as well.
Also, your Fun could work only with string literals; so I used function
parameters.
import std.traits : isSomeString;
// isSomeString may or may not be useful below. (?)
auto Fun(S)(S str)
if (isSomeString!S) {
import std.traits : Unqual;
import std.conv : to;
alias T = Unqual!S;
// Note: The following may or may not copy the string
// depending on whether S is the same as T.
return str.to!T;
}
void printBytes(S)(S str) {
import std.stdio : writefln;
import std.conv : to;
// The following cast does not copy anything.
writefln!"%(%02X-%)"(cast(ubyte[])str);
}
void main()
{
printBytes(Fun("β€Ş")); // CE-B2-E2-82-AC-C5-9E
printBytes(Fun("β€Ş"w)); // B2-03-AC-20-5E-01
printBytes(Fun("β€Ş"d)); // B2-03-00-00-AC-20-00-00-5E-01-00-00
}
Ali
More information about the Digitalmars-d-learn
mailing list