-
Notifications
You must be signed in to change notification settings - Fork 0
/
mstr.h
246 lines (183 loc) · 5.49 KB
/
mstr.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#ifndef MSTR_H
#define MSTR_H
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#define MSTR_FLG_SSO 0
#define MSTR_FLG_HEAP 1
#define MSTR_SSO_CAP (sizeof (mstr_heap_t) - 1)
#define attr_nonnull(...) __attribute__ ((nonnull (__VA_ARGS__)))
typedef union mstr_t mstr_t;
typedef struct mstr_sso_t mstr_sso_t;
typedef struct mstr_heap_t mstr_heap_t;
struct mstr_heap_t
{
size_t cap;
size_t len;
char *data;
};
struct mstr_sso_t
{
unsigned char flg : 1;
unsigned char len : 7;
char data[MSTR_SSO_CAP];
};
union mstr_t
{
mstr_sso_t sso;
mstr_heap_t heap;
};
#define MSTR_INIT \
(mstr_t) {}
#define MSTR_VIEW(src, size) \
(mstr_t) { .heap = { .data = (char *)(src), .len = (size), .cap = 1 }, }
#define mstr_is_sso(str) ((str)->sso.flg == MSTR_FLG_SSO)
#define mstr_is_heap(str) ((str)->sso.flg == MSTR_FLG_HEAP)
#define mstr_cap(str) (mstr_is_sso (str) ? MSTR_SSO_CAP : (str)->heap.cap)
#define mstr_len(str) (mstr_is_sso (str) ? (str)->sso.len : (str)->heap.len)
#define mstr_data(str) (mstr_is_sso (str) ? (str)->sso.data : (str)->heap.data)
#define mstr_at(str, pos) ((pos) < mstr_len (str) ? mstr_data (str)[pos] : -1)
/* free */
extern void mstr_free (mstr_t *str) attr_nonnull (1);
/* clear */
extern void mstr_clear (mstr_t *str) attr_nonnull (1);
/* reserve */
extern mstr_t *mstr_reserve (mstr_t *str, size_t cap) attr_nonnull (1);
/* remove */
extern mstr_t *mstr_remove (mstr_t *str, size_t start, size_t n)
attr_nonnull (1);
/* substr */
extern mstr_t *mstr_substr (mstr_t *save, const mstr_t *from, size_t start,
size_t n) attr_nonnull (1, 2);
/* format */
extern mstr_t *mstr_format (mstr_t *str, const char *fmt, ...)
attr_nonnull (1, 2) __attribute__ ((format (printf, 2, 3)));
/* trim */
extern void mstr_trim (mstr_t *str, const char *s) attr_nonnull (1);
/* is start with */
extern bool mstr_start_with_byte (const mstr_t *str, const void *src, size_t n)
attr_nonnull (1, 2);
static inline bool
mstr_start_with_char (const mstr_t *str, char ch)
{
return mstr_start_with_byte (str, &ch, 1);
}
static inline bool
mstr_start_with_cstr (const mstr_t *str, const char *cstr)
{
return mstr_start_with_byte (str, cstr, strlen (cstr));
}
static inline bool
mstr_start_with_mstr (const mstr_t *str, const mstr_t *other)
{
return mstr_start_with_byte (str, mstr_data (other), mstr_len (other));
}
/* is end with */
extern bool mstr_end_with_byte (const mstr_t *str, const void *src, size_t n)
attr_nonnull (1, 2);
static inline bool
mstr_end_with_char (const mstr_t *str, char ch)
{
return mstr_end_with_byte (str, &ch, 1);
}
static inline bool
mstr_end_with_cstr (const mstr_t *str, const char *cstr)
{
return mstr_end_with_byte (str, cstr, strlen (cstr));
}
static inline bool
mstr_end_with_mstr (const mstr_t *str, const mstr_t *other)
{
return mstr_end_with_byte (str, mstr_data (other), mstr_len (other));
}
/* compare */
extern int mstr_cmp_byte (const mstr_t *str, const void *src, size_t n)
attr_nonnull (1, 2);
static inline int
mstr_cmp_char (const mstr_t *str, char ch)
{
return mstr_cmp_byte (str, &ch, 1);
}
static inline int
mstr_cmp_cstr (const mstr_t *str, const char *cstr)
{
return mstr_cmp_byte (str, cstr, strlen (cstr));
}
static inline int
mstr_cmp_mstr (const mstr_t *str, const mstr_t *other)
{
return mstr_cmp_byte (str, mstr_data (other), mstr_len (other));
}
/* icompare */
extern int mstr_icmp_byte (const mstr_t *str, const void *src, size_t n)
attr_nonnull (1, 2);
static inline int
mstr_icmp_char (const mstr_t *str, char ch)
{
return mstr_icmp_byte (str, &ch, 1);
}
static inline int
mstr_icmp_cstr (const mstr_t *str, const char *cstr)
{
return mstr_icmp_byte (str, cstr, strlen (cstr));
}
static inline int
mstr_icmp_mstr (const mstr_t *str, const mstr_t *other)
{
return mstr_icmp_byte (str, mstr_data (other), mstr_len (other));
}
/* append */
extern mstr_t *mstr_cat_byte (mstr_t *str, const void *src, size_t n)
attr_nonnull (1, 2);
static inline mstr_t *
mstr_cat_char (mstr_t *str, char ch)
{
return mstr_cat_byte (str, &ch, 1);
}
static inline mstr_t *
mstr_cat_cstr (mstr_t *str, const char *cstr)
{
return mstr_cat_byte (str, cstr, strlen (cstr));
}
static inline mstr_t *
mstr_cat_mstr (mstr_t *str, const mstr_t *other)
{
return mstr_cat_byte (str, mstr_data (other), mstr_len (other));
}
/* insert */
extern mstr_t *mstr_insert_byte (mstr_t *str, size_t pos, const void *src,
size_t n) attr_nonnull (1, 3);
static inline mstr_t *
mstr_insert_char (mstr_t *str, size_t pos, char ch)
{
return mstr_insert_byte (str, pos, &ch, 1);
}
static inline mstr_t *
mstr_insert_cstr (mstr_t *str, size_t pos, const char *cstr)
{
return mstr_insert_byte (str, pos, cstr, strlen (cstr));
}
static inline mstr_t *
mstr_insert_mstr (mstr_t *str, size_t pos, const mstr_t *other)
{
return mstr_insert_byte (str, pos, mstr_data (other), mstr_len (other));
}
/* assign */
extern mstr_t *mstr_assign_byte (mstr_t *str, const void *src, size_t n)
attr_nonnull (1, 2);
static inline mstr_t *
mstr_assign_char (mstr_t *str, char ch)
{
return mstr_assign_byte (str, &ch, 1);
}
static inline mstr_t *
mstr_assign_cstr (mstr_t *str, const char *cstr)
{
return mstr_assign_byte (str, cstr, strlen (cstr));
}
static inline mstr_t *
mstr_assign_mstr (mstr_t *str, const mstr_t *other)
{
return mstr_assign_byte (str, mstr_data (other), mstr_len (other));
}
#endif