2004-07-16 02:26:21 -04:00
|
|
|
#include "factor.h"
|
|
|
|
|
|
|
|
/* untagged */
|
2004-08-27 02:09:24 -04:00
|
|
|
STRING* allot_string(FIXNUM capacity)
|
2004-07-16 02:26:21 -04:00
|
|
|
{
|
2004-08-27 02:09:24 -04:00
|
|
|
STRING* string;
|
|
|
|
if(capacity < 0)
|
|
|
|
general_error(ERROR_NEGATIVE_ARRAY_SIZE,tag_fixnum(capacity));
|
|
|
|
string = allot_object(STRING_TYPE,
|
2004-07-16 02:26:21 -04:00
|
|
|
sizeof(STRING) + capacity * CHARS);
|
|
|
|
string->capacity = capacity;
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* call this after constructing a string */
|
|
|
|
/* uses same algorithm as java.lang.String for compatibility */
|
|
|
|
void hash_string(STRING* str)
|
|
|
|
{
|
2004-08-01 19:26:43 -04:00
|
|
|
FIXNUM hash = 0;
|
|
|
|
CELL i;
|
2004-07-16 02:26:21 -04:00
|
|
|
for(i = 0; i < str->capacity; i++)
|
|
|
|
hash = 31*hash + string_nth(str,i);
|
|
|
|
str->hashcode = hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* untagged */
|
2004-08-27 02:09:24 -04:00
|
|
|
STRING* string(FIXNUM capacity, CELL fill)
|
2004-07-16 02:26:21 -04:00
|
|
|
{
|
2004-08-01 19:26:43 -04:00
|
|
|
CELL i;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
STRING* string = allot_string(capacity);
|
|
|
|
|
|
|
|
for(i = 0; i < capacity; i++)
|
2004-08-07 18:45:48 -04:00
|
|
|
cput(SREF(string,i),fill);
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
hash_string(string);
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2004-08-27 02:09:24 -04:00
|
|
|
STRING* grow_string(STRING* string, FIXNUM capacity, CHAR fill)
|
2004-07-16 02:26:21 -04:00
|
|
|
{
|
|
|
|
/* later on, do an optimization: if end of array is here, just grow */
|
2004-08-01 19:26:43 -04:00
|
|
|
CELL i;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
STRING* new_string = allot_string(capacity);
|
|
|
|
|
|
|
|
memcpy(new_string + 1,string + 1,string->capacity * CHARS);
|
|
|
|
|
|
|
|
for(i = string->capacity; i < capacity; i++)
|
2004-08-07 18:45:48 -04:00
|
|
|
cput(SREF(new_string,i),fill);
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
return new_string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* untagged */
|
2004-07-24 17:37:42 -04:00
|
|
|
STRING* from_c_string(const char* c_string)
|
2004-07-16 02:26:21 -04:00
|
|
|
{
|
|
|
|
CELL length = strlen(c_string);
|
|
|
|
STRING* s = allot_string(length);
|
2004-08-01 19:26:43 -04:00
|
|
|
CELL i;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
for(i = 0; i < length; i++)
|
|
|
|
{
|
2004-08-07 18:45:48 -04:00
|
|
|
cput(SREF(s,i),*c_string);
|
2004-07-16 02:26:21 -04:00
|
|
|
c_string++;
|
|
|
|
}
|
|
|
|
|
|
|
|
hash_string(s);
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* untagged */
|
|
|
|
char* to_c_string(STRING* s)
|
|
|
|
{
|
2004-08-29 01:04:42 -04:00
|
|
|
STRING* _c_str = allot_string(s->capacity / CHARS + 1);
|
2004-08-01 19:26:43 -04:00
|
|
|
CELL i;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
char* c_str = (char*)(_c_str + 1);
|
|
|
|
|
|
|
|
for(i = 0; i < s->capacity; i++)
|
|
|
|
c_str[i] = string_nth(s,i);
|
|
|
|
|
|
|
|
c_str[s->capacity] = '\0';
|
|
|
|
|
|
|
|
return c_str;
|
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_stringp(void)
|
|
|
|
{
|
2004-08-12 17:36:36 -04:00
|
|
|
drepl(tag_boolean(typep(STRING_TYPE,dpeek())));
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_string_length(void)
|
|
|
|
{
|
2004-08-12 17:36:36 -04:00
|
|
|
drepl(tag_fixnum(untag_string(dpeek())->capacity));
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_string_nth(void)
|
|
|
|
{
|
2004-08-12 17:36:36 -04:00
|
|
|
STRING* string = untag_string(dpop());
|
2004-07-29 17:18:41 -04:00
|
|
|
CELL index = to_fixnum(dpop());
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
if(index < 0 || index >= string->capacity)
|
2004-07-24 17:37:42 -04:00
|
|
|
range_error(tag_object(string),index,string->capacity);
|
2004-08-12 17:36:36 -04:00
|
|
|
dpush(tag_fixnum(string_nth(string,index)));
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|
|
|
|
|
2004-08-12 01:07:22 -04:00
|
|
|
FIXNUM string_compare_head(STRING* s1, STRING* s2, CELL len)
|
2004-07-16 02:26:21 -04:00
|
|
|
{
|
|
|
|
CELL i = 0;
|
2004-08-12 01:07:22 -04:00
|
|
|
while(i < len)
|
2004-07-16 02:26:21 -04:00
|
|
|
{
|
|
|
|
CHAR c1 = string_nth(s1,i);
|
|
|
|
CHAR c2 = string_nth(s2,i);
|
|
|
|
if(c1 != c2)
|
|
|
|
return c1 - c2;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2004-08-12 01:07:22 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FIXNUM string_compare(STRING* s1, STRING* s2)
|
|
|
|
{
|
|
|
|
CELL len1 = s1->capacity;
|
|
|
|
CELL len2 = s2->capacity;
|
|
|
|
|
|
|
|
CELL limit = (len1 < len2 ? len1 : len2);
|
|
|
|
|
|
|
|
CELL comp = string_compare_head(s1,s2,limit);
|
|
|
|
if(comp != 0)
|
|
|
|
return comp;
|
|
|
|
else
|
|
|
|
return len1 - len2;
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_string_compare(void)
|
|
|
|
{
|
2004-08-12 17:36:36 -04:00
|
|
|
STRING* s2 = untag_string(dpop());
|
2004-08-12 02:13:43 -04:00
|
|
|
STRING* s1 = untag_string(dpop());
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2004-08-12 17:36:36 -04:00
|
|
|
dpush(tag_fixnum(string_compare(s1,s2)));
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool string_eq(STRING* s1, STRING* s2)
|
|
|
|
{
|
|
|
|
if(s1->hashcode != s2->hashcode)
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return (string_compare(s1,s2) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_string_eq(void)
|
|
|
|
{
|
2004-08-12 17:36:36 -04:00
|
|
|
STRING* s1 = untag_string(dpop());
|
2004-07-16 02:26:21 -04:00
|
|
|
CELL with = dpop();
|
|
|
|
if(typep(STRING_TYPE,with))
|
2004-08-12 17:36:36 -04:00
|
|
|
dpush(tag_boolean(string_eq(s1,(STRING*)UNTAG(with))));
|
2004-07-16 02:26:21 -04:00
|
|
|
else
|
2004-08-12 17:36:36 -04:00
|
|
|
dpush(F);
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void primitive_string_hashcode(void)
|
|
|
|
{
|
2004-08-29 15:56:30 -04:00
|
|
|
drepl(tag_fixnum(untag_string(dpeek())->hashcode));
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|
|
|
|
|
2004-08-04 03:12:55 -04:00
|
|
|
CELL index_of_ch(CELL index, STRING* string, CELL ch)
|
2004-07-16 02:26:21 -04:00
|
|
|
{
|
|
|
|
while(index < string->capacity)
|
|
|
|
{
|
|
|
|
if(string_nth(string,index) == ch)
|
|
|
|
return index;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2004-08-04 03:12:55 -04:00
|
|
|
INLINE FIXNUM index_of_str(FIXNUM index, STRING* string, STRING* substring)
|
2004-07-16 02:26:21 -04:00
|
|
|
{
|
2004-08-04 03:12:55 -04:00
|
|
|
CELL i = index;
|
|
|
|
CELL limit = string->capacity - substring->capacity;
|
|
|
|
CELL scan;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2004-08-04 03:12:55 -04:00
|
|
|
if(substring->capacity == 1)
|
|
|
|
return index_of_ch(index,string,string_nth(substring,0));
|
|
|
|
|
|
|
|
if(substring->capacity > string->capacity)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
outer: if(i <= limit)
|
|
|
|
{
|
|
|
|
for(scan = 0; scan < substring->capacity; scan++)
|
|
|
|
{
|
|
|
|
if(string_nth(string,i + scan)
|
|
|
|
!= string_nth(substring,scan))
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
goto outer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We reached here and every char in the substring matched */
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We reached here and nothing matched */
|
|
|
|
return -1;
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* index string substring -- index */
|
|
|
|
void primitive_index_of(void)
|
|
|
|
{
|
2004-08-12 17:36:36 -04:00
|
|
|
CELL ch = dpop();
|
2004-07-16 02:26:21 -04:00
|
|
|
STRING* string;
|
2004-08-04 03:12:55 -04:00
|
|
|
FIXNUM index;
|
2004-07-16 02:26:21 -04:00
|
|
|
CELL result;
|
|
|
|
string = untag_string(dpop());
|
2004-07-29 17:18:41 -04:00
|
|
|
index = to_fixnum(dpop());
|
2004-08-04 03:12:55 -04:00
|
|
|
if(index < 0 || index > string->capacity)
|
2004-08-04 22:43:58 -04:00
|
|
|
{
|
2004-08-04 03:12:55 -04:00
|
|
|
range_error(tag_object(string),index,string->capacity);
|
2004-08-04 22:43:58 -04:00
|
|
|
result = -1; /* can't happen */
|
|
|
|
}
|
2004-08-04 03:12:55 -04:00
|
|
|
else if(TAG(ch) == FIXNUM_TYPE)
|
2004-07-29 17:18:41 -04:00
|
|
|
result = index_of_ch(index,string,to_fixnum(ch));
|
2004-07-16 02:26:21 -04:00
|
|
|
else
|
|
|
|
result = index_of_str(index,string,untag_string(ch));
|
2004-08-12 17:36:36 -04:00
|
|
|
dpush(tag_fixnum(result));
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
INLINE STRING* substring(CELL start, CELL end, STRING* string)
|
|
|
|
{
|
|
|
|
STRING* result;
|
|
|
|
|
2004-07-24 17:37:42 -04:00
|
|
|
if(start < 0)
|
|
|
|
range_error(tag_object(string),start,string->capacity);
|
|
|
|
|
2004-08-27 02:09:24 -04:00
|
|
|
if(end < start || end > string->capacity)
|
2004-07-24 17:37:42 -04:00
|
|
|
range_error(tag_object(string),end,string->capacity);
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
result = allot_string(end - start);
|
|
|
|
memcpy(result + 1,
|
2004-08-04 22:43:58 -04:00
|
|
|
(void*)((CELL)(string + 1) + CHARS * start),
|
2004-07-16 02:26:21 -04:00
|
|
|
CHARS * (end - start));
|
|
|
|
hash_string(result);
|
2004-07-27 22:52:35 -04:00
|
|
|
|
2004-07-16 02:26:21 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start end string -- string */
|
|
|
|
void primitive_substring(void)
|
|
|
|
{
|
2004-08-12 17:36:36 -04:00
|
|
|
STRING* string = untag_string(dpop());
|
2004-07-29 17:18:41 -04:00
|
|
|
CELL end = to_fixnum(dpop());
|
|
|
|
CELL start = to_fixnum(dpop());
|
2004-08-12 17:36:36 -04:00
|
|
|
dpush(tag_object(substring(start,end,string)));
|
2004-07-16 02:26:21 -04:00
|
|
|
}
|