[Swan-dev] can shunk_t (string chunk) be merged into chunk_t (byte chunk)

Andrew Cagney andrew.cagney at gmail.com
Mon Jan 21 18:40:59 UTC 2019


This came up in an offline discussion.  While chunk_t is intended for
bytes and shunk_t is intended for characters, they do both provide
pointer+length abstractions.  This begs the question: should they be
merged?  It turns out that they have a critical difference:

- chunk_t points at writable data but shunk_t points at read-only data

so I'd argue no.  For instance, a construct like:

    const char string[4] = { 'H', 'e', 'l', 'p', };
    shunk_t s = { .ptr = string, .len = sizeof(string), };

is valid (unlike strspn() et.al., shunks don't assume NUL
termination), where as:

    chunk_t c = { .ptr = (uint8_t*) string, .len = sizeof(string), };

is not.

What might useful are:

- think of a better name - string_t would be terrible, slice_t might be better
- change .ptr to 'const uint8_t*', but that would break things like
.ptr = "a string" :-(

Andrew


More information about the Swan-dev mailing list