-
Notifications
You must be signed in to change notification settings - Fork 1
/
string.c
48 lines (40 loc) · 884 Bytes
/
string.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "object.h"
#include "mem.h"
#include "log.h"
static object* internal_make_string(char *str) {
object *obj;
char *p;
int len;
if (str == NULL) {
return NULL;
}
obj = alloc_object();
/* do not use strdup, for a consistent way to do gc */
len = strlen(str);
p = sc_malloc(len + 1);
if (p == NULL) {
sc_log("%s", "no memory for string");
exit(1);
}
strcpy(p, str);
obj_sv(obj) = p;
obj_slenv(obj) = len;
type(obj) = STRING;
return obj;
}
object* make_string(char *str) {
return internal_make_string(str);
}
int is_string(object *obj) {
return obj != NULL && type(obj) == STRING;
}
void string_free(object *obj) {
char *str;
if (!is_string(obj)) {
str = obj_sv(obj);
sc_free(str);
}
}