CoreFW  Check-in [499faa2771]

Overview
Comment:Add a few new string functions.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 499faa27715b426d0b74180f2f726592dbc3f2ddc65cf335b4d8e47ff24098b6
User & Date: js on 2012-09-30 01:46:16
Other Links: manifest | tags
Context
2012-09-30
02:21
Add cfw_map_{get,set}_c() for convenience. check-in: 082d69b467 user: js tags: trunk
01:46
Add a few new string functions. check-in: 499faa2771 user: js tags: trunk
01:34
Rename eof to at_end. check-in: 6893b9f5ab user: js tags: trunk
Changes

Modified src/string.c from [6218da6faa] to [61c34032c7].

164
165
166
167
168
169
170
171

172
173
174
175
176
177
178
164
165
166
167
168
169
170

171
172
173
174
175
176
177
178







-
+







	new->len = str->len;

	memcpy(new->data, str->data, str->len + 1);

	return new;
}

const char*
char*
cfw_string_c(CFWString *str)
{
	return str->data;
}

size_t
cfw_string_length(CFWString *string)
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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+



















+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







	new[str->len + append->len] = 0;

	str->data = new;
	str->len += append->len;

	return true;
}

bool
cfw_string_append_c(CFWString *str, const char *append)
{
	size_t append_len = strlen(append);
	char *new;

	if ((new = realloc(str->data, str->len + append_len + 1)) == NULL)
		return false;

	memcpy(new + str->len, append, append_len);
	new[str->len + append_len] = 0;

	str->data = new;
	str->len += append_len;

	return true;
}

bool
cfw_string_has_prefix(CFWString *str, CFWString *prefix)
{
	if (prefix->len > str->len)
		return false;

	return !memcmp(str->data, prefix->data, prefix->len);
}

bool
cfw_string_has_prefix_c(CFWString *str, const char *prefix)
{
	size_t prefix_len = strlen(prefix);

	if (prefix_len > str->len)
		return false;

	return !memcmp(str->data, prefix, prefix_len);
}

bool
cfw_string_has_suffix(CFWString *str, CFWString *suffix)
{
	if (suffix->len > str->len)
		return false;

	return !memcmp(str->data, suffix->data, suffix->len);
}

bool
cfw_string_has_suffix_c(CFWString *str, const char *suffix)
{
	size_t suffix_len = strlen(suffix);

	if (suffix_len > str->len)
		return false;

	return !memcmp(str->data, suffix, suffix_len);
}

size_t
cfw_string_find(CFWString *str, CFWString *substr, cfw_range_t range)
{
	size_t i;

	if (range.start > str->len)
		return SIZE_MAX;

	if (range.length == SIZE_MAX)
		range.length = str->len - range.start;

	if (range.start + range.length > str->len || substr->len > range.length)
		return SIZE_MAX;

	for (i = range.start; i <= range.start + range.length - substr->len;
	    i++)
		if (!memcmp(str->data + i, substr->data, substr->len))
			return i;

	return SIZE_MAX;
}

size_t
cfw_string_find_c(CFWString *str, const char *substr, cfw_range_t range)
{
	size_t substr_len = strlen(substr);
	size_t i;

	if (range.start > str->len)
		return SIZE_MAX;

	if (range.length == SIZE_MAX)
		range.length = str->len - range.start;

	if (range.start + range.length > str->len || substr_len > range.length)
		return SIZE_MAX;

	for (i = range.start; i <= range.start + range.length - substr_len;
	    i++)
		if (!memcmp(str->data + i, substr, substr_len))
			return i;

	return SIZE_MAX;
}

static CFWClass class = {
	.name = "CFWString",
	.size = sizeof(CFWString),

Modified src/string.h from [01937810ff] to [1e17b24605].

31
32
33
34
35
36
37
38

39
40
41
42





43

44
45
31
32
33
34
35
36
37

38
39
40
41
42
43
44
45
46
47
48
49
50
51







-
+




+
+
+
+
+

+


#include "range.h"

typedef struct CFWString CFWString;
extern CFWClass *cfw_string;
extern size_t cfw_strnlen(const char*, size_t);
extern char* cfw_strdup(const char*);
extern char* cfw_strndup(const char*, size_t);
extern const char* cfw_string_c(CFWString*);
extern char* cfw_string_c(CFWString*);
extern size_t cfw_string_length(CFWString*);
extern bool cfw_string_set(CFWString*, const char*);
extern void cfw_string_set_nocopy(CFWString*, char*, size_t);
extern bool cfw_string_append(CFWString*, CFWString*);
extern bool cfw_string_append_c(CFWString*, const char*);
extern bool cfw_string_has_prefix(CFWString*, CFWString*);
extern bool cfw_string_has_prefix_c(CFWString*, const char*);
extern bool cfw_string_has_suffix(CFWString*, CFWString*);
extern bool cfw_string_has_suffix_c(CFWString*, const char*);
extern size_t cfw_string_find(CFWString*, CFWString*, cfw_range_t);
extern size_t cfw_string_find_c(CFWString*, const char*, cfw_range_t);

#endif