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 | |
| parent | 54bf1a9db4b5777725dc52096be48d3879aeec82 (diff) | |
| download | libstrvec-c-44b67786e23d4254b918f4ebaae5459f463979e6.tar.gz libstrvec-c-44b67786e23d4254b918f4ebaae5459f463979e6.zip | |
Add strvec_at, strvec_empty, strvec_front, strvec_back
| -rw-r--r-- | include/strvec.h | 36 | ||||
| -rw-r--r-- | libstrvec.api | 1 | ||||
| -rw-r--r-- | libstrvec.lds | 1 | ||||
| -rw-r--r-- | src/strvec.c | 8 |
4 files changed, 45 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) { diff --git a/libstrvec.api b/libstrvec.api index 616ba3c..2701543 100644 --- a/libstrvec.api +++ b/libstrvec.api @@ -12,6 +12,7 @@ strvec_reserve strvec_shrink strvec_stra +strvec_at strvec_len strvec_pushn diff --git a/libstrvec.lds b/libstrvec.lds index 15033b8..84dad30 100644 --- a/libstrvec.lds +++ b/libstrvec.lds @@ -14,6 +14,7 @@ LIBSTRVEC_1.0.0 { strvec_shrink; strvec_stra; + strvec_at; strvec_len; strvec_pushn; diff --git a/src/strvec.c b/src/strvec.c index 13699a3..f6432b9 100644 --- a/src/strvec.c +++ b/src/strvec.c @@ -166,6 +166,14 @@ strvec_stra(struct strvec *strvec) return strvec->vec.data; } +const char ** +strvec_at(struct strvec *strvec, size_t index) +{ + LIBSTRVEC_ABORT_ON_ARGS(!strvec); + + return dvec_at(&strvec->vec, index); +} + size_t strvec_len(struct strvec *strvec) { |
