diff options
| author | Louis Burda <quent.burda@gmail.com> | 2023-05-13 21:12:17 +0200 |
|---|---|---|
| committer | Louis Burda <quent.burda@gmail.com> | 2023-05-13 21:12:17 +0200 |
| commit | 9fe89a48bb89dc6740787c9ed8c95b5339bba905 (patch) | |
| tree | bca73651945caef5e188328a538ca782470c43dd /include | |
| parent | 110b99563e127abe1439fffb0084400b47eea5b3 (diff) | |
| download | libstrvec-c-9fe89a48bb89dc6740787c9ed8c95b5339bba905.tar.gz libstrvec-c-9fe89a48bb89dc6740787c9ed8c95b5339bba905.zip | |
Add initial version
Diffstat (limited to 'include')
| -rw-r--r-- | include/strvec.h | 60 |
1 files changed, 55 insertions, 5 deletions
diff --git a/include/strvec.h b/include/strvec.h index 21624e9..a88931b 100644 --- a/include/strvec.h +++ b/include/strvec.h @@ -2,14 +2,64 @@ #include "allocator.h" -#include <stdlib.h> +#define STRVEC_ITER(strvec, p) (p) = NULL; ((p) = strvec_iter_fwd((strvec), (p))); +#define STRVEC_ITER_BWD(strvec, p) (p) = NULL; ((p) = strvec_iter_bwd((strvec), (p))); + + +#ifdef LIBSTRVEC_ASSERT_ARGS +#include "stdlib.h" +#define LIBSTRVEC_ABORT_ON_ARGS(cond) do { if (cond) abort(); } while (0) +#else +#define LIBSTRVEC_ABORT_ON_ARGS(cond) +#endif + +#ifdef LIBSTRVEC_ASSERT_ALLOC +#include "stdlib.h" +#define LIBSTRVEC_ABORT_ON_ALLOC(cond) do { if (cond) abort(); } while (0) +#else +#define LIBSTRVEC_ABORT_ON_ALLOC(cond) +#endif struct strvec; -void strvec_init(struct strvec *strvec, size_t cap, struct allocator *allocator); -void strvec_deinit(struct strvec *strvec); +int strvec_init(struct strvec *strvec, size_t cap, struct allocator *allocator); +int strvec_deinit(struct strvec *strvec); + +struct strvec *strvec_alloc(size_t reserved, struct allocator *allocator, int *rc); +int strvec_free(struct strvec *strvec); + +int strvec_copy(struct strvec *dst, struct strvec *src, + const struct allocator *allocator); +void strvec_move(struct strvec *dst, struct strvec *src); +void strvec_swap(struct strvec *dst, struct strvec *src); + +void strvec_clear(struct strvec *strvec); +int strvec_reserve(struct strvec *strvec, size_t cap); +int strvec_shrink(struct strvec *strvec); + +const char **strvec_stra(struct strvec *strvec); +size_t strvec_len(struct strvec *strvec); + +int strvec_pushn(struct strvec *strvec, const char **str, size_t n); +static inline int strvec_push(struct strvec *strvec, const char *str); +const char **strvec_popn(struct strvec *strvec, size_t n); +static inline const char *strvec_pop(struct strvec *strvec); +void strvec_replace(struct strvec *strvec, size_t index, const char *str); +void strvec_remove(struct strvec *strvec, size_t index, size_t n); + +const char **strvec_iter_fwd(const struct strvec *strvec, const char **p); +const char **strvec_iter_bwd(const struct strvec *strvec, const char **p); -struct strvec *strvec_alloc(size_t reserved, struct allocator *allocator); -void strvec_free(struct strvec *strvec); +static inline int +strvec_push(struct strvec *strvec, const char *str) +{ + return strvec_pushn(strvec, &str, 1); +} +static inline const char * +strvec_pop(struct strvec *strvec) +{ + return *strvec_popn(strvec, 1); +} +extern const size_t strvec_dsize; |
