line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
/* |
2
|
|
|
|
|
|
|
Copyright (C) 2015-2017 Alexander Borisov |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or |
5
|
|
|
|
|
|
|
modify it under the terms of the GNU Lesser General Public |
6
|
|
|
|
|
|
|
License as published by the Free Software Foundation; either |
7
|
|
|
|
|
|
|
version 2.1 of the License, or (at your option) any later version. |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful, |
10
|
|
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12
|
|
|
|
|
|
|
Lesser General Public License for more details. |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public |
15
|
|
|
|
|
|
|
License along with this library; if not, write to the Free Software |
16
|
|
|
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
Author: lex.borisov@gmail.com (Alexander Borisov) |
19
|
|
|
|
|
|
|
*/ |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
#include "mycore/mystring.h" |
22
|
|
|
|
|
|
|
#include "mycore/utils/resources.h" |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
///////////////////////////////////////////////////////// |
25
|
|
|
|
|
|
|
//// Init, Clean, Destroy |
26
|
|
|
|
|
|
|
//// |
27
|
|
|
|
|
|
|
///////////////////////////////////////////////////////// |
28
|
1515
|
|
|
|
|
|
char * mycore_string_init(mchar_async_t *mchar, size_t node_idx, mycore_string_t* str, size_t size) |
29
|
|
|
|
|
|
|
{ |
30
|
1515
|
|
|
|
|
|
str->data = mchar_async_malloc(mchar, node_idx, size); |
31
|
1515
|
|
|
|
|
|
str->size = size; |
32
|
1515
|
|
|
|
|
|
str->node_idx = node_idx; |
33
|
1515
|
|
|
|
|
|
str->mchar = mchar; |
34
|
|
|
|
|
|
|
|
35
|
1515
|
|
|
|
|
|
mycore_string_clean(str); |
36
|
|
|
|
|
|
|
|
37
|
1515
|
|
|
|
|
|
return str->data; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
1515
|
|
|
|
|
|
void mycore_string_clean(mycore_string_t* str) |
41
|
|
|
|
|
|
|
{ |
42
|
1515
|
|
|
|
|
|
str->length = 0; |
43
|
1515
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
3924
|
|
|
|
|
|
void mycore_string_clean_all(mycore_string_t* str) |
46
|
|
|
|
|
|
|
{ |
47
|
3924
|
|
|
|
|
|
memset(str, 0, sizeof(mycore_string_t)); |
48
|
3924
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
170
|
|
|
|
|
|
mycore_string_t * mycore_string_destroy(mycore_string_t* str, bool destroy_obj) |
51
|
|
|
|
|
|
|
{ |
52
|
170
|
50
|
|
|
|
|
if(str == NULL) |
53
|
0
|
|
|
|
|
|
return NULL; |
54
|
|
|
|
|
|
|
|
55
|
170
|
50
|
|
|
|
|
if(str->data && str->mchar) |
|
|
50
|
|
|
|
|
|
56
|
170
|
|
|
|
|
|
mchar_async_free(str->mchar, str->node_idx, str->data); |
57
|
|
|
|
|
|
|
|
58
|
170
|
50
|
|
|
|
|
if(destroy_obj && str) { |
|
|
0
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
mycore_free(str); |
60
|
0
|
|
|
|
|
|
return NULL; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
170
|
|
|
|
|
|
return str; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
void mycore_string_raw_clean(mycore_string_raw_t* str_raw) |
67
|
|
|
|
|
|
|
{ |
68
|
0
|
|
|
|
|
|
str_raw->length = 0; |
69
|
0
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
void mycore_string_raw_clean_all(mycore_string_raw_t* str_raw) |
72
|
|
|
|
|
|
|
{ |
73
|
0
|
|
|
|
|
|
memset(str_raw, 0, sizeof(mycore_string_raw_t)); |
74
|
0
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
0
|
|
|
|
|
|
mycore_string_raw_t * mycore_string_raw_destroy(mycore_string_raw_t* str_raw, bool destroy_obj) |
77
|
|
|
|
|
|
|
{ |
78
|
0
|
0
|
|
|
|
|
if(str_raw == NULL) |
79
|
0
|
|
|
|
|
|
return NULL; |
80
|
|
|
|
|
|
|
|
81
|
0
|
0
|
|
|
|
|
if(str_raw->data) { |
82
|
0
|
|
|
|
|
|
mycore_free(str_raw->data); |
83
|
0
|
|
|
|
|
|
str_raw->data = NULL; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
0
|
0
|
|
|
|
|
if(destroy_obj && str_raw) { |
|
|
0
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
mycore_free(str_raw); |
88
|
0
|
|
|
|
|
|
return NULL; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
return str_raw; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
1401
|
|
|
|
|
|
char * mycore_string_realloc(mycore_string_t *str, size_t new_size) |
95
|
|
|
|
|
|
|
{ |
96
|
1401
|
50
|
|
|
|
|
if(str == NULL) |
97
|
0
|
|
|
|
|
|
return NULL; |
98
|
|
|
|
|
|
|
|
99
|
1401
|
|
|
|
|
|
char *tmp = mchar_async_realloc(str->mchar, str->node_idx, str->data, str->length, new_size); |
100
|
|
|
|
|
|
|
|
101
|
1401
|
50
|
|
|
|
|
if(tmp) { |
102
|
1401
|
|
|
|
|
|
str->size = new_size; |
103
|
1401
|
|
|
|
|
|
str->data = tmp; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
else |
106
|
0
|
|
|
|
|
|
return NULL; |
107
|
|
|
|
|
|
|
|
108
|
1401
|
|
|
|
|
|
return tmp; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
///////////////////////////////////////////////////////// |
112
|
|
|
|
|
|
|
//// Basic API |
113
|
|
|
|
|
|
|
//// |
114
|
|
|
|
|
|
|
///////////////////////////////////////////////////////// |
115
|
0
|
|
|
|
|
|
char * mycore_string_data_alloc(mchar_async_t *mchar, size_t node_id, size_t size) |
116
|
|
|
|
|
|
|
{ |
117
|
0
|
|
|
|
|
|
return mchar_async_malloc(mchar, node_id, size); |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
|
120
|
0
|
|
|
|
|
|
char * mycore_string_data_realloc(mchar_async_t *mchar, size_t node_id, char *data, size_t len_to_copy, size_t size) |
121
|
|
|
|
|
|
|
{ |
122
|
0
|
|
|
|
|
|
return mchar_async_realloc(mchar, node_id, data, len_to_copy, size); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
0
|
|
|
|
|
|
void mycore_string_data_free(mchar_async_t *mchar, size_t node_id, char *data) |
126
|
|
|
|
|
|
|
{ |
127
|
0
|
|
|
|
|
|
mchar_async_free(mchar, node_id, data); |
128
|
0
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
0
|
|
|
|
|
|
char * mycore_string_data(mycore_string_t *str) |
131
|
|
|
|
|
|
|
{ |
132
|
0
|
0
|
|
|
|
|
if(str == NULL) |
133
|
0
|
|
|
|
|
|
return NULL; |
134
|
|
|
|
|
|
|
|
135
|
0
|
|
|
|
|
|
return str->data; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
0
|
|
|
|
|
|
size_t mycore_string_length(mycore_string_t *str) |
139
|
|
|
|
|
|
|
{ |
140
|
0
|
0
|
|
|
|
|
if(str == NULL) |
141
|
0
|
|
|
|
|
|
return 0; |
142
|
|
|
|
|
|
|
|
143
|
0
|
|
|
|
|
|
return str->length; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
0
|
|
|
|
|
|
size_t mycore_string_size(mycore_string_t *str) |
147
|
|
|
|
|
|
|
{ |
148
|
0
|
0
|
|
|
|
|
if(str == NULL) |
149
|
0
|
|
|
|
|
|
return 0; |
150
|
|
|
|
|
|
|
|
151
|
0
|
|
|
|
|
|
return str->size; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
0
|
|
|
|
|
|
char * mycore_string_data_set(mycore_string_t *str, char *data) |
155
|
|
|
|
|
|
|
{ |
156
|
0
|
0
|
|
|
|
|
if(str == NULL) |
157
|
0
|
|
|
|
|
|
return NULL; |
158
|
|
|
|
|
|
|
|
159
|
0
|
|
|
|
|
|
str->data = data; |
160
|
0
|
|
|
|
|
|
return str->data; |
161
|
|
|
|
|
|
|
} |
162
|
|
|
|
|
|
|
|
163
|
0
|
|
|
|
|
|
size_t mycore_string_size_set(mycore_string_t *str, size_t size) |
164
|
|
|
|
|
|
|
{ |
165
|
0
|
0
|
|
|
|
|
if(str == NULL) |
166
|
0
|
|
|
|
|
|
return 0; |
167
|
|
|
|
|
|
|
|
168
|
0
|
|
|
|
|
|
str->size = size; |
169
|
0
|
|
|
|
|
|
return str->size; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
0
|
|
|
|
|
|
size_t mycore_string_length_set(mycore_string_t *str, size_t length) |
173
|
|
|
|
|
|
|
{ |
174
|
0
|
0
|
|
|
|
|
if(str == NULL) |
175
|
0
|
|
|
|
|
|
return 0; |
176
|
|
|
|
|
|
|
|
177
|
0
|
|
|
|
|
|
str->length = length; |
178
|
0
|
|
|
|
|
|
return str->length; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
///////////////////////////////////////////////////////// |
182
|
|
|
|
|
|
|
//// Append API |
183
|
|
|
|
|
|
|
//// |
184
|
|
|
|
|
|
|
///////////////////////////////////////////////////////// |
185
|
178
|
|
|
|
|
|
void mycore_string_append(mycore_string_t* str, const char* buff, size_t length) |
186
|
|
|
|
|
|
|
{ |
187
|
178
|
100
|
|
|
|
|
MyCORE_STRING_REALLOC_IF_NEED(str, (length + 1), 0); |
188
|
|
|
|
|
|
|
|
189
|
178
|
|
|
|
|
|
memcpy(&str->data[str->length], buff, (sizeof(char) * length)); |
190
|
|
|
|
|
|
|
|
191
|
178
|
|
|
|
|
|
str->length += length; |
192
|
178
|
|
|
|
|
|
str->data[str->length] = '\0'; |
193
|
178
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
34
|
|
|
|
|
|
void mycore_string_append_one(mycore_string_t* str, const char data) |
196
|
|
|
|
|
|
|
{ |
197
|
34
|
100
|
|
|
|
|
MyCORE_STRING_REALLOC_IF_NEED(str, 2, 1); |
198
|
34
|
|
|
|
|
|
MyCORE_STRING_APPEND_BYTE_WITHOUT_REALLOC(data, str); |
199
|
34
|
|
|
|
|
|
MyCORE_STRING_APPEND_BYTE_WITHOUT_INCREMENT_REALLOC('\0', str); |
200
|
34
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
41
|
|
|
|
|
|
void mycore_string_append_lowercase(mycore_string_t* str, const char* data, size_t length) |
203
|
|
|
|
|
|
|
{ |
204
|
41
|
50
|
|
|
|
|
MyCORE_STRING_REALLOC_IF_NEED(str, (length + 1), 0); |
205
|
|
|
|
|
|
|
|
206
|
41
|
|
|
|
|
|
unsigned char *ref = (unsigned char*)&str->data[str->length]; |
207
|
41
|
|
|
|
|
|
const unsigned char *buf = (const unsigned char*)data; |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
size_t i; |
210
|
219
|
100
|
|
|
|
|
for(i = 0; i < length; i++) { |
211
|
178
|
|
|
|
|
|
ref[i] = mycore_string_chars_lowercase_map[ buf[i] ]; |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
|
214
|
41
|
|
|
|
|
|
ref[i] = '\0'; |
215
|
41
|
|
|
|
|
|
str->length += length; |
216
|
41
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
|
218
|
0
|
|
|
|
|
|
void mycore_string_copy(mycore_string_t* dest, mycore_string_t* target) |
219
|
|
|
|
|
|
|
{ |
220
|
0
|
|
|
|
|
|
mycore_string_append(dest, target->data, target->length); |
221
|
0
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
|
223
|
74
|
|
|
|
|
|
size_t mycore_string_raw_copy(char* str1, const char* str2, size_t size) |
224
|
|
|
|
|
|
|
{ |
225
|
74
|
|
|
|
|
|
str1[size] = '\0'; |
226
|
|
|
|
|
|
|
|
227
|
870
|
100
|
|
|
|
|
while(size) { |
228
|
796
|
|
|
|
|
|
size--; |
229
|
796
|
|
|
|
|
|
str1[size] = str2[size]; |
230
|
|
|
|
|
|
|
} |
231
|
|
|
|
|
|
|
|
232
|
74
|
|
|
|
|
|
return size; |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
|
235
|
0
|
|
|
|
|
|
size_t mycore_string_raw_set_replacement_character(mycore_string_t* target, size_t position) |
236
|
|
|
|
|
|
|
{ |
237
|
|
|
|
|
|
|
// Unicode Character 'REPLACEMENT CHARACTER' (U+FFFD) |
238
|
0
|
|
|
|
|
|
target->data[(position)] = (char)0xEF; |
239
|
0
|
|
|
|
|
|
target->data[(position + 1)] = (char)0xBF; |
240
|
0
|
|
|
|
|
|
target->data[(position + 2)] = (char)0xBD; |
241
|
|
|
|
|
|
|
|
242
|
0
|
|
|
|
|
|
return 3; |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
|
245
|
0
|
|
|
|
|
|
void mycore_string_append_with_replacement_null_characters(mycore_string_t* str, const char* buff, size_t length) |
246
|
|
|
|
|
|
|
{ |
247
|
0
|
0
|
|
|
|
|
MyCORE_STRING_REALLOC_IF_NEED(str, (length + 1), 0); |
248
|
|
|
|
|
|
|
|
249
|
0
|
|
|
|
|
|
unsigned char *data = (unsigned char*)str->data; |
250
|
0
|
|
|
|
|
|
const unsigned char *u_buff = (const unsigned char*)buff; |
251
|
|
|
|
|
|
|
|
252
|
0
|
0
|
|
|
|
|
for (size_t i = 0; i < length; i++) |
253
|
|
|
|
|
|
|
{ |
254
|
0
|
0
|
|
|
|
|
if(u_buff[i] == 0x00) { |
255
|
0
|
|
|
|
|
|
mycore_string_realloc(str, (str->size + 5)); |
256
|
0
|
|
|
|
|
|
data = (unsigned char*)str->data; |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
// Unicode Character 'REPLACEMENT CHARACTER' (U+FFFD) |
259
|
0
|
|
|
|
|
|
data[str->length] = 0xEF; str->length++; |
260
|
0
|
|
|
|
|
|
data[str->length] = 0xBF; str->length++; |
261
|
0
|
|
|
|
|
|
data[str->length] = 0xBD; |
262
|
|
|
|
|
|
|
} |
263
|
|
|
|
|
|
|
else |
264
|
0
|
|
|
|
|
|
data[str->length] = u_buff[i]; |
265
|
|
|
|
|
|
|
|
266
|
0
|
|
|
|
|
|
str->length++; |
267
|
|
|
|
|
|
|
} |
268
|
|
|
|
|
|
|
|
269
|
0
|
|
|
|
|
|
str->data[str->length] = '\0'; |
270
|
0
|
|
|
|
|
|
} |
271
|
|
|
|
|
|
|
|
272
|
0
|
|
|
|
|
|
void mycore_string_stay_only_whitespace(mycore_string_t* target) |
273
|
|
|
|
|
|
|
{ |
274
|
0
|
|
|
|
|
|
char *data = target->data; |
275
|
0
|
|
|
|
|
|
size_t pos = 0; |
276
|
|
|
|
|
|
|
|
277
|
0
|
0
|
|
|
|
|
for(size_t i = 0; i < target->length; i++) |
278
|
|
|
|
|
|
|
{ |
279
|
0
|
0
|
|
|
|
|
if(mycore_utils_whithspace(data[i], ==, ||)) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
280
|
0
|
|
|
|
|
|
data[pos] = data[i]; |
281
|
0
|
|
|
|
|
|
pos++; |
282
|
|
|
|
|
|
|
} |
283
|
|
|
|
|
|
|
} |
284
|
|
|
|
|
|
|
|
285
|
0
|
|
|
|
|
|
target->length = pos; |
286
|
0
|
|
|
|
|
|
} |
287
|
|
|
|
|
|
|
|
288
|
45
|
|
|
|
|
|
size_t mycore_string_crop_whitespace_from_begin(mycore_string_t* target) |
289
|
|
|
|
|
|
|
{ |
290
|
45
|
|
|
|
|
|
char *data = target->data; |
291
|
|
|
|
|
|
|
size_t i; |
292
|
|
|
|
|
|
|
|
293
|
45
|
50
|
|
|
|
|
for(i = 0; i < target->length; i++) { |
294
|
45
|
50
|
|
|
|
|
if(mycore_utils_whithspace(data[i], !=, &&)) |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
295
|
45
|
|
|
|
|
|
break; |
296
|
|
|
|
|
|
|
} |
297
|
|
|
|
|
|
|
|
298
|
45
|
50
|
|
|
|
|
if(i) |
299
|
0
|
|
|
|
|
|
target->data = mchar_async_crop_first_chars_without_cache(target->data, i); |
300
|
|
|
|
|
|
|
|
301
|
45
|
|
|
|
|
|
target->length -= i; |
302
|
|
|
|
|
|
|
|
303
|
45
|
|
|
|
|
|
return i; |
304
|
|
|
|
|
|
|
} |
305
|
|
|
|
|
|
|
|
306
|
30
|
|
|
|
|
|
size_t mycore_string_whitespace_from_begin(mycore_string_t* target) |
307
|
|
|
|
|
|
|
{ |
308
|
30
|
|
|
|
|
|
char *data = target->data; |
309
|
|
|
|
|
|
|
size_t i; |
310
|
|
|
|
|
|
|
|
311
|
30
|
50
|
|
|
|
|
for(i = 0; i < target->length; i++) { |
312
|
30
|
50
|
|
|
|
|
if(mycore_utils_whithspace(data[i], !=, &&)) |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
313
|
30
|
|
|
|
|
|
break; |
314
|
|
|
|
|
|
|
} |
315
|
|
|
|
|
|
|
|
316
|
30
|
|
|
|
|
|
return i; |
317
|
|
|
|
|
|
|
} |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
|