-
Notifications
You must be signed in to change notification settings - Fork 0
/
qstr.c
202 lines (181 loc) · 4.13 KB
/
qstr.c
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
/* See LICENSE file for copyright and license details. */
#include <qstr.h>
#include <stdarg.h>
#include <stdio.h>
#ifndef QSTR_ALLOCATOR
#include <stdlib.h>
#define QSTR_ALLOCATOR &malloc
#endif
#ifndef QSTR_REALLOCATOR
#include <stdlib.h>
#define QSTR_REALLOCATOR &realloc
#endif
#ifndef QSTR_MEMCPY
#include <string.h>
#define QSTR_MEMCPY &memcpy
#endif
#ifndef QSTR_DEALLOC
#include <stdlib.h>
#define QSTR_DEALLOC &free
#endif
static qstr_alloc __qstr_allocator = QSTR_ALLOCATOR;
static qstr_realloc __qstr_reallocator = QSTR_REALLOCATOR;
static qstr_memcpy __qstr_memcpy = QSTR_MEMCPY;
static qstr_dealloc __qstr_dealloc = QSTR_DEALLOC;
/**
* We get qsthdr when we minus `str` with sizeof(qsthdr)
* as the memory is continguous.
*/
static inline qstrhdr* qstrtoqstrhdr(const qstr str) {
return (qstrhdr*) (str - sizeof(qstrhdr));
}
/**
* We get qstr when we add `1` with qsthdr
* as the memory is continguous.
*/
static inline qstr qstrhdrtoqstr(const qstrhdr* hdr) {
return (qstr) (hdr + 1);
}
/**
* Contructs a new qstrhdr with size `n`.
* TODO check if '*' is necessary when using inline funcs,
* to prevent copies
*/
static inline qstrhdr* qstrnewalloc(size_t* n) {
return (qstrhdr*)__qstr_allocator(
sizeof(qstrhdr) // size of the header
+ (sizeof(qstr) * (*n)) // actual size of the string
+ 1 // space for the null character
);
}
qstr qstrmalloc(size_t n) {
qstrhdr* hdr = qstrnewalloc(&n);
*hdr = n;
return qstrhdrtoqstr(hdr);
}
qstr qstrnnew(const char* str, size_t n) {
qstrhdr* hdr = qstrnewalloc(&n);
*hdr = n;
char* destptr = qstrhdrtoqstr(hdr);
memcpy(destptr, str, n + 1);
return destptr;
}
qstr qstrnew(const char* str) {
if (str == NULL) {
return NULL;
}
size_t n = strlen(str);
return qstrnnew(str, n);
}
qstr qstrrealloc(const qstr str, size_t newsize) {
qstrhdr* hdr = NULL;
if (str != NULL) {
hdr = qstrtoqstrhdr(str);
}
hdr = (qstrhdr*)__qstr_reallocator(hdr, sizeof(qstrhdr) + newsize + 1);
*hdr = newsize;
return qstrhdrtoqstr(hdr);
}
void qstrfree(qstr str) {
if (str == NULL) {
return;
}
qstrhdr* hdr = qstrtoqstrhdr(str);
__qstr_dealloc(hdr);
}
qstr qstrdup(const qstr oldstr) {
qstrhdr* oldhdr = qstrtoqstrhdr(oldstr);
size_t newsize = sizeof(qstrhdr) + *oldhdr + 1;
qstrhdr* newhdr = __qstr_allocator(newsize);
__qstr_memcpy(newhdr, oldhdr, newsize);
return qstrhdrtoqstr(newhdr);
}
int qstrcpy(qstr dest, const qstr src) {
size_t destlen = qstrlen(dest);
size_t srclen = qstrlen(src);
if (destlen >= srclen) {
memcpy(dest, src, srclen);
return srclen;
} else {
memcpy(dest, src, destlen);
return destlen;
}
}
size_t qstrchr(const qstr cstr, int c, char** ptr) {
size_t slen = qstrlen(cstr);
size_t i = 0;
for (; i < slen; i++) {
if(cstr[i] == c) {
if (ptr != NULL) {
*ptr = cstr + i;
}
return i;
}
}
return 0;
}
size_t qstrrchr(const qstr cstr, int c, char** ptr) {
size_t slen = qstrlen(cstr);
size_t i = slen;
do {
if(cstr[i] == c) {
if (ptr != NULL) {
*ptr = cstr + i;
}
return i;
}
i--;
} while (i > 0);
// TODO handle this part gracefully
if (i == 1) {
i--;
if(cstr[i] == c) {
if (ptr != NULL) {
*ptr = cstr + i;
}
return i;
}
}
return -1;
}
int qstrcmp(const qstr str1, const qstr str2) {
size_t i = 0;
size_t sz1 = qstrlen(str1);
size_t sz2 = qstrlen(str2);
if (sz1 != sz2) {
return 0;
}
for(;(i < sz1) && (str1[i] == str2[i]); i++);
int res = i == sz1;
return res? 0 : res;
}
qstr qstrcat(const qstr str1, const qstr str2) {
size_t sz1 = qstrlen(str1);
size_t sz2 = qstrlen(str2);
size_t sz3 = sz1 + sz2;
qstr cqstr = qstrmalloc(sz3);
qstrhdr* hdr3 = qstrtoqstrhdr(cqstr);
*hdr3 = sz3;
__qstr_memcpy(cqstr, str1, sz1);
__qstr_memcpy(cqstr + sz1, str2, sz2);
return cqstr;
}
int qstrsprintf(qstr* dest, const char* format, ...) {
if (dest == NULL) {
return 0;
}
va_list ap;
va_start(ap, format);
// TODO check if this works everywhere
int n = vsnprintf(NULL, 0, format, ap);
va_end(ap);
va_start(ap, format);
qstr newmem = qstrmalloc(n);
int m = vsnprintf(newmem, n + 1, format, ap);
va_end(ap);
if (*dest != NULL) {
qstrfree(*dest);
}
*dest = newmem;
return m == n;
}