-
Notifications
You must be signed in to change notification settings - Fork 138
/
stringbuilder.pmc
584 lines (420 loc) · 14.7 KB
/
stringbuilder.pmc
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/*
Copyright (C) 2010-2015, Parrot Foundation.
=head1 NAME
src/pmc/stringbuilder.pmc - StringBuilder PMC
=head1 DESCRIPTION
Helper class to create and manipulate strings, which are outside of
StringBuilder immutable.
=head2 Methods
=over 4
=cut
*/
#include "parrot/string_funcs.h"
/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
PARROT_WARN_UNUSED_RESULT
PARROT_CONST_FUNCTION
static size_t calculate_capacity(PARROT_INTERP, size_t needed);
static void convert_encoding(PARROT_INTERP,
ARGIN(STR_VTABLE *dest_encoding),
ARGMOD(STRING *buffer),
size_t size_to_add)
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3)
FUNC_MODIFIES(*buffer);
#define ASSERT_ARGS_calculate_capacity __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_convert_encoding __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(dest_encoding) \
, PARROT_ASSERT_ARG(buffer))
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: static */
#define INITIAL_STRING_CAPACITY 128
pmclass StringBuilder provides string auto_attrs {
ATTR STRING *buffer; /* Mutable string to gather results */
/*
=item C<void init()>
Initializes the StringBuilder.
=cut
*/
VTABLE void init() :manual_wb {
STATICSELF.init_int(INITIAL_STRING_CAPACITY);
}
/*
=item C<void init_int()>
Initializes the StringBuilder with initial size of buffer.
=cut
*/
VTABLE void init_int(INTVAL initial_size) {
STRING * const buffer = Parrot_gc_new_string_header(INTERP, 0);
if (initial_size < INITIAL_STRING_CAPACITY)
initial_size = INITIAL_STRING_CAPACITY;
Parrot_gc_allocate_string_storage(INTERP, buffer, initial_size);
buffer->encoding = Parrot_default_encoding_ptr;
SET_ATTR_buffer(INTERP, SELF, buffer);
PObj_custom_mark_SET(SELF);
}
/*
=item C<void init_pmc()>
Initializes the StringBuilder with an array of STRINGs.
=cut
*/
VTABLE void init_pmc(PMC *ar) :manual_wb {
const INTVAL count = VTABLE_elements(INTERP, ar);
if (!count)
STATICSELF.init_int(INITIAL_STRING_CAPACITY);
else {
STRING * const first = VTABLE_get_string_keyed_int(INTERP, ar, 0);
const INTVAL size = Parrot_str_byte_length(INTERP, first);
INTVAL i;
/* it's just an estimate, but estimates help */
STATICSELF.init_int(size * count);
SELF.push_string(first);
for (i = 1; i < count; ++i)
SELF.push_string(VTABLE_get_string_keyed_int(INTERP, ar, i));
}
}
/*
=item C<void mark()>
Mark the buffer.
=cut
*/
VTABLE void mark() :no_wb {
if (PMC_data(SELF)) {
STRING *buffer;
GET_ATTR_buffer(INTERP, SELF, buffer);
Parrot_gc_mark_STRING_alive(INTERP, buffer);
}
}
/*
=item C<STRING *get_string()>
Returns created string.
=cut
*/
VTABLE STRING *get_string() :no_wb {
STRING *buffer;
GET_ATTR_buffer(INTERP, SELF, buffer);
/* We need to build a new string because outside of StringBuilder
* strings are immutable. */
return Parrot_str_clone(INTERP, buffer);
}
/*
=item C<STRING *push_string()>
Append string to current buffer.
=cut
*/
VTABLE void push_string(STRING *s) {
STRING *buffer;
size_t total_size;
/* Early return on NULL strings */
if (STRING_IS_NULL(s) || s->strlen == 0)
return;
GET_ATTR_buffer(INTERP, SELF, buffer);
if (buffer->bufused == 0) {
/* Always copy the encoding of the first string. The IO functions
assume that the concatenation of utf8 strings doesn't change
the encoding. */
buffer->encoding = s->encoding;
}
else {
const STR_VTABLE * const enc =
buffer->encoding == s->encoding
? buffer->encoding
: Parrot_str_rep_compatible(interp, buffer, s);
if (enc) {
buffer->encoding = enc;
}
else {
/* If strings are incompatible - convert them to utf8 */
if (s->encoding != Parrot_utf8_encoding_ptr)
s = Parrot_utf8_encoding_ptr->to_encoding(interp, s);
if (buffer->encoding != Parrot_utf8_encoding_ptr)
convert_encoding(INTERP, Parrot_utf8_encoding_ptr, buffer, s->bufused);
}
}
total_size = buffer->bufused + s->bufused;
/* Reallocate if necessary */
if (total_size > buffer->_buflen) {
/* Calculate (possibly new) total size */
total_size = calculate_capacity(INTERP, total_size);
Parrot_gc_reallocate_string_storage(INTERP, buffer, total_size);
buffer->_buflen = total_size;
}
/* Tack s on the end of buffer */
memcpy((char *)buffer->_bufstart + buffer->bufused,
s->strstart, s->bufused);
/* Update buffer */
buffer->bufused += s->bufused;
buffer->strstart = (char *)buffer->_bufstart;
buffer->strlen += s->strlen;
buffer->hashval = 0; /* hash is invalid */
PARROT_ASSERT(buffer->bufused <= Buffer_buflen(buffer));
}
VTABLE void push_integer(INTVAL value) {
STRING * s = PARROT_STRINGBUILDER(SELF)->buffer;
String_iter iter;
size_t total_size;
if (s->encoding != Parrot_utf8_encoding_ptr && value > 0x7F) {
if (s->strlen == 0)
s->encoding = Parrot_utf8_encoding_ptr;
else
convert_encoding(INTERP, Parrot_utf8_encoding_ptr, s, sizeof (INTVAL));
}
total_size = s->bufused + sizeof (INTVAL);
if (total_size > s->_buflen) {
total_size = calculate_capacity(INTERP, total_size);
Parrot_gc_reallocate_string_storage(INTERP, s, total_size);
}
STRING_ITER_INIT(INTERP, &iter);
iter.charpos = s->strlen;
iter.bytepos = s->bufused;
STRING_iter_set_and_advance(INTERP, s, &iter, value);
s->strlen = iter.charpos;
s->bufused = iter.bytepos;
}
/*
=item C<VTABLE i_concatenate_str()>
=item C<VTABLE i_concatenate()>
Append string. Synonym for push_string
=cut
*/
VTABLE void i_concatenate_str(STRING *s) :manual_wb {
STATICSELF.push_string(s);
}
VTABLE void i_concatenate(PMC *p) :manual_wb {
if (p->vtable->base_type == enum_class_StringBuilder) {
STRING *buffer;
GET_ATTR_buffer(INTERP, p, buffer);
STATICSELF.push_string(buffer);
}
else {
STATICSELF.push_string(VTABLE_get_string(INTERP, p));
}
}
VTABLE void push_pmc(PMC *p) :manual_wb {
if (p->vtable->base_type == enum_class_StringBuilder) {
STRING *buffer;
GET_ATTR_buffer(INTERP, p, buffer);
STATICSELF.push_string(buffer);
}
else {
STATICSELF.push_string(VTABLE_get_string(INTERP, p));
}
}
/*
=item C<VTABLE set_string_native(STRING)>
=item C<VTABLE set_pmc(PMC)>
Set content of buffer to passed string or PMC
=cut
*/
VTABLE void set_string_native(STRING *s) {
STRING * buffer;
/* Calculate (possibly new) total size */
const size_t total_size = calculate_capacity(INTERP, s->bufused);
GET_ATTR_buffer(INTERP, SELF, buffer);
/* Reallocate if necessary */
if (total_size > Buffer_buflen(buffer)) {
Parrot_gc_reallocate_string_storage(INTERP, buffer, total_size);
buffer->strstart = (char*)buffer->_bufstart;
}
/* Tack s on the buffer */
memcpy((void *)((char*)buffer->_bufstart),
s->strstart, s->bufused);
/* Update buffer */
buffer->bufused = s->bufused;
buffer->strlen = Parrot_str_length(INTERP, s);
buffer->encoding = s->encoding;
}
VTABLE void set_pmc(PMC *s) :manual_wb {
if (s->vtable->base_type == enum_class_StringBuilder) {
STRING *buffer;
GET_ATTR_buffer(INTERP, s, buffer);
STATICSELF.push_string(buffer);
}
else {
STATICSELF.set_string_native(VTABLE_get_string(INTERP, s));
}
}
/*
=item C<VTABLE get_integer()>
Returns current capacity of allocated buffer.
For testing purpose only?
=cut
*/
INTVAL get_integer() :no_wb {
STRING *buffer;
GET_ATTR_buffer(INTERP, SELF, buffer);
return Buffer_buflen(buffer);
}
/*
=item C<VTABLE substr()>
Returns a copied substring of the STRING.
=cut
*/
VTABLE STRING *substr(INTVAL offset, INTVAL length) :no_wb {
STRING *buffer;
GET_ATTR_buffer(INTERP, SELF, buffer);
/* STRING_substr already creates a copy for us, GH #1123 */
return STRING_substr(INTERP, buffer, offset, length);
}
/*
=item C<append_format(string fmt [, pmc args ] [, pmc hash ])>
Add a line to a C<StringBuilder> object according to C<fmt>.
The C<fmt> string can contain any number of "%-replacements"
which are replaced by the corresponding values from C<args>
or C<hash> prior to being appended to the string. (Here
C<args> is a slurpy array, and C<hash> is a slurpy hash.)
The currently defined replacements include:
%0 %1 ... %9 the value from the args array at index 0..9
%, the values of the args array separated by commas
%% a percent sign
A percent-sign followed by any other character that is a hash
key receives the value of the hash element.
=cut
*/
METHOD append_format(STRING *fmt, PMC *args :slurpy, PMC *hash :slurpy :named) {
STRING * const percent = CONST_STRING(INTERP, "%");
STRING * const comma = CONST_STRING(INTERP, ",");
STRING * const comma_space = CONST_STRING(INTERP, ", ");
PMC *stringbuilder = SELF;
INTVAL pos = 0;
/* Loop over the format string, splitting it into chunks
* for the string builder. */
while (pos >= 0) {
/* Find the next % */
const INTVAL percentPos = STRING_index(INTERP, fmt, percent, pos);
STRING *key;
if (percentPos < 0) {
if (pos == 0) {
VTABLE_push_string(INTERP, stringbuilder, fmt);
}
else {
/* remaining string can be added as is. */
VTABLE_push_string(INTERP, stringbuilder,
STRING_substr(INTERP, fmt, pos,
Parrot_str_length(INTERP, fmt) -pos));
}
break;
}
else {
/* slurp up to just before the % sign... */
VTABLE_push_string(INTERP, stringbuilder,
STRING_substr(INTERP, fmt, pos, percentPos - pos));
/* skip the % sign */
pos = percentPos + 1 ;
}
/* key is always a single character */
key = STRING_substr(INTERP, fmt, pos++, 1);
if (VTABLE_exists_keyed_str(INTERP, hash, key)) {
VTABLE_push_string(INTERP, stringbuilder,
VTABLE_get_string_keyed_str(INTERP, hash, key));
}
else if (Parrot_str_is_cclass(INTERP, enum_cclass_numeric, key, 0)) {
VTABLE_push_string(INTERP, stringbuilder,
VTABLE_get_string_keyed_int(INTERP, args,
Parrot_str_to_int(INTERP, key)));
}
else if (STRING_equal(INTERP, key, comma)) {
INTVAL num_args = VTABLE_elements(INTERP, args);
INTVAL pos_args;
for (pos_args = 0; pos_args < num_args; ++pos_args) {
if (pos_args > 0)
VTABLE_push_string(INTERP, stringbuilder, comma_space);
VTABLE_push_string(INTERP, stringbuilder,
VTABLE_get_string_keyed_int(INTERP, args, pos_args));
}
}
else if (STRING_equal(INTERP, key, percent)) {
VTABLE_push_string(INTERP, stringbuilder, percent);
}
else {
/* %foo has no special meaning, pass it through unchanged */
VTABLE_push_string(INTERP, stringbuilder,
STRING_substr(INTERP, fmt, pos-2, 2));
}
}
RETURN(PMC *SELF);
}
/*
=item C<INTVAL get_string_length()>
Returns length of currently built string.
=cut
*/
METHOD get_string_length() :no_wb {
STRING *buffer;
INTVAL length;
GET_ATTR_buffer(INTERP, SELF, buffer);
length = Parrot_str_length(INTERP, buffer);
RETURN(INTVAL length);
}
/*
=back
=cut
*/
}
/*
=head2 Helper functions.
=over 4
=cut
*/
/*
=item C<static size_t calculate_capacity(PARROT_INTERP, size_t needed)>
Calculate capacity for string. We overallocate by 2 for smaller buffers
resp. onto the next block size.
=cut
*/
PARROT_WARN_UNUSED_RESULT
PARROT_CONST_FUNCTION
static size_t
calculate_capacity(SHIM_INTERP, size_t needed)
{
ASSERT_ARGS(calculate_capacity)
if (needed < 8192) {
#if 1
needed += needed/2; /* overallocate by 1.5 */
#else
needed *= 2; /* overallocate by 2 */
#endif
needed = (needed + 15) & ~15; /* round up to 16 */
}
else {
needed &= ~0xfff;
needed += 4096; /* next block */
}
return needed;
}
/*
=item C<static void convert_encoding(PARROT_INTERP, STR_VTABLE *dest_encoding,
STRING *buffer, size_t size_to_add)>
Convert buffer content to the encoding specified and increase its size,
reallocating it if needed.
=back
=cut
*/
static void
convert_encoding(PARROT_INTERP, ARGIN(STR_VTABLE *dest_encoding),
ARGMOD(STRING *buffer), size_t size_to_add)
{
ASSERT_ARGS(convert_encoding)
STRING * new_buffer;
size_t total_size;
new_buffer = dest_encoding->to_encoding(interp, buffer);
total_size = new_buffer->bufused + size_to_add;
if (total_size > buffer->_buflen) {
/* Reallocate */
total_size = calculate_capacity(interp, total_size);
Parrot_gc_reallocate_string_storage(interp, buffer, total_size);
}
buffer->bufused = new_buffer->bufused;
buffer->encoding = new_buffer->encoding;
memcpy(buffer->strstart, new_buffer->strstart,
new_buffer->bufused);
}
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/