attractivechaos/klib — pull request #21 by jmarshall (2013-07-23 15:38:43+00:00)
Title: Make ks_resize(&kstr,1) useful for ensuring the string is allocated and NUL-terminated

For example, htslib/sam.c's `sam_hdr_read()` calls `sam_hdr_parse(0, NULL)` when the SAM file has no header lines. This crashes, while `sam_hdr_parse(0, "")` would be correct.

I also attempted to add `ks_init(kstring_t*)` and `ks_destroy(kstring_t*)` functions as wrappers so client code would not need to know exactly which fields to initialise to 0/NULL and which to free. However these names are unfortunately already taken by `kstream_t` in kseq.h.

Any thoughts about what to do here? Probably `kstring_t` is simple enough that a comment showing what to set to zero suffices. Another option would be to add a macro like the following to kstring.h:

#define EMPTY_KSTRING { 0, 0, NULL }

so that client code could write

kstring_t str = EMPTY_KSTRING;

I would be happy to add a init/destroy comment or such a macro to this pull request.