6 string* factor_vm::allot_string_internal(cell capacity) {
7 string* str = allot<string>(string_size(capacity));
9 str->length = tag_fixnum(capacity);
10 str->hashcode = false_object;
11 str->aux = false_object;
17 void factor_vm::fill_string(string* str_, cell start, cell capacity,
19 data_root<string> str(str_, this);
22 memset(&str->data()[start], (uint8_t)fill, capacity - start);
25 if (to_boolean(str->aux))
26 aux = untag<byte_array>(str->aux);
29 allot_uninitialized_array<byte_array>(untag_fixnum(str->length) * 2);
30 str->aux = tag<byte_array>(aux);
31 write_barrier(&str->aux);
34 uint8_t lo_fill = (uint8_t)((fill & 0x7f) | 0x80);
35 uint16_t hi_fill = (uint16_t)((fill >> 7) ^ 0x1);
36 memset(&str->data()[start], lo_fill, capacity - start);
37 memset_2(&aux->data<uint16_t>()[start], hi_fill,
38 (capacity - start) * sizeof(uint16_t));
43 string* factor_vm::allot_string(cell capacity, cell fill) {
44 data_root<string> str(allot_string_internal(capacity), this);
45 fill_string(str.untagged(), 0, capacity, fill);
46 return str.untagged();
50 void factor_vm::primitive_string() {
51 cell initial = to_cell(ctx->pop());
52 cell length = unbox_array_size();
53 ctx->push(tag<string>(allot_string(length, initial)));
56 bool factor_vm::reallot_string_in_place_p(string* str, cell capacity) {
57 return data->nursery->contains_p(str) &&
58 (!to_boolean(str->aux) ||
59 data->nursery->contains_p(untag<byte_array>(str->aux))) &&
60 capacity <= string_capacity(str);
64 string* factor_vm::reallot_string(string* str_, cell capacity) {
65 data_root<string> str(str_, this);
67 if (reallot_string_in_place_p(str.untagged(), capacity)) {
68 str->length = tag_fixnum(capacity);
70 if (to_boolean(str->aux)) {
71 byte_array* aux = untag<byte_array>(str->aux);
72 aux->capacity = tag_fixnum(capacity * 2);
75 return str.untagged();
77 cell to_copy = string_capacity(str.untagged());
78 if (capacity < to_copy)
81 data_root<string> new_str(allot_string_internal(capacity), this);
83 memcpy(new_str->data(), str->data(), to_copy);
85 if (to_boolean(str->aux)) {
86 byte_array* new_aux = allot_uninitialized_array<byte_array>(capacity * 2);
87 new_str->aux = tag<byte_array>(new_aux);
88 write_barrier(&new_str->aux);
90 byte_array* aux = untag<byte_array>(str->aux);
91 memcpy(new_aux->data<uint16_t>(), aux->data<uint16_t>(),
92 to_copy * sizeof(uint16_t));
95 fill_string(new_str.untagged(), to_copy, capacity, '\0');
96 return new_str.untagged();
101 void factor_vm::primitive_resize_string() {
102 data_root<string> str(ctx->pop(), this);
104 cell capacity = unbox_array_size();
105 ctx->push(tag<string>(reallot_string(str.untagged(), capacity)));
108 void factor_vm::primitive_set_string_nth_fast() {
109 string* str = untag<string>(ctx->pop());
110 cell index = untag_fixnum(ctx->pop());
111 cell value = untag_fixnum(ctx->pop());
112 str->data()[index] = (uint8_t)value;