diff options
| author | Louis Burda <quent.burda@gmail.com> | 2023-05-15 16:45:21 +0200 |
|---|---|---|
| committer | Louis Burda <quent.burda@gmail.com> | 2023-05-15 16:45:21 +0200 |
| commit | 44b67786e23d4254b918f4ebaae5459f463979e6 (patch) | |
| tree | 19291148480433fccce746da76752456b474ec63 /include | |
| parent | 54bf1a9db4b5777725dc52096be48d3879aeec82 (diff) | |
| download | libstrvec-c-44b67786e23d4254b918f4ebaae5459f463979e6.tar.gz libstrvec-c-44b67786e23d4254b918f4ebaae5459f463979e6.zip | |
Add strvec_at, strvec_empty, strvec_front, strvec_back
Diffstat (limited to 'include')
| -rw-r--r-- | include/strvec.h | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/include/strvec.h b/include/strvec.h index a88931b..648487c 100644 --- a/include/strvec.h +++ b/include/strvec.h @@ -2,10 +2,11 @@ #include "allocator.h" +#include <stdbool.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) @@ -38,7 +39,14 @@ int strvec_reserve(struct strvec *strvec, size_t cap); int strvec_shrink(struct strvec *strvec); const char **strvec_stra(struct strvec *strvec); + +const char **strvec_at(struct strvec *strvec, size_t index); +static inline const char *strvec_get(struct strvec *strvec, size_t index); +static inline const char *strvec_front(struct strvec *strvec); +static inline const char *strvec_back(struct strvec *strvec); size_t strvec_len(struct strvec *strvec); +static inline bool strvec_empty(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); @@ -50,6 +58,32 @@ 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); +static inline bool +strvec_empty(struct strvec *strvec) +{ + return strvec_len(strvec) == 0; +} + +static inline const char * +strvec_get(struct strvec *strvec, size_t index) +{ + return *strvec_at(strvec, index); +} + +static inline const char * +strvec_front(struct strvec *strvec) +{ + return *strvec_at(strvec, 0); +} + +static inline const char * +strvec_back(struct strvec *strvec) +{ + LIBSTRVEC_ABORT_ON_ARGS(!strvec || strvec_empty(strvec)); + + return *strvec_at(strvec, strvec_len(strvec) - 1); +} + static inline int strvec_push(struct strvec *strvec, const char *str) { |
