Global generation
This commit is contained in:
43
src/lisp_string.c
Normal file
43
src/lisp_string.c
Normal file
@ -0,0 +1,43 @@
|
||||
#include "lisp_string.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
LispVal *make_lisp_string(const char *data, size_t length, bool take,
|
||||
bool copy) {
|
||||
LispString *obj = lisp_alloc_object(sizeof(LispString), TYPE_STRING);
|
||||
obj->owned = take;
|
||||
obj->length = length;
|
||||
if (copy) {
|
||||
obj->data = lisp_malloc(length + 1);
|
||||
memcpy(obj->data, data, length);
|
||||
obj->data[length] = '\0';
|
||||
} else {
|
||||
obj->data = (char *) data;
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
DEFUN(strings_equal, "strings-equal", (LispVal * string1, LispVal *string2),
|
||||
"(string1 string2)", "") {
|
||||
// TODO type checking
|
||||
if (((LispString *) string1)->length != ((LispString *) string2)->length) {
|
||||
return Qnil;
|
||||
}
|
||||
return memcmp(((LispString *) string1)->data,
|
||||
((LispString *) string2)->data,
|
||||
((LispString *) string1)->length)
|
||||
== 0
|
||||
? Qt
|
||||
: Qnil;
|
||||
}
|
||||
|
||||
DEFUN(hash_string, "hash-string", (LispVal * string), "(string)", "") {
|
||||
// TODO type checking
|
||||
size_t len = ((LispString *) string)->length;
|
||||
const char *str = ((LispString *) string)->data;
|
||||
uintptr_t hash = 5381;
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
hash = ((hash << 5) + hash) + str[i];
|
||||
}
|
||||
return MAKE_FIXNUM(hash);
|
||||
}
|
||||
Reference in New Issue
Block a user