summaryrefslogtreecommitdiff
path: root/usr/src/lib/libdemangle/common/str.c
blob: 014ce8a737121a729d3c59ddf6fdbafee5004880 (plain)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2017 Jason King
 */
#include <sys/debug.h>
#include <sys/sysmacros.h>
#include <string.h>
#include "str.h"
#include "demangle_int.h"

#define	STR_CHUNK_SZ	(64U)

/* are we storing a reference vs. a dynamically allocated copy? */
#define	IS_REF(s) ((s)->str_s != NULL && (s)->str_size == 0)

/*
 * Dynamically resizeable strings, with lazy allocation when initialized
 * with a constant string value
 *
 * NOTE: these are not necessairly 0-terminated
 *
 * Additionally, these can store references instead of copies of strings
 * (as indicated by the IS_REF() macro.  However mutation may cause a
 * string to convert from a refence to a dynamically allocated copy.
 */

void
str_init(str_t *restrict s, sysdem_ops_t *restrict ops)
{
	(void) memset(s, 0, sizeof (*s));
	s->str_ops = (ops != NULL) ? ops : sysdem_ops_default;
}

void
str_fini(str_t *s)
{
	if (s == NULL)
		return;
	if (!IS_REF(s))
		xfree(s->str_ops, s->str_s, s->str_size);
	(void) memset(s, 0, sizeof (*s));
}

size_t
str_length(const str_t *s)
{
	return (s->str_len);
}

/*
 * store as a reference instead of a copy
 * if len == 0, means store entire copy of 0 terminated string
 */
void
str_set(str_t *s, const char *cstr, size_t len)
{
	sysdem_ops_t *ops = s->str_ops;

	str_fini(s);
	s->str_ops = ops;
	s->str_s = (char *)cstr;
	s->str_len = (len == 0 && cstr != NULL) ? strlen(cstr) : len;
}

boolean_t
str_copy(const str_t *src, str_t *dest)
{
	str_fini(dest);
	str_init(dest, src->str_ops);

	if (src->str_len == 0)
		return (B_TRUE);

	size_t len = roundup(src->str_len, STR_CHUNK_SZ);
	dest->str_s = zalloc(src->str_ops, len);
	if (dest->str_s == NULL)
		return (B_FALSE);

	(void) memcpy(dest->str_s, src->str_s, src->str_len);
	dest->str_len = src->str_len;
	dest->str_size = len;

	return (B_TRUE);
}

/*
 * ensure s has at least amt bytes free, resizing if necessary
 */
static boolean_t
str_reserve(str_t *s, size_t amt)
{
	size_t newlen = s->str_len + amt;

	/* overflow check */
	if (newlen < s->str_len || newlen < amt)
		return (B_FALSE);

	if ((amt > 0) && (s->str_len + amt <= s->str_size))
		return (B_TRUE);

	size_t newsize = roundup(newlen, STR_CHUNK_SZ);
	void *temp;

	if (IS_REF(s)) {
		temp = zalloc(s->str_ops, newsize);
		if (temp == NULL)
			return (B_FALSE);

		(void) memcpy(temp, s->str_s, s->str_len);
	} else {
		temp = xrealloc(s->str_ops, s->str_s, s->str_size, newsize);
		if (temp == NULL)
			return (B_FALSE);
	}

	s->str_s = temp;
	s->str_size = newsize;

	return (B_TRUE);
}

/* append to s, cstrlen == 0 means entire length of string */
boolean_t
str_append(str_t *s, const char *cstr, size_t cstrlen)
{
	if (cstr != NULL && cstrlen == 0)
		cstrlen = strlen(cstr);

	const str_t src = {
		.str_s = (char *)cstr,
		.str_len = cstrlen,
		.str_ops = s->str_ops
	};

	return (str_append_str(s, &src));
}

boolean_t
str_append_str(str_t *dest, const str_t *src)
{
	/* empty string is a noop */
	if (src->str_s == NULL || src->str_len == 0)
		return (B_TRUE);

	/* if src is a reference, we can just copy that */
	if (dest->str_s == NULL && IS_REF(src)) {
		*dest = *src;
		return (B_TRUE);
	}

	if (!str_reserve(dest, src->str_len))
		return (B_FALSE);

	(void) memcpy(dest->str_s + dest->str_len, src->str_s, src->str_len);
	dest->str_len += src->str_len;
	return (B_TRUE);
}

boolean_t
str_append_c(str_t *s, char c)
{
	if (!str_reserve(s, 1))
		return (B_FALSE);

	s->str_s[s->str_len++] = c;
	return (B_TRUE);
}

boolean_t
str_insert(str_t *s, size_t idx, const char *cstr, size_t cstrlen)
{
	if (cstr == NULL)
		return (B_TRUE);

	if (cstrlen == 0)
		cstrlen = strlen(cstr);

	str_t src = {
		.str_s = (char *)cstr,
		.str_len = cstrlen,
		.str_ops = s->str_ops,
		.str_size = 0
	};

	return (str_insert_str(s, idx, &src));
}

boolean_t
str_insert_str(str_t *dest, size_t idx, const str_t *src)
{
	ASSERT3U(idx, <=, dest->str_len);

	if (idx == dest->str_len)
		return (str_append_str(dest, src));

	if (idx == 0 && dest->str_s == NULL && IS_REF(src)) {
		sysdem_ops_t *ops = dest->str_ops;
		*dest = *src;
		dest->str_ops = ops;
		return (B_TRUE);
	}

	if (!str_reserve(dest, src->str_len))
		return (B_FALSE);

	/*
	 * Shift the contents of dest over at the insertion point.  Since
	 * src and dest ranges will overlap, and unlike some programmers,
	 * *I* can read man pages - memmove() is the appropriate function
	 * to this.
	 */
	(void) memmove(dest->str_s + idx + src->str_len, dest->str_s + idx,
	    dest->str_len - idx);

	/*
	 * However the content to insert does not overlap with the destination
	 * so memcpy() is fine here.
	 */
	(void) memcpy(dest->str_s + idx, src->str_s, src->str_len);
	dest->str_len += src->str_len;

	return (B_TRUE);
}

boolean_t
str_erase(str_t *s, size_t pos, size_t len)
{
	ASSERT3U(pos, <, s->str_len);
	ASSERT3U(pos + len, <=, s->str_len);

	if (IS_REF(s)) {
		if (!str_reserve(s, 0))
			return (B_FALSE);
	}

	(void) memmove(s->str_s + pos, s->str_s + pos + len, s->str_len - len);
	s->str_len -= len;
	return (B_TRUE);
}

str_pair_t *
str_pair_init(str_pair_t *sp, sysdem_ops_t *ops)
{
	(void) memset(sp, 0, sizeof (*sp));
	str_init(&sp->strp_l, ops);
	str_init(&sp->strp_r, ops);
	return (sp);
}

void
str_pair_fini(str_pair_t *sp)
{
	str_fini(&sp->strp_l);
	str_fini(&sp->strp_r);
}

/* combine left and right parts and put result into left part */
boolean_t
str_pair_merge(str_pair_t *sp)
{
	/* if right side is empty, don't need to do anything */
	if (str_length(&sp->strp_r) == 0)
		return (B_TRUE);

	/* if left side is empty, just move right to left */
	if (str_length(&sp->strp_l) == 0) {
		str_fini(&sp->strp_l);
		sp->strp_l = sp->strp_r;
		sp->strp_r.str_s = NULL;
		sp->strp_r.str_len = sp->strp_r.str_size = 0;
		return (B_TRUE);
	}

	if (!str_append_str(&sp->strp_l, &sp->strp_r))
		return (B_FALSE);

	str_fini(&sp->strp_r);
	str_init(&sp->strp_r, sp->strp_l.str_ops);
	return (B_TRUE);
}

boolean_t
str_pair_copy(const str_pair_t *src, str_pair_t *dest)
{
	boolean_t ok = B_TRUE;

	ok &= str_copy(&src->strp_l, &dest->strp_l);
	ok &= str_copy(&src->strp_r, &dest->strp_r);

	if (!ok) {
		str_fini(&dest->strp_l);
		str_fini(&dest->strp_r);
		return (B_FALSE);
	}

	return (B_TRUE);
}

size_t
str_pair_len(const str_pair_t *sp)
{
	return (str_length(&sp->strp_l) + str_length(&sp->strp_r));
}