]> gitweb.factorcode.org Git - factor.git/blob - vm/strings.cpp
VM: Refactor strings.cpp/hpp to Factor style
[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], (u8) 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     u8 lo_fill = (u8)((fill & 0x7f) | 0x80);
35     u16 hi_fill = (u16)((fill >> 7) ^ 0x1);
36     memset(&str->data()[start], lo_fill, capacity - start);
37     memset_2(&aux->data<u16>()[start], hi_fill,
38              (capacity - start) * sizeof(u16));
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 nursery.contains_p(str) &&
58          (!to_boolean(str->aux) ||
59           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<u16>(), aux->data<u16>(), to_copy * sizeof(u16));
92     }
93
94     fill_string(new_str.untagged(), to_copy, capacity, '\0');
95     return new_str.untagged();
96   }
97 }
98
99 /* Allocates memory */
100 void factor_vm::primitive_resize_string() {
101   data_root<string> str(ctx->pop(), this);
102   str.untag_check(this);
103   cell capacity = unbox_array_size();
104   ctx->push(tag<string>(reallot_string(str.untagged(), capacity)));
105 }
106
107 void factor_vm::primitive_set_string_nth_fast() {
108   string* str = untag<string>(ctx->pop());
109   cell index = untag_fixnum(ctx->pop());
110   cell value = untag_fixnum(ctx->pop());
111   str->data()[index] = (u8) value;
112 }
113
114 }