std.digest toHexString
Carl Sturtivant via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Mar 16 09:13:33 PDT 2017
What's going on here?
```
import std.digest.md, std.stdio;
void main() {
string ans = hex("qwertyuiop");
writeln(ans);
}
string hex(string arg) {
string ans = md5Of(arg).toHexString();
writeln(ans);
return ans;
}
```
This compiles, and when run writes out corrupt nonsense from the
writeln in main, while just before that writing out
6EEA9B7EF19179A06954EDD0F6C05CEB from the function hex.
If I replace md5Of(arg).toHexString() with
toHexString(md5Of(arg)) it won't compile, claiming that
"toHexString.d(11): Error: function expected before (), not
module toHexString of type void". Yet the examples here
http://dlang.org/phobos/std_digest_md.html
use toHexString freely in this way. I thought I was using
toHexString by UFCS, in the earlier example, but this appears to
be untrue.
The return from the function hex above somehow corrupts the
string returned, suggesting deallocation is occurring in some
fashion. So I replaced md5Of(arg).toHexString() with
md5Of(arg).toHexString().dup and the problem vanished. Now
6EEA9B7EF19179A06954EDD0F6C05CEB is printed out twice.
More information about the Digitalmars-d-learn
mailing list