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:
parent
842322bedc
commit
0bedfa70c5
1 changed files with 10 additions and 2 deletions
|
@ -86,8 +86,6 @@ typedef Slice(u8) Str;
|
||||||
|
|
||||||
/* Creates a `Str` from a string literal */
|
/* Creates a `Str` from a string literal */
|
||||||
#define Sl(s) ((Str){ (u8 *)s, (isize)lengthof(s) })
|
#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. */
|
/* Creates a `Str` from a C string. */
|
||||||
#define Str_from_c(s) ((Str){ (u8 *)s, (isize)(s != nil ? strlen(s) : 0) })
|
#define Str_from_c(s) ((Str){ (u8 *)s, (isize)(s != nil ? strlen(s) : 0) })
|
||||||
#define Str_empty(s) ((s).len == 0)
|
#define Str_empty(s) ((s).len == 0)
|
||||||
|
@ -96,6 +94,16 @@ typedef Slice(u8) Str;
|
||||||
int
|
int
|
||||||
vsnprintf(char *, unsigned long, const char *, va_list);
|
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
|
/* "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-terminated already, no conversion is made, but ensures that the
|
||||||
* null terminator is present. */
|
* null terminator is present. */
|
||||||
|
|
Loading…
Add table
Reference in a new issue