convert Sb macro to a proper function

...this way we can assert that the provided buffer is actually
null-terminated.
`Sb()` -> `Str_from_buf()`.
This commit is contained in:
tocariimaa 2025-01-13 14:34:25 -03:00
parent 842322bedc
commit 0bedfa70c5

View file

@ -86,8 +86,6 @@ typedef Slice(u8) Str;
/* Creates a `Str` from a string literal */
#define Sl(s) ((Str){ (u8 *)s, (isize)lengthof(s) })
/* Creates a `Str` from a buffer of size `len` */
#define Sb(s, len) ((Str){ (u8 *)s, (isize)len })
/* Creates a `Str` from a C string. */
#define Str_from_c(s) ((Str){ (u8 *)s, (isize)(s != nil ? strlen(s) : 0) })
#define Str_empty(s) ((s).len == 0)
@ -96,6 +94,16 @@ typedef Slice(u8) Str;
int
vsnprintf(char *, unsigned long, const char *, va_list);
/* Creates a `Str` from a buffer of size `len` */
static inline Str
Str_from_buf(u8 *buf, isize len)
{
/* If this triggers an OOB, its because the code that created the string was
* broken anyways. ASan will catch it. */
Assert(buf[len] == '\0');
return (Str){ buf, len };
}
/* "Converts" a `Str` into a C string. Since `Str` are meant to be
* null-terminated already, no conversion is made, but ensures that the
* null terminator is present. */