-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString.c
47 lines (41 loc) · 897 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"
String String_new()
{
String str;
str.alloc = false;
str.buf = NULL;
return str;
}
void String_copyFromRaw(String * str, const char * src)
{
String_dealloc(str);
str->buf = malloc(sizeof(src));
strcpy(str->buf, src);
str->alloc = true;
}
void String_copyFromStr(String * str, const String src)
{
String_dealloc(str);
str->buf = malloc(sizeof(src.buf));
strcpy(str->buf, src.buf);
str->alloc = true;
}
void String_moveFromRaw(String * str, const char * src)
{
String_dealloc(str);
strcpy(str->buf, src);
}
void String_moveFromStr(String * str, const String src)
{
String_dealloc(str);
strcpy(str->buf, src.buf);
}
void String_dealloc(String * str)
{
if (str->alloc)
{
free(str->buf);
str->buf = NULL;
str->alloc = false;
}
}