cstring

Lightweight string library for C
git clone git://git.margiolis.net/cstring.git
Log | Files | Refs | README | LICENSE

cstring.h (5372B)


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