-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdyn_string.c
42 lines (36 loc) · 998 Bytes
/
dyn_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
#include "dyn_string.h"
#include <stdlib.h>
void DynStringInit(struct DynString* s) {
s->capacity = 8;
s->length = 0;
s->data = (char*)malloc(s->capacity * sizeof(char));
}
void DynStringFree(struct DynString* s) {
s->capacity = 0;
s->length = 0;
free(s->data); // Здесь обязательно делать только эту инструкцию
s->data = NULL;
}
void DynStringAdd(struct DynString* s, char c) {
if (s->length >= s->capacity) {
s->capacity *= 2;
char* temp = (char*)malloc(s->capacity * sizeof(char));
for (int i = 0; i < s->length; ++i) {
temp[i] = s->data[i];
}
free(s->data);
s->data = temp;
}
s->data[s->length++] = c;
}
void DynStringClear(struct DynString* s) {
s->length = 0;
}
char* DynStringGet(const struct DynString* s) {
char* result = (char*)malloc((s->length + 1) * sizeof(char));
for (int i = 0; i < s->length; ++i) {
result[i] = s->data[i];
}
result[s->length] = '\0';
return result;
}