cstring.h (5317B)
1 /* See LICENSE file for copyright and license details. */ 2 #ifndef _CSTRING_H_ 3 #define _CSTRING_H_ 4 5 #ifdef __cplusplus 6 "C" { 7 #endif /* __cplusplus */ 8 9 #define CSTRING_NPOS -1 10 #define CSTRING_INIT_EMPTY "" 11 #define CSTRING_OUT_OF_BOUNDS(len, pos) ((pos) > (len)) 12 #define CSTRING_ARR_LEN(x) (sizeof((x)) / sizeof((x)[0])) 13 14 typedef struct _cstring { 15 size_t len; /* strlen of str */ 16 size_t capacity; /* total capacity of the array */ 17 char *str; /* the string */ 18 19 #define CSTRING_SORT_ASCENDING 0x01 /* sort in ascending order */ 20 #define CSTRING_SORT_DESCENDING 0x02 /* sort in descending order */ 21 #define CSTRING_SORT_CALLBACK 0x04 /* sort using a callback function */ 22 #define CSTRING_SORT_REST 0x10 /* sort the rest of the array */ 23 } cstring; 24 25 typedef int (*cstring_sort_callback)(const void *, const void *); 26 27 cstring cstring_create(const char *); 28 void cstring_delete(cstring *); 29 void cstring_assign(cstring *, const char *); 30 void cstring_insert(cstring *, const char *, size_t); 31 void cstring_erase(cstring *, size_t, size_t); 32 void cstring_erase_matching(cstring *, const char *); 33 void cstring_erase_all_matching(cstring *, const char *); 34 void cstring_trim(cstring *, const char *); 35 void cstring_push_back(cstring *, char); 36 void cstring_pop_back(cstring *); 37 void cstring_replace_char(cstring *, size_t, char); 38 void cstring_replace_str(cstring *, const char *, size_t, size_t); 39 cstring cstring_substr(const cstring *, size_t, size_t); 40 void cstring_swap(cstring *, cstring *); 41 void cstring_sort_partial(cstring *, size_t, size_t, int, cstring_sort_callback); 42 void cstring_sort_chars_partial(cstring *cs, size_t, size_t, int, cstring_sort_callback); 43 void cstring_clear(cstring *); 44 size_t cstring_find(const cstring *, const char *); 45 size_t cstring_rfind(const cstring *, const char *); 46 size_t cstring_find_first_of(const cstring *, const char *); 47 size_t cstring_find_first_not_of(const cstring *,const char *); 48 size_t cstring_find_last_of(const cstring *, const char *); 49 size_t cstring_find_last_not_of(const cstring *, const char *); 50 int cstring_ends_with_str(const cstring *, const char *); 51 char *cstring_copy(const char *); 52 void cstring_resize(cstring *, size_t); 53 cstring *cstring_getline(FILE *, cstring *, char); 54 55 /* static inlines */ 56 static inline void cstring_prepend(cstring *, const char *); 57 static inline void cstring_append(cstring *, const char *); 58 static inline void cstring_sort(cstring *, size_t, int, cstring_sort_callback); 59 static inline void cstring_sort_chars(cstring *, int, cstring_sort_callback); 60 static inline void cstring_shrink_to_fit(cstring *); 61 static inline int cstring_empty(const cstring *); 62 static inline char cstring_front(const cstring *); 63 static inline char cstring_back(const cstring *); 64 static inline int cstring_starts_with_str(const cstring *, const char *); 65 static inline int cstring_starts_with_char(const cstring *, char); 66 static inline int cstring_ends_with_char(const cstring *, char); 67 static inline void *cstring_data(const cstring *); 68 static inline int cstring_equal(const cstring *, const cstring *); 69 static inline int cstring_greater(const cstring *, const cstring *); 70 static inline int cstring_greater_or_equal(const cstring *, const cstring *); 71 static inline int cstring_less(const cstring *, const cstring *); 72 static inline int cstring_less_or_equal(const cstring *, const cstring *); 73 74 static inline void 75 cstring_prepend(cstring *cs, const char *s) 76 { 77 cstring_insert(cs, s, 0); 78 } 79 80 static inline void 81 cstring_append(cstring *cs, const char *s) 82 { 83 cstring_insert(cs, s, cs->len); 84 } 85 86 static inline void 87 cstring_sort(cstring *cs, size_t len, int flags, cstring_sort_callback cb) 88 { 89 cstring_sort_partial(cs, 0, len, flags, cb); 90 } 91 92 static inline void 93 cstring_sort_chars(cstring *cs, int flags, cstring_sort_callback cb) 94 { 95 cstring_sort_chars_partial(cs, 0, cs->len, flags, cb); 96 } 97 98 static inline void 99 cstring_shrink_to_fit(cstring *cs) 100 { 101 cstring_resize(cs, cs->len); 102 } 103 104 static inline int 105 cstring_empty(const cstring *cs) 106 { 107 return (cs->str == NULL && cs->len == 0); 108 } 109 110 static inline char 111 cstring_front(const cstring *cs) 112 { 113 return (cs->str[0]); 114 } 115 116 static inline char 117 cstring_back(const cstring *cs) 118 { 119 return (!cstring_empty(cs) ? cs->str[cs->len - 1] : cs->str[0]); 120 } 121 122 static inline int 123 cstring_starts_with_str(const cstring *cs, const char *s) 124 { 125 return (cstring_find(cs, s) == 0); 126 } 127 128 static inline int 129 cstring_starts_with_char(const cstring *cs, char c) 130 { 131 return (cs->str[0] == c); 132 } 133 134 static inline int 135 cstring_ends_with_char(const cstring *cs, char c) 136 { 137 return (cs->str[cs->len] == c); 138 } 139 140 static inline void * 141 cstring_data(const cstring *cs) 142 { 143 return ((void *)cs->str); 144 } 145 146 static inline int 147 cstring_equal(const cstring *lhs, const cstring *rhs) 148 { 149 return (strcmp(lhs->str, rhs->str) == 0); 150 } 151 152 static inline int 153 cstring_greater(const cstring *lhs, const cstring *rhs) 154 { 155 return (strcmp(lhs->str, rhs->str) > 0); 156 } 157 158 static inline int 159 cstring_greater_or_equal(const cstring *lhs, const cstring *rhs) 160 { 161 return (strcmp(lhs->str, rhs->str) >= 0); 162 } 163 164 static inline int 165 cstring_less(const cstring *lhs, const cstring *rhs) 166 { 167 return (strcmp(lhs->str, rhs->str) < 0); 168 } 169 170 static inline int 171 cstring_less_or_equal(const cstring *lhs, const cstring *rhs) 172 { 173 return (strcmp(lhs->str, rhs->str) <= 0); 174 } 175 176 #ifdef __cplusplus 177 } 178 #endif /* __cplusplus */ 179 180 #endif /* _CSTRING_H_ */