forked from microsoft/devicescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.c
More file actions
333 lines (304 loc) · 11.3 KB
/
string.c
File metadata and controls
333 lines (304 loc) · 11.3 KB
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
#include "devs_internal.h"
#include <math.h>
bool devs_is_string(devs_ctx_t *ctx, value_t v) {
unsigned tag;
switch (devs_handle_type(v)) {
case DEVS_HANDLE_TYPE_GC_OBJECT:
tag = devs_gc_tag(devs_handle_ptr_value(ctx, v));
return tag == DEVS_GC_TAG_STRING || tag == DEVS_GC_TAG_STRING_JMP;
case DEVS_HANDLE_TYPE_IMG_BUFFERISH:
return !devs_bufferish_is_buffer(v);
default:
return false;
}
}
bool devs_is_number(value_t v) {
return devs_is_tagged_int(v) || devs_handle_type(v) == DEVS_HANDLE_TYPE_FLOAT64;
}
const devs_utf8_string_t *devs_string_get_utf8_struct(devs_ctx_t *ctx, value_t v) {
switch (devs_handle_type(v)) {
case DEVS_HANDLE_TYPE_GC_OBJECT: {
void *ptr = devs_handle_ptr_value(ctx, v);
if (devs_gc_tag(ptr) == DEVS_GC_TAG_STRING_JMP) {
devs_string_jmp_t *s = ptr;
return &s->inner;
}
return NULL;
}
case DEVS_HANDLE_TYPE_IMG_BUFFERISH: {
unsigned idx = devs_handle_value(v);
unsigned tp = (uint16_t)idx >> DEVS_STRIDX__SHIFT;
idx &= (1 << DEVS_STRIDX__SHIFT) - 1;
if (tp == DEVS_STRIDX_UTF8)
return devs_img_get_string_jmp(ctx->img, idx);
return NULL;
}
default:
return NULL;
}
}
const char *devs_string_get_utf8(devs_ctx_t *ctx, value_t v, unsigned *size) {
switch (devs_handle_type(v)) {
case DEVS_HANDLE_TYPE_GC_OBJECT: {
void *ptr = devs_handle_ptr_value(ctx, v);
if (devs_gc_tag(ptr) == DEVS_GC_TAG_STRING) {
devs_string_t *s = ptr;
if (size)
*size = s->length;
return s->data;
} else if (devs_gc_tag(ptr) == DEVS_GC_TAG_STRING_JMP) {
devs_string_jmp_t *s = ptr;
if (size)
*size = s->inner.size;
return devs_utf8_string_data(&s->inner);
}
return NULL;
}
case DEVS_HANDLE_TYPE_IMG_BUFFERISH:
return devs_bufferish_is_buffer(v) ? NULL
: devs_get_static_utf8(ctx, devs_handle_value(v), size);
default:
return NULL;
}
}
value_t devs_builtin_string(unsigned idx) {
return devs_value_from_handle(DEVS_HANDLE_TYPE_IMG_BUFFERISH,
(DEVS_STRIDX_BUILTIN << DEVS_STRIDX__SHIFT) | idx);
}
value_t devs_string_vsprintf(devs_ctx_t *ctx, const char *format, va_list ap) {
if (strstr(format, "%-s"))
JD_PANIC();
va_list ap2;
va_copy(ap2, ap);
unsigned len;
int sz = jd_vsprintf_ext(NULL, 0, format, &len, ap);
// len includes final NUL; devs_string_try_alloc() allocates the final NUL, but doesn't count it
// in its len
len--;
sz--;
value_t r;
char *d = devs_string_prep(ctx, &r, sz, len);
if (d) {
sz = jd_vsprintf_ext(d, sz + 1, format, &len, ap2);
len--;
sz--;
devs_string_finish(ctx, &r, sz, len);
}
return r;
}
value_t devs_string_sprintf(devs_ctx_t *ctx, const char *format, ...) {
va_list arg;
va_start(arg, format);
value_t r = devs_string_vsprintf(ctx, format, arg);
va_end(arg);
return r;
}
value_t devs_string_from_utf8(devs_ctx_t *ctx, const uint8_t *utf8, unsigned size) {
devs_any_string_t *s = devs_string_try_alloc_init(ctx, (const char *)utf8, size);
if (s == NULL) {
return devs_undefined;
} else {
return devs_value_from_gc_obj(ctx, s);
}
}
static value_t buffer_to_string(devs_ctx_t *ctx, value_t v) {
unsigned sz;
const void *data = devs_bufferish_data(ctx, v, &sz);
JD_ASSERT(data != NULL);
unsigned maxbuf = 32;
if (sz > maxbuf) {
return devs_string_sprintf(ctx, "[Buffer[%u] %*p...]", sz, maxbuf, data);
} else {
return devs_string_sprintf(ctx, "[Buffer[%u] %*p]", sz, sz, data);
}
}
value_t devs_value_to_string(devs_ctx_t *ctx, value_t v) {
if (devs_is_string(ctx, v))
return v;
uint32_t hv;
switch (devs_handle_type(v)) {
case DEVS_HANDLE_TYPE_FLOAT64: {
char buf[64];
jd_print_double(buf, devs_value_to_double(ctx, v), 7);
return devs_string_sprintf(ctx, "%s", buf);
}
case DEVS_HANDLE_TYPE_SPECIAL:
switch ((hv = devs_handle_value(v))) {
case DEVS_SPECIAL_NULL:
return devs_builtin_string(DEVS_BUILTIN_STRING_NULL);
case DEVS_SPECIAL_UNDEFINED:
return devs_builtin_string(DEVS_BUILTIN_STRING_UNDEFINED);
case DEVS_SPECIAL_FALSE:
return devs_builtin_string(DEVS_BUILTIN_STRING_FALSE);
case DEVS_SPECIAL_TRUE:
return devs_builtin_string(DEVS_BUILTIN_STRING_TRUE);
case DEVS_SPECIAL_NAN:
return devs_builtin_string(DEVS_BUILTIN_STRING_NAN);
case DEVS_SPECIAL_INF:
return devs_builtin_string(DEVS_BUILTIN_STRING_INFINITY);
case DEVS_SPECIAL_MINF:
return devs_builtin_string(DEVS_BUILTIN_STRING_MINFINITY);
default: {
if (devs_handle_is_builtin(hv))
return devs_string_sprintf(ctx, "[Static Obj: %d]",
(int)hv - DEVS_SPECIAL_BUILTIN_OBJ_FIRST);
else if (devs_handle_is_throw_jmp(hv)) {
return devs_string_sprintf(ctx, "[Throw: %x]", (unsigned)devs_handle_value(v));
} else
JD_PANIC();
}
}
case DEVS_HANDLE_TYPE_FIBER:
return devs_string_sprintf(ctx, "[Fiber: %x]", (unsigned)devs_handle_value(v));
case DEVS_HANDLE_TYPE_STATIC_FUNCTION:
return devs_string_sprintf(ctx, "[Function: %s]",
devs_img_fun_name(ctx->img, devs_handle_value(v)));
case DEVS_HANDLE_TYPE_CLOSURE:
return devs_string_sprintf(ctx, "[Closure: %s]",
devs_img_fun_name(ctx->img, devs_handle_high_value(v)));
case DEVS_HANDLE_TYPE_BOUND_FUNCTION:
case DEVS_HANDLE_TYPE_BOUND_FUNCTION_STATIC:
return devs_string_sprintf(ctx, "[Method: %s]",
devs_img_fun_name(ctx->img, devs_handle_high_value(v)));
case DEVS_HANDLE_TYPE_GC_OBJECT:
switch (devs_gc_tag(devs_handle_ptr_value(ctx, v))) {
case DEVS_GC_TAG_ARRAY:
return devs_builtin_string(DEVS_BUILTIN_STRING_ARRAY); // TODO stringify array?
case DEVS_GC_TAG_BOUND_FUNCTION:
return devs_builtin_string(DEVS_BUILTIN_STRING_FUNCTION); // TODO?
case DEVS_GC_TAG_BUFFER:
return buffer_to_string(ctx, v);
case DEVS_GC_TAG_PACKET: {
devs_packet_t *pkt = devs_handle_ptr_value(ctx, v);
return devs_string_sprintf(ctx, "[Packet: %s cmd=%x sz=%d]",
devs_role_name(ctx, pkt->roleidx), pkt->service_command,
pkt->payload->length);
}
case DEVS_GC_TAG_IMAGE: {
devs_gimage_t *img = devs_handle_ptr_value(ctx, v);
return devs_string_sprintf(ctx, "[Image: %dx%d (%d bpp)]", img->width, img->height,
img->bpp);
}
case DEVS_GC_TAG_SHORT_MAP:
case DEVS_GC_TAG_HALF_STATIC_MAP:
case DEVS_GC_TAG_MAP:
return devs_builtin_string(DEVS_BUILTIN_STRING_MAP);
case DEVS_GC_TAG_BUILTIN_PROTO: // can't happen
case DEVS_GC_TAG_STRING_JMP: // handled on top
case DEVS_GC_TAG_STRING: // handled on top
default:
JD_PANIC();
}
case DEVS_HANDLE_TYPE_IMG_BUFFERISH:
JD_ASSERT(devs_bufferish_is_buffer(v));
return buffer_to_string(ctx, v);
case DEVS_HANDLE_TYPE_ROLE:
return devs_string_sprintf(ctx, "[Role: %s]", devs_role_name(ctx, devs_handle_value(v)));
case DEVS_HANDLE_TYPE_ROLE_MEMBER: {
unsigned roleidx;
const devs_service_spec_t *spec = devs_value_to_service_spec(ctx, v);
if (spec)
return devs_string_sprintf(ctx, "[ServiceSpec: %s]",
devs_img_get_utf8(ctx->img, spec->name_idx, NULL));
const devs_packet_spec_t *pkt = devs_decode_role_packet(ctx, v, &roleidx);
if (roleidx == DEVS_ROLE_INVALID) {
spec = devs_img_get_service_spec(ctx->img, devs_packet_spec_parent(ctx, pkt));
return devs_string_sprintf(ctx, "[PacketSpec: %s.%s]",
devs_img_get_utf8(ctx->img, spec->name_idx, NULL),
devs_img_get_utf8(ctx->img, pkt->name_idx, NULL));
} else
return devs_string_sprintf(ctx, "[Role: %s.%s]", devs_role_name(ctx, roleidx),
devs_img_get_utf8(ctx->img, pkt->name_idx, NULL));
}
default:
JD_PANIC();
}
}
static value_t devs_value_to_string_and_pin(devs_ctx_t *ctx, value_t a) {
if (!devs_is_string(ctx, a)) {
value_t tmp = devs_value_to_string(ctx, a);
devs_value_pin(ctx, tmp);
devs_value_unpin(ctx, a);
return tmp;
} else {
return a;
}
}
void devs_map_set_string_field(devs_ctx_t *ctx, devs_map_t *m, unsigned builtin_str, value_t msg) {
devs_value_pin(ctx, msg);
msg = devs_value_to_string_and_pin(ctx, msg);
devs_map_set(ctx, m, devs_builtin_string(builtin_str), msg);
devs_value_unpin(ctx, msg);
}
value_t devs_string_concat(devs_ctx_t *ctx, value_t a, value_t b) {
bool dup = (a.u64 == b.u64);
devs_value_pin(ctx, a);
if (!dup)
devs_value_pin(ctx, b);
a = devs_value_to_string_and_pin(ctx, a);
if (dup)
b = a;
else
b = devs_value_to_string_and_pin(ctx, b);
const char *ap, *bp;
unsigned asz, bsz, alen, blen;
ap = devs_string_get_utf8(ctx, a, &asz);
bp = devs_string_get_utf8(ctx, b, &bsz);
alen = devs_string_length(ctx, a);
blen = devs_string_length(ctx, b);
value_t r;
if (ap == NULL || bp == NULL) {
// strange...
devs_invalid_program(ctx, 60126);
r = devs_undefined;
} else if (asz == 0) {
r = b;
} else if (bsz == 0) {
r = a;
} else {
unsigned sz = asz + bsz;
unsigned len = alen + blen;
char *p = devs_string_prep(ctx, &r, sz, len);
if (p) {
memcpy(p, ap, asz);
memcpy(p + asz, bp, bsz);
devs_string_finish(ctx, &r, sz, len);
}
}
devs_value_unpin(ctx, a);
if (!dup)
devs_value_unpin(ctx, b);
return r;
}
static int sanitize_idx(int sz, int start) {
if (start < 0) {
start += sz;
if (start < 0)
start = 0;
}
if (start > sz)
start = sz;
return start;
}
value_t devs_string_slice(devs_ctx_t *ctx, value_t str, int start, int endp) {
unsigned sz;
const char *data = devs_string_get_utf8(ctx, str, &sz);
if (!data)
return devs_undefined;
int slen = devs_string_length(ctx, str);
start = sanitize_idx(slen, start);
endp = sanitize_idx(slen, endp);
int len = endp - start;
if (len <= 0)
return devs_builtin_string(DEVS_BUILTIN_STRING__EMPTY);
if (start == 0 && len == slen)
return str;
start = devs_string_index(ctx, str, start);
endp = devs_string_index(ctx, str, endp);
if (start < 0)
start = sz; // shouldn't happen
if (endp < 0)
endp = sz;
devs_any_string_t *r = devs_string_try_alloc_init(ctx, data + start, endp - start);
return devs_value_from_gc_obj(ctx, r);
}