www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - md5 + coupling

reply "Matthew" <admin stlsoft.dot.dot.dot.dot.org> writes:
Speaking of coupling, as we were a short while ago, I note that the md5 
module has a printDigest() function, which uses printf(). Is there 
really any good reason for this method, and its inherent coupling to 
printf()? It seems pointless to me, since the impl of printDigest() is 
simply

    void printDigest(ubyte digest[16])
    {
        foreach (ubyte u; digest)
            printf("%02x", u);
    }

???
Jan 27 2005
parent "Regan Heath" <regan netwin.co.nz> writes:
On Fri, 28 Jan 2005 09:55:50 +1100, Matthew  
<admin stlsoft.dot.dot.dot.dot.org> wrote:
 Speaking of coupling, as we were a short while ago, I note that the md5
 module has a printDigest() function, which uses printf(). Is there
 really any good reason for this method, and its inherent coupling to
 printf()? It seems pointless to me, since the impl of printDigest() is
 simply

     void printDigest(ubyte digest[16])
     {
         foreach (ubyte u; digest)
             printf("%02x", u);
     }

AFAIKS there is no reason for it using printf instead of placing the information into a char[]. My hash code in Deimos uses this function to output all the different digests from all the various hashing methods: # template hexStringT(T) # { # char[] hexStringT(T d) # { # char[] result; # # if (d.length != 0) { # typeof(d[0]) u; # uint sz = u.sizeof*2; # uint ndigits = 0; # # result = new char[sz*d.length]; # for(int i = d.length-1; i >= 0; i--) { # u = d[i]; # for(; u; u /= 16) { # result[result.length-1-ndigits] = hexdigits[u & 15]; # ndigits++; # } # for(; ndigits < (d.length-i)*sz; ndigits++) # result[result.length-1-ndigits] = '0'; # } # } # # return result; # } # } # # char[] toHexString(ubyte[] array) # { # return hexStringT!(typeof(array))(array); # } # # char[] toHexString(ulong[] array) # { # return hexStringT!(typeof(array))(array); # } # # char[] toHexString(uint[] array) # { # return hexStringT!(typeof(array))(array); # } Regan
Jan 27 2005