Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2024-12-06 17:40:16


Christian Mazakas wrote:
> update() works properly for hashing byte streams (as it must) but because it's
> intended as part of an implementation, it's not exactly suitable for most end-
> users who just want to hash their 3 GB file.

It is, though.

Here's the actual example:

https://pdimov.github.io/hash2/doc/html/hash2.html#example_md5sum

static void md5sum( std::FILE* f, char const* fn )
{
    boost::hash2::md5_128 hash;

    int const N = 4096;
    unsigned char buffer[ N ];

    for( ;; )
    {
        std::size_t n = std::fread( buffer, 1, N, f );

        if( std::ferror( f ) )
        {
            std::fprintf( stderr, "'%s': read error: %s\n", fn, std::strerror( errno ) );
            return;
        }

        if( n == 0 ) break;

        hash.update( buffer, n );
    }

    std::string digest = to_string( hash.result() );

    std::printf( "%s *%s\n", digest.c_str(), fn );
}

Note how the `update` call is perfectly intuitive and suitable for end use.

If we try to replace this with a `span`, it's not going to become any better.

Why is that? We know that `span` must result in better code?

Well it is because std::fread doesn't return a `span`, as doesn't most of
the rest of existing code in 2025. If everything used `span`, everyone else
should also use it, but we aren't there yet.

Nowadays you get a `size_t`, and the existing `update` will happily take it,
without forcing you to construct an unnecessary `span` out of it.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk