Sort an array of StrViews with qsort and sv_cmp().
#include "mem/mem.c"
#include "str/str.c"
#include <stdio.h>
#include <stdlib.h>
static int cmp_views(const void *a, const void *b) {
}
int main(void) {
SV(
"Raskolnikov"),
SV(
"Porfiry") };
size_t n = sizeof names / sizeof names[0];
qsort(names, n, sizeof names[0], cmp_views);
for (
size_t i = 0; i < n; i++) printf(
"%.*s\n",
STR_FMT(names[i]));
return EXIT_SUCCESS;
}
#define SV(lit)
View of a string literal, with its length computed at compile time.
#define STR_FMT(s)
printf helper: printf("%.*s", STR_FMT(s)) prints exactly len bytes.
int sv_cmp(StrView a, StrView b)
Byte order like memcmp.
Non-owning view of bytes; handy for passing slices and literals around.