]> gitweb.factorcode.org Git - factor.git/blob - vm/strings.cpp
strings: move string-nth primitive out of the VM and into the library
[factor.git] / vm / strings.cpp
1 #include "master.hpp"
2
3 namespace factor
4 {
5
6 /* Allocates memory */
7 string *factor_vm::allot_string_internal(cell capacity)
8 {
9         string *str = allot<string>(string_size(capacity));
10
11         str->length = tag_fixnum(capacity);
12         str->hashcode = false_object;
13         str->aux = false_object;
14
15         return str;
16 }
17
18 /* Allocates memory */
19 void factor_vm::fill_string(string *str_, cell start, cell capacity, cell fill)
20 {
21         data_root<string> str(str_,this);
22
23         if(fill <= 0x7f)
24                 memset(&str->data()[start],(u8)fill,capacity - start);
25         else
26         {
27                 byte_array *aux;
28                 if(to_boolean(str->aux))
29                         aux = untag<byte_array>(str->aux);
30                 else
31                 {
32                         aux = allot_uninitialized_array<byte_array>(untag_fixnum(str->length) * 2);
33                         str->aux = tag<byte_array>(aux);
34                         write_barrier(&str->aux);
35                 }
36
37                 u8 lo_fill = (u8)((fill & 0x7f) | 0x80);
38                 u16 hi_fill = (u16)((fill >> 7) ^ 0x1);
39                 memset(&str->data()[start],lo_fill,capacity - start);
40                 memset_2(&aux->data<u16>()[start],hi_fill,(capacity - start) * sizeof(u16));
41         }
42 }
43
44 /* Allocates memory */
45 string *factor_vm::allot_string(cell capacity, cell fill)
46 {
47         data_root<string> str(allot_string_internal(capacity),this);
48         fill_string(str.untagged(),0,capacity,fill);
49         return str.untagged();
50 }
51
52 void factor_vm::primitive_string()
53 {
54         cell initial = to_cell(ctx->pop());
55         cell length = unbox_array_size();
56         ctx->push(tag<string>(allot_string(length,initial)));
57 }
58
59 bool factor_vm::reallot_string_in_place_p(string *str, cell capacity)
60 {
61         return nursery.contains_p(str)
62                 && (!to_boolean(str->aux) || nursery.contains_p(untag<byte_array>(str->aux)))
63                 && capacity <= string_capacity(str);
64 }
65
66 string* factor_vm::reallot_string(string *str_, cell capacity)
67 {
68         data_root<string> str(str_,this);
69
70         if(reallot_string_in_place_p(str.untagged(),capacity))
71         {
72                 str->length = tag_fixnum(capacity);
73
74                 if(to_boolean(str->aux))
75                 {
76                         byte_array *aux = untag<byte_array>(str->aux);
77                         aux->capacity = tag_fixnum(capacity * 2);
78                 }
79
80                 return str.untagged();
81         }
82         else
83         {
84                 cell to_copy = string_capacity(str.untagged());
85                 if(capacity < to_copy)
86                         to_copy = capacity;
87
88                 data_root<string> new_str(allot_string_internal(capacity),this);
89
90                 memcpy(new_str->data(),str->data(),to_copy);
91
92                 if(to_boolean(str->aux))
93                 {
94                         byte_array *new_aux = allot_uninitialized_array<byte_array>(capacity * 2);
95                         new_str->aux = tag<byte_array>(new_aux);
96                         write_barrier(&new_str->aux);
97
98                         byte_array *aux = untag<byte_array>(str->aux);
99                         memcpy(new_aux->data<u16>(),aux->data<u16>(),to_copy * sizeof(u16));
100                 }
101
102                 fill_string(new_str.untagged(),to_copy,capacity,'\0');
103                 return new_str.untagged();
104         }
105 }
106
107 void factor_vm::primitive_resize_string()
108 {
109         data_root<string> str(ctx->pop(),this);
110         str.untag_check(this);
111         cell capacity = unbox_array_size();
112         ctx->push(tag<string>(reallot_string(str.untagged(),capacity)));
113 }
114
115 void factor_vm::primitive_set_string_nth_fast()
116 {
117         string *str = untag<string>(ctx->pop());
118         cell index = untag_fixnum(ctx->pop());
119         cell value = untag_fixnum(ctx->pop());
120         str->data()[index] = (u8)value;
121 }
122
123 }