Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std.algorithm.copy and std.digest

Tags:

copy

range

digest

d

When I use std.algorithm.copy on std.digest objects, I get different results compared when I am using put byte by byte. Why?

import std.stdio;
import std.digest.digest;
import std.digest.md;
import std.algorithm;

void main() {
    string s = "Hello!\n";
    auto d1 = makeDigest!MD5;
    auto d2 = makeDigest!MD5;
    foreach (ubyte b; s) {
        d1.put(b);
    }
    s.copy(d2);
    writeln(digest!MD5(s).toHexString);
    writeln(d1.finish().toHexString);
    writeln(d2.finish().toHexString);
}

Output:

E134CED312B3511D88943D57CCD70C83
E134CED312B3511D88943D57CCD70C83
D41D8CD98F00B204E9800998ECF8427E
like image 364
Tamas Avatar asked Sep 24 '22 02:09

Tamas


1 Answers

d2 is passed by value to copy. The data gets copied inside the function, but then when it returns, the d2 variable on the outside is unmodified!

I kinda think this might be a bug: the current behavior doesn't make a great deal of sense to me. When you are copying it, it makes sense to do it by reference. The unittests only test arrays which are half-reference (they are pointers) and it works for them.

like image 83
Adam D. Ruppe Avatar answered Oct 11 '22 13:10

Adam D. Ruppe