This repository has been archived by the owner on Mar 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
string.c
192 lines (157 loc) · 3.98 KB
/
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* Project name:
* Implementace interpretu imperativního jazyka IFJ14
*
* Repository:
* https://github.com/Dasio/IFJ
*
* Team:
* Dávid Mikuš (xmikus15)
* Peter Hostačný (xhosta03)
* Tomáš Kello (xkello00)
* Adam Lučanský (xlucan01)
* Michaela Lukášová (xlukas09)
*/
#include "string.h"
String initEmptyString()
{
String str = {
.length = 0,
.allocated_size = STRING_INIT_ALLOC_SIZE,
.data = malloc(STRING_INIT_ALLOC_SIZE * sizeof(char))
};
MALLOC_TEST(str.data);
str.data[0] = '\0';
return str;
}
String initStringSize(uint32_t size)
{
String str = {
.length = 0,
.allocated_size = size,
.data = malloc(size * sizeof(char))
};
MALLOC_TEST(str.data);
str.data[0] = '\0';
return str;
}
void destroyString(String *dst) {
if(dst->allocated_size > 0)
{
free(dst->data);
dst->data = NULL;
}
dst->allocated_size = 0;
dst->length = 0;
}
char atString(String *dst, uint32_t pos) {
assert(dst);
assert(pos <= dst->length);
return dst->data[pos];
}
char setAtString(String *dst, uint32_t pos, char c) {
assert(dst);
assert(pos <= dst->length);
return dst->data[pos] = c;
}
void printString(String *dst)
{
assert(dst);
if(dst->length > 0) {
printf("%s", dst->data);
}
}
void aboutString(String *dst) {
printf("STR: allocated %u ,length %u\n", dst->allocated_size, dst->length);
}
String initString(char *src)
{
assert(src);
uint32_t size = strlen(src);
String str = {
.length = size,
.allocated_size = size + 1,
.data = malloc((size + 1) * sizeof(char)) // +1 for last character '\0'
};
MALLOC_TEST(str.data);
memcpy(str.data,src,size);
str.data[size] = '\0';
return str;
}
void appendCharToString(String *dst, char c)
{
assert(dst);
assert(dst->allocated_size > 0 &&
"Performed operation on uninitialized String");
// Double allocated space every potencial overflow
// !! Side-effect condition
if(++dst->length + 1 > dst->allocated_size) {
dst->allocated_size *= 2;
dst->data = realloc(dst->data, dst->allocated_size);
assert(dst->length < dst->allocated_size);
}
assert((int32_t) dst->length - 1 >= 0);
dst->data[dst->length - 1] = c;
dst->data[dst->length] = (char) 0;
// Due to performance, not clearing all remaning bytes
}
void appendCharsToString(String *dst, char *c) {
// TODO: Optimize!
while(*c != (char)0) {
appendCharToString(dst, *(c++));
}
}
String *concatStringToString(String *src1, String *src2) {
String *tmp = malloc(sizeof(String));
MALLOC_TEST(tmp);
uint32_t resulting_length = src1->length + src2->length;
tmp->length = resulting_length;
tmp->allocated_size = resulting_length + 1;
tmp->data = malloc(tmp->allocated_size * sizeof(char));
MALLOC_TEST(tmp->data);
memcpy(tmp->data, src1->data, src1->length);
memcpy(tmp->data + src1->length, src2->data, src2->length);
tmp->data[resulting_length] = (char) 0;
return tmp;
}
void appendStringToString(String *dst, String *src) {
assert(src);
assert(dst);
assert(dst->allocated_size > 0 &&
"Performed operation on uninitialized String");
if (src->length > 0)
{
assert(src->allocated_size > 0 &&
"Performed operation on uninitialized String");
uint32_t resulting_length = src->length + dst->length;
uint32_t allocated_size = resulting_length + 1;
if (allocated_size > dst->allocated_size)
{
dst->data = realloc(dst->data, allocated_size);
dst->allocated_size = allocated_size;
}
memcpy(dst->data + dst->length, src->data, src->length);
dst->length = resulting_length;
dst->data[resulting_length] = '\0';
}
}
void truncateString(String *dst) {
assert(dst);
if(dst->allocated_size > 0) {
// Cutting string with zero byte
dst->data[0] = (char) 0;
dst->length = 0;
}
}
// Do not use! Use streq()
bool equalsStringChars(String *str1, char *str2)
{
assert(str1); assert(str2);
return (strcmp(str1->data, str2) == 0);
}
// Do not use! Use streq()
bool equalsStringString(String *str1, String *str2)
{
assert(str1); assert(str2);
return (strcmp(str1->data, str2->data) == 0);
}