80#define SV(lit) ((StrView){ .data = "" lit "", .len = sizeof(lit) - 1 })
85#define STR_FMT(s) (int)(s).len, (s).data
157MULE_NODISCARD
bool str_appendf(
Str *s,
const char *fmt, ...) MULE_PRINTF(2, 3);
217#define SV_NPOS SIZE_MAX
Where a module gets its memory.
size_t len
Number of bytes in the view.
size_t capacity
Content bytes that fit before the next realloc.
const char * data
First byte.
const Allocator * alloc
Source of data.
size_t len
Bytes of content, excluding the terminator.
char * data
Buffer, or null when nothing is allocated.
const char * str_cstr(const Str *s)
NUL-terminated contents of s, for C APIs such as fopen and puts.
size_t sv_find(StrView v, StrView needle)
Index of the first needle in v.
bool str_shrink_to_fit(Str *s)
Shrink the allocation to exactly len + 1 bytes.
StrView sv_from_cstr(const char *cstr)
View of cstr, measured with strlen.
bool sv_parse_f64(StrView v, double *out)
Parse all of v as a double, in any form strtod accepts in the C locale.
void str_clear(Str *s)
Set the length to zero.
StrView str_view(const Str *s)
Borrow the whole of s.
bool sv_token_next(StrView *rest, StrView delims, StrView *token)
strtok without the mutation: skip delims, then cut the next run of other bytes.
bool str_from(Str *s, StrView v)
Initialise s as a copy of v.
void str_free(Str *s)
Free the buffer and reset s to the zero state.
bool str_append_file(Str *s, FILE *f)
Append everything left to read from f.
bool str_appendf(Str *s, const char *fmt,...)
Append the printf-style expansion of fmt.
bool str_push(Str *s, char c)
Append the single byte c.
StrView sv_trim(StrView v)
v without leading and trailing ASCII whitespace: space, \t, \n, \v, \f and \r.
bool sv_ends_with(StrView v, StrView suffix)
True when v ends with suffix.
bool str_read_line(Str *s, FILE *f)
Replace the contents of s with the next line of f, without its \n.
bool str_reserve(Str *s, size_t additional)
Ensure additional more bytes fit without another allocation.
bool str_append(Str *s, StrView v)
Append v.
bool sv_starts_with(StrView v, StrView prefix)
True when v begins with prefix.
bool str_eq(StrView a, StrView b)
True when a and b hold the same bytes.
bool sv_split_next(StrView *rest, StrView sep, StrView *piece)
Cut the piece before sep off rest.
bool sv_parse_i64(StrView v, int64_t *out)
Parse all of v as a base-10 integer with an optional sign.
bool str_from_cstr(Str *s, const char *cstr)
Initialise s as a copy of cstr.
bool str_with_capacity(Str *s, size_t capacity)
Initialise s as empty with room for capacity bytes of content.
StrView sv_slice(StrView v, size_t from, size_t to)
Bytes from up to but not including to.
int sv_cmp(StrView a, StrView b)
Byte order like memcmp.
size_t sv_find_byte(StrView v, char c)
Index of the first c in v.
bool str_vappendf(Str *s, const char *fmt, va_list ap)
str_appendf() for a va_list, like vprintf.
bool str_append_cstr(Str *s, const char *cstr)
Append cstr.
Owned, growable byte string.
Non-owning view of bytes; handy for passing slices and literals around.