]> gitweb.factorcode.org Git - factor.git/blob - vm/strings.cpp
xmode.marker: faster update-match-group
[factor.git] / vm / strings.cpp
1 #include "master.hpp"
2
3 namespace factor {
4
5 // Allocates memory
6 string* factor_vm::allot_string_internal(cell capacity) {
7   string* str = allot<string>(string_size(capacity));
8
9   str->length = tag_fixnum(capacity);
10   str->hashcode = false_object;
11   str->aux = false_object;
12
13   return str;
14 }
15
16 // Allocates memory
17 void factor_vm::fill_string(string* str_, cell start, cell capacity,
18                             cell fill) {
19   data_root<string> str(str_, this);
20
21   if (fill <= 0x7f)
22     memset(&str->data()[start], (uint8_t)fill, capacity - start);
23   else {
24     byte_array* aux;
25     if (to_boolean(str->aux))
26       aux = untag<byte_array>(str->aux);
27     else {
28       aux =
29           allot_uninitialized_array<byte_array>(untag_fixnum(str->length) * 2);
30       str->aux = tag<byte_array>(aux);
31       write_barrier(&str->aux);
32     }
33
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));
39   }
40 }
41
42 // Allocates memory
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();
47 }
48
49 // Allocates memory
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)));
54 }
55
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);
61 }
62
63 // Allocates memory
64 string* factor_vm::reallot_string(string* str_, cell capacity) {
65   data_root<string> str(str_, this);
66
67   if (reallot_string_in_place_p(str.untagged(), capacity)) {
68     str->length = tag_fixnum(capacity);
69
70     if (to_boolean(str->aux)) {
71       byte_array* aux = untag<byte_array>(str->aux);
72       aux->capacity = tag_fixnum(capacity * 2);
73     }
74
75     return str.untagged();
76   } else {
77     cell to_copy = string_capacity(str.untagged());
78     if (capacity < to_copy)
79       to_copy = capacity;
80
81     data_root<string> new_str(allot_string_internal(capacity), this);
82
83     memcpy(new_str->data(), str->data(), to_copy);
84
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);
89
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));
93     }
94
95     fill_string(new_str.untagged(), to_copy, capacity, '\0');
96     return new_str.untagged();
97   }
98 }
99
100 // Allocates memory
101 void factor_vm::primitive_resize_string() {
102   data_root<string> str(ctx->pop(), this);
103   check_tagged(str);
104   cell capacity = unbox_array_size();
105   ctx->push(tag<string>(reallot_string(str.untagged(), capacity)));
106 }
107
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;
113 }
114
115 }