Slice99
0.7.7
Memory slices for C99
|
A slice of some array. More...
Go to the source code of this file.
Data Structures | |
struct | Slice99 |
A slice of some array. More... | |
Macros | |
#define | SLICE99_ASSERT assert |
Like assert . | |
#define | SLICE99_MEMCMP memcmp |
Like memcmp . | |
#define | SLICE99_MEMCPY memcpy |
Like memcpy . | |
#define | SLICE99_MEMMOVE memmove |
Like memmove . | |
#define | SLICE99_STRLEN strlen |
Like strlen . | |
#define | SLICE99_MAJOR 0 |
The major version number. | |
#define | SLICE99_MINOR 7 |
The minor version number. | |
#define | SLICE99_PATCH 7 |
The patch version number. | |
#define | SLICE99_DEF_TYPED(name, T) |
Defines the strongly typed slice name containing items of type T . More... | |
#define | SLICE99_TO_TYPED(self) { .ptr = (self).ptr, .len = (self).len } |
Converts Slice99 to a typed representation. More... | |
#define | SLICE99_TO_UNTYPED(self) Slice99_new((self).ptr, sizeof(*(self).ptr), (self).len) |
Converts the typed slice self to Slice99. More... | |
#define | SLICE99_ARRAY_LEN(...) (sizeof(__VA_ARGS__) / sizeof((__VA_ARGS__)[0])) |
Computes a number of items in an array expression. | |
#define | SLICE99_APPEND(buffer, obj) ((void *)((char *)SLICE99_MEMCPY((buffer), &(obj), sizeof(obj)) + sizeof(obj))) |
Copies obj to buffer , returning the next position of buffer to write to. More... | |
#define | SLICE99_APPEND_ARRAY(buffer, ptr, len) |
Copies the array of length len accessible through ptr into buffer , returning the next position of buffer to write to. More... | |
#define | SLICE99_TO_OCTETS(obj) U8Slice99_new((uint8_t *)&(obj), sizeof(obj)) |
Constructs U8Slice99 from obj . More... | |
#define | Slice99_from_array(...) Slice99_new((void *)(__VA_ARGS__), sizeof((__VA_ARGS__)[0]), SLICE99_ARRAY_LEN(__VA_ARGS__)) |
Constructs a slice from an array expression. More... | |
#define | Slice99_typed_from_array(...) { .ptr = (__VA_ARGS__), .len = SLICE99_ARRAY_LEN(__VA_ARGS__) } |
The same as Slice99_from_array but for typed slices. More... | |
#define | Slice99_from_typed_ptr(ptr, len) Slice99_new(ptr, sizeof(*(ptr)), len) |
Constructs a slice from a pointer of a non-void type and a length. More... | |
#define | CharSlice99_alloca_c_str(self) CharSlice99_c_str((self), alloca((self).len + 1)) |
Makes a null-terminated string out of CharSlice99 using alloca . More... | |
#define | SLICE99_VSPRINTF vsprintf |
Like vsprintf . More... | |
#define | SLICE99_VSNPRINTF vsnprintf |
Like vsnprintf . More... | |
#define | SLICE99_SNPRINTF snprintf |
Like snprintf . More... | |
#define | CharSlice99_alloca_fmt(fmt, ...) CharSlice99_fmt(alloca(SLICE99_SNPRINTF(NULL, 0, (fmt), __VA_ARGS__) + 1), (fmt), __VA_ARGS__) |
Printfs a formatted string to a character slice using alloca . More... | |
Functions | |
static SLICE99_WARN_UNUSED_RESULT Slice99 | Slice99_new (void *ptr, size_t item_size, size_t len) |
Constructs a slice. More... | |
static SLICE99_WARN_UNUSED_RESULT Slice99 | Slice99_from_str (char *str) |
Constructs a slice from str . More... | |
static SLICE99_WARN_UNUSED_RESULT Slice99 | Slice99_from_ptrdiff (void *start, void *end, size_t item_size) |
Constructs a slice residing between start (inclusively) and end (exclusively). More... | |
static SLICE99_WARN_UNUSED_RESULT Slice99 | Slice99_empty (size_t item_size) |
Constructs an empty slice. More... | |
static SLICE99_WARN_UNUSED_RESULT Slice99 | Slice99_update_len (Slice99 self, size_t new_len) |
Updates self with the new length new_len . More... | |
static SLICE99_WARN_UNUSED_RESULT SLICE99_CONST bool | Slice99_is_empty (Slice99 self) |
Checks whether self is empty or not. More... | |
static SLICE99_WARN_UNUSED_RESULT SLICE99_CONST size_t | Slice99_size (Slice99 self) |
Computes a total size in bytes. More... | |
static SLICE99_WARN_UNUSED_RESULT SLICE99_CONST void * | Slice99_get (Slice99 self, ptrdiff_t i) |
Computes a pointer to the i -indexed item. More... | |
static SLICE99_WARN_UNUSED_RESULT SLICE99_CONST void * | Slice99_first (Slice99 self) |
Computes a pointer to the first item. More... | |
static SLICE99_WARN_UNUSED_RESULT SLICE99_CONST void * | Slice99_last (Slice99 self) |
Computes a pointer to the last item. More... | |
static SLICE99_WARN_UNUSED_RESULT Slice99 | Slice99_sub (Slice99 self, ptrdiff_t start_idx, ptrdiff_t end_idx) |
Subslicing self with [start_idx .. end_idx ]. More... | |
static SLICE99_WARN_UNUSED_RESULT Slice99 | Slice99_advance (Slice99 self, ptrdiff_t offset) |
Advances self by offset items. More... | |
static SLICE99_WARN_UNUSED_RESULT bool | Slice99_primitive_eq (Slice99 lhs, Slice99 rhs) |
Performs a byte-by-byte comparison of lhs with rhs . More... | |
static SLICE99_WARN_UNUSED_RESULT bool | Slice99_eq (Slice99 lhs, Slice99 rhs, int(*cmp)(const void *, const void *)) |
Performs a comparison of lhs with rhs with a user-supplied comparator. More... | |
static SLICE99_WARN_UNUSED_RESULT bool | Slice99_primitive_starts_with (Slice99 self, Slice99 prefix) |
Checks whether prefix is a prefix of self , byte-by-byte. More... | |
static SLICE99_WARN_UNUSED_RESULT bool | Slice99_starts_with (Slice99 self, Slice99 prefix, int(*cmp)(const void *, const void *)) |
Checks whether prefix is a prefix of self with a user-supplied comparator. More... | |
static SLICE99_WARN_UNUSED_RESULT bool | Slice99_primitive_ends_with (Slice99 self, Slice99 postfix) |
Checks whether postfix is a postfix of self , byte-by-byte. More... | |
static SLICE99_WARN_UNUSED_RESULT bool | Slice99_ends_with (Slice99 self, Slice99 postfix, int(*cmp)(const void *, const void *)) |
Checks whether postfix is a postfix of self with a user-supplied comparator. More... | |
static void | Slice99_copy (Slice99 self, Slice99 other) |
Copies other to the beginning of self , byte-by-byte. More... | |
static void | Slice99_copy_non_overlapping (Slice99 self, Slice99 other) |
The same as Slice99_copy except that self and other must be non-overlapping. More... | |
static void | Slice99_swap (Slice99 self, ptrdiff_t lhs, ptrdiff_t rhs, void *restrict backup) |
Swaps the lhs -indexed and rhs -indexed items. More... | |
static void | Slice99_swap_with_slice (Slice99 self, Slice99 other, void *restrict backup) |
Swaps all the items in self with those in other . More... | |
static void | Slice99_reverse (Slice99 self, void *restrict backup) |
Reverses the order of items in self . More... | |
static void | Slice99_split_at (Slice99 self, size_t i, Slice99 *restrict lhs, Slice99 *restrict rhs) |
Splits self into two parts. More... | |
static char * | Slice99_c_str (Slice99 self, char out[restrict]) |
Copies self to out and appends '\0' to the end. More... | |
SLICE99_DEF_TYPED (CharSlice99, char) | |
SLICE99_DEF_TYPED (SCharSlice99, signed char) | |
SLICE99_DEF_TYPED (UCharSlice99, unsigned char) | |
SLICE99_DEF_TYPED (ShortSlice99, short) | |
SLICE99_DEF_TYPED (UShortSlice99, unsigned short) | |
SLICE99_DEF_TYPED (IntSlice99, int) | |
SLICE99_DEF_TYPED (UIntSlice99, unsigned int) | |
SLICE99_DEF_TYPED (LongSlice99, long) | |
SLICE99_DEF_TYPED (ULongSlice99, unsigned long) | |
SLICE99_DEF_TYPED (LongLongSlice99, long long) | |
SLICE99_DEF_TYPED (ULongLongSlice99, unsigned long long) | |
SLICE99_DEF_TYPED (FloatSlice99, float) | |
SLICE99_DEF_TYPED (DoubleSlice99, double) | |
SLICE99_DEF_TYPED (LongDoubleSlice99, long double) | |
SLICE99_DEF_TYPED (BoolSlice99, _Bool) | |
SLICE99_DEF_TYPED (U8Slice99, uint8_t) | |
SLICE99_DEF_TYPED (U16Slice99, uint16_t) | |
SLICE99_DEF_TYPED (U32Slice99, uint32_t) | |
SLICE99_DEF_TYPED (U64Slice99, uint64_t) | |
SLICE99_DEF_TYPED (I8Slice99, int8_t) | |
SLICE99_DEF_TYPED (I16Slice99, int16_t) | |
SLICE99_DEF_TYPED (I32Slice99, int32_t) | |
SLICE99_DEF_TYPED (I64Slice99, int64_t) | |
static SLICE99_WARN_UNUSED_RESULT CharSlice99 | CharSlice99_from_str (char *str) |
The same as Slice99_from_str. | |
static char * | CharSlice99_c_str (CharSlice99 self, char out[restrict]) |
The same as Slice99_c_str. | |
static SLICE99_WARN_UNUSED_RESULT SLICE99_FORMAT_HINT_2_0 CharSlice99 | CharSlice99_vfmt (char out[restrict], const char *restrict fmt, va_list list) |
Prints a formatted string to out and returns the corresponding character slice. More... | |
static SLICE99_WARN_UNUSED_RESULT SLICE99_FORMAT_HINT_2_3 CharSlice99 | CharSlice99_fmt (char out[restrict], const char *restrict fmt,...) |
The CharSlice99_vfmt twin. More... | |
static SLICE99_WARN_UNUSED_RESULT SLICE99_FORMAT_HINT_3_0 CharSlice99 | CharSlice99_vnfmt (char out[restrict], size_t bufsz, const char *restrict fmt, va_list list) |
The same as CharSlice99_vfmt but writes at most bufsz - 1 characters. More... | |
static SLICE99_WARN_UNUSED_RESULT SLICE99_FORMAT_HINT_3_4 CharSlice99 | CharSlice99_nfmt (char out[restrict], size_t bufsz, const char *restrict fmt,...) |
The CharSlice99_vnfmt twin. More... | |
A slice of some array.
Some macros are automatically defined in case they have not been defined before including this header file; these are: SLICE99_ASSERT, SLICE99_MEMCMP, SLICE99_MEMCPY, SLICE99_MEMMOVE, SLICE99_STRLEN, SLICE99_VSPRINTF, SLICE99_VSNPRINTF, and SLICE99_SNPRINTF. They represent the corresponding standard library's functions, although actual implementations can differ. If you develop software for a freestanding environment, these macros must be defined beforehand. If you do not want to implement string formatting macros from stdio.h
, define SLICE99_DISABLE_STDIO
and Slice99 will not require them from you.
#define CharSlice99_alloca_c_str | ( | self | ) | CharSlice99_c_str((self), alloca((self).len + 1)) |
Makes a null-terminated string out of CharSlice99
using alloca
.
The same as CharSlice99_c_str, except that the second parameter is allocated using alloca
. Do not use this macro for big strings to avoid stack overflow.
#define CharSlice99_alloca_fmt | ( | fmt, | |
... | |||
) | CharSlice99_fmt(alloca(SLICE99_SNPRINTF(NULL, 0, (fmt), __VA_ARGS__) + 1), (fmt), __VA_ARGS__) |
Printfs a formatted string to a character slice using alloca
.
The same as CharSlice99_fmt, except that the first parameter is allocated using alloca
. Do not use this macro for big strings to avoid stack overflow.
Defined only if SLICE99_DISABLE_STDIO
is not defined.
#define SLICE99_APPEND | ( | buffer, | |
obj | |||
) | ((void *)((char *)SLICE99_MEMCPY((buffer), &(obj), sizeof(obj)) + sizeof(obj))) |
Copies obj
to buffer
, returning the next position of buffer
to write to.
This macro is no different from memcpy
except for the return value. It is useful when you want to write a sequence of objects to some memory buffer, e.g., to a packet header:
[out] | buffer | The memory area to write to. |
[in] | obj | The object (lvalue) that will be copied to buffer , byte-by-byte. |
(void *)((char *)buffer + sizeof(obj))
buffer
must be capable of holding at least sizeof(obj)
bytes. buffer
and obj
must be non-overlapping.#define SLICE99_APPEND_ARRAY | ( | buffer, | |
ptr, | |||
len | |||
) |
Copies the array of length len
accessible through ptr
into buffer
, returning the next position of buffer
to write to.
This function has the same requirements and a return value as of SLICE99_APPEND.
#define SLICE99_DEF_TYPED | ( | name, | |
T | |||
) |
Defines the strongly typed slice name
containing items of type T
.
This macro defines
Also, it specialises all the functions operating on Slice99. Every function (except for Slice99_from_str and Slice99_c_str) is defined by the following rules:
Slice99_*
becomes name_*
.void *
is replaced by T *
.Slice99
is replaced by name
.size_t item_size
parameters are removed (e.g., as in Slice99_new, Slice99_empty, Slice99_from_ptrdiff).Slice99_from_str and Slice99_c_str are derived only for CharSlice99
.
#define Slice99_from_array | ( | ... | ) | Slice99_new((void *)(__VA_ARGS__), sizeof((__VA_ARGS__)[0]), SLICE99_ARRAY_LEN(__VA_ARGS__)) |
Constructs a slice from an array expression.
The resulting slice will have Slice99.len equal to the number of items in the array, and Slice99.item_size equal to the size of each item.
#define Slice99_from_typed_ptr | ( | ptr, | |
len | |||
) | Slice99_new(ptr, sizeof(*(ptr)), len) |
Constructs a slice from a pointer of a non-void
type and a length.
It is equivalent to Slice99_new but it automatically computes an item size as sizeof(*ptr)
.
[in] | ptr | The pointer of the resulting slice. Must not point to void . |
[in] | len | The length of the resulting slice. |
#define SLICE99_SNPRINTF snprintf |
Like snprintf
.
Defined only if it has not been defined previously and SLICE99_DISABLE_STDIO
is not defined.
#define SLICE99_TO_OCTETS | ( | obj | ) | U8Slice99_new((uint8_t *)&(obj), sizeof(obj)) |
Constructs U8Slice99
from obj
.
[in] | obj | The object (lvalue) to which the resulting slice will point to. |
obj
of length sizeof(obj)
. #define SLICE99_TO_TYPED | ( | self | ) | { .ptr = (self).ptr, .len = (self).len } |
Converts Slice99 to a typed representation.
self
must be an expression of type Slice99.#define SLICE99_TO_UNTYPED | ( | self | ) | Slice99_new((self).ptr, sizeof(*(self).ptr), (self).len) |
Converts the typed slice self
to Slice99.
self
must be an expression of type defined by SLICE99_DEF_TYPED.#define Slice99_typed_from_array | ( | ... | ) | { .ptr = (__VA_ARGS__), .len = SLICE99_ARRAY_LEN(__VA_ARGS__) } |
The same as Slice99_from_array but for typed slices.
#define SLICE99_VSNPRINTF vsnprintf |
Like vsnprintf
.
Defined only if it has not been defined previously and SLICE99_DISABLE_STDIO
is not defined.
#define SLICE99_VSPRINTF vsprintf |
Like vsprintf
.
Defined only if it has not been defined previously and SLICE99_DISABLE_STDIO
is not defined.
|
inlinestatic |
The CharSlice99_vfmt twin.
Defined only if SLICE99_DISABLE_STDIO
is not defined.
|
inlinestatic |
The CharSlice99_vnfmt twin.
Defined only if SLICE99_DISABLE_STDIO
is not defined.
|
inlinestatic |
Prints a formatted string to out
and returns the corresponding character slice.
Defined only if SLICE99_DISABLE_STDIO
is not defined.
[out] | out | The buffer that must be capable of holding all characters that will be written by SLICE99_VSPRINTF. |
[in] | fmt | The printf -like format string. |
[in] | list | The variadic function arguments reified into va_list . |
out
.out != NULL
fmt != NULL
|
inlinestatic |
The same as CharSlice99_vfmt but writes at most bufsz - 1
characters.
Defined only if SLICE99_DISABLE_STDIO
is not defined.
|
inlinestatic |
Advances self
by offset
items.
[in] | self | The original slice. |
[in] | offset | The number of items to advance. Can be negative. |
offset
items.offset <= (self).len
self.len
must be representable as ptrdiff_t
.
|
inlinestatic |
Copies self
to out
and appends '\0' to the end.
[in] | self | The slice that will be copied. |
[in] | out | The memory area to which self and the null character will be copied. |
out
.out
must be capable of writing Slice99_size(self) + 1
bytes. out
must not overlap with self
. Copies other
to the beginning of self
, byte-by-byte.
[out] | self | The location to which the whole other will be copied. |
[in] | other | The slice to be copied to self . |
The same as Slice99_copy except that self
and other
must be non-overlapping.
self
and other
must be non-overlapping.
|
inlinestatic |
Constructs an empty slice.
[in] | item_size | The value of Slice99::item_size. |
item_size > 0
|
inlinestatic |
Checks whether postfix
is a postfix of self
with a user-supplied comparator.
[in] | self | The slice to be checked for postfix . |
[in] | postfix | The slice to be checked whether it is a postfix of self . |
[in] | cmp | The function deciding whether two items are equal ot not (0 if equal, any other value otherwise). |
true
if postfix
is a postfix of self
, otherwise false
.self.item_size == postfix.item_size
cmp != NULL
self.len
and postfix.len
must be representable as ptrdiff_t
.
|
inlinestatic |
Performs a comparison of lhs
with rhs
with a user-supplied comparator.
[in] | lhs | The first slice to be compared. |
[in] | rhs | The second slice to be compared. |
[in] | cmp | The function deciding whether two items are equal ot not (0 if equal, any other value otherwise). |
true
if lhs
and rhs
are equal, false
otherwise.lhs.item_size == rhs.item_size
cmp != NULL
lhs.len
and rhs.len
must be representable as ptrdiff_t
.
|
inlinestatic |
Computes a pointer to the first item.
[in] | self | The slice upon which the pointer will be computed. |
|
inlinestatic |
Constructs a slice residing between start
(inclusively) and end
(exclusively).
[in] | start | The start position of a returned slice, inclusively. |
[in] | end | The end position of a returned slice, exlusively. |
[in] | item_size | The value of Slice99::item_size. |
start != NULL
end != NULL
((char *)end - (char *)start) >= 0
(((char *)end - (char *)start)) % item_size == 0
|
inlinestatic |
Constructs a slice from str
.
[in] | str | Any null-terminated string. |
str != NULL
|
inlinestatic |
Computes a pointer to the i
-indexed item.
[in] | self | The slice upon which the pointer will be computed. |
[in] | i | The index of a desired item. Can be negative. |
self.item_size
must be representable as ptrdiff_t
.
|
inlinestatic |
Checks whether self
is empty or not.
[in] | self | The checked slice. |
true
if self
is empty, otherwise false
.
|
inlinestatic |
Computes a pointer to the last item.
[in] | self | The slice upon which the pointer will be computed. |
self.len
must be representable as ptrdiff_t
.
|
inlinestatic |
Constructs a slice.
[in] | ptr | The value of Slice99::ptr. |
[in] | item_size | The value of Slice99::item_size. |
[in] | len | The value of Slice99::len. |
ptr != NULL
item_size > 0
|
inlinestatic |
Checks whether postfix
is a postfix of self
, byte-by-byte.
[in] | self | The slice to be checked for postfix . |
[in] | postfix | The slice to be checked whether it is a postfix of self . |
true
if postfix
is a postfix of self
, otherwise false
.self.len
and postfix.len
must be representable as ptrdiff_t
.
|
inlinestatic |
Performs a byte-by-byte comparison of lhs
with rhs
.
[in] | lhs | The first slice to be compared. |
[in] | rhs | The second slice to be compared. |
true
if lhs
and rhs
are equal, false
otherwise.
|
inlinestatic |
Checks whether prefix
is a prefix of self
, byte-by-byte.
[in] | self | The slice to be checked for prefix . |
[in] | prefix | The slice to be checked whether it is a prefix of self . |
true
if prefix
is a prefix of self
, otherwise false
.prefix.len
must be representable as ptrdiff_t
.
|
inlinestatic |
Reverses the order of items in self
.
[out] | self | The slice to be reversed. |
[out] | backup | The memory area of self.item_size bytes accessible for reading and writing. |
backup != NULL
self.len
must be representable as ptrdiff_t
.
|
inlinestatic |
Computes a total size in bytes.
[in] | self | The slice whose size is to be computed. |
|
inlinestatic |
Splits self
into two parts.
[in] | self | The slice to be splitted into lhs and rhs . |
[in] | i | The index at which self will be splitted. |
[out] | lhs | The first part of self indexed as [0; i ). |
[out] | rhs | The second part of self indexed as [i ; self.len ). |
i <= self.len
lhs != NULL
rhs != NULL
self.len
and i
must be representable as ptrdiff_t
.
|
inlinestatic |
Checks whether prefix
is a prefix of self
with a user-supplied comparator.
[in] | self | The slice to be checked for prefix . |
[in] | prefix | The slice to be checked whether it is a prefix of self . |
[in] | cmp | The function deciding whether two items are equal ot not (0 if equal, any other value otherwise). |
true
if prefix
is a prefix of self
, otherwise false
.self.item_size == prefix.item_size
cmp != NULL
prefix.len
must be representable as ptrdiff_t
.
|
inlinestatic |
Subslicing self
with [start_idx
..
end_idx
].
[in] | self | The original slice. |
[in] | start_idx | The index at which a new slice will reside, inclusively. |
[in] | end_idx | The index at which a new slice will end, exclusively. |
start_idx <= end_idx
|
inlinestatic |
Swaps the lhs
-indexed and rhs
-indexed items.
[out] | self | The slice in which lhs and rhs will be swapped. |
[in] | lhs | The index of the first item. |
[in] | rhs | The index of the second item. |
[out] | backup | The memory area of self.item_size bytes accessible for reading and writing. |
backup != NULL
backup
must not overlap with Slice99_get(self, lhs)
and Slice99_get(self, rhs)
. Slice99_get(self, lhs)
and Slice99_get(self, rhs)
must not overlap.
|
inlinestatic |
Swaps all the items in self
with those in other
.
[out] | self | The first slice to be swapped. |
[out] | other | The second slice to be swapped. |
[out] | backup | The memory area of self.item_size bytes accessible for reading and writing. |
self.len == other.len
self.item_size == other.item_size
backup
must not overlap with self
and other
. self
and other
must not overlap. self.len
must be representable as ptrdiff_t
.