strvec.h (3701B)
1#pragma once 2 3#include "dvec.h" 4#include "allocator.h" 5 6#include <stddef.h> 7#include <stdbool.h> 8 9#define STRVEC_ITER(strvec, p) (p) = NULL; ((p) = strvec_iter_fwd((strvec), (p))); 10#define STRVEC_ITER_BWD(strvec, p) (p) = NULL; ((p) = strvec_iter_bwd((strvec), (p))); 11 12#ifdef LIBSTRVEC_ASSERT_ARGS 13#include "stdlib.h" 14#define LIBSTRVEC_ABORT_ON_ARGS(cond) do { if (cond) abort(); } while (0) 15#else 16#define LIBSTRVEC_ABORT_ON_ARGS(cond) 17#endif 18 19#ifdef LIBSTRVEC_ASSERT_ALLOC 20#include "stdlib.h" 21#define LIBSTRVEC_ABORT_ON_ALLOC(cond) do { if (cond) abort(); } while (0) 22#else 23#define LIBSTRVEC_ABORT_ON_ALLOC(cond) 24#endif 25 26struct strvec { 27 struct dvec vec; 28}; 29 30int strvec_init(struct strvec *strvec, size_t cap, 31 const struct allocator *allocator); 32int strvec_deinit(struct strvec *strvec); 33 34struct strvec *strvec_alloc(size_t reserved, 35 const struct allocator *allocator, int *rc); 36int strvec_free(struct strvec *strvec); 37 38int strvec_copy(struct strvec *dst, struct strvec *src, 39 const struct allocator *allocator); 40void strvec_move(struct strvec *dst, struct strvec *src); 41void strvec_swap(struct strvec *dst, struct strvec *src); 42 43void strvec_clear(struct strvec *strvec); 44int strvec_reserve(struct strvec *strvec, size_t cap); 45int strvec_shrink(struct strvec *strvec); 46 47static inline const char **strvec_stra(struct strvec *strvec); 48 49static inline const char **strvec_at(struct strvec *strvec, size_t index); 50static inline const char *strvec_get(struct strvec *strvec, size_t index); 51static inline const char *strvec_front(struct strvec *strvec); 52static inline const char *strvec_back(struct strvec *strvec); 53static inline size_t strvec_len(struct strvec *strvec); 54static inline bool strvec_empty(struct strvec *strvec); 55 56int strvec_pushn(struct strvec *strvec, const char **str, size_t n); 57static inline int strvec_push(struct strvec *strvec, const char *str); 58const char **strvec_popn(struct strvec *strvec, size_t n); 59static inline const char *strvec_pop(struct strvec *strvec); 60void strvec_replace(struct strvec *strvec, size_t index, const char *str); 61void strvec_remove(struct strvec *strvec, size_t index, size_t n); 62int strvec_remove_str(struct strvec *strvec, const char *str, 63 const struct allocator *allocator); 64ssize_t strvec_find(struct strvec *strvec, size_t start, const char *str); 65 66char *strvec_join(struct strvec *strvec, const char *sep, 67 const struct allocator *allocator, int *rc); 68 69const char **strvec_iter_fwd(const struct strvec *strvec, const char **p); 70const char **strvec_iter_bwd(const struct strvec *strvec, const char **p); 71 72static inline const char ** 73strvec_stra(struct strvec *strvec) 74{ 75 LIBSTRVEC_ABORT_ON_ARGS(!strvec && !strvec->vec.data); 76 77 return (const char **)strvec->vec.data; 78} 79 80static inline const char ** 81strvec_at(struct strvec *strvec, size_t index) 82{ 83 LIBSTRVEC_ABORT_ON_ARGS(!strvec); 84 85 return dvec_at(&strvec->vec, index); 86} 87 88static inline size_t 89strvec_len(struct strvec *strvec) 90{ 91 LIBSTRVEC_ABORT_ON_ARGS(!strvec); 92 93 return strvec->vec.len; 94} 95 96static inline bool 97strvec_empty(struct strvec *strvec) 98{ 99 return strvec_len(strvec) == 0; 100} 101 102static inline const char * 103strvec_get(struct strvec *strvec, size_t index) 104{ 105 return *strvec_at(strvec, index); 106} 107 108static inline const char * 109strvec_front(struct strvec *strvec) 110{ 111 return *strvec_at(strvec, 0); 112} 113 114static inline const char * 115strvec_back(struct strvec *strvec) 116{ 117 LIBSTRVEC_ABORT_ON_ARGS(!strvec || strvec_empty(strvec)); 118 119 return *strvec_at(strvec, strvec_len(strvec) - 1); 120} 121 122static inline int 123strvec_push(struct strvec *strvec, const char *str) 124{ 125 return strvec_pushn(strvec, &str, 1); 126} 127 128static inline const char * 129strvec_pop(struct strvec *strvec) 130{ 131 return *strvec_popn(strvec, 1); 132}