www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 10453] New: Allow a hex string as literal for an array of ubytes?

http://d.puremagic.com/issues/show_bug.cgi?id=10453

           Summary: Allow a hex string as literal for an array of ubytes?
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2013-06-23 13:05:44 PDT ---
Usually I prefer to avoid implicit conversions in a language, but in Phobos I
can find about one hundred hex strings used to initialize a ubyte[] or
ubyte[N]:


std\md5.d(450): assert(digest ==
cast(ubyte[])x"d41d8cd98f00b204e9800998ecf8427e");
std\md5.d(453): assert(digest ==
cast(ubyte[])x"0cc175b9c0f1b6a831c399e269772661");
std\md5.d(456): assert(digest ==
cast(ubyte[])x"900150983cd24fb0d6963f7d28e17f72");
std\md5.d(459): assert(digest ==
cast(ubyte[])x"f96b697d7cb7938d525a2f31aaf161d0");
std\md5.d(462): assert(digest ==
cast(ubyte[])x"c3fcd3d76192e4007dfb496cca67e13b");
std\md5.d(465): assert(digest ==
cast(ubyte[])x"d174ab98d277d9f5a5611c2c9f419d9f");
std\md5.d(470): assert(digest ==
cast(ubyte[])x"57edf4a22be3c955ac49da2e2107b67a");
std\md5.d(472):
assert(digestToString(cast(ubyte[16])x"c3fcd3d76192e4007dfb496cca67e13b")
std\digest\crc.d(336): assert(crc.peek() == cast(ubyte[])x"bd50274c");
std\digest\crc.d(339): assert(crc.finish() == cast(ubyte[])x"00000000");
std\digest\crc.d(342): assert(digest == cast(ubyte[])x"00000000");
std\digest\crc.d(348): assert(digest == cast(ubyte[])x"43beb7e8");
std\digest\crc.d(351): assert(digest == cast(ubyte[])x"c2412435");
std\digest\crc.d(354): assert(digest == cast(ubyte[])x"5f3f1a17");
std\digest\crc.d(357): assert(digest == cast(ubyte[])x"7f9d1520");
std\digest\crc.d(360): assert(digest == cast(ubyte[])x"d2e6c21f");
std\digest\crc.d(364): assert(digest == cast(ubyte[])x"724aa97c");
std\digest\crc.d(366): assert(crcHexString(cast(ubyte[4])x"c3fcd3d7") ==
"D7D3FCC3");
std\digest\crc.d(489): assert(crc.peek() == cast(ubyte[])x"bd50274c");
std\digest\crc.d(492): assert(crc.finish() == cast(ubyte[])x"00000000");
std\digest\crc.d(497): assert(result[0 .. 4] == result2 && result2 ==
cast(ubyte[])x"bd50274c");
std\digest\crc.d(504): assert(crc.digest("") == cast(ubyte[])x"00000000");
std\digest\crc.d(506): assert(crc.digest("a") == cast(ubyte[])x"43beb7e8");
std\digest\crc.d(508): assert(crc.digest("abc") == cast(ubyte[])x"c2412435");
std\digest\crc.d(511): == cast(ubyte[])x"5f3f1a17");
std\digest\crc.d(513): assert(crc.digest("message digest") ==
cast(ubyte[])x"7f9d1520");
std\digest\crc.d(516): == cast(ubyte[])x"bd50274c");
std\digest\crc.d(519): == cast(ubyte[])x"d2e6c21f");
std\digest\crc.d(523): == cast(ubyte[])x"724aa97c");
std\digest\crc.d(528): assert(digest == cast(ubyte[])x"BCBF25DC");
std\digest\crc.d(532): assert(digest == cast(ubyte[])x"BCBF25DC");
std\digest\md.d(525): assert(md5.finish() ==
cast(ubyte[])x"d41d8cd98f00b204e9800998ecf8427e");
std\digest\md.d(528): assert(digest ==
cast(ubyte[])x"d41d8cd98f00b204e9800998ecf8427e");
std\digest\md.d(531): assert(digest ==
cast(ubyte[])x"0cc175b9c0f1b6a831c399e269772661");
std\digest\md.d(534): assert(digest ==
cast(ubyte[])x"900150983cd24fb0d6963f7d28e17f72");
std\digest\md.d(537): assert(digest ==
cast(ubyte[])x"8215ef0796a20bcaaae116d3876c664a");
std\digest\md.d(540): assert(digest ==
cast(ubyte[])x"f96b697d7cb7938d525a2f31aaf161d0");
std\digest\md.d(543): assert(digest ==
cast(ubyte[])x"c3fcd3d76192e4007dfb496cca67e13b");
std\digest\md.d(546): assert(digest ==
cast(ubyte[])x"d174ab98d277d9f5a5611c2c9f419d9f");
std\digest\md.d(550): assert(digest ==
cast(ubyte[])x"57edf4a22be3c955ac49da2e2107b67a");
std\digest\md.d(552):
assert(toHexString(cast(ubyte[16])x"c3fcd3d76192e4007dfb496cca67e13b")
std\digest\md.d(558): assert(digest ==
cast(ubyte[])x"7707D6AE4E027C70EEA2A935C2296F21");
std\digest\md.d(562): assert(digest ==
cast(ubyte[])x"7707D6AE4E027C70EEA2A935C2296F21");
std\digest\md.d(655): assert(md5.finish() ==
cast(ubyte[])x"d41d8cd98f00b204e9800998ecf8427e");
std\digest\md.d(660): assert(result[0 .. 16] == result2 && result2 ==
cast(ubyte[])x"c3fcd3d76192e4007dfb496cca67e13b");
std\digest\md.d(667): assert(md5.digest("") ==
cast(ubyte[])x"d41d8cd98f00b204e9800998ecf8427e");
std\digest\md.d(669): assert(md5.digest("a") ==
cast(ubyte[])x"0cc175b9c0f1b6a831c399e269772661");
std\digest\md.d(671): assert(md5.digest("abc") ==
cast(ubyte[])x"900150983cd24fb0d6963f7d28e17f72");
std\digest\md.d(674): == cast(ubyte[])x"8215ef0796a20bcaaae116d3876c664a");
std\digest\md.d(676): assert(md5.digest("message digest") ==
cast(ubyte[])x"f96b697d7cb7938d525a2f31aaf161d0");
std\digest\md.d(679): == cast(ubyte[])x"c3fcd3d76192e4007dfb496cca67e13b");
std\digest\md.d(682): == cast(ubyte[])x"d174ab98d277d9f5a5611c2c9f419d9f");
std\digest\md.d(686): == cast(ubyte[])x"57edf4a22be3c955ac49da2e2107b67a");
std\digest\ripemd.d(674): assert(md.finish() ==
cast(ubyte[])x"9c1185a5c5e9fc54612808977ee8f548b2258d31");
std\digest\ripemd.d(677): assert(digest ==
cast(ubyte[])x"9c1185a5c5e9fc54612808977ee8f548b2258d31");
std\digest\ripemd.d(680): assert(digest ==
cast(ubyte[])x"0bdc9d2d256b3ee9daae347be6f4dc835a467ffe");
std\digest\ripemd.d(683): assert(digest ==
cast(ubyte[])x"8eb208f7e05d987a9b044a8e98c6b087f15a0bfc");
std\digest\ripemd.d(686): assert(digest ==
cast(ubyte[])x"12a053384a9c0c88e405a06c27dcf49ada62eb2b");
std\digest\ripemd.d(689): assert(digest ==
cast(ubyte[])x"5d0689ef49d2fae572b881b123a85ffa21595f36");
std\digest\ripemd.d(692): assert(digest ==
cast(ubyte[])x"f71c27109c692c1b56bbdceb5b9d2865b3708dbc");
std\digest\ripemd.d(695): assert(digest ==
cast(ubyte[])x"b0e20b6e3116640286ed3a87a5713079b21f5189");
std\digest\ripemd.d(699): assert(digest ==
cast(ubyte[])x"9b752e45573d4b39f4dbd3323cab82bf63326bfb");
std\digest\ripemd.d(701):
assert(toHexString(cast(ubyte[20])x"f71c27109c692c1b56bbdceb5b9d2865b3708dbc")
std\digest\ripemd.d(707): assert(digest ==
cast(ubyte[])x"52783243c1697bdbe16d37f97f68f08325dc1528");
std\digest\ripemd.d(711): assert(digest ==
cast(ubyte[])x"52783243c1697bdbe16d37f97f68f08325dc1528");
std\digest\ripemd.d(804): assert(md.finish() ==
cast(ubyte[])x"9c1185a5c5e9fc54612808977ee8f548b2258d31");
std\digest\ripemd.d(809): assert(result[0 .. 20] == result2 && result2 ==
cast(ubyte[])x"f71c27109c692c1b56bbdceb5b9d2865b3708dbc");
std\digest\ripemd.d(816): assert(md.digest("") ==
cast(ubyte[])x"9c1185a5c5e9fc54612808977ee8f548b2258d31");
std\digest\ripemd.d(818): assert(md.digest("a") ==
cast(ubyte[])x"0bdc9d2d256b3ee9daae347be6f4dc835a467ffe");
std\digest\ripemd.d(820): assert(md.digest("abc") ==
cast(ubyte[])x"8eb208f7e05d987a9b044a8e98c6b087f15a0bfc");
std\digest\ripemd.d(823): ==
cast(ubyte[])x"12a053384a9c0c88e405a06c27dcf49ada62eb2b");
std\digest\ripemd.d(825): assert(md.digest("message digest") ==
cast(ubyte[])x"5d0689ef49d2fae572b881b123a85ffa21595f36");
std\digest\ripemd.d(828): ==
cast(ubyte[])x"f71c27109c692c1b56bbdceb5b9d2865b3708dbc");
std\digest\ripemd.d(831): ==
cast(ubyte[])x"b0e20b6e3116640286ed3a87a5713079b21f5189");
std\digest\ripemd.d(835): ==
cast(ubyte[])x"9b752e45573d4b39f4dbd3323cab82bf63326bfb");
std\digest\ripemd.d(838): ==
cast(ubyte[])x"5c00bd4aca04a9057c09b20b05f723f2e23deb65");
std\digest\sha.d(592): assert(sha.finish() ==
cast(ubyte[])x"da39a3ee5e6b4b0d3255bfef95601890afd80709");
std\digest\sha.d(595): assert(digest ==
cast(ubyte[])x"da39a3ee5e6b4b0d3255bfef95601890afd80709");
std\digest\sha.d(598): assert(digest ==
cast(ubyte[])x"86f7e437faa5a7fce15d1ddcb9eaeaea377667b8");
std\digest\sha.d(601): assert(digest ==
cast(ubyte[])x"a9993e364706816aba3e25717850c26c9cd0d89d");
std\digest\sha.d(604): assert(digest ==
cast(ubyte[])x"84983e441c3bd26ebaae4aa1f95129e5e54670f1");
std\digest\sha.d(607): assert(digest ==
cast(ubyte[])x"c12252ceda8be8994d5fa0290a47231c1d16aae3");
std\digest\sha.d(610): assert(digest ==
cast(ubyte[])x"32d10c7b8cf96570ca04ce37f2a19d84240d3a89");
std\digest\sha.d(613): assert(digest ==
cast(ubyte[])x"761c457bf73b14d27e9e9265c46f4b4dda11f940");
std\digest\sha.d(617): assert(digest ==
cast(ubyte[])x"50abf5706a150990a08b2c5ea40fa0e585554732");
std\digest\sha.d(622): assert(digest ==
cast(ubyte[])x"34aa973cd4c4daa4f61eeb2bdbad27316534016f");
std\digest\sha.d(626): assert(digest ==
cast(ubyte[])x"34aa973cd4c4daa4f61eeb2bdbad27316534016f");
std\digest\sha.d(628):
assert(toHexString(cast(ubyte[20])x"a9993e364706816aba3e25717850c26c9cd0d89d")
std\digest\sha.d(733): assert(sha.finish() ==
cast(ubyte[])x"da39a3ee5e6b4b0d3255bfef95601890afd80709");
std\digest\sha.d(738): assert(result[0 .. 20] == result2 && result2 ==
cast(ubyte[])x"32d10c7b8cf96570ca04ce37f2a19d84240d3a89");
std\digest\sha.d(745): assert(sha.digest("") ==
cast(ubyte[])x"da39a3ee5e6b4b0d3255bfef95601890afd80709");
std\digest\sha.d(747): assert(sha.digest("a") ==
cast(ubyte[])x"86f7e437faa5a7fce15d1ddcb9eaeaea377667b8");
std\digest\sha.d(749): assert(sha.digest("abc") ==
cast(ubyte[])x"a9993e364706816aba3e25717850c26c9cd0d89d");
std\digest\sha.d(752): ==
cast(ubyte[])x"84983e441c3bd26ebaae4aa1f95129e5e54670f1");
std\digest\sha.d(754): assert(sha.digest("message digest") ==
cast(ubyte[])x"c12252ceda8be8994d5fa0290a47231c1d16aae3");
std\digest\sha.d(757): ==
cast(ubyte[])x"32d10c7b8cf96570ca04ce37f2a19d84240d3a89");
std\digest\sha.d(760): ==
cast(ubyte[])x"761c457bf73b14d27e9e9265c46f4b4dda11f940");
std\digest\sha.d(764): ==
cast(ubyte[])x"50abf5706a150990a08b2c5ea40fa0e585554732");
std\digest\sha.d(768): assert(sha.digest(onemilliona) ==
cast(ubyte[])x"34aa973cd4c4daa4f61eeb2bdbad27316534016f");



I think all those casts don't help much. And according to D documentation "The
hex data need not form valid UTF characters.", so they are good as a way to
insert a little of binary data in a D program.

So maybe it's worth allowing the implicit conversion from hex string literal to
a ubyte[] (and maybe even void[]?):

void main() {
    immutable ubyte[20] data1 = x"f71c27109c692c1b56bbdceb5b9d2865b3708dbc";
    immutable ubyte[] data2 =   x"f71c27109c692c1b56bbdceb5b9d2865b3708dbc";
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 23 2013