summaryrefslogtreecommitdiff
path: root/sheep/string.c
blob: 97660422caefc6f8e22561bed38a0d44a9d52f5f (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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
 * sheep/string.c
 *
 * Copyright (c) 2009 Johannes Weiner <hannes@cmpxchg.org>
 */
#include <sheep/compile.h>
#include <sheep/object.h>
#include <sheep/unpack.h>
#include <sheep/util.h>
#include <sheep/gc.h>
#include <sheep/vm.h>
#include <string.h>
#include <stdio.h>

#include <sheep/string.h>

static void string_free(struct sheep_vm *vm, sheep_t sheep)
{
	struct sheep_string *string;

	string = sheep_string(sheep);
	sheep_free(string->bytes);
	sheep_free(string);
}

static int string_test(sheep_t sheep)
{
	struct sheep_string *string;

	string = sheep_string(sheep);
	return string->nr_bytes != 0;
}

static int string_equal(sheep_t a, sheep_t b)
{
	struct sheep_string *sa, *sb;

	sa = sheep_string(a);
	sb = sheep_string(b);
	if (sa->nr_bytes != sb->nr_bytes)
		return 0;
	return !memcmp(sa->bytes, sb->bytes, sa->nr_bytes);
}

static void string_format(sheep_t sheep, struct sheep_strbuf *sb, int repr)
{
	if (repr)
		sheep_strbuf_addf(sb, "\"%s\"", sheep_rawstring(sheep));
	else
		sheep_strbuf_add(sb, sheep_rawstring(sheep));
}

static size_t string_length(sheep_t sheep)
{
	struct sheep_string *string;

	string = sheep_string(sheep);
	return string->nr_bytes;
}

static sheep_t string_concat(struct sheep_vm *vm, sheep_t sheep,
			unsigned int nr_args)
{
	struct sheep_string *string;
	struct sheep_strbuf sb;
	unsigned int i;

	memset(&sb, 0, sizeof(struct sheep_strbuf));

	string = sheep_string(sheep);
	sheep_strbuf_addn(&sb, string->bytes, string->nr_bytes);
	for (i = 1; i < nr_args; i++) {
		sheep_t string_;

		string_ = vm->stack.items[vm->stack.nr_items - nr_args + i];
		if (sheep_unpack(vm, string_, 'S', &string)) {
			sheep_free(sb.bytes);
			return NULL;
		}

		sheep_strbuf_addn(&sb, string->bytes, string->nr_bytes);
	}

	vm->stack.nr_items -= nr_args;
	return __sheep_make_string(vm, sb.bytes, sb.nr_bytes);
}

static sheep_t string_reverse(struct sheep_vm *vm, sheep_t sheep)
{
	struct sheep_string *string;
	char *result;
	size_t pos;

	string = sheep_string(sheep);
	result = sheep_malloc(string->nr_bytes + 1);
	for (pos = 0; pos < string->nr_bytes; pos++)
		result[pos] = string->bytes[string->nr_bytes - pos - 1];
	result[pos] = 0;
	return __sheep_make_string(vm, result, pos);
}

static sheep_t string_nth(struct sheep_vm *vm, size_t n, sheep_t sheep)
{
	struct sheep_string *string;
	char *new;

	string = sheep_string(sheep);
	new = sheep_zalloc(1 + 1);
	if (string->nr_bytes > n)
		new[0] = string->bytes[n];
	return __sheep_make_string(vm, new, 1);
}

static sheep_t string_slice(struct sheep_vm *vm, sheep_t sheep,
			size_t from, size_t to)
{
	struct sheep_string *string;
	size_t length = 0;
	char *new;

	string = sheep_string(sheep);
	if (to > string->nr_bytes) {
		sheep_error(vm, "index %ld out of range [0, %ld)",
			to, string->nr_bytes);
		return NULL;
	}
	length = to - from;
	new = sheep_malloc(length + 1);
	memcpy(new, string->bytes + from, length);
	new[length] = 0;
	return __sheep_make_string(vm, new, length);
}

static sheep_t string_position(struct sheep_vm *vm, sheep_t item, sheep_t sheep)
{
	const char *str, *pos;

	if (sheep_type(item) != &sheep_string_type) {
		sheep_error(vm, "string can not contain `%s'",
			sheep_type(item)->name);
		return NULL;
	}

	str = sheep_rawstring(sheep);
	pos = strstr(str, sheep_rawstring(item));
	if (pos)
		return sheep_make_number(vm, pos - str);
	return &sheep_nil;
}

static const struct sheep_sequence string_sequence = {
	.length = string_length,
	.concat = string_concat,
	.reverse = string_reverse,
	.nth = string_nth,
	.slice = string_slice,
	.position = string_position,
};

const struct sheep_type sheep_string_type = {
	.name = "string",
	.free = string_free,
	.compile = sheep_compile_constant,
	.test = string_test,
	.equal = string_equal,
	.format = string_format,
	.sequence = &string_sequence,
};

sheep_t __sheep_make_string(struct sheep_vm *vm, const char *str, size_t len)
{
	struct sheep_string *string;

	string = sheep_malloc(sizeof(struct sheep_string));
	string->bytes = str;
	string->nr_bytes = len;
	return sheep_make_object(vm, &sheep_string_type, string);
}

sheep_t sheep_make_string(struct sheep_vm *vm, const char *str)
{
	return __sheep_make_string(vm, sheep_strdup(str), strlen(str));
}

void __sheep_format(sheep_t sheep, struct sheep_strbuf *sb, int repr)
{
	sheep_type(sheep)->format(sheep, sb, repr);
}

char *sheep_format(sheep_t sheep)
{
	struct sheep_strbuf sb;

	memset(&sb, 0, sizeof(struct sheep_strbuf));
	__sheep_format(sheep, &sb, 0);
	return sb.bytes;
}

char *sheep_repr(sheep_t sheep)
{
	struct sheep_strbuf sb;

	memset(&sb, 0, sizeof(struct sheep_strbuf));
	__sheep_format(sheep, &sb, 1);
	return sb.bytes;
}

/* (string expression) */
static sheep_t builtin_string(struct sheep_vm *vm, unsigned int nr_args)
{
	sheep_t sheep;
	char *buf;

	if (sheep_unpack_stack(vm, nr_args, "o", &sheep))
		return NULL;

	if (sheep_type(sheep) == &sheep_string_type)
		return sheep;

	buf = sheep_repr(sheep);
	return __sheep_make_string(vm, buf, strlen(buf));
}

static char *do_split(char **stringp, const char *delim)
{
	char *match, *result;

	match = strstr(*stringp, delim);
	result = *stringp;
	if (!match) {
		*stringp = NULL;
		return result;
	}

	*match = 0;
	*stringp = match + strlen(delim);
	return result;
}

/* (split delimiter string) */
static sheep_t builtin_split(struct sheep_vm *vm, unsigned int nr_args)
{
	sheep_t string_, delim_, list_;
	struct sheep_list *list;
	const char *delim;
	char *pos, *orig;
	int empty;

	if (sheep_unpack_stack(vm, nr_args, "ss", &delim_, &string_))
		return NULL;

	sheep_protect(vm, delim_);
	pos = orig = sheep_strdup(sheep_rawstring(string_));

	list_ = sheep_make_cons(vm, NULL, NULL);
	sheep_protect(vm, list_);

	delim = sheep_rawstring(delim_);
	empty = sheep_string(delim_)->nr_bytes == 0;

	list = sheep_list(list_);
	while (pos) {
		sheep_t item;
		/*
		 * Empty splitting separates out every single
		 * character: (split "" "foo") => ("f" "o" "o")
		 */
		if (empty) {
			char str[2] = { *pos, 0 };

			item = sheep_make_string(vm, str);
			if (!pos[0] || !pos[1])
				pos = NULL;
			else
				pos++;
		} else
			item = sheep_make_string(vm, do_split(&pos, delim));

		list->head = item;
		list->tail = sheep_make_cons(vm, NULL, NULL);
		list = sheep_list(list->tail);
	}
	sheep_free(orig);

	sheep_unprotect(vm, list_);
	sheep_unprotect(vm, delim_);

	return list_;
}

/* (join delimiter list-of-strings) */
static sheep_t builtin_join(struct sheep_vm *vm, unsigned int nr_args)
{
	char *new = NULL, *result = NULL;
	struct sheep_string *delim;
	struct sheep_list *list;
	sheep_t delim_, list_;
	size_t length = 0;

	if (sheep_unpack_stack(vm, nr_args, "sl", &delim_, &list_))
		return NULL;

	sheep_protect(vm, delim_);
	sheep_protect(vm, list_);

	delim = sheep_string(delim_);
	list = sheep_list(list_);

	while (list->head) {
		struct sheep_string *string;
		size_t newlength;

		if (sheep_unpack_list(vm, list, "Sr", &string, &list))
			goto out;

		newlength = length + string->nr_bytes;
		if (list->head)
			newlength += delim->nr_bytes;
		new = sheep_realloc(new, newlength + 1);
		strcpy(new + length, string->bytes);
		if (list->head)
			strcpy(new + newlength - delim->nr_bytes, delim->bytes);
		length = newlength;
	}

	if (new)
		result = new;
	else
		result = sheep_strdup("");
out:
	sheep_unprotect(vm, list_);
	sheep_unprotect(vm, delim_);

	if (result)
		return __sheep_make_string(vm, result, length);
	return NULL;
}

/* (print &rest objects) */
static sheep_t builtin_print(struct sheep_vm *vm, unsigned int nr_args)
{
	unsigned int offset = nr_args;

	while (offset) {
		unsigned long index;
		char *tmp;

		index = vm->stack.nr_items - offset;
		tmp = sheep_format(vm->stack.items[index]);
		printf("%s", tmp);
		sheep_free(tmp);
		offset--;
	}
	puts("");
	vm->stack.nr_items -= nr_args;

	return &sheep_nil;
}

void sheep_string_builtins(struct sheep_vm *vm)
{
	sheep_vm_function(vm, "string", builtin_string);
	sheep_vm_function(vm, "split", builtin_split);
	sheep_vm_function(vm, "join", builtin_join);
	sheep_vm_function(vm, "print", builtin_print);
}