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

Andrew Cagney andrew.cagney at gmail.com
Fri Oct 11 15:50:35 UTC 2019


On Mon, 21 Jan 2019 at 13:40, Andrew Cagney <andrew.cagney at gmail.com> wrote:
>
> 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:

The code has evolved:

- rather than having duplication, operations that work equally on
chunk(rw) or shunk(ro) are being merged into hunk_*(), for instance:

    hunk_streq(chunk, "a string")
    hunk_streq(shunk, "a string")

- shunk_t is no longer string (const char *) centric - it's .ptr type
was changed to 'const void *'; this means code needs an explicit and
local conversion of .ptr (I find that makes things more readable as
there's no question as to the type/sign of the data);I think new
chunk_t code should follow this lead (and existing code shouldn't have
explicit .ptr casts stripped out)

- code uses functions with 'void *' and/or 'const void *' parameters
and type<->void conversion, and not macros with casts; the latter
suppresses const conversion warnings

So circling back to the name.  I find the  'c' in chunk_t really
unfortunate - c[onstant]hunk_t, c[har]hunk_t.   I guess chunk_t /
shunk_t should have been called rw_hunk_t / ro_hunk_t; oops.



> - 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