factor/vm/strings.cpp

190 lines
4.5 KiB
C++
Raw Normal View History

2009-05-02 05:04:19 -04:00
#include "master.hpp"
CELL string_nth(F_STRING* string, CELL index)
{
/* If high bit is set, the most significant 16 bits of the char
come from the aux vector. The least significant bit of the
corresponding aux vector entry is negated, so that we can
XOR the two components together and get the original code point
back. */
CELL ch = bget(SREF(string,index));
if((ch & 0x80) == 0)
return ch;
else
{
F_BYTE_ARRAY *aux = untag<F_BYTE_ARRAY>(string->aux);
2009-05-02 05:04:19 -04:00
return (cget(BREF(aux,index * sizeof(u16))) << 7) ^ ch;
}
}
2009-05-02 10:19:09 -04:00
void set_string_nth_fast(F_STRING *string, CELL index, CELL ch)
2009-05-02 05:04:19 -04:00
{
bput(SREF(string,index),ch);
}
2009-05-02 10:19:09 -04:00
void set_string_nth_slow(F_STRING *string_, CELL index, CELL ch)
2009-05-02 05:04:19 -04:00
{
2009-05-02 10:19:09 -04:00
gc_root<F_STRING> string(string_);
2009-05-02 05:04:19 -04:00
F_BYTE_ARRAY *aux;
2009-05-02 10:19:09 -04:00
bput(SREF(string.untagged(),index),(ch & 0x7f) | 0x80);
2009-05-02 05:04:19 -04:00
if(string->aux == F)
{
/* We don't need to pre-initialize the
byte array with any data, since we
only ever read from the aux vector
if the most significant bit of a
character is set. Initially all of
the bits are clear. */
2009-05-02 05:43:58 -04:00
aux = allot_array_internal<F_BYTE_ARRAY>(
untag_fixnum(string->length)
2009-05-02 05:04:19 -04:00
* sizeof(u16));
2009-05-02 10:19:09 -04:00
write_barrier(string.value());
string->aux = tag<F_BYTE_ARRAY>(aux);
2009-05-02 05:04:19 -04:00
}
else
aux = untag<F_BYTE_ARRAY>(string->aux);
2009-05-02 05:04:19 -04:00
cput(BREF(aux,index * sizeof(u16)),(ch >> 7) ^ 1);
}
/* allocates memory */
void set_string_nth(F_STRING* string, CELL index, CELL ch)
{
if(ch <= 0x7f)
set_string_nth_fast(string,index,ch);
else
set_string_nth_slow(string,index,ch);
}
2009-05-02 10:19:09 -04:00
/* Allocates memory */
F_STRING *allot_string_internal(CELL capacity)
2009-05-02 05:04:19 -04:00
{
2009-05-02 10:19:09 -04:00
F_STRING *string = allot<F_STRING>(string_size(capacity));
2009-05-02 05:04:19 -04:00
string->length = tag_fixnum(capacity);
string->hashcode = F;
string->aux = F;
return string;
}
2009-05-02 10:19:09 -04:00
/* Allocates memory */
void fill_string(F_STRING *string_, CELL start, CELL capacity, CELL fill)
2009-05-02 05:04:19 -04:00
{
2009-05-02 10:19:09 -04:00
gc_root<F_STRING> string(string_);
2009-05-02 05:04:19 -04:00
if(fill <= 0x7f)
2009-05-02 10:19:09 -04:00
memset((void *)SREF(string.untagged(),start),fill,capacity - start);
2009-05-02 05:04:19 -04:00
else
{
CELL i;
for(i = start; i < capacity; i++)
2009-05-02 10:19:09 -04:00
set_string_nth(string.untagged(),i,fill);
2009-05-02 05:04:19 -04:00
}
}
2009-05-02 10:19:09 -04:00
/* Allocates memory */
2009-05-02 05:04:19 -04:00
F_STRING *allot_string(CELL capacity, CELL fill)
{
2009-05-02 10:19:09 -04:00
gc_root<F_STRING> string(allot_string_internal(capacity));
fill_string(string.untagged(),0,capacity,fill);
return string.untagged();
2009-05-02 05:04:19 -04:00
}
void primitive_string(void)
{
CELL initial = to_cell(dpop());
CELL length = unbox_array_size();
dpush(tag<F_STRING>(allot_string(length,initial)));
2009-05-02 05:04:19 -04:00
}
static bool reallot_string_in_place_p(F_STRING *string, CELL capacity)
{
return in_zone(&nursery,(CELL)string) && capacity <= string_capacity(string);
}
2009-05-02 10:19:09 -04:00
F_STRING* reallot_string(F_STRING *string_, CELL capacity)
2009-05-02 05:04:19 -04:00
{
2009-05-02 10:19:09 -04:00
gc_root<F_STRING> string(string_);
if(reallot_string_in_place_p(string.untagged(),capacity))
2009-05-02 05:04:19 -04:00
{
string->length = tag_fixnum(capacity);
if(string->aux != F)
{
F_BYTE_ARRAY *aux = untag<F_BYTE_ARRAY>(string->aux);
2009-05-02 05:04:19 -04:00
aux->capacity = tag_fixnum(capacity * 2);
}
2009-05-02 10:19:09 -04:00
return string.untagged();
2009-05-02 05:04:19 -04:00
}
else
{
2009-05-02 10:19:09 -04:00
CELL to_copy = string_capacity(string.untagged());
2009-05-02 05:04:19 -04:00
if(capacity < to_copy)
to_copy = capacity;
2009-05-02 10:19:09 -04:00
gc_root<F_STRING> new_string(allot_string_internal(capacity));
2009-05-02 05:04:19 -04:00
2009-05-02 10:19:09 -04:00
memcpy(new_string.untagged() + 1,string.untagged() + 1,to_copy);
2009-05-02 05:04:19 -04:00
if(string->aux != F)
{
F_BYTE_ARRAY *new_aux = allot_byte_array(capacity * sizeof(u16));
2009-05-02 10:19:09 -04:00
write_barrier(new_string.value());
new_string->aux = tag<F_BYTE_ARRAY>(new_aux);
2009-05-02 05:04:19 -04:00
F_BYTE_ARRAY *aux = untag<F_BYTE_ARRAY>(string->aux);
2009-05-02 05:04:19 -04:00
memcpy(new_aux + 1,aux + 1,to_copy * sizeof(u16));
}
2009-05-02 10:19:09 -04:00
fill_string(new_string.untagged(),to_copy,capacity,'\0');
return new_string.untagged();
2009-05-02 05:04:19 -04:00
}
}
void primitive_resize_string(void)
{
F_STRING* string = untag_check<F_STRING>(dpop());
2009-05-02 05:04:19 -04:00
CELL capacity = unbox_array_size();
dpush(tag<F_STRING>(reallot_string(string,capacity)));
2009-05-02 05:04:19 -04:00
}
void primitive_string_nth(void)
{
F_STRING *string = untag<F_STRING>(dpop());
CELL index = untag_fixnum(dpop());
2009-05-02 05:04:19 -04:00
dpush(tag_fixnum(string_nth(string,index)));
}
void primitive_set_string_nth(void)
{
F_STRING *string = untag<F_STRING>(dpop());
CELL index = untag_fixnum(dpop());
CELL value = untag_fixnum(dpop());
2009-05-02 05:04:19 -04:00
set_string_nth(string,index,value);
}
void primitive_set_string_nth_fast(void)
{
F_STRING *string = untag<F_STRING>(dpop());
CELL index = untag_fixnum(dpop());
CELL value = untag_fixnum(dpop());
2009-05-02 05:04:19 -04:00
set_string_nth_fast(string,index,value);
}
void primitive_set_string_nth_slow(void)
{
F_STRING *string = untag<F_STRING>(dpop());
CELL index = untag_fixnum(dpop());
CELL value = untag_fixnum(dpop());
2009-05-02 05:04:19 -04:00
set_string_nth_slow(string,index,value);
}