-
Notifications
You must be signed in to change notification settings - Fork 0
/
str.c
49 lines (44 loc) · 774 Bytes
/
str.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
48
49
#include "dat.h"
#include "fn.h"
#include <string.h>
void
strinit(Object *s, Object *p)
{
for(char *c = p->beg ; c < p->ptr;)
*s->ptr++ = *c++;
*s->ptr = 0;
}
Object*
strraise(Object *s, int ns)
{
Object *dst = newstr(gc, ns + 1);
strinit(dst, s);
return dst;
}
Object*
strputc(Object *s, int c)
{
if(s->ptr + 1 >= s->end)
s = strraise(s, (s->end - s->beg) * 2);
*s->ptr++ = c;
*s->ptr = 0;
return s;
}
Object*
strputs(Object *s, Object *ptr)
{
int l = ptr->ptr - ptr->beg;
if(s->ptr + l>= s->end)
s = strraise(s, s->end - s->beg + l);
memcpy(s->ptr, ptr->beg, l);
s->ptr += l;
*s->ptr = 0;
return s;
}
int
strequal(Object *a, Object *b)
{
int la = a->ptr - a->beg;
int lb = b->ptr - b->beg;
return la == lb && memcmp(a->beg, b->beg, la) == 0;
}