MULE
0.0.1
Multi-Use Library Essentials
Toggle main menu visibility
Loading...
Searching...
No Matches
01_build.c
Build a
Str
with appends that chain with
&&
, print it two ways, free it.
/* Str on its own: build a string, read it, free it. */
#include "mem/mem.c"
#include "str/str.c"
#include <stdio.h>
#include <stdlib.h>
int
main(
void
) {
Str
s = { 0 };
/* A failed call leaves `s` unchanged, so && stops at the first failure. */
bool
ok =
str_append_cstr
(&s,
"Rodion"
) &&
str_push
(&s,
' '
) &&
str_append
(&s,
SV
(
"Raskolnikov"
)) &&
str_appendf
(&s,
", former student, age %d"
, 23);
if
(!ok) {
str_free
(&s);
return
EXIT_FAILURE;
}
printf(
"%.*s\n"
,
STR_FMT
(s));
printf(
"len %zu, capacity %zu\n"
, s.
len
, s.
capacity
);
puts(
str_cstr
(&s));
str_free
(&s);
return
EXIT_SUCCESS;
}
Str::capacity
size_t capacity
Content bytes that fit before the next realloc.
Definition
str.h:67
Str::len
size_t len
Bytes of content, excluding the terminator.
Definition
str.h:66
str_cstr
const char * str_cstr(const Str *s)
NUL-terminated contents of s, for C APIs such as fopen and puts.
Definition
str.c:216
SV
#define SV(lit)
View of a string literal, with its length computed at compile time.
Definition
str.h:80
STR_FMT
#define STR_FMT(s)
printf helper: printf("%.*s", STR_FMT(s)) prints exactly len bytes.
Definition
str.h:85
str_free
void str_free(Str *s)
Free the buffer and reset s to the zero state.
Definition
str.c:50
str_appendf
bool str_appendf(Str *s, const char *fmt,...)
Append the printf-style expansion of fmt.
Definition
str.c:123
str_push
bool str_push(Str *s, char c)
Append the single byte c.
Definition
str.c:76
str_append
bool str_append(Str *s, StrView v)
Append v.
Definition
str.c:89
str_append_cstr
bool str_append_cstr(Str *s, const char *cstr)
Append cstr.
Definition
str.c:119
Str
Owned, growable byte string.
Definition
str.h:64
Generated by
1.18.0