forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduk_api_string.c
314 lines (255 loc) · 8.98 KB
/
duk_api_string.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
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
/*
* String manipulation
*/
#include "duk_internal.h"
DUK_LOCAL void duk__concat_and_join_helper(duk_context *ctx, duk_idx_t count_in, duk_bool_t is_join) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_uint_t count;
duk_uint_t i;
duk_size_t idx;
duk_size_t len;
duk_hstring *h;
duk_uint8_t *buf;
DUK_ASSERT(ctx != NULL);
if (DUK_UNLIKELY(count_in <= 0)) {
if (count_in < 0) {
DUK_ERROR(thr, DUK_ERR_API_ERROR, DUK_STR_INVALID_COUNT);
return;
}
DUK_ASSERT(count_in == 0);
duk_push_hstring_stridx(ctx, DUK_STRIDX_EMPTY_STRING);
return;
}
count = (duk_uint_t) count_in;
if (is_join) {
duk_size_t t1, t2, limit;
h = duk_to_hstring(ctx, -((duk_idx_t) count) - 1);
DUK_ASSERT(h != NULL);
/* A bit tricky overflow test, see doc/code-issues.txt. */
t1 = (duk_size_t) DUK_HSTRING_GET_BYTELEN(h);
t2 = (duk_size_t) (count - 1);
limit = (duk_size_t) DUK_HSTRING_MAX_BYTELEN;
if (DUK_UNLIKELY(t2 != 0 && t1 > limit / t2)) {
/* Combined size of separators already overflows */
goto error_overflow;
}
len = (duk_size_t) (t1 * t2);
} else {
len = (duk_size_t) 0;
}
for (i = count; i >= 1; i--) {
duk_size_t new_len;
duk_to_string(ctx, -((duk_idx_t) i));
h = duk_require_hstring(ctx, -((duk_idx_t) i));
new_len = len + (duk_size_t) DUK_HSTRING_GET_BYTELEN(h);
/* Impose a string maximum length, need to handle overflow
* correctly.
*/
if (new_len < len || /* wrapped */
new_len > (duk_size_t) DUK_HSTRING_MAX_BYTELEN) {
goto error_overflow;
}
len = new_len;
}
DUK_DDD(DUK_DDDPRINT("join/concat %lu strings, total length %lu bytes",
(unsigned long) count, (unsigned long) len));
/* use stack allocated buffer to ensure reachability in errors (e.g. intern error) */
buf = (duk_uint8_t *) duk_push_fixed_buffer(ctx, len);
DUK_ASSERT(buf != NULL);
/* [... (sep) str1 str2 ... strN buf] */
idx = 0;
for (i = count; i >= 1; i--) {
if (is_join && i != count) {
h = duk_require_hstring(ctx, -((duk_idx_t) count) - 2); /* extra -1 for buffer */
DUK_MEMCPY(buf + idx, DUK_HSTRING_GET_DATA(h), DUK_HSTRING_GET_BYTELEN(h));
idx += DUK_HSTRING_GET_BYTELEN(h);
}
h = duk_require_hstring(ctx, -((duk_idx_t) i) - 1); /* extra -1 for buffer */
DUK_MEMCPY(buf + idx, DUK_HSTRING_GET_DATA(h), DUK_HSTRING_GET_BYTELEN(h));
idx += DUK_HSTRING_GET_BYTELEN(h);
}
DUK_ASSERT(idx == len);
/* [... (sep) str1 str2 ... strN buf] */
/* get rid of the strings early to minimize memory use before intern */
if (is_join) {
duk_replace(ctx, -((duk_idx_t) count) - 2); /* overwrite sep */
duk_pop_n(ctx, count);
} else {
duk_replace(ctx, -((duk_idx_t) count) - 1); /* overwrite str1 */
duk_pop_n(ctx, count-1);
}
/* [... buf] */
(void) duk_to_string(ctx, -1);
/* [... res] */
return;
error_overflow:
DUK_ERROR(thr, DUK_ERR_RANGE_ERROR, DUK_STR_CONCAT_RESULT_TOO_LONG);
}
DUK_EXTERNAL void duk_concat(duk_context *ctx, duk_idx_t count) {
duk__concat_and_join_helper(ctx, count, 0 /*is_join*/);
}
DUK_EXTERNAL void duk_join(duk_context *ctx, duk_idx_t count) {
duk__concat_and_join_helper(ctx, count, 1 /*is_join*/);
}
/* XXX: could map/decode be unified with duk_unicode_support.c code?
* Case conversion needs also the character surroundings though.
*/
DUK_EXTERNAL void duk_decode_string(duk_context *ctx, duk_idx_t index, duk_decode_char_function callback, void *udata) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_hstring *h_input;
const duk_uint8_t *p, *p_start, *p_end;
duk_codepoint_t cp;
h_input = duk_require_hstring(ctx, index);
DUK_ASSERT(h_input != NULL);
p_start = (duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input);
p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input);
p = p_start;
for (;;) {
if (p >= p_end) {
break;
}
cp = (int) duk_unicode_decode_xutf8_checked(thr, &p, p_start, p_end);
callback(udata, cp);
}
}
DUK_EXTERNAL void duk_map_string(duk_context *ctx, duk_idx_t index, duk_map_char_function callback, void *udata) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_hstring *h_input;
duk_hbuffer_dynamic *h_buf;
const duk_uint8_t *p, *p_start, *p_end;
duk_codepoint_t cp;
index = duk_normalize_index(ctx, index);
h_input = duk_require_hstring(ctx, index);
DUK_ASSERT(h_input != NULL);
/* XXX: should init with a spare of at least h_input->blen? */
duk_push_dynamic_buffer(ctx, 0);
h_buf = (duk_hbuffer_dynamic *) duk_get_hbuffer(ctx, -1);
DUK_ASSERT(h_buf != NULL);
DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(h_buf));
p_start = (duk_uint8_t *) DUK_HSTRING_GET_DATA(h_input);
p_end = p_start + DUK_HSTRING_GET_BYTELEN(h_input);
p = p_start;
for (;;) {
if (p >= p_end) {
break;
}
cp = (int) duk_unicode_decode_xutf8_checked(thr, &p, p_start, p_end);
cp = callback(udata, cp);
duk_hbuffer_append_xutf8(thr, h_buf, cp);
}
duk_to_string(ctx, -1); /* invalidates h_buf pointer */
duk_replace(ctx, index);
}
DUK_EXTERNAL void duk_substring(duk_context *ctx, duk_idx_t index, duk_size_t start_offset, duk_size_t end_offset) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_hstring *h;
duk_hstring *res;
duk_size_t start_byte_offset;
duk_size_t end_byte_offset;
DUK_ASSERT(ctx != NULL);
index = duk_require_normalize_index(ctx, index);
h = duk_require_hstring(ctx, index);
DUK_ASSERT(h != NULL);
if (end_offset >= DUK_HSTRING_GET_CHARLEN(h)) {
end_offset = DUK_HSTRING_GET_CHARLEN(h);
}
if (start_offset > end_offset) {
start_offset = end_offset;
}
DUK_ASSERT_DISABLE(start_offset >= 0);
DUK_ASSERT(start_offset <= end_offset && start_offset <= DUK_HSTRING_GET_CHARLEN(h));
DUK_ASSERT_DISABLE(end_offset >= 0);
DUK_ASSERT(end_offset >= start_offset && end_offset <= DUK_HSTRING_GET_CHARLEN(h));
/* guaranteed by string limits */
DUK_ASSERT(start_offset <= DUK_UINT32_MAX);
DUK_ASSERT(end_offset <= DUK_UINT32_MAX);
start_byte_offset = (duk_size_t) duk_heap_strcache_offset_char2byte(thr, h, (duk_uint_fast32_t) start_offset);
end_byte_offset = (duk_size_t) duk_heap_strcache_offset_char2byte(thr, h, (duk_uint_fast32_t) end_offset);
DUK_ASSERT(end_byte_offset >= start_byte_offset);
DUK_ASSERT(end_byte_offset - start_byte_offset <= DUK_UINT32_MAX); /* guaranteed by string limits */
/* no size check is necessary */
res = duk_heap_string_intern_checked(thr,
DUK_HSTRING_GET_DATA(h) + start_byte_offset,
(duk_uint32_t) (end_byte_offset - start_byte_offset));
duk_push_hstring(ctx, res);
duk_replace(ctx, index);
}
/* XXX: this is quite clunky. Add Unicode helpers to scan backwards and
* forwards with a callback to process codepoints?
*/
DUK_EXTERNAL void duk_trim(duk_context *ctx, duk_idx_t index) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_hstring *h;
const duk_uint8_t *p, *p_start, *p_end, *p_tmp1, *p_tmp2; /* pointers for scanning */
const duk_uint8_t *q_start, *q_end; /* start (incl) and end (excl) of trimmed part */
duk_codepoint_t cp;
index = duk_require_normalize_index(ctx, index);
h = duk_require_hstring(ctx, index);
DUK_ASSERT(h != NULL);
p_start = DUK_HSTRING_GET_DATA(h);
p_end = p_start + DUK_HSTRING_GET_BYTELEN(h);
p = p_start;
while (p < p_end) {
p_tmp1 = p;
cp = (duk_codepoint_t) duk_unicode_decode_xutf8_checked(thr, &p_tmp1, p_start, p_end);
if (!(duk_unicode_is_whitespace(cp) || duk_unicode_is_line_terminator(cp))) {
break;
}
p = p_tmp1;
}
q_start = p;
if (p == p_end) {
/* entire string is whitespace */
q_end = p;
goto scan_done;
}
p = p_end;
while (p > p_start) {
p_tmp1 = p;
while (p > p_start) {
p--;
if (((*p) & 0xc0) != 0x80) {
break;
}
}
p_tmp2 = p;
cp = (duk_codepoint_t) duk_unicode_decode_xutf8_checked(thr, &p_tmp2, p_start, p_end);
if (!(duk_unicode_is_whitespace(cp) || duk_unicode_is_line_terminator(cp))) {
p = p_tmp1;
break;
}
}
q_end = p;
scan_done:
/* This may happen when forward and backward scanning disagree
* (possible for non-extended-UTF-8 strings).
*/
if (q_end < q_start) {
q_end = q_start;
}
DUK_ASSERT(q_start >= p_start && q_start <= p_end);
DUK_ASSERT(q_end >= p_start && q_end <= p_end);
DUK_ASSERT(q_end >= q_start);
DUK_DDD(DUK_DDDPRINT("trim: p_start=%p, p_end=%p, q_start=%p, q_end=%p",
(void *) p_start, (void *) p_end, (void *) q_start, (void *) q_end));
if (q_start == p_start && q_end == p_end) {
DUK_DDD(DUK_DDDPRINT("nothing was trimmed: avoid interning (hashing etc)"));
return;
}
duk_push_lstring(ctx, (const char *) q_start, (duk_size_t) (q_end - q_start));
duk_replace(ctx, index);
}
DUK_EXTERNAL duk_codepoint_t duk_char_code_at(duk_context *ctx, duk_idx_t index, duk_size_t char_offset) {
duk_hthread *thr = (duk_hthread *) ctx;
duk_hstring *h;
duk_ucodepoint_t cp;
h = duk_require_hstring(ctx, index);
DUK_ASSERT(h != NULL);
DUK_ASSERT_DISABLE(char_offset >= 0); /* always true, arg is unsigned */
if (char_offset >= DUK_HSTRING_GET_CHARLEN(h)) {
return 0;
}
DUK_ASSERT(char_offset <= DUK_UINT_MAX); /* guaranteed by string limits */
cp = duk_hstring_char_code_at_raw(thr, h, (duk_uint_t) char_offset);
return (duk_codepoint_t) cp;
}