GCC Code Coverage Report


Directory: src/gate/
File: src/gate/strings.c
Date: 2025-12-12 23:40:09
Exec Total Coverage
Lines: 1138 1859 61.2%
Functions: 167 250 66.8%
Branches: 614 1432 42.9%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28
29 #include "gate/strings.h"
30 #include "gate/memalloc.h"
31 #include "gate/mathematics.h"
32 #include "gate/results.h"
33 #include "gate/debugging.h"
34
35
36 #define U8_TERM ((gate_char8_t)(0x80))
37 #define U8_2B ((gate_char8_t)(0xC0))
38 #define U8_3B ((gate_char8_t)(0xE0))
39 #define U8_4B ((gate_char8_t)(0xF0))
40
41 #define U32_07 ((gate_char32_t)(0x07))
42 #define U32_0F ((gate_char32_t)(0x0F))
43 #define U32_3F ((gate_char32_t)(0x3F))
44
45 #define U16_1 ((gate_char16_t)(0xD800))
46 #define U16_2 ((gate_char16_t)(0xDC00))
47 #define U16_M ((gate_char16_t)(0x03FF))
48
49
50 1179 gate_size_t gate_char_read_utf8(gate_char8_t const* src, gate_size_t srclen, gate_char32_t* dst)
51 {
52
1/2
✓ Branch 0 taken 1179 times.
✗ Branch 1 not taken.
1179 if (srclen > 0)
53 {
54
1/2
✓ Branch 0 taken 1179 times.
✗ Branch 1 not taken.
1179 if ((*src & 0x80) == 0)
55 {
56 1179 *dst = (gate_char32_t)*src;
57 1179 return 1;
58 }
59 else if (((src[0] & 0xE0) == 0xC0) && (srclen > 1) && ((src[1] & 0xC0) == 0x80))
60 {
61 *dst = (gate_char32_t)((gate_char32_t)(src[0] & 0x1F) << 6) | (gate_char32_t)(src[1] & 0x3F);
62 return 2;
63 }
64 else if (((src[0] & 0xF0) == 0xE0) && (srclen > 2) && ((src[1] & 0xC0) == 0x80) && ((src[2] & 0xC0) == 0x80))
65 {
66 *dst = (gate_char32_t)((gate_char32_t)(src[0] & 0x0F) << 12) | (gate_char32_t)((gate_char32_t)(src[1] & 0x3F) << 6) | (gate_char32_t)(src[2] & 0x3F);
67 return 3;
68 }
69 else if (((src[0] & 0xF8) == 0xF0) && (srclen > 3) && ((src[1] & 0xC0) == 0x80) && ((src[2] & 0xC0) == 0x80) && ((src[3] & 0xC0) == 0x80))
70 {
71 *dst = (gate_char32_t)((gate_char32_t)(src[0] & 0x07) << 18) | (gate_char32_t)((gate_char32_t)(src[1] & 0x3F) << 12) | (gate_char32_t)((gate_char32_t)(src[2] & 0x3F) << 6) | (gate_char32_t)(src[3] & 0x3F);
72 return 4;
73 }
74 }
75 return 0;
76 }
77 54 gate_size_t gate_char_read_utf16(gate_char16_t const* src, gate_size_t srclen, gate_char32_t* dst)
78 {
79
1/2
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
54 if (srclen > 0)
80 {
81
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 54 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
54 if (((src[0] & U16_1) == U16_1) && (srclen > 1) && ((src[1] & U16_2) == U16_2))
82 {
83 *dst = ((gate_char32_t)(src[0] & U16_M) << 10) | (gate_char32_t)(src[1] & U16_M);
84 *dst += 0x10000;
85 return 2;
86 }
87 else
88 {
89 54 *dst = (gate_char32_t)(src[0]);
90 54 return 1;
91 }
92 }
93 return 0;
94 }
95 38 gate_size_t gate_char_read_utf32(gate_char32_t const* src, gate_size_t srclen, gate_char32_t* dst)
96 {
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (srclen < 1)
98 {
99 return 0;
100 }
101 38 *dst = *src;
102 38 return 1;
103 }
104 151 gate_size_t gate_char_write_utf8(gate_char32_t src, gate_char8_t* dst, gate_size_t dstlen)
105 {
106
1/2
✓ Branch 0 taken 151 times.
✗ Branch 1 not taken.
151 if (src < 0x80)
107 {
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 151 times.
151 if (dstlen == 0)
109 {
110 return 0;
111 }
112 151 dst[0] = (gate_char8_t)src;
113 151 return 1;
114 }
115 else if (src < 0x800)
116 {
117 if (dstlen < 2)
118 {
119 return 0;
120 }
121 dst[0] = U8_2B | (gate_char8_t)(src >> 6);
122 dst[1] = U8_TERM | (gate_char8_t)(src & U32_3F);
123 return 2;
124 }
125 else if (src < 0x010000)
126 {
127 if (dstlen < 3)
128 {
129 return 0;
130 }
131 dst[0] = U8_3B | (gate_char8_t)((src >> 12) & U32_0F);
132 dst[1] = U8_TERM | (gate_char8_t)((src >> 6) & U32_3F);
133 dst[2] = U8_TERM | (gate_char8_t)(src & U32_3F);
134 return 3;
135 }
136 else if (src < 0x0110000)
137 {
138 if (dstlen < 4)
139 {
140 return 0;
141 }
142 dst[0] = U8_4B | (gate_char8_t)(((src >> 18) & U32_07));
143 dst[1] = U8_TERM | (gate_char8_t)(((src >> 12) & U32_3F));
144 dst[2] = U8_TERM | (gate_char8_t)(((src >> 6) & U32_3F));
145 dst[3] = U8_TERM | (gate_char8_t)((src & U32_3F));
146 return 4;
147 }
148 return 0;
149 }
150 1093 gate_size_t gate_char_write_utf16(gate_char32_t src, gate_char16_t* dst, gate_size_t dstlen)
151 {
152
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1093 times.
1093 if (src > 0x10000)
153 {
154 if (dstlen < 2)
155 {
156 return 0;
157 }
158 src -= 0x10000;
159 dst[0] = (gate_char16_t)(src >> 10) | U16_1;
160 dst[1] = (gate_char16_t)(src & (gate_char32_t)(U16_M)) | U16_2;
161 return 2;
162 }
163 else
164 {
165 1093 dst[0] = (gate_char16_t)src;
166 1093 return 1;
167 }
168
169 }
170 1 gate_size_t gate_char_write_utf32(gate_char32_t src, gate_char32_t* dst, gate_size_t dstlen)
171 {
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (dstlen < 1)
173 {
174 return 0;
175 }
176 1 *dst = src;
177 1 return 1;
178 }
179
180
181
182
183 #define UTF_CONVERT(reader, writer, src, srclen, dst, dstlen) \
184 gate_size_t ret = 0; \
185 gate_size_t decoded; \
186 gate_size_t encoded; \
187 gate_char32_t u32; \
188 while((srclen != 0) && (dstlen != 0)) \
189 { \
190 decoded = reader(src, srclen, &u32); \
191 if(decoded == 0) \
192 { \
193 break; \
194 } \
195 src += decoded; \
196 srclen -= decoded; \
197 encoded = writer(u32, dst, dstlen); \
198 if(encoded == 0) \
199 { \
200 break; \
201 } \
202 dst += encoded; \
203 dstlen -= encoded; \
204 ret += encoded; \
205 } \
206 if(dstlen != 0) \
207 { \
208 *dst = 0; \
209 } \
210 return ret;
211
212 2 gate_size_t gate_str_utf8_2_utf16(gate_char8_t const* src, gate_size_t srclen, gate_char16_t* dst, gate_size_t dstlen)
213 {
214
7/10
✗ Branch 1 not taken.
✓ Branch 2 taken 1040 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1040 times.
✓ Branch 6 taken 1040 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 1040 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 times.
✓ Branch 11 taken 1 times.
1042 UTF_CONVERT(gate_char_read_utf8, gate_char_write_utf16, src, srclen, dst, dstlen);
215 }
216 gate_size_t gate_str_utf8_2_utf32(gate_char8_t const* src, gate_size_t srclen, gate_char32_t* dst, gate_size_t dstlen)
217 {
218 UTF_CONVERT(gate_char_read_utf8, gate_char_write_utf32, src, srclen, dst, dstlen);
219 }
220 gate_size_t gate_str_utf16_2_utf8(gate_char16_t const* src, gate_size_t srclen, gate_char8_t* dst, gate_size_t dstlen)
221 {
222 UTF_CONVERT(gate_char_read_utf16, gate_char_write_utf8, src, srclen, dst, dstlen);
223 }
224 gate_size_t gate_str_utf16_2_utf32(gate_char16_t const* src, gate_size_t srclen, gate_char32_t* dst, gate_size_t dstlen)
225 {
226 UTF_CONVERT(gate_char_read_utf16, gate_char_write_utf32, src, srclen, dst, dstlen);
227 }
228 9 gate_size_t gate_str_utf32_2_utf8(gate_char32_t const* src, gate_size_t srclen, gate_char8_t* dst, gate_size_t dstlen)
229 {
230
6/10
✗ Branch 1 not taken.
✓ Branch 2 taken 36 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 36 times.
✓ Branch 6 taken 36 times.
✓ Branch 7 taken 9 times.
✓ Branch 8 taken 36 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 9 times.
✗ Branch 11 not taken.
45 UTF_CONVERT(gate_char_read_utf32, gate_char_write_utf8, src, srclen, dst, dstlen);
231 }
232 gate_size_t gate_str_utf32_2_utf16(gate_char32_t const* src, gate_size_t srclen, gate_char16_t* dst, gate_size_t dstlen)
233 {
234 UTF_CONVERT(gate_char_read_utf32, gate_char_write_utf16, src, srclen, dst, dstlen);
235 }
236
237
238 7727 int gate_char_lowercase(int chr)
239 {
240
4/4
✓ Branch 0 taken 6788 times.
✓ Branch 1 taken 939 times.
✓ Branch 2 taken 133 times.
✓ Branch 3 taken 6655 times.
7727 if ((chr >= 'A') && (chr <= 'Z'))
241 {
242 133 return chr + ('a' - 'A');
243 }
244 7594 return chr;
245 }
246 7 int gate_char_uppercase(int chr)
247 {
248
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 if ((chr >= 'a') && (chr <= 'z'))
249 {
250 7 return chr - ('a' - 'A');
251 }
252 return chr;
253 }
254 28 gate_bool_t gate_char_is_digit(int chr)
255 {
256
4/4
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 22 times.
✓ Branch 3 taken 3 times.
28 if ((chr >= '0') && (chr <= '9'))
257 {
258 22 return true;
259 }
260 else
261 {
262 6 return false;
263 }
264 }
265 133 gate_bool_t gate_char_is_whitespace(int chr)
266 {
267 static char const ws[] = GATE_STR_WHITESPACES;
268 gate_size_t ndx;
269
2/2
✓ Branch 0 taken 457 times.
✓ Branch 1 taken 108 times.
565 for (ndx = 0; ndx != GATE_STR_WHITESPACES_LENGTH; ++ndx)
270 {
271
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 432 times.
457 if (chr == ws[ndx])
272 {
273 25 return true;
274 }
275 }
276 108 return false;
277 }
278
279 #define GATE_STR_CHAR_POS(str, len, find, startat) \
280 gate_size_t ret; \
281 if((str == NULL) || (len == 0) || (startat >= len)) \
282 { \
283 return GATE_STR_NPOS; \
284 } \
285 ret = startat; \
286 str += startat; \
287 len -= startat; \
288 while(len-- > 0) \
289 { \
290 if(*str == find) \
291 { \
292 return ret; \
293 } \
294 ++str; \
295 ++ret; \
296 } \
297 return GATE_STR_NPOS;
298
299 3096 gate_size_t gate_str_char_pos(gate_char8_t const* str, gate_size_t len, gate_char8_t find, gate_size_t startat)
300 {
301
10/10
✓ Branch 0 taken 3094 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 3090 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 3088 times.
✓ Branch 6 taken 2277 times.
✓ Branch 7 taken 83446 times.
✓ Branch 8 taken 85723 times.
✓ Branch 9 taken 811 times.
86542 GATE_STR_CHAR_POS(str, len, find, startat);
302 }
303 gate_size_t gate_str16_char_pos(gate_char16_t const* str, gate_size_t len, gate_char16_t find, gate_size_t startat)
304 {
305 GATE_STR_CHAR_POS(str, len, find, startat);
306 }
307 gate_size_t gate_str32_char_pos(gate_char32_t const* str, gate_size_t len, gate_char32_t find, gate_size_t startat)
308 {
309 GATE_STR_CHAR_POS(str, len, find, startat);
310 }
311
312 #define GATE_STR_CHAR_POS_LAST(str, len, find) \
313 gate_size_t ret; \
314 if((str == NULL) || (len == 0)) \
315 { \
316 return GATE_STR_NPOS; \
317 } \
318 ret = len; \
319 str += len; \
320 while(len-- != 0) \
321 { \
322 --ret; \
323 --str; \
324 if(*str == find) \
325 { \
326 return ret; \
327 } \
328 } \
329 return GATE_STR_NPOS;
330
331 315 gate_size_t gate_str_char_pos_last(gate_char8_t const* str, gate_size_t len, gate_char8_t find)
332 {
333
6/8
✓ Branch 0 taken 315 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 315 times.
✓ Branch 4 taken 177 times.
✓ Branch 5 taken 2171 times.
✓ Branch 6 taken 2348 times.
✓ Branch 7 taken 138 times.
2486 GATE_STR_CHAR_POS_LAST(str, len, find);
334 }
335 gate_size_t gate_str16_char_pos_last(gate_char16_t const* str, gate_size_t len, gate_char16_t find)
336 {
337 GATE_STR_CHAR_POS_LAST(str, len, find);
338 }
339 gate_size_t gate_str32_char_pos_last(gate_char32_t const* str, gate_size_t len, gate_char32_t find)
340 {
341 GATE_STR_CHAR_POS_LAST(str, len, find);
342 }
343
344
345
346
347
348 #define GATE_STR_POS(charfinder, rangecomparer, str, len, find, findlen, startat) \
349 gate_size_t ret = startat; \
350 if((str == NULL) || (len == 0) || (find == NULL) || (findlen == 0) || (findlen > len) || (startat > len - findlen)) \
351 { \
352 return GATE_STR_NPOS; \
353 } \
354 str += startat; \
355 len -= startat; \
356 for(;;) \
357 { \
358 gate_size_t pos = charfinder(str, len, *find, 0); \
359 if(pos == GATE_STR_NPOS) \
360 { \
361 return pos; \
362 } \
363 ret += pos; \
364 str += pos; \
365 len -= pos; \
366 if(rangecomparer(str, find, findlen) == 0) \
367 { \
368 break; \
369 } \
370 ++str; \
371 --len; \
372 ++ret; \
373 } \
374 return ret;
375
376
377 593 gate_size_t gate_str_pos(gate_char8_t const* str, gate_size_t len, gate_char8_t const* find, gate_size_t findlen, gate_size_t startat)
378 {
379
13/16
✓ Branch 0 taken 593 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 561 times.
✓ Branch 3 taken 32 times.
✓ Branch 4 taken 561 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 561 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 292 times.
✓ Branch 9 taken 269 times.
✓ Branch 10 taken 2 times.
✓ Branch 11 taken 290 times.
✓ Branch 13 taken 224 times.
✓ Branch 14 taken 268 times.
✓ Branch 16 taken 66 times.
✓ Branch 17 taken 202 times.
795 GATE_STR_POS(gate_str_char_pos, gate_str_comp_range, str, len, find, findlen, startat);
380 }
381 gate_size_t gate_str16_pos(gate_char16_t const* str, gate_size_t len, gate_char16_t const* find, gate_size_t findlen, gate_size_t startat)
382 {
383 GATE_STR_POS(gate_str16_char_pos, gate_str16_comp_range, str, len, find, findlen, startat);
384 }
385 gate_size_t gate_str32_pos(gate_char32_t const* str, gate_size_t len, gate_char32_t const* find, gate_size_t findlen, gate_size_t startat)
386 {
387 GATE_STR_POS(gate_str32_char_pos, gate_str32_comp_range, str, len, find, findlen, startat);
388 }
389
390
391 #define GATE_STR_POS_LAST(rangecomparer, str, len, find, findlen) \
392 if((str == NULL) || (len == 0) || (find == NULL) || (findlen == 0) || (findlen > len)) \
393 { \
394 return GATE_STR_NPOS; \
395 } \
396 str += (len - findlen); \
397 len -= (findlen - 1); \
398 while(len-- > 0) \
399 { \
400 if(rangecomparer(str, find, findlen) == 0) \
401 { \
402 return len; \
403 } \
404 --str; \
405 } \
406 return GATE_STR_NPOS;
407
408
409 2 gate_size_t gate_str_pos_last(gate_char8_t const* str, gate_size_t len, gate_char8_t const* find, gate_size_t findlen)
410 {
411
9/14
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 2 times.
✓ Branch 11 taken 1 times.
✓ Branch 12 taken 9 times.
✓ Branch 13 taken 10 times.
✓ Branch 14 taken 1 times.
11 GATE_STR_POS_LAST(gate_str_comp_range, str, len, find, findlen);
412 }
413 gate_size_t gate_str16_pos_last(gate_char16_t const* str, gate_size_t len, gate_char16_t const* find, gate_size_t findlen)
414 {
415 GATE_STR_POS_LAST(gate_str16_comp_range, str, len, find, findlen);
416 }
417 gate_size_t gate_str32_pos_last(gate_char32_t const* str, gate_size_t len, gate_char32_t const* find, gate_size_t findlen)
418 {
419 GATE_STR_POS_LAST(gate_str32_comp_range, str, len, find, findlen);
420 }
421
422
423
424
425 #define GATE_STR_LEN(src) \
426 gate_size_t ret = 0; \
427 if(src != NULL) \
428 { \
429 while(*src != 0) \
430 { \
431 ++src; \
432 ++ret; \
433 } \
434 } \
435 return ret;
436
437 807197 gate_size_t gate_str_length(gate_char8_t const* src)
438 {
439
4/4
✓ Branch 0 taken 807108 times.
✓ Branch 1 taken 89 times.
✓ Branch 2 taken 32845749 times.
✓ Branch 3 taken 807108 times.
33652946 GATE_STR_LEN(src);
440 }
441 gate_size_t gate_str16_length(gate_char16_t const* src)
442 {
443 GATE_STR_LEN(src);
444 }
445 18 gate_size_t gate_str32_length(gate_char32_t const* src)
446 {
447
3/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
✓ Branch 3 taken 18 times.
74 GATE_STR_LEN(src);
448 }
449
450 #define GATE_STR_LEN_MAX(src, capacity) \
451 gate_size_t ret = 0; \
452 if(src != NULL) \
453 { \
454 while((*src != 0) && (capacity-- != 0)) \
455 { \
456 ++src; \
457 ++ret; \
458 } \
459 } \
460 return ret;
461
462 44 gate_size_t gate_str_length_max(gate_char8_t const* src, gate_size_t capacity)
463 {
464
4/6
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 392 times.
✓ Branch 3 taken 44 times.
✓ Branch 4 taken 392 times.
✗ Branch 5 not taken.
436 GATE_STR_LEN_MAX(src, capacity);
465 }
466
467 gate_size_t gate_str16_length_max(gate_char16_t const* src, gate_size_t capacity)
468 {
469 GATE_STR_LEN_MAX(src, capacity);
470 }
471
472 gate_size_t gate_str32_length_max(gate_char32_t const* src, gate_size_t capacity)
473 {
474 GATE_STR_LEN_MAX(src, capacity);
475 }
476
477 #define GATE_STR_IS_EMPTY(src) \
478 if(src == NULL) return true; \
479 return *src == 0
480
481
482 3140 gate_bool_t gate_str_is_empty(gate_char8_t const* src)
483 {
484
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 3123 times.
3140 GATE_STR_IS_EMPTY(src);
485 }
486 gate_bool_t gate_str16_is_empty(gate_char16_t const* src)
487 {
488 GATE_STR_IS_EMPTY(src);
489 }
490 gate_bool_t gate_str32_is_empty(gate_char32_t const* src)
491 {
492 GATE_STR_IS_EMPTY(src);
493 }
494
495
496 #define GATE_STR_COMP_RANGE(str1, str2, len) \
497 if(str1 != str2) \
498 { \
499 while(len-- > 0) \
500 { \
501 if(*str1 < *str2) return -1; \
502 if(*str1 > *str2) return +1; \
503 ++str1; \
504 ++str2; \
505 } \
506 } \
507 return 0;
508
509
510 23247 int gate_str_comp_range(gate_char8_t const* str1, gate_char8_t const* str2, gate_size_t len)
511 {
512
8/8
✓ Branch 0 taken 23024 times.
✓ Branch 1 taken 223 times.
✓ Branch 2 taken 12111 times.
✓ Branch 3 taken 51288 times.
✓ Branch 4 taken 7144 times.
✓ Branch 5 taken 44144 times.
✓ Branch 6 taken 63399 times.
✓ Branch 7 taken 3769 times.
67391 GATE_STR_COMP_RANGE(str1, str2, len);
513 }
514 1 int gate_str16_comp_range(gate_char16_t const* str1, gate_char16_t const* str2, gate_size_t len)
515 {
516
5/8
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 6 times.
✓ Branch 7 taken 1 times.
7 GATE_STR_COMP_RANGE(str1, str2, len);
517 }
518 3 int gate_str32_comp_range(gate_char32_t const* str1, gate_char32_t const* str2, gate_size_t len)
519 {
520
6/8
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 2 times.
5 GATE_STR_COMP_RANGE(str1, str2, len);
521 }
522
523
524
525
526 #define GATE_STR_COMP_RANGE_IC(str1, str2, len) \
527 if(str1 != str2) \
528 { \
529 while(len-- > 0) \
530 { \
531 gate_char8_t c1 = gate_char_lowercase(*str1); \
532 gate_char8_t c2 = gate_char_lowercase(*str2); \
533 if(c1 < c2) return -1; \
534 if(c1 > c2) return +1; \
535 ++str1; \
536 ++str2; \
537 } \
538 } \
539 return 0;
540
541 2787 int gate_str_comp_range_ic(gate_char8_t const* str1, gate_char8_t const* str2, gate_size_t len)
542 {
543
8/8
✓ Branch 0 taken 2786 times.
✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1987 times.
✓ Branch 5 taken 1873 times.
✓ Branch 6 taken 631 times.
✓ Branch 7 taken 1242 times.
✓ Branch 8 taken 3860 times.
✓ Branch 9 taken 168 times.
4029 GATE_STR_COMP_RANGE_IC(str1, str2, len);
544 }
545 int gate_str16_comp_range_ic(gate_char16_t const* str1, gate_char16_t const* str2, gate_size_t len)
546 {
547 GATE_STR_COMP_RANGE_IC(str1, str2, len);
548 }
549 int gate_str32_comp_range_ic(gate_char32_t const* str1, gate_char32_t const* str2, gate_size_t len)
550 {
551 GATE_STR_COMP_RANGE_IC(str1, str2, len);
552 }
553
554 #define GATE_STR_TO_LOWER(str, len) \
555 while(len-- > 0) \
556 { \
557 *str = gate_char_lowercase(*str); \
558 ++str; \
559 }
560
561 1 void gate_str_to_lower(gate_char8_t* str, gate_size_t len)
562 {
563
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 1 times.
7 GATE_STR_TO_LOWER(str, len);
564 1 }
565 void gate_str16_to_lower(gate_char16_t* str, gate_size_t len)
566 {
567 GATE_STR_TO_LOWER(str, len);
568 }
569 void gate_str32_to_lower(gate_char32_t* str, gate_size_t len)
570 {
571 GATE_STR_TO_LOWER(str, len);
572 }
573
574
575
576
577 #define GATE_STR_TO_UPPER(str, len) \
578 while(len-- > 0) \
579 { \
580 *str = gate_char_uppercase(*str); \
581 ++str; \
582 }
583
584 1 void gate_str_to_upper(gate_char8_t* str, gate_size_t len)
585 {
586
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 1 times.
7 GATE_STR_TO_UPPER(str, len);
587 1 }
588 void gate_str16_to_upper(gate_char16_t* str, gate_size_t len)
589 {
590 GATE_STR_TO_UPPER(str, len);
591 }
592 void gate_str32_to_upper(gate_char32_t* str, gate_size_t len)
593 {
594 GATE_STR_TO_UPPER(str, len);
595 }
596
597
598
599 #define GATE_STR_REVERSE(type, src, len) \
600 if(len > 1) \
601 {\
602 type* dst = src + len - 1; \
603 len /= 2; \
604 while(len != 0) \
605 { \
606 type chr = *src; \
607 *src = *dst; \
608 *dst = chr; \
609 ++src; \
610 --dst; \
611 --len; \
612 } \
613 }
614
615
616 133494 void gate_str_reverse(gate_char8_t* src, gate_size_t len)
617 {
618
3/4
✓ Branch 0 taken 133494 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 133932 times.
✓ Branch 3 taken 133494 times.
267426 GATE_STR_REVERSE(gate_char8_t, src, len);
619 133494 }
620 void gate_str16_reverse(gate_char16_t* src, gate_size_t len)
621 {
622 GATE_STR_REVERSE(gate_char16_t, src, len);
623 }
624 void gate_str32_reverse(gate_char32_t* src, gate_size_t len)
625 {
626 GATE_STR_REVERSE(gate_char32_t, src, len);
627 }
628
629 #define GATE_STR_IS_NUMERIC(str, len) \
630 gate_size_t ret = 0; \
631 for(; len != 0; --len, ++str) \
632 { \
633 if(gate_char_is_digit((int)*str)) \
634 { \
635 ++ret; \
636 } \
637 else \
638 { \
639 break; \
640 } \
641 } \
642 return ret;
643
644 gate_size_t gate_str_is_numeric(gate_char8_t const* str, gate_size_t len)
645 {
646 GATE_STR_IS_NUMERIC(str, len)
647 }
648 gate_size_t gate_str16_is_numeric(gate_char16_t const* str, gate_size_t len)
649 {
650 GATE_STR_IS_NUMERIC(str, len)
651 }
652 gate_size_t gate_str32_is_numeric(gate_char32_t const* str, gate_size_t len)
653 {
654 GATE_STR_IS_NUMERIC(str, len)
655 }
656
657
658
659 180 gate_size_t gate_str_count_chars(gate_char8_t const* str, gate_size_t len, gate_char8_t chr)
660 {
661 180 gate_size_t count = 0;
662
2/2
✓ Branch 0 taken 1740 times.
✓ Branch 1 taken 180 times.
1920 while (len-- != 0)
663 {
664
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1738 times.
1740 if (*str == chr)
665 {
666 2 ++count;
667 }
668 1740 ++str;
669 }
670 180 return count;
671 }
672 140 int gate_str_comp(gate_char8_t const* str1, gate_char8_t const* str2)
673 {
674 140 return gate_str_compare(str1, gate_str_length(str1), str2, gate_str_length(str2));
675 }
676 19648 int gate_str_compare(gate_char8_t const* str1, gate_size_t len1, gate_char8_t const* str2, gate_size_t len2)
677 {
678 19648 gate_size_t len = len1 < len2 ? len1 : len2;
679 19648 int ret = gate_str_comp_range(str1, str2, len);
680
2/2
✓ Branch 0 taken 2715 times.
✓ Branch 1 taken 16933 times.
19648 if (ret == 0)
681 {
682
2/2
✓ Branch 0 taken 127 times.
✓ Branch 1 taken 2588 times.
2715 if (len1 < len2) ret = -1;
683
2/2
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 2656 times.
2715 if (len1 > len2) ret = 1;
684 }
685 19648 return ret;
686 }
687 14 int gate_str_comp_ic(gate_char8_t const* str1, gate_char8_t const* str2)
688 {
689 14 return gate_str_compare_ic(str1, gate_str_length(str1), str2, gate_str_length(str2));
690 }
691 2534 int gate_str_compare_ic(gate_char8_t const* str1, gate_size_t len1, gate_char8_t const* str2, gate_size_t len2)
692 {
693 2534 gate_size_t len = len1 < len2 ? len1 : len2;
694 2534 int ret = gate_str_comp_range_ic(str1, str2, len);
695
2/2
✓ Branch 0 taken 149 times.
✓ Branch 1 taken 2385 times.
2534 if (ret == 0)
696 {
697
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 142 times.
149 if (len1 < len2) ret = -1;
698
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 131 times.
149 if (len1 > len2) ret = 1;
699 }
700 2534 return ret;
701 }
702
703 23 gate_bool_t gate_str_wildcard_match(gate_char8_t const* str, gate_size_t len, gate_char8_t const* like, gate_size_t likelen)
704 {
705
4/4
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 1 times.
33 while ((len != 0) && (likelen != 0))
706 {
707
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 26 times.
30 if (*like == '*')
708 {
709 4 ++like;
710 4 --likelen;
711
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (likelen == 0)
712 {
713 /* like ends with '*' -> this is always a match */
714 return true;
715 }
716
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 2 times.
19 while (len != 0)
717 {
718 /* start a new match attempt at each position in str */
719
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 15 times.
17 if (gate_str_wildcard_match(str, len, like, likelen))
720 {
721 2 return true;
722 }
723 15 ++str;
724 15 --len;
725 }
726 2 return false;
727 }
728
729
3/4
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 16 times.
26 if ((*like == '?') || (*str == *like))
730 {
731 10 ++str;
732 10 --len;
733 10 ++like;
734 10 --likelen;
735 }
736 else
737 {
738 16 return false;
739 }
740 }
741 3 return (len == likelen);
742 }
743
744 6 gate_bool_t gate_str_like(gate_char8_t const* str, gate_size_t len, gate_char8_t const* like, gate_size_t likelen)
745 {
746
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6 if ((len == 0) && (likelen == 1))
747 {
748 /* empty is like anything -> OK */
749 return *like == '*';
750 }
751
752 6 return gate_str_wildcard_match(str, len, like, likelen);
753 }
754
755 1 gate_bool_t gate_str_like_one_of(gate_char8_t const* str, gate_size_t len, gate_char8_t const* like, gate_size_t likelen, gate_char8_t like_separator)
756 {
757 1 gate_size_t startat = 0;
758 gate_size_t pos;
759 gate_size_t token_len;
760
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 while (startat < likelen)
761 {
762 3 pos = gate_str_char_pos(like, likelen, like_separator, startat);
763
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (pos == GATE_STR_NPOS)
764 {
765 1 pos = likelen;
766 }
767 //a;b;c
768 3 token_len = (pos - startat);
769
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (token_len != 0)
770 {
771
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
3 if (gate_str_like(str, len, like + startat, token_len))
772 {
773 1 return true;
774 }
775 }
776 2 startat = pos + 1;
777 }
778 return false;
779 }
780
781
782 504 gate_size_t gate_str_find_first_of(gate_char8_t const* str, gate_size_t len, gate_char8_t const* chars, gate_size_t charcount, gate_size_t startat)
783 {
784 gate_size_t ret;
785 gate_size_t ndx;
786
5/10
✓ Branch 0 taken 504 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 504 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 504 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 504 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 504 times.
504 if ((str == NULL) || (len == 0) || (startat >= len) || (chars == NULL) || (charcount == 0))
787 {
788 return GATE_STR_NPOS;
789 }
790 504 ret = startat;
791 504 str += startat;
792 504 len -= startat;
793
2/2
✓ Branch 0 taken 3523 times.
✓ Branch 1 taken 393 times.
3916 while (len-- > 0)
794 {
795
2/2
✓ Branch 0 taken 13701 times.
✓ Branch 1 taken 3412 times.
17113 for (ndx = 0; ndx != charcount; ++ndx)
796 {
797
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 13590 times.
13701 if (*str == chars[ndx])
798 {
799 /* first character found, that is included in 'chars' */
800 111 return ret;
801 }
802 }
803 3412 ++str;
804 3412 ++ret;
805 }
806 393 return GATE_STR_NPOS;
807 }
808
809 5333 gate_size_t gate_str_find_first_not_of(gate_char8_t const* str, gate_size_t len, gate_char8_t const* chars, gate_size_t charcount, gate_size_t startat)
810 {
811 gate_size_t ret;
812 gate_size_t ndx;
813
7/10
✓ Branch 0 taken 5258 times.
✓ Branch 1 taken 75 times.
✓ Branch 2 taken 5256 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 5256 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5256 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 5256 times.
5333 if ((str == NULL) || (len == 0) || (startat >= len) || (chars == NULL) || (charcount == 0))
814 {
815 77 return GATE_STR_NPOS;
816 }
817 5256 ret = startat;
818 5256 str += startat;
819 5256 len -= startat;
820
2/2
✓ Branch 0 taken 12684 times.
✓ Branch 1 taken 1 times.
12685 while (len-- > 0)
821 {
822
2/2
✓ Branch 0 taken 29623 times.
✓ Branch 1 taken 5255 times.
34878 for (ndx = 0; ndx != charcount; ++ndx)
823 {
824
2/2
✓ Branch 0 taken 7429 times.
✓ Branch 1 taken 22194 times.
29623 if (*str == chars[ndx])
825 {
826 7429 break;
827 }
828 }
829
2/2
✓ Branch 0 taken 5255 times.
✓ Branch 1 taken 7429 times.
12684 if (ndx == charcount)
830 {
831 /* first character found, that is not included in 'chars' */
832 5255 return ret;
833 }
834 7429 ++str;
835 7429 ++ret;
836 }
837 1 return GATE_STR_NPOS;
838 }
839 23 gate_size_t gate_str_find_last_of(gate_char8_t const* str, gate_size_t len, gate_char8_t const* chars, gate_size_t charcount)
840 {
841 gate_size_t ret;
842 gate_size_t ndx;
843
4/8
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 23 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 23 times.
23 if ((str == NULL) || (len == 0) || (chars == NULL) || (charcount == 0))
844 {
845 return GATE_STR_NPOS;
846 }
847 23 ret = len;
848 23 str += len;
849
2/2
✓ Branch 0 taken 402 times.
✓ Branch 1 taken 1 times.
403 while (len-- > 0)
850 {
851 402 --str;
852 402 --ret;
853
2/2
✓ Branch 0 taken 406 times.
✓ Branch 1 taken 380 times.
786 for (ndx = 0; ndx != charcount; ++ndx)
854 {
855
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 384 times.
406 if (*str == chars[ndx])
856 {
857 /* last character found, that is included in 'chars' */
858 22 return ret;
859 }
860 }
861 }
862 1 return GATE_STR_NPOS;
863 }
864 3448 gate_size_t gate_str_find_last_not_of(gate_char8_t const* str, gate_size_t len, gate_char8_t const* chars, gate_size_t charcount)
865 {
866 gate_size_t ret;
867 gate_size_t ndx;
868
5/8
✓ Branch 0 taken 3448 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3444 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 3444 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 3444 times.
3448 if ((str == NULL) || (len == 0) || (chars == NULL) || (charcount == 0))
869 {
870 4 return GATE_STR_NPOS;
871 }
872 3444 ret = len;
873 3444 str += len;
874
2/2
✓ Branch 0 taken 5244 times.
✓ Branch 1 taken 160 times.
5404 while (len-- > 0)
875 {
876 5244 --str;
877 5244 --ret;
878
2/2
✓ Branch 0 taken 20614 times.
✓ Branch 1 taken 3284 times.
23898 for (ndx = 0; ndx != charcount; ++ndx)
879 {
880
2/2
✓ Branch 0 taken 1960 times.
✓ Branch 1 taken 18654 times.
20614 if (*str == chars[ndx])
881 {
882 1960 break;
883 }
884 }
885
2/2
✓ Branch 0 taken 3284 times.
✓ Branch 1 taken 1960 times.
5244 if (ndx == charcount)
886 {
887 /* last character found, that is not included in 'chars' */
888 3284 return ret;
889 }
890 }
891 160 return GATE_STR_NPOS;
892
893 }
894 1 gate_uint32_t gate_str_hash(gate_char8_t const* str, gate_size_t len)
895 {
896 1 gate_uint32_t hash = 5381;
897
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 while (len-- != 0)
898 {
899 4 hash += (hash << 5) + (gate_uint32_t)(gate_uint8_t) * (str++);
900 }
901 1 return hash;
902 }
903
904 gate_size_t gate_str_replace(gate_char8_t* str, gate_size_t len, gate_char8_t find, gate_char8_t replace)
905 {
906 gate_size_t counter = 0;
907 for (; len != 0; --len, ++str)
908 {
909 if (*str == find)
910 {
911 *str = replace;
912 ++counter;
913 }
914 }
915 return counter;
916 }
917
918
919
920
921 3294 gate_bool_t gate_str_starts_with(gate_char8_t const* str, gate_size_t len, gate_char8_t const* starttoken, gate_size_t startlen)
922 {
923
4/4
✓ Branch 0 taken 3287 times.
✓ Branch 1 taken 7 times.
✓ Branch 3 taken 1176 times.
✓ Branch 4 taken 2111 times.
3294 return (startlen > len) ? false : (0 == gate_str_comp_range(str, starttoken, startlen));
924 }
925 34 gate_bool_t gate_str_ends_with(gate_char8_t const* str, gate_size_t len, gate_char8_t const* endtoken, gate_size_t endlen)
926 {
927
2/4
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 34 times.
✗ Branch 4 not taken.
34 return (endlen > len) ? false : (0 == gate_str_comp_range(str + len - endlen, endtoken, endlen));
928 }
929
930 317 gate_bool_t gate_str_starts_with_ic(gate_char8_t const* str, gate_size_t len, gate_char8_t const* starttoken, gate_size_t startlen)
931 {
932
4/4
✓ Branch 0 taken 253 times.
✓ Branch 1 taken 64 times.
✓ Branch 3 taken 20 times.
✓ Branch 4 taken 233 times.
317 return (startlen > len) ? false : (0 == gate_str_comp_range_ic(str, starttoken, startlen));
933 }
934 gate_bool_t gate_str_ends_with_ic(gate_char8_t const* str, gate_size_t len, gate_char8_t const* endtoken, gate_size_t endlen)
935 {
936 return (endlen > len) ? false : (0 == gate_str_comp_range_ic(str + len - endlen, endtoken, endlen));
937 }
938
939
940 gate_bool_t gate_str16_starts_with(gate_char16_t const* str, gate_size_t len, gate_char16_t const* starttoken, gate_size_t startlen)
941 {
942 return (startlen > len) ? false : (0 == gate_str16_comp_range(str, starttoken, startlen));
943 }
944 gate_bool_t gate_str16_ends_with(gate_char16_t const* str, gate_size_t len, gate_char16_t const* endtoken, gate_size_t endlen)
945 {
946 return (endlen > len) ? false : (0 == gate_str16_comp_range(str + len - endlen, endtoken, endlen));
947 }
948 gate_bool_t gate_str32_starts_with(gate_char32_t const* str, gate_size_t len, gate_char32_t const* starttoken, gate_size_t startlen)
949 {
950 return (startlen > len) ? false : (0 == gate_str32_comp_range(str, starttoken, startlen));
951 }
952 gate_bool_t gate_str32_ends_with(gate_char32_t const* str, gate_size_t len, gate_char32_t const* endtoken, gate_size_t endlen)
953 {
954 return (endlen > len) ? false : (0 == gate_str32_comp_range(str + len - endlen, endtoken, endlen));
955 }
956
957
958
959 int gate_str16_comp(gate_char16_t const* str1, gate_char16_t const* str2)
960 {
961 return gate_str16_compare(str1, gate_str16_length(str1), str2, gate_str16_length(str2));
962 }
963 1 int gate_str16_compare(gate_char16_t const* str1, gate_size_t len1, gate_char16_t const* str2, gate_size_t len2)
964 {
965 1 gate_size_t len = len1 < len2 ? len1 : len2;
966 1 int ret = gate_str16_comp_range(str1, str2, len);
967
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ret == 0)
968 {
969
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (len1 < len2) ret = -1;
970
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (len1 > len2) ret = 1;
971 }
972 1 return ret;
973 }
974 int gate_str16_comp_ic(gate_char16_t const* str1, gate_char16_t const* str2)
975 {
976 return gate_str16_compare_ic(str1, gate_str16_length(str1), str2, gate_str16_length(str2));
977 }
978 int gate_str16_compare_ic(gate_char16_t const* str1, gate_size_t len1, gate_char16_t const* str2, gate_size_t len2)
979 {
980 gate_size_t len = len1 < len2 ? len1 : len2;
981 int ret = gate_str16_comp_range_ic(str1, str2, len);
982 if (ret == 0)
983 {
984 if (len1 < len2) ret = -1;
985 if (len1 > len2) ret = 1;
986 }
987 return ret;
988 }
989
990
991 3 int gate_str32_comp(gate_char32_t const* str1, gate_char32_t const* str2)
992 {
993 3 return gate_str32_compare(str1, gate_str32_length(str1), str2, gate_str32_length(str2));
994 }
995 3 int gate_str32_compare(gate_char32_t const* str1, gate_size_t len1, gate_char32_t const* str2, gate_size_t len2)
996 {
997 3 gate_size_t len = len1 < len2 ? len1 : len2;
998 3 int ret = gate_str32_comp_range(str1, str2, len);
999
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (ret == 0)
1000 {
1001
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (len1 < len2) ret = -1;
1002
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (len1 > len2) ret = 1;
1003 }
1004 3 return ret;
1005 }
1006 int gate_str32_comp_ic(gate_char32_t const* str1, gate_char32_t const* str2)
1007 {
1008 return gate_str32_compare_ic(str1, gate_str32_length(str1), str2, gate_str32_length(str2));
1009 }
1010 int gate_str32_compare_ic(gate_char32_t const* str1, gate_size_t len1, gate_char32_t const* str2, gate_size_t len2)
1011 {
1012 gate_size_t len = len1 < len2 ? len1 : len2;
1013 int ret = gate_str32_comp_range_ic(str1, str2, len);
1014 if (ret == 0)
1015 {
1016 if (len1 < len2) ret = -1;
1017 if (len1 > len2) ret = 1;
1018 }
1019 return ret;
1020 }
1021
1022
1023
1024
1025 /********************************
1026 * StringBuilder implementation *
1027 ********************************/
1028
1029 4382 static gate_stringbuffer_t* gate_strbuilder_resize_heap(gate_stringbuffer_t* src, gate_size_t srclength, gate_size_t newcapacity)
1030 {
1031 gate_stringbuffer_t* ret;
1032
2/2
✓ Branch 0 taken 888 times.
✓ Branch 1 taken 3494 times.
4382 if (newcapacity == 0)
1033 {
1034
2/2
✓ Branch 0 taken 113 times.
✓ Branch 1 taken 775 times.
888 if (src != NULL)
1035 {
1036 113 gate_mem_dealloc(src);
1037 }
1038 888 ret = NULL;
1039 }
1040 else
1041 {
1042
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3494 times.
3494 if (newcapacity <= srclength)
1043 {
1044 ret = src;
1045 }
1046 else
1047 {
1048 3494 ++newcapacity;
1049 3494 ret = (gate_stringbuffer_t*)gate_mem_realloc(src, sizeof(gate_stringbuffer_t) + newcapacity);
1050
1/2
✓ Branch 0 taken 3494 times.
✗ Branch 1 not taken.
3494 if (ret != NULL)
1051 {
1052 3494 ret->data[newcapacity] = 0;
1053 3494 ret->refcount = 0;
1054 }
1055 }
1056 }
1057 4382 return ret;
1058 }
1059
1060 31 static gate_stringbuffer_t* gate_strbuilder_resize_disabled(gate_stringbuffer_t* src, gate_size_t srclength, gate_size_t newcapacity)
1061 {
1062 (void)src;
1063 (void)srclength;
1064 (void)newcapacity;
1065 31 return NULL;
1066 }
1067
1068 888 void gate_strbuilder_create(gate_strbuilder_t* builder, gate_size_t capacity)
1069 {
1070 888 builder->ptr_data = NULL;
1071 888 builder->length = 0;
1072 888 builder->capacity = 0;
1073 888 builder->resize = &gate_strbuilder_resize_heap;
1074 888 builder->buffer = NULL;
1075
2/2
✓ Branch 0 taken 765 times.
✓ Branch 1 taken 123 times.
888 if (capacity != 0)
1076 {
1077 765 builder->buffer = builder->resize(NULL, 0, capacity);
1078
1/2
✓ Branch 0 taken 765 times.
✗ Branch 1 not taken.
765 if (builder->buffer != NULL)
1079 {
1080 765 builder->ptr_data = builder->buffer->data;
1081 }
1082 }
1083 888 }
1084 84 void gate_strbuilder_create_static(gate_strbuilder_t* builder, char* buffer, gate_size_t capacity, gate_size_t length_used)
1085 {
1086 84 builder->ptr_data = buffer;
1087 84 builder->length = length_used;
1088 84 builder->capacity = capacity;
1089 84 builder->resize = &gate_strbuilder_resize_disabled;
1090 84 builder->buffer = NULL;
1091 84 builder->ptr_data[length_used] = 0;
1092 84 }
1093
1094
1095 943 void gate_strbuilder_release(gate_strbuilder_t* builder)
1096 {
1097
2/2
✓ Branch 0 taken 917 times.
✓ Branch 1 taken 26 times.
943 if ((builder->resize != NULL))
1098 {
1099 917 builder->buffer = builder->resize(builder->buffer, builder->length, 0);
1100 917 builder->capacity = 0;
1101 }
1102 943 builder->ptr_data = NULL;
1103 943 builder->length = 0;
1104 943 }
1105 1458 gate_size_t gate_strbuilder_length(gate_strbuilder_t const* builder)
1106 {
1107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1458 times.
1458 if (builder == NULL)
1108 {
1109 return 0;
1110 }
1111 else
1112 {
1113 1458 return builder->length;
1114 }
1115 }
1116 976 gate_char8_t const* gate_strbuilder_ptr(gate_strbuilder_t const* builder, gate_size_t charpos)
1117 {
1118
2/4
✓ Branch 0 taken 976 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 976 times.
976 if ((builder == NULL) || (builder->ptr_data == NULL))
1119 {
1120 return NULL;
1121 }
1122 else
1123 {
1124
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 927 times.
976 if (charpos >= builder->length)
1125 {
1126 49 return NULL;
1127 }
1128 else
1129 {
1130 927 return &builder->ptr_data[charpos];
1131 }
1132 }
1133 }
1134 2731 gate_size_t gate_strbuilder_resize(gate_strbuilder_t* builder, gate_size_t sz)
1135 {
1136 gate_stringbuffer_t* newbuffer;
1137
2/4
✓ Branch 0 taken 2731 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2731 times.
✗ Branch 3 not taken.
2731 if ((builder->resize != NULL) && (sz != 0))
1138 {
1139 2731 newbuffer = builder->resize(builder->buffer, builder->length, sz + 1);
1140
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2729 times.
2731 if (newbuffer == NULL)
1141 {
1142 /* allocation failed */
1143 2 return 0;
1144 }
1145 2729 builder->buffer = newbuffer;
1146 2729 builder->ptr_data = newbuffer->data;
1147 2729 builder->capacity = sz;
1148 2729 return builder->capacity;
1149 }
1150 else
1151 {
1152 return 0;
1153 }
1154 }
1155
1156 2724 gate_size_t gate_strbuilder_increase(gate_strbuilder_t* builder, gate_size_t sz)
1157 {
1158 2724 return gate_strbuilder_resize(builder, builder->capacity + sz + 1);
1159 }
1160
1161
1162 817197 gate_size_t gate_strbuilder_append_text(gate_strbuilder_t* builder, gate_char8_t const* txt, gate_size_t txtlen)
1163 {
1164
3/4
✓ Branch 0 taken 817178 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 817178 times.
✗ Branch 3 not taken.
817197 if ((txtlen != 0) && (txt != NULL))
1165 {
1166 817178 gate_size_t const free_chars = builder->capacity - builder->length;
1167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 817178 times.
817178 GATE_DEBUG_ASSERT(builder->capacity >= builder->length);
1168
2/2
✓ Branch 0 taken 2691 times.
✓ Branch 1 taken 814487 times.
817178 if (txtlen >= free_chars)
1169 {
1170
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2691 times.
2691 if (gate_strbuilder_increase(builder, txtlen + builder->length / 2) == 0)
1171 {
1172 return 0;
1173 }
1174 }
1175 817178 gate_mem_copy(&builder->ptr_data[builder->length], txt, txtlen);
1176 817178 builder->length += txtlen;
1177 817178 builder->ptr_data[builder->length] = 0;
1178 817178 return txtlen;
1179 }
1180 else
1181 {
1182 19 return 0;
1183 }
1184 }
1185 224 gate_size_t gate_strbuilder_append_chars(gate_strbuilder_t* builder, gate_size_t char_count, char chr)
1186 {
1187
2/2
✓ Branch 0 taken 209 times.
✓ Branch 1 taken 15 times.
224 if (char_count != 0)
1188 {
1189 gate_size_t cnt;
1190 char* ptr;
1191
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 178 times.
209 if (char_count >= builder->capacity - builder->length)
1192 {
1193
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
31 if (gate_strbuilder_increase(builder, char_count + builder->length / 2) == 0)
1194 {
1195 return 0;
1196 }
1197 }
1198 209 ptr = &builder->ptr_data[builder->length];
1199 209 cnt = char_count;
1200
2/2
✓ Branch 0 taken 313 times.
✓ Branch 1 taken 209 times.
522 while (cnt-- != 0)
1201 {
1202 313 *ptr = chr;
1203 313 ++ptr;
1204 }
1205 209 builder->length += char_count;
1206 209 builder->ptr_data[builder->length] = 0;
1207 209 return char_count;
1208 }
1209 else
1210 {
1211 15 return 0;
1212 }
1213 }
1214
1215 659473 gate_size_t gate_strbuilder_append_cstr(gate_strbuilder_t* builder, char const* txt)
1216 {
1217
1/2
✓ Branch 0 taken 659473 times.
✗ Branch 1 not taken.
659473 if (txt)
1218 {
1219 659473 return gate_strbuilder_append_text(builder, txt, gate_str_length(txt));
1220 }
1221 return 0;
1222 }
1223
1224 343 gate_size_t gate_strbuilder_append_string(gate_strbuilder_t* builder, gate_string_t const* txt)
1225 {
1226
1/2
✓ Branch 0 taken 343 times.
✗ Branch 1 not taken.
343 if (txt)
1227 {
1228 343 return gate_strbuilder_append_text(builder, txt->str, txt->length);
1229 }
1230 return 0;
1231 }
1232
1233 1 gate_size_t gate_strbuilder_append_text16(gate_strbuilder_t* builder, gate_char16_t const* txt, gate_size_t txtlen)
1234 {
1235 1 gate_size_t ret = 0;
1236 gate_char32_t chr;
1237 gate_size_t len_decoded, len_encoded;
1238 gate_char8_t utf8[16];
1239 1 gate_strbuilder_increase(builder, txtlen);
1240
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 while (txtlen != 0)
1241 {
1242 1 len_decoded = gate_char_read_utf16(txt, txtlen, &chr);
1243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (len_decoded == 0)
1244 {
1245 break;
1246 }
1247 1 txt += len_decoded;
1248 1 txtlen -= len_decoded;
1249 1 len_encoded = gate_char_write_utf8(chr, utf8, sizeof(utf8));
1250 1 gate_strbuilder_append_text(builder, utf8, len_encoded);
1251 1 ret += len_encoded;
1252 }
1253 1 return ret;
1254 }
1255 1 gate_size_t gate_strbuilder_append_text32(gate_strbuilder_t* builder, gate_char32_t const* txt, gate_size_t txtlen)
1256 {
1257 1 gate_size_t ret = 0;
1258 gate_char32_t chr;
1259 gate_size_t len_decoded, len_encoded;
1260 gate_char8_t utf8[16];
1261 1 gate_strbuilder_increase(builder, txtlen);
1262
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 while (txtlen != 0)
1263 {
1264 1 len_decoded = gate_char_read_utf32(txt, txtlen, &chr);
1265
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (len_decoded == 0)
1266 {
1267 break;
1268 }
1269 1 txt += len_decoded;
1270 1 txtlen -= len_decoded;
1271 1 len_encoded = gate_char_write_utf8(chr, utf8, sizeof(utf8));
1272 1 gate_strbuilder_append_text(builder, utf8, len_encoded);
1273 1 ret += len_encoded;
1274 }
1275 1 return ret;
1276 }
1277
1278
1279 1 gate_size_t gate_strbuilder_append_int16(gate_strbuilder_t* builder, gate_int16_t num)
1280 {
1281 gate_char8_t buffer[12];
1282 1 gate_size_t bufferlen = sizeof(buffer) / sizeof(buffer[0]);
1283 1 gate_size_t bufferused = gate_str_print_int16(&buffer[0], bufferlen, num);
1284 1 return gate_strbuilder_append_text(builder, &buffer[0], bufferused);
1285 }
1286 24 gate_size_t gate_strbuilder_append_uint16(gate_strbuilder_t* builder, gate_uint16_t num)
1287 {
1288 gate_char8_t buffer[12];
1289 24 gate_size_t bufferlen = sizeof(buffer) / sizeof(buffer[0]);
1290 24 gate_size_t bufferused = gate_str_print_uint16(&buffer[0], bufferlen, num);
1291 24 return gate_strbuilder_append_text(builder, &buffer[0], bufferused);
1292 }
1293 29 gate_size_t gate_strbuilder_append_int32(gate_strbuilder_t* builder, gate_int32_t num)
1294 {
1295 gate_char8_t buffer[16];
1296 29 gate_size_t bufferlen = sizeof(buffer) / sizeof(buffer[0]);
1297 29 gate_size_t bufferused = gate_str_print_int32(&buffer[0], bufferlen, num);
1298 29 return gate_strbuilder_append_text(builder, &buffer[0], bufferused);
1299 }
1300 131865 gate_size_t gate_strbuilder_append_uint32(gate_strbuilder_t* builder, gate_uint32_t num)
1301 {
1302 gate_char8_t buffer[16];
1303 131865 gate_size_t bufferlen = sizeof(buffer) / sizeof(buffer[0]);
1304 131865 gate_size_t bufferused = gate_str_print_uint32(&buffer[0], bufferlen, num);
1305 131865 return gate_strbuilder_append_text(builder, &buffer[0], bufferused);
1306 }
1307 39 gate_size_t gate_strbuilder_append_int64(gate_strbuilder_t* builder, gate_int64_t num)
1308 {
1309 gate_char8_t buffer[24];
1310 39 gate_size_t bufferlen = sizeof(buffer) / sizeof(buffer[0]);
1311 39 gate_size_t bufferused = gate_str_print_int64(&buffer[0], bufferlen, num);
1312 39 return gate_strbuilder_append_text(builder, &buffer[0], bufferused);
1313 }
1314 7 gate_size_t gate_strbuilder_append_uint64(gate_strbuilder_t* builder, gate_uint64_t num)
1315 {
1316 gate_char8_t buffer[24];
1317 7 gate_size_t bufferlen = sizeof(buffer) / sizeof(buffer[0]);
1318 7 gate_size_t bufferused = gate_str_print_int64(&buffer[0], bufferlen, num);
1319 7 return gate_strbuilder_append_text(builder, &buffer[0], bufferused);
1320 }
1321 13 gate_size_t gate_strbuilder_append_real(gate_strbuilder_t* builder, gate_real64_t num, unsigned intlen, unsigned decimallen, unsigned grouplen)
1322 {
1323 gate_char8_t buffer[64];
1324 13 gate_size_t bufferlen = sizeof(buffer) / sizeof(buffer[0]);
1325 13 gate_size_t bufferused = gate_str_print_real(&buffer[0], bufferlen, num, intlen, decimallen, grouplen);
1326 13 return gate_strbuilder_append_text(builder, &buffer[0], bufferused);
1327 }
1328 6 gate_size_t gate_strbuilder_append_hex(gate_strbuilder_t* builder, gate_uint8_t const* bytes, gate_size_t byte_count, gate_bool_t uppercase)
1329 {
1330 6 gate_size_t ret = 0;
1331 gate_char8_t chr[4];
1332
1333
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 while (byte_count-- != 0)
1334 {
1335 6 chr[0] = gate_str_print_hex_nibble((gate_uint8_t)((*bytes >> 4) & 0x0f), uppercase);
1336 6 chr[1] = gate_str_print_hex_nibble((gate_uint8_t)((*bytes) & 0x0f), uppercase);
1337 6 ret += gate_strbuilder_append_text(builder, chr, 2);
1338 6 ++bytes;
1339 }
1340 6 return ret;
1341 }
1342
1343
1344 418 static gate_size_t gate_strbuilder_printer_callback(void* printer_param, gate_char8_t const* data, gate_size_t datalen)
1345 {
1346 418 gate_strbuilder_t* builder = (gate_strbuilder_t*)printer_param;
1347 418 return gate_strbuilder_append_text(builder, data, datalen);
1348 }
1349
1350 59 gate_size_t gate_strbuilder_append(gate_strbuilder_t* builder, ...)
1351 {
1352 gate_size_t ret;
1353 va_list vl;
1354
1355 59 va_start(vl, builder);
1356
1357 59 ret = gate_str_print_to(&gate_strbuilder_printer_callback, builder, vl);
1358
1359 59 va_end(vl);
1360
1361 59 return ret;
1362 }
1363
1364 gate_size_t gate_strbuilder_insert(gate_strbuilder_t* builder, gate_size_t pos, char const* txt, gate_size_t txtlen)
1365 {
1366 gate_size_t ret = 0;
1367 if (pos >= builder->length)
1368 {
1369 ret = gate_strbuilder_append_text(builder, txt, txtlen);
1370 }
1371 else
1372 {
1373 ret = gate_strbuilder_resize(builder, builder->length + txtlen + 2);
1374 if (ret != 0)
1375 {
1376 gate_mem_move(&builder->buffer->data[pos + txtlen], &builder->buffer->data[pos], builder->length - pos);
1377 gate_mem_copy(&builder->buffer->data[pos], txt, txtlen);
1378 builder->length += txtlen;
1379 builder->buffer->data[builder->length] = 0;
1380 ret = txtlen;
1381 }
1382 }
1383 return ret;
1384 }
1385
1386 gate_size_t gate_strbuilder_remove(gate_strbuilder_t* builder, gate_size_t pos, gate_size_t length)
1387 {
1388 if (pos >= builder->length)
1389 {
1390 return 0;
1391 }
1392 if (pos + length > builder->length)
1393 {
1394 length = builder->length - pos;
1395 if (length == 0)
1396 {
1397 return 0;
1398 }
1399 }
1400 if (pos + length < builder->length)
1401 {
1402 gate_mem_move(&builder->buffer->data[pos], &builder->buffer->data[pos + length], builder->length - pos - length);
1403 }
1404
1405 builder->length -= length;
1406 builder->buffer->data[builder->length] = 0;
1407
1408 return length;
1409 }
1410
1411 30 gate_size_t gate_strbuilder_replace(gate_strbuilder_t* builder, gate_size_t pos, gate_size_t len, char const* txt, gate_size_t txtlen)
1412 {
1413 30 gate_size_t ret = 0; /* default result: no replacement applied */
1414 gate_size_t diff;
1415 gate_size_t movelen;
1416 do
1417 {
1418
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
30 if (pos + len > builder->length)
1419 {
1420 /* out of bounds */
1421 break;
1422 }
1423
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 21 times.
30 if (txtlen > len)
1424 {
1425 /* replacement requires string expansion */
1426 9 diff = txtlen - len;
1427
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
9 if (builder->length + diff + 1 > builder->capacity)
1428 {
1429
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
7 if (0 == gate_strbuilder_resize(builder, builder->length + diff + 1))
1430 {
1431 /* allocation failed */
1432 break;
1433 }
1434 }
1435 9 movelen = builder->length - (pos + len);
1436
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 if (movelen > 0)
1437 {
1438 6 gate_mem_move(&builder->ptr_data[pos + txtlen], &builder->ptr_data[pos + len], movelen);
1439 }
1440 9 gate_mem_copy(&builder->ptr_data[pos], txt, txtlen);
1441 9 builder->length += diff;
1442 9 builder->ptr_data[builder->length] = 0;
1443 }
1444 else
1445 {
1446 /* inline replacement */
1447
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1 times.
21 if (txtlen > 0)
1448 {
1449 20 gate_mem_copy(&builder->ptr_data[pos], txt, txtlen);
1450 }
1451
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 1 times.
21 if (len > txtlen)
1452 {
1453 /* discard characters */
1454 20 diff = len - txtlen;
1455 20 movelen = builder->length - (pos + len);
1456
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 8 times.
20 if (movelen > 0)
1457 {
1458 12 gate_mem_move(&builder->ptr_data[pos + txtlen], &builder->ptr_data[pos + len], movelen);
1459 }
1460 20 builder->length -= diff;
1461 20 builder->ptr_data[builder->length] = 0;
1462 }
1463 }
1464 30 ret = 1; /* one replacement applied */
1465 } while (0);
1466 30 return ret;
1467
1468 }
1469
1470 gate_size_t gate_strbuilder_replace_str(gate_strbuilder_t* builder, char const* find, gate_size_t find_len, char const* replace_with, gate_size_t replace_with_len, gate_size_t start_pos, gate_size_t max_replace)
1471 {
1472 gate_size_t replaced_count = 0;
1473 gate_size_t find_pos = 0;
1474
1475 while (max_replace-- > 0)
1476 {
1477 find_pos = gate_strbuilder_str_pos(builder, find, find_len, start_pos);
1478 if (find_pos == GATE_STR_NPOS)
1479 {
1480 /* nothing more found */
1481 break;
1482 }
1483 if (0 == gate_strbuilder_replace(builder, find_pos, find_len, replace_with, replace_with_len))
1484 {
1485 /* native replace operation failed */
1486 break;
1487 }
1488 start_pos = find_pos;
1489 ++replaced_count;
1490 }
1491 return replaced_count;
1492 }
1493
1494 gate_size_t gate_strbuilder_replace_string(gate_strbuilder_t* builder, gate_string_t const* find, gate_string_t const* replace_with, gate_size_t start_pos, gate_size_t max_replace)
1495 {
1496 return gate_strbuilder_replace_str(builder,
1497 gate_string_ptr(find, 0), gate_string_length(find),
1498 gate_string_ptr(replace_with, 0), gate_string_length(replace_with),
1499 start_pos, max_replace);
1500 }
1501
1502
1503
1504 774 gate_size_t gate_strbuilder_discard(gate_strbuilder_t* builder, gate_size_t charcount)
1505 {
1506
2/2
✓ Branch 0 taken 540 times.
✓ Branch 1 taken 234 times.
774 if (charcount >= builder->length)
1507 {
1508 540 charcount = builder->length;
1509 540 builder->length = 0;
1510 }
1511 else
1512 {
1513
1/2
✓ Branch 0 taken 234 times.
✗ Branch 1 not taken.
234 if (charcount > 0)
1514 {
1515 234 gate_mem_move(&builder->ptr_data[0], &builder->ptr_data[charcount], builder->length - charcount);
1516 234 builder->length -= charcount;
1517 234 builder->ptr_data[builder->length] = 0;
1518 }
1519 }
1520 774 return charcount;
1521 }
1522 1 gate_size_t gate_strbuilder_discard_back(gate_strbuilder_t* builder, gate_size_t charcount)
1523 {
1524
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (charcount > builder->length)
1525 {
1526 charcount = builder->length;
1527 }
1528
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (charcount > 0)
1529 {
1530 1 builder->length -= charcount;
1531 1 builder->ptr_data[builder->length] = 0;
1532 }
1533 1 return charcount;
1534 }
1535
1536 528 gate_size_t gate_strbuilder_str_pos(gate_strbuilder_t const* builder, gate_char8_t const* str, gate_size_t strlength, gate_size_t start_at)
1537 {
1538
3/6
✓ Branch 0 taken 528 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 528 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 528 times.
528 if (!builder || !str || (strlength == 0))
1539 {
1540 return GATE_STR_NPOS;
1541 }
1542 528 return gate_str_pos(builder->ptr_data, builder->length, str, strlength, start_at);
1543 }
1544
1545 gate_size_t gate_strbuilder_pos(gate_strbuilder_t const* builder, gate_string_t const* token, gate_size_t start_at)
1546 {
1547 if (!builder || (gate_string_length(token) == 0))
1548 {
1549 return GATE_STR_NPOS;
1550 }
1551 return gate_str_pos(builder->ptr_data, builder->length, token->str, token->length, start_at);
1552 }
1553 gate_size_t gate_strbuilder_char_pos(gate_strbuilder_t const* builder, gate_char8_t chr, gate_size_t start_at)
1554 {
1555 return gate_str_char_pos(builder->ptr_data, builder->length, chr, start_at);
1556 }
1557
1558
1559 782 gate_string_t* gate_strbuilder_to_string(gate_strbuilder_t* builder, gate_string_t* str)
1560 {
1561
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 766 times.
782 if (builder->length == 0)
1562 {
1563 16 gate_string_create_empty(str);
1564 16 gate_strbuilder_release(builder);
1565 }
1566 else
1567 {
1568 766 str->buffer = builder->buffer;
1569 766 str->str = builder->ptr_data;
1570 766 str->length = builder->length;
1571
2/2
✓ Branch 0 taken 763 times.
✓ Branch 1 taken 3 times.
766 if (str->buffer != NULL)
1572 {
1573 763 gate_atomic_int_init(&str->buffer->refcount, 1);
1574 }
1575
1576 766 builder->buffer = NULL;
1577 766 builder->ptr_data = NULL;
1578 766 builder->capacity = 0;
1579 766 builder->length = 0;
1580 }
1581 782 return str;
1582 }
1583 4 gate_string_t* gate_strbuilder_copy_to_string(gate_strbuilder_t const* builder, gate_string_t* str)
1584 {
1585 4 return gate_string_create(str, &builder->ptr_data[0], builder->length);
1586 }
1587 gate_result_t gate_strbuilder_copy_constructor(void* destMem, void const* srcMem)
1588 {
1589 gate_strbuilder_t* dst = (gate_strbuilder_t*)destMem;
1590 gate_strbuilder_t const* src = (gate_strbuilder_t const*)srcMem;
1591 gate_size_t len = gate_strbuilder_length(src);
1592 gate_strbuilder_create(dst, 0);
1593 if (len > 0)
1594 {
1595 if (0 == gate_strbuilder_append_text(dst, gate_strbuilder_ptr(src, 0), len))
1596 {
1597 gate_strbuilder_release(dst);
1598 return GATE_RESULT_OUTOFMEMORY;
1599 }
1600 }
1601 return GATE_RESULT_OK;
1602 }
1603 void gate_strbuilder_destructor(void* dest)
1604 {
1605 gate_strbuilder_t* dst = (gate_strbuilder_t*)dest;
1606 gate_strbuilder_release(dst);
1607 }
1608
1609
1610
1611
1612
1613
1614
1615 #define GATE_STR_PRINT_TXT(dst, dstlen, txt, txtlen) \
1616 if((dstlen) == 0) \
1617 { \
1618 return 0; \
1619 } \
1620 if((txtlen) >= (dstlen)) \
1621 { \
1622 txtlen = dstlen - 1; \
1623 } \
1624 if((txtlen) != 0) \
1625 { \
1626 gate_mem_copy((dst), (txt), (txtlen) * sizeof( (dst)[0] )); \
1627 } \
1628 dst[txtlen] = 0; \
1629 return txtlen;
1630
1631 137037 gate_size_t gate_str_print_text(gate_char8_t* dst, gate_size_t dstlen, gate_char8_t const* txt, gate_size_t txtlen)
1632 {
1633
5/6
✗ Branch 0 not taken.
✓ Branch 1 taken 137037 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 137033 times.
✓ Branch 4 taken 136938 times.
✓ Branch 5 taken 99 times.
137037 GATE_STR_PRINT_TXT(dst, dstlen, txt, txtlen);
1634 }
1635 gate_size_t gate_str16_print_text(gate_char16_t* dst, gate_size_t dstlen, gate_char16_t const* txt, gate_size_t txtlen)
1636 {
1637 GATE_STR_PRINT_TXT(dst, dstlen, txt, txtlen);
1638 }
1639 gate_size_t gate_str32_print_text(gate_char32_t* dst, gate_size_t dstlen, gate_char32_t const* txt, gate_size_t txtlen)
1640 {
1641 GATE_STR_PRINT_TXT(dst, dstlen, txt, txtlen);
1642 }
1643
1644 9 gate_size_t gate_str_print_wtext(gate_char8_t* dst, gate_size_t dstlen, wchar_t const* txt, gate_size_t txtlen)
1645 {
1646 #if defined(GATE_TYPE_WCHAR_IS_UTF16)
1647 return gate_str_utf16_2_utf8((gate_char16_t const*)txt, txtlen, dst, dstlen);
1648 #else
1649 9 return gate_str_utf32_2_utf8((gate_char32_t const*)txt, txtlen, dst, dstlen);
1650 #endif
1651 }
1652 gate_size_t gate_str16_print_wtext(gate_char16_t* dst, gate_size_t dstlen, wchar_t const* txt, gate_size_t txtlen)
1653 {
1654 #if defined(GATE_TYPE_WCHAR_IS_UTF16)
1655 return gate_str16_print_text(dst, dstlen, txt, txtlen);
1656 #else
1657 return gate_str_utf32_2_utf16((gate_char32_t const*)txt, txtlen, dst, dstlen);
1658 #endif
1659 }
1660 gate_size_t gate_str32_print_wtext(gate_char32_t* dst, gate_size_t dstlen, wchar_t const* txt, gate_size_t txtlen)
1661 {
1662 #if defined(GATE_TYPE_WCHAR_IS_UTF16)
1663 return gate_str_utf16_2_utf32((gate_char16_t const*)txt, txtlen, dst, dstlen);
1664 #else
1665 return gate_str32_print_text(dst, dstlen, txt, txtlen);
1666 #endif
1667
1668 }
1669
1670
1671
1672
1673 #define GATE_STR_PRINT_NUM(buffer, bufferlen, ptr, bufferused, numtype, num, reverter) \
1674 { \
1675 ptr = &buffer[0]; \
1676 bufferused = 0; \
1677 while(bufferlen != 0) \
1678 { \
1679 *ptr = ('0' + (gate_char8_t)(num % (numtype)10)); \
1680 num /= (numtype)10; \
1681 ++bufferused; \
1682 ++ptr; \
1683 --bufferlen; \
1684 if(num == 0) break; \
1685 } \
1686 if(bufferused >= 2) \
1687 { \
1688 reverter(&buffer[0], bufferused); \
1689 } \
1690 if(bufferlen != 0) \
1691 { \
1692 *ptr = 0; \
1693 } \
1694 }
1695
1696
1697 #define GATE_STR_PRINT_INUM(retvar, buffer, bufferlen, unum, num, uint_type, uint_func) \
1698 { \
1699 retvar = 0; \
1700 if(num < 0) \
1701 { \
1702 *buffer = '-'; \
1703 ++buffer; \
1704 --bufferlen; \
1705 ++retvar; \
1706 unum = (uint_type)-num; \
1707 } \
1708 else \
1709 { \
1710 unum = (uint_type)num; \
1711 } \
1712 retvar += uint_func(buffer, bufferlen, unum) ; \
1713 }
1714
1715
1716 306 gate_size_t gate_str_print_uint16(gate_char8_t* dst, gate_size_t dstlen, gate_uint16_t num)
1717 {
1718 306 gate_size_t ret = 0;
1719
2/4
✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 306 times.
✗ Branch 3 not taken.
306 if ((dst != NULL) && (dstlen != 0))
1720 {
1721 gate_char8_t* ptr;
1722
6/8
✓ Branch 0 taken 306 times.
✓ Branch 1 taken 199 times.
✓ Branch 2 taken 505 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 142 times.
✓ Branch 5 taken 164 times.
✓ Branch 7 taken 306 times.
✗ Branch 8 not taken.
505 GATE_STR_PRINT_NUM(dst, dstlen, ptr, ret, gate_uint16_t, num, gate_str_reverse);
1723 }
1724 306 return ret;
1725 }
1726 3 gate_size_t gate_str_print_int16(gate_char8_t* dst, gate_size_t dstlen, gate_int16_t num)
1727 {
1728 3 gate_size_t ret = 0;
1729
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 if ((dst != NULL) && (dstlen != 0))
1730 {
1731 gate_uint16_t unum;
1732
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 GATE_STR_PRINT_INUM(ret, dst, dstlen, unum, num, gate_uint16_t, gate_str_print_uint16);
1733 }
1734 3 return ret;
1735 }
1736 133035 gate_size_t gate_str_print_uint32(gate_char8_t* dst, gate_size_t dstlen, gate_uint32_t num)
1737 {
1738 133035 gate_size_t ret = 0;
1739
2/4
✓ Branch 0 taken 133035 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 133035 times.
✗ Branch 3 not taken.
133035 if ((dst != NULL) && (dstlen != 0))
1740 {
1741 gate_char8_t* ptr;
1742
6/8
✓ Branch 0 taken 133035 times.
✓ Branch 1 taken 255176 times.
✓ Branch 2 taken 388211 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 132702 times.
✓ Branch 5 taken 333 times.
✓ Branch 7 taken 133035 times.
✗ Branch 8 not taken.
388211 GATE_STR_PRINT_NUM(dst, dstlen, ptr, ret, gate_uint32_t, num, gate_str_reverse);
1743 }
1744 133035 return ret;
1745 }
1746 328 gate_size_t gate_str_print_int32(gate_char8_t* dst, gate_size_t dstlen, gate_int32_t num)
1747 {
1748 328 gate_size_t ret = 0;
1749
2/4
✓ Branch 0 taken 328 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 328 times.
✗ Branch 3 not taken.
328 if ((dst != NULL) && (dstlen != 0))
1750 {
1751 gate_uint32_t unum;
1752
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 328 times.
328 GATE_STR_PRINT_INUM(ret, dst, dstlen, unum, num, gate_uint32_t, gate_str_print_uint32);
1753 }
1754 328 return ret;
1755 }
1756 1096 gate_size_t gate_str_print_uint64(gate_char8_t* dst, gate_size_t dstlen, gate_uint64_t num)
1757 {
1758 1096 gate_size_t ret = 0;
1759
2/4
✓ Branch 0 taken 1096 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1096 times.
✗ Branch 3 not taken.
1096 if ((dst != NULL) && (dstlen != 0))
1760 {
1761 gate_char8_t* ptr;
1762
8/8
✓ Branch 0 taken 1095 times.
✓ Branch 1 taken 1389 times.
✓ Branch 2 taken 2484 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 650 times.
✓ Branch 5 taken 446 times.
✓ Branch 7 taken 1095 times.
✓ Branch 8 taken 1 times.
2485 GATE_STR_PRINT_NUM(dst, dstlen, ptr, ret, gate_uint64_t, num, gate_str_reverse);
1763 }
1764 1096 return ret;
1765 }
1766 278 gate_size_t gate_str_print_int64(gate_char8_t* dst, gate_size_t dstlen, gate_int64_t num)
1767 {
1768 278 gate_size_t ret = 0;
1769
2/4
✓ Branch 0 taken 278 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 278 times.
✗ Branch 3 not taken.
278 if ((dst != NULL) && (dstlen != 0))
1770 {
1771 gate_uint64_t unum;
1772
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 241 times.
278 GATE_STR_PRINT_INUM(ret, dst, dstlen, unum, num, gate_uint64_t, gate_str_print_uint64);
1773 }
1774 278 return ret;
1775 }
1776
1777
1778 gate_size_t gate_str16_print_uint16(gate_char16_t* dst, gate_size_t dstlen, gate_uint16_t num)
1779 {
1780 gate_size_t ret = 0;
1781 if ((dst != NULL) && (dstlen != 0))
1782 {
1783 gate_char16_t* ptr;
1784 GATE_STR_PRINT_NUM(dst, dstlen, ptr, ret, gate_uint16_t, num, gate_str16_reverse);
1785 }
1786 return ret;
1787 }
1788 gate_size_t gate_str16_print_int16(gate_char16_t* dst, gate_size_t dstlen, gate_int16_t num)
1789 {
1790 gate_size_t ret = 0;
1791 if ((dst != NULL) && (dstlen != 0))
1792 {
1793 gate_uint16_t unum;
1794 GATE_STR_PRINT_INUM(ret, dst, dstlen, unum, num, gate_uint16_t, gate_str16_print_uint16);
1795 }
1796 return ret;
1797 }
1798 gate_size_t gate_str16_print_uint32(gate_char16_t* dst, gate_size_t dstlen, gate_uint32_t num)
1799 {
1800 gate_size_t ret = 0;
1801 if ((dst != NULL) && (dstlen != 0))
1802 {
1803 gate_char16_t* ptr;
1804 GATE_STR_PRINT_NUM(dst, dstlen, ptr, ret, gate_uint32_t, num, gate_str16_reverse);
1805 }
1806 return ret;
1807 }
1808 gate_size_t gate_str16_print_int32(gate_char16_t* dst, gate_size_t dstlen, gate_int32_t num)
1809 {
1810 gate_size_t ret = 0;
1811 if ((dst != NULL) && (dstlen != 0))
1812 {
1813 gate_uint32_t unum;
1814 GATE_STR_PRINT_INUM(ret, dst, dstlen, unum, num, gate_uint32_t, gate_str16_print_uint32);
1815 }
1816 return ret;
1817 }
1818 gate_size_t gate_str16_print_uint64(gate_char16_t* dst, gate_size_t dstlen, gate_uint64_t num)
1819 {
1820 gate_size_t ret = 0;
1821 if ((dst != NULL) && (dstlen != 0))
1822 {
1823 gate_char16_t* ptr;
1824 GATE_STR_PRINT_NUM(dst, dstlen, ptr, ret, gate_uint64_t, num, gate_str16_reverse);
1825 }
1826 return ret;
1827 }
1828 gate_size_t gate_str16_print_int64(gate_char16_t* dst, gate_size_t dstlen, gate_int64_t num)
1829 {
1830 gate_size_t ret = 0;
1831 if ((dst != NULL) && (dstlen != 0))
1832 {
1833 gate_uint64_t unum;
1834 GATE_STR_PRINT_INUM(ret, dst, dstlen, unum, num, gate_uint64_t, gate_str16_print_uint64);
1835 }
1836 return ret;
1837 }
1838
1839
1840 gate_size_t gate_str32_print_uint16(gate_char32_t* dst, gate_size_t dstlen, gate_uint16_t num)
1841 {
1842 gate_size_t ret = 0;
1843 if ((dst != NULL) && (dstlen != 0))
1844 {
1845 gate_char32_t* ptr;
1846 GATE_STR_PRINT_NUM(dst, dstlen, ptr, ret, gate_uint16_t, num, gate_str32_reverse);
1847 }
1848 return ret;
1849 }
1850 gate_size_t gate_str32_print_int16(gate_char32_t* dst, gate_size_t dstlen, gate_int16_t num)
1851 {
1852 gate_size_t ret = 0;
1853 if ((dst != NULL) && (dstlen != 0))
1854 {
1855 gate_uint16_t unum;
1856 GATE_STR_PRINT_INUM(ret, dst, dstlen, unum, num, gate_uint16_t, gate_str32_print_uint16);
1857 }
1858 return ret;
1859 }
1860 gate_size_t gate_str32_print_uint32(gate_char32_t* dst, gate_size_t dstlen, gate_uint32_t num)
1861 {
1862 gate_size_t ret = 0;
1863 if ((dst != NULL) && (dstlen != 0))
1864 {
1865 gate_char32_t* ptr;
1866 GATE_STR_PRINT_NUM(dst, dstlen, ptr, ret, gate_uint32_t, num, gate_str32_reverse);
1867 }
1868 return ret;
1869 }
1870 gate_size_t gate_str32_print_int32(gate_char32_t* dst, gate_size_t dstlen, gate_int32_t num)
1871 {
1872 gate_size_t ret = 0;
1873 if ((dst != NULL) && (dstlen != 0))
1874 {
1875 gate_uint32_t unum;
1876 GATE_STR_PRINT_INUM(ret, dst, dstlen, unum, num, gate_uint32_t, gate_str32_print_uint32);
1877 }
1878 return ret;
1879 }
1880 gate_size_t gate_str32_print_uint64(gate_char32_t* dst, gate_size_t dstlen, gate_uint64_t num)
1881 {
1882 gate_size_t ret = 0;
1883 if ((dst != NULL) && (dstlen != 0))
1884 {
1885 gate_char32_t* ptr;
1886 GATE_STR_PRINT_NUM(dst, dstlen, ptr, ret, gate_uint64_t, num, gate_str32_reverse);
1887 }
1888 return ret;
1889 }
1890 gate_size_t gate_str32_print_int64(gate_char32_t* dst, gate_size_t dstlen, gate_int64_t num)
1891 {
1892 gate_size_t ret = 0;
1893 if ((dst != NULL) && (dstlen != 0))
1894 {
1895 gate_uint64_t unum;
1896 GATE_STR_PRINT_INUM(ret, dst, dstlen, unum, num, gate_uint64_t, gate_str32_print_uint64);
1897 }
1898 return ret;
1899 }
1900
1901
1902
1903
1904 444 gate_size_t gate_str_print_uint(gate_char8_t* dst, gate_size_t dstlen, gate_uint64_t num, gate_size_t intlen)
1905 {
1906 444 gate_size_t ret = 0;
1907 gate_char8_t buffer[24];
1908 444 gate_char8_t* ptr = &buffer[0];
1909 444 gate_size_t bytesused = gate_str_print_uint64(ptr, sizeof(buffer), num);
1910
1911
2/2
✓ Branch 0 taken 254 times.
✓ Branch 1 taken 190 times.
444 if (intlen > 0)
1912 {
1913
2/2
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 132 times.
254 if (intlen > bytesused)
1914 {
1915 gate_size_t ndx;
1916
3/4
✓ Branch 0 taken 132 times.
✓ Branch 1 taken 122 times.
✓ Branch 2 taken 132 times.
✗ Branch 3 not taken.
254 for (ndx = bytesused; (ndx < intlen) && (dstlen != 0); ++ndx)
1917 {
1918 132 *dst = '0';
1919 132 ++dst;
1920 132 --dstlen;
1921 132 ++ret;
1922 }
1923 }
1924
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 128 times.
132 else if (bytesused > intlen)
1925 {
1926 4 ptr += (bytesused - intlen);
1927 4 bytesused -= intlen;
1928 }
1929 }
1930
1931
2/2
✓ Branch 0 taken 442 times.
✓ Branch 1 taken 2 times.
444 if (dstlen >= bytesused)
1932 {
1933 442 gate_mem_copy(dst, ptr, bytesused);
1934 442 ret += bytesused;
1935 442 dst += bytesused;
1936 442 dstlen -= bytesused;
1937 }
1938
1939
2/2
✓ Branch 0 taken 397 times.
✓ Branch 1 taken 47 times.
444 if (dstlen != 0)
1940 {
1941 397 *dst = '\0';
1942 }
1943 444 return ret;
1944
1945 }
1946
1947 11 static gate_size_t gate_str_print_grouped(char* bufferptr, gate_size_t bufferlen, char const* src, gate_size_t srclen, gate_size_t grouplen)
1948 {
1949 11 gate_size_t ret = 0;
1950 11 gate_size_t count = 0;
1951 11 gate_size_t mod = 0;
1952 gate_size_t len;
1953 gate_size_t ndx;
1954
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 if (grouplen != 0)
1955 {
1956 11 count = srclen / grouplen;
1957 11 mod = srclen % grouplen;
1958 }
1959
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 3 times.
11 if (mod != 0)
1960 {
1961 8 len = gate_str_print_text(&bufferptr[ret], bufferlen - ret, src, mod);
1962 8 ret += len;
1963
1964
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (count != 0)
1965 {
1966 bufferptr[ret] = ' ';
1967 ++ret;
1968 }
1969
1970 8 src += mod;
1971 8 srclen -= mod;
1972 }
1973
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 11 times.
15 for (ndx = 0; ndx != count; )
1974 {
1975 4 ++ndx;
1976 4 len = gate_str_print_text(&bufferptr[ret], bufferlen - ret, src, grouplen);
1977 4 ret += len;
1978
1979
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (ndx != count)
1980 {
1981 1 bufferptr[ret] = ' ';
1982 1 ++ret;
1983 }
1984
1985 4 src += mod;
1986 4 srclen -= mod;
1987 }
1988 11 return ret;
1989 }
1990
1991 290 gate_size_t gate_str_print_real(gate_char8_t* dst, gate_size_t dstlen, gate_real64_t num,
1992 unsigned intlen, unsigned decimallen, unsigned grouplen)
1993 {
1994 290 gate_size_t ret = 0;
1995 gate_real64_t decimalnum;
1996 gate_real64_t factor;
1997 int infini;
1998 gate_char8_t intbuffer[64];
1999 gate_char8_t decimalbuffer[64];
2000 gate_char8_t buffer[64];
2001 290 gate_char8_t* bufferptr = &buffer[0];
2002 290 gate_size_t bufferlen = sizeof(buffer) - 1;
2003
2004 290 gate_size_t intcount = 0;
2005 290 gate_size_t decimalcount = 0;
2006 290 gate_size_t bufferused = 0;
2007 gate_size_t ndx;
2008
2009
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 290 times.
290 if (dstlen < 3)
2010 {
2011 return 0;
2012 }
2013
2014
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 290 times.
290 if (gate_math_isnan(num))
2015 {
2016 return gate_str_print_text(dst, dstlen, "NaN", 3);
2017 }
2018 290 infini = gate_math_isinfinite(num);
2019
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 290 times.
290 if (infini > 0)
2020 {
2021 return gate_str_print_text(dst, dstlen, "Inf+", 4);
2022 }
2023
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 290 times.
290 else if (infini < 0)
2024 {
2025 return gate_str_print_text(dst, dstlen, "Inf-", 4);
2026 }
2027
2028
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 290 times.
290 if (gate_math_iszero(num))
2029 {
2030 intbuffer[0] = '0';
2031 intcount = 1;
2032 }
2033 else
2034 {
2035
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 288 times.
290 if (num < 0.0)
2036 {
2037 2 *bufferptr = '-';
2038 2 ++bufferptr;
2039 2 --bufferlen;
2040 2 num = -num;
2041 }
2042
2043 290 intcount = gate_str_print_uint64(intbuffer, sizeof(intbuffer) - 1, (gate_uint64_t)num);
2044 290 decimalnum = num - (gate_real64_t)(gate_int64_t)num;
2045 290 factor = 1.0;
2046
3/4
✓ Branch 1 taken 1391 times.
✓ Branch 2 taken 290 times.
✓ Branch 3 taken 1391 times.
✗ Branch 4 not taken.
1681 while (!gate_math_iszerof((gate_real32_t)(decimalnum * factor)) && (decimalcount < sizeof(decimalbuffer) - 1))
2047 {
2048 1391 factor *= 0.1;
2049 1391 decimalnum *= 10.0;
2050 1391 decimalbuffer[decimalcount] = '0' + (gate_char8_t)(gate_uint64_t)decimalnum;
2051 1391 ++decimalcount;
2052 1391 decimalnum -= (gate_real64_t)(gate_int64_t)decimalnum;
2053 }
2054 }
2055
2056
2057
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 289 times.
290 if (intlen > 0)
2058 {
2059 1 ret = 0;
2060
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (intlen > intcount)
2061 {
2062 for (ndx = 0; ndx < (intlen - intcount); ++ndx)
2063 {
2064 *dst = '0';
2065 ++dst;
2066 --dstlen;
2067 ++ret;
2068 }
2069 ndx = gate_str_print_text(bufferptr, bufferlen, intbuffer, intcount);
2070 }
2071 else
2072 {
2073
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (grouplen != 0)
2074 {
2075 1 ndx = gate_str_print_grouped(bufferptr, bufferlen, &intbuffer[intcount - intlen], intlen, grouplen);
2076 }
2077 else
2078 {
2079 ndx = gate_str_print_text(bufferptr, bufferlen, &intbuffer[intcount - intlen], intlen);
2080 }
2081 }
2082 }
2083 else
2084 {
2085
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 279 times.
289 if (grouplen != 0)
2086 {
2087 10 ndx = gate_str_print_grouped(bufferptr, bufferlen, &intbuffer[0], intcount, grouplen);
2088 }
2089 else
2090 {
2091 279 ndx = gate_str_print_text(bufferptr, bufferlen, &intbuffer[0], intcount);
2092 }
2093 }
2094 290 bufferptr += ndx;
2095 290 bufferlen -= ndx;
2096 290 bufferused += ndx;
2097
2098
2099
2100
4/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 282 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 4 times.
290 if ((decimallen > 0) || (decimalcount > 0))
2101 {
2102 286 *bufferptr = '.';
2103 286 ++bufferptr;
2104 286 --bufferlen;
2105 286 ++bufferused;
2106
2107
2/2
✓ Branch 0 taken 282 times.
✓ Branch 1 taken 4 times.
286 if (decimallen > 0)
2108 {
2109
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 192 times.
282 if (decimallen > decimalcount)
2110 {
2111 90 ndx = gate_str_print_text(bufferptr, bufferlen, &decimalbuffer[0], decimalcount);
2112
2/2
✓ Branch 0 taken 446 times.
✓ Branch 1 taken 90 times.
536 for (; ndx < decimallen; ++ndx)
2113 {
2114 446 bufferptr[ndx] = '0';
2115 }
2116 }
2117 else
2118 {
2119 192 ndx = gate_str_print_text(bufferptr, bufferlen, &decimalbuffer[0], decimallen);
2120 }
2121 }
2122 else
2123 {
2124 4 ndx = gate_str_print_text(bufferptr, bufferlen, &decimalbuffer[0], decimalcount);
2125 }
2126 286 bufferptr += ndx;
2127 286 bufferlen -= ndx;
2128 286 bufferused += ndx;
2129 }
2130
2131 290 return gate_str_print_text(dst, dstlen, &buffer[0], bufferused);
2132 }
2133
2134 392 gate_char8_t gate_str_print_hex_nibble(gate_uint8_t nibble, gate_bool_t uppercase)
2135 {
2136
2/2
✓ Branch 0 taken 349 times.
✓ Branch 1 taken 43 times.
392 if (nibble < 10)
2137 {
2138 349 return '0' + nibble;
2139 }
2140 else
2141 {
2142
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 20 times.
43 if (uppercase)
2143 {
2144 23 return 'A' + (nibble - 10);
2145 }
2146 else
2147 {
2148 20 return 'a' + (nibble - 10);
2149 }
2150 }
2151 }
2152
2153 190 gate_size_t gate_str_print_hex_byte(gate_char8_t* dst, gate_size_t dstlen, gate_uint8_t num, gate_bool_t uppercase)
2154 {
2155
2/4
✓ Branch 0 taken 190 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 190 times.
190 if ((dst == NULL) || (dstlen < 2))
2156 {
2157 return 0;
2158 }
2159 190 dst[0] = gate_str_print_hex_nibble((gate_uint8_t)((num >> 4) & 0x0f), uppercase);
2160 190 dst[1] = gate_str_print_hex_nibble((gate_uint8_t)((num) & 0x0f), uppercase);
2161
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 170 times.
190 if (dstlen > 2)
2162 {
2163 20 dst[2] = '\0';
2164 }
2165 190 return 2;
2166 }
2167 49 gate_size_t gate_str_print_hex(gate_char8_t* dst, gate_size_t dstlen, gate_uint64_t num, gate_size_t numlen, gate_bool_t uppercase)
2168 {
2169 49 gate_size_t cnt = 8;
2170 gate_uint8_t bytes[8];
2171 49 gate_uint8_t* ptr = &bytes[0];
2172
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
49 if (numlen < 8)
2173 {
2174 49 ptr += 8 - numlen;
2175 49 cnt -= 8 - numlen;
2176 }
2177 49 bytes[0] = (gate_uint8_t)((num >> 56) & 0xff);
2178 49 bytes[1] = (gate_uint8_t)((num >> 48) & 0xff);
2179 49 bytes[2] = (gate_uint8_t)((num >> 40) & 0xff);
2180 49 bytes[3] = (gate_uint8_t)((num >> 32) & 0xff);
2181 49 bytes[4] = (gate_uint8_t)((num >> 24) & 0xff);
2182 49 bytes[5] = (gate_uint8_t)((num >> 16) & 0xff);
2183 49 bytes[6] = (gate_uint8_t)((num >> 8) & 0xff);
2184 49 bytes[7] = (gate_uint8_t)((num) & 0xff);
2185
2/4
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 49 times.
49 if ((dst == NULL) || (dstlen < cnt * 2))
2186 {
2187 return 0;
2188 }
2189
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 15 times.
49 if (dstlen > cnt * 2)
2190 {
2191 34 dst[cnt * 2] = 0;
2192 }
2193 49 dstlen = cnt * 2;
2194
2/2
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 49 times.
151 while (cnt != 0)
2195 {
2196 102 gate_str_print_hex_byte(dst, 2, *ptr, uppercase);
2197 102 dst += 2;
2198 102 ++ptr;
2199 102 --cnt;
2200 }
2201 49 return dstlen;
2202 }
2203 4 gate_size_t gate_str_print_hex_num(gate_char8_t* dst, gate_size_t dstlen, gate_uint64_t num, gate_bool_t uppercase)
2204 {
2205 4 gate_size_t numlen = 8;
2206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (num < 256) { numlen = 1; }
2207
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 else if (num < 65536) { numlen = 2; }
2208 else if (num < 16777216) { numlen = 3; }
2209 else if (num < 4294967296) { numlen = 4; }
2210 else if (num < 4294967296 * 256) { numlen = 5; }
2211 else if (num < 4294967296 * 256 * 256) { numlen = 6; }
2212 else if (num < 4294967296 * 256 * 256 * 256) { numlen = 7; }
2213
2214 4 return gate_str_print_hex(dst, dstlen, num, numlen, uppercase);
2215 }
2216 34 gate_size_t gate_str_print_hex_uint16(gate_char8_t* dst, gate_size_t dstlen, gate_uint16_t num, gate_bool_t uppercase)
2217 {
2218 34 return gate_str_print_hex(dst, dstlen, num, 2, uppercase);
2219 }
2220 5 gate_size_t gate_str_print_hex_uint32(gate_char8_t* dst, gate_size_t dstlen, gate_uint32_t num, gate_bool_t uppercase)
2221 {
2222 5 return gate_str_print_hex(dst, dstlen, num, 4, uppercase);
2223 }
2224 gate_size_t gate_str_print_hex_uint64(gate_char8_t* dst, gate_size_t dstlen, gate_uint64_t num, gate_bool_t uppercase)
2225 {
2226 return gate_str_print_hex(dst, dstlen, num, 8, uppercase);
2227 }
2228
2229 gate_size_t gate_str_print_hex_buffer(gate_char8_t* dst, gate_size_t dstlen, void const* buffer, gate_size_t bufferlen, gate_bool_t uppercase)
2230 {
2231 gate_size_t ret = 0;
2232 gate_uint8_t const* ptr = (gate_uint8_t const*)buffer;
2233 while ((dstlen > 1) && (bufferlen != 0))
2234 {
2235 gate_str_print_hex_byte(dst, dstlen, *ptr, uppercase);
2236 dst += 2;
2237 dstlen -= 2;
2238 ret += 2;
2239 ++ptr;
2240 --bufferlen;
2241 }
2242 if (dstlen != 0)
2243 {
2244 *dst = 0;
2245 }
2246 return ret;
2247 }
2248
2249 101 gate_size_t gate_str_print_to(gate_str_printer_t printer, void* printer_param, va_list vl)
2250 {
2251 101 gate_size_t used = 0;
2252 101 gate_bool_t continue_loop = true;
2253 char buffer[512];
2254
2255 /* INFO: types small than [int] are converted to [int]
2256 https://stackoverflow.com/questions/23983471/char-is-promoted-to-int-when-passed-through-in-c
2257 */
2258
2/2
✓ Branch 0 taken 638 times.
✓ Branch 1 taken 101 times.
739 while (continue_loop)
2259 {
2260 638 int print_type = va_arg(vl, int);
2261 638 char const* ptr_text = NULL;
2262 638 gate_size_t text_len = 0;
2263
2264
11/25
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 18 times.
✓ Branch 6 taken 277 times.
✓ Branch 7 taken 96 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 16 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 26 times.
✓ Branch 14 taken 6 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 66 times.
✓ Branch 17 taken 16 times.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
638 switch (print_type)
2265 {
2266 101 case GATE_PRINT_END:
2267 {
2268 101 text_len = 0;
2269 101 continue_loop = false;
2270 101 break;
2271 }
2272 3 case GATE_PRINT_CHAR:
2273 {
2274 3 buffer[0] = (char)va_arg(vl, int);
2275 3 ptr_text = &buffer[0];
2276 3 text_len = 1;
2277 3 break;
2278 }
2279 13 case GATE_PRINT_NEWLINE:
2280 {
2281 13 ptr_text = GATE_STR_NEWLINE;
2282 13 text_len = GATE_STR_NEWLINE_LENGTH;
2283 13 break;
2284 }
2285 case GATE_PRINT_CR:
2286 {
2287 ptr_text = GATE_STR_CR;
2288 text_len = 1;
2289 break;
2290 }
2291 case GATE_PRINT_LF:
2292 {
2293 ptr_text = GATE_STR_LF;
2294 text_len = 1;
2295 break;
2296 }
2297 18 case GATE_PRINT_CRLF:
2298 {
2299 18 ptr_text = GATE_STR_CRLF;
2300 18 text_len = 2;
2301 18 break;
2302 }
2303 277 case GATE_PRINT_CSTR:
2304 {
2305 277 char const* val_cstr = va_arg(vl, char const*);
2306 277 ptr_text = val_cstr;
2307 277 text_len = gate_str_length(val_cstr);
2308 277 break;
2309 }
2310 96 case GATE_PRINT_STRING:
2311 {
2312 96 gate_string_t const* val_str = va_arg(vl, gate_string_t const*);
2313 96 ptr_text = gate_string_ptr(val_str, 0);
2314 96 text_len = gate_string_length(val_str);
2315 96 break;
2316 }
2317 case GATE_PRINT_BOOL:
2318 {
2319 gate_bool_t val_bool = (gate_bool_t)va_arg(vl, int);
2320 ptr_text = val_bool ? "true" : "false";
2321 text_len = val_bool ? 4 : 5;
2322 break;
2323 }
2324 case GATE_PRINT_I8:
2325 {
2326 gate_int8_t val_i8 = (gate_int8_t)va_arg(vl, int);
2327 text_len = gate_str_print_int16(buffer, sizeof(buffer), val_i8);
2328 ptr_text = buffer;
2329 break;
2330 }
2331 16 case GATE_PRINT_UI8:
2332 {
2333 16 gate_uint8_t val_ui8 = (gate_uint8_t)va_arg(vl, int);
2334 16 text_len = gate_str_print_uint16(buffer, sizeof(buffer), val_ui8);
2335 16 ptr_text = buffer;
2336 16 break;
2337 }
2338 case GATE_PRINT_I16:
2339 {
2340 gate_int16_t val_i16 = (gate_int16_t)va_arg(vl, int);
2341 text_len = gate_str_print_int16(buffer, sizeof(buffer), val_i16);
2342 ptr_text = buffer;
2343 break;
2344 }
2345 case GATE_PRINT_UI16:
2346 {
2347 gate_uint16_t val_ui16 = (gate_uint16_t)va_arg(vl, int);
2348 text_len = gate_str_print_uint16(buffer, sizeof(buffer), val_ui16);
2349 ptr_text = buffer;
2350 break;
2351 }
2352 26 case GATE_PRINT_I32:
2353 {
2354 26 gate_int32_t val_i32 = va_arg(vl, gate_int32_t);
2355 26 text_len = gate_str_print_int32(buffer, sizeof(buffer), val_i32);
2356 26 ptr_text = buffer;
2357 26 break;
2358 }
2359 6 case GATE_PRINT_UI32:
2360 {
2361 6 gate_uint32_t val_ui32 = va_arg(vl, gate_uint32_t);
2362 6 text_len = gate_str_print_uint32(buffer, sizeof(buffer), val_ui32);
2363 6 ptr_text = buffer;
2364 6 break;
2365 }
2366 case GATE_PRINT_I64:
2367 {
2368 gate_int64_t val_i64 = va_arg(vl, gate_int64_t);
2369 text_len = gate_str_print_int64(buffer, sizeof(buffer), val_i64);
2370 ptr_text = buffer;
2371 break;
2372 }
2373 66 case GATE_PRINT_UI64:
2374 {
2375 66 gate_uint64_t val_ui64 = va_arg(vl, gate_uint64_t);
2376 66 text_len = gate_str_print_uint64(buffer, sizeof(buffer), val_ui64);
2377 66 ptr_text = buffer;
2378 66 break;
2379 }
2380 16 case GATE_PRINT_H8:
2381 {
2382 16 gate_uint8_t val_ui8 = (gate_uint8_t)va_arg(vl, unsigned);
2383 16 text_len = gate_str_print_hex_byte(buffer, sizeof(buffer), val_ui8, false);
2384 16 ptr_text = buffer;
2385 16 break;
2386 }
2387 case GATE_PRINT_H16:
2388 {
2389 gate_uint16_t val_ui16 = (gate_uint16_t)va_arg(vl, unsigned);
2390 text_len = gate_str_print_hex(buffer, sizeof(buffer), val_ui16, 2, false);
2391 ptr_text = buffer;
2392 break;
2393 }
2394 case GATE_PRINT_H32:
2395 {
2396 gate_uint32_t val_ui32 = va_arg(vl, gate_uint32_t);
2397 text_len = gate_str_print_hex(buffer, sizeof(buffer), val_ui32, 4, false);
2398 ptr_text = buffer;
2399 break;
2400 }
2401 case GATE_PRINT_H64:
2402 {
2403 gate_uint64_t val_ui64 = va_arg(vl, gate_uint64_t);
2404 text_len = gate_str_print_hex(buffer, sizeof(buffer), val_ui64, 8, false);
2405 ptr_text = buffer;
2406 break;
2407 }
2408 case GATE_PRINT_R32:
2409 {
2410 gate_real32_t val_r32 = (gate_real32_t)va_arg(vl, double);
2411 text_len = gate_str_print_real(buffer, sizeof(buffer), val_r32, 0, 6, 0);
2412 ptr_text = buffer;
2413 break;
2414 }
2415 case GATE_PRINT_R64:
2416 {
2417 gate_real64_t val_r64 = va_arg(vl, gate_real64_t);
2418 text_len = gate_str_print_real(buffer, sizeof(buffer), val_r64, 0, 6, 0);
2419 ptr_text = buffer;
2420 break;
2421 }
2422 case GATE_PRINT_ADDRESS:
2423 {
2424 void* val_ptr = va_arg(vl, void*);
2425 text_len = gate_str_print_hex(buffer, sizeof(buffer), (gate_uint64_t)(gate_uintptr_t)val_ptr, sizeof(val_ptr) * 2, true);
2426 ptr_text = buffer;
2427 break;
2428 }
2429 default:
2430 {
2431 text_len = 0;
2432 continue_loop = false;
2433 break;
2434 }
2435 }
2436
2/2
✓ Branch 0 taken 537 times.
✓ Branch 1 taken 101 times.
638 if (text_len > 0)
2437 {
2438 537 gate_size_t chars_printed = printer(printer_param, ptr_text, text_len);
2439 537 used += chars_printed;
2440 }
2441 }
2442 101 return used;
2443 }
2444
2445 struct gate_str_printer_param
2446 {
2447 gate_char8_t* dst;
2448 gate_size_t dstlen;
2449 };
2450
2451 119 static gate_size_t gate_str_printer_callback(void* printer_param, gate_char8_t const* data, gate_size_t data_len)
2452 {
2453 119 struct gate_str_printer_param* param = (struct gate_str_printer_param*)printer_param;
2454 119 gate_size_t len_printed = gate_str_print_text(param->dst, param->dstlen, data, data_len);
2455 119 param->dst += len_printed;
2456 119 param->dstlen -= len_printed;
2457 119 return len_printed;
2458 }
2459
2460 42 gate_size_t gate_str_print(gate_char8_t* dst, gate_size_t dstlen, ...)
2461 {
2462 struct gate_str_printer_param param;
2463 42 gate_size_t ret = 0;
2464 va_list vl;
2465
2466 42 va_start(vl, dstlen);
2467
2468 42 param.dst = dst;
2469 42 param.dstlen = dstlen;
2470 42 ret = gate_str_print_to(&gate_str_printer_callback, &param, vl);
2471
2472 42 va_end(vl);
2473 42 return ret;
2474 }
2475
2476 gate_size_t gate_str_print_values(gate_char8_t* dst, gate_size_t dstlen, gate_print_value_t const* values, gate_size_t values_count)
2477 {
2478 gate_size_t used = 0;
2479 gate_bool_t continue_loop = true;
2480 gate_bool_t val_bool;
2481 gate_string_t const* val_str;
2482 gate_int8_t val_i8;
2483 gate_uint8_t val_ui8;
2484 gate_int16_t val_i16;
2485 gate_uint16_t val_ui16;
2486 gate_int32_t val_i32;
2487 gate_uint32_t val_ui32;
2488 gate_int64_t val_i64;
2489 gate_uint64_t val_ui64;
2490 gate_real32_t val_r32;
2491 gate_real64_t val_r64;
2492 char const* ptr_text = NULL;
2493 char buffer[512];
2494 gate_size_t text_len;
2495 gate_size_t chars_printed;
2496
2497 while ((values_count-- != 0) && continue_loop)
2498 {
2499 ptr_text = NULL;
2500 text_len = 0;
2501 switch (values->value_type)
2502 {
2503 case GATE_PRINT_END:
2504 {
2505 text_len = 0;
2506 continue_loop = false;
2507 break;
2508 }
2509 case GATE_PRINT_CHAR:
2510 {
2511 ptr_text = (char const*)values->value_ptr;
2512 text_len = 1;
2513 break;
2514 }
2515 case GATE_PRINT_NEWLINE:
2516 {
2517 ptr_text = GATE_STR_NEWLINE;
2518 text_len = GATE_STR_NEWLINE_LENGTH;
2519 break;
2520 }
2521 case GATE_PRINT_CR:
2522 {
2523 ptr_text = GATE_STR_CR;
2524 text_len = 1;
2525 break;
2526 }
2527 case GATE_PRINT_LF:
2528 {
2529 ptr_text = GATE_STR_LF;
2530 text_len = 1;
2531 break;
2532 }
2533 case GATE_PRINT_CRLF:
2534 {
2535 ptr_text = GATE_STR_CRLF;
2536 text_len = 2;
2537 break;
2538 }
2539 case GATE_PRINT_CSTR:
2540 {
2541 ptr_text = (char const*)values->value_ptr;
2542 text_len = gate_str_length(ptr_text);
2543 break;
2544 }
2545 case GATE_PRINT_STRING:
2546 {
2547 val_str = (gate_string_t const*)values->value_ptr;
2548 ptr_text = val_str->str;
2549 text_len = val_str->length;
2550 break;
2551 }
2552 case GATE_PRINT_BOOL:
2553 {
2554 val_bool = *(gate_bool_t const*)values->value_ptr;
2555 ptr_text = val_bool ? "true" : "false";
2556 text_len = val_bool ? 4 : 5;
2557 break;
2558 }
2559 case GATE_PRINT_I8:
2560 {
2561 val_i8 = *(gate_int8_t const*)values->value_ptr;
2562 text_len = gate_str_print_int16(buffer, sizeof(buffer), val_i8);
2563 ptr_text = buffer;
2564 break;
2565 }
2566 case GATE_PRINT_UI8:
2567 {
2568 val_ui8 = *(gate_uint8_t const*)values->value_ptr;
2569 text_len = gate_str_print_uint16(buffer, sizeof(buffer), val_ui8);
2570 ptr_text = buffer;
2571 break;
2572 }
2573 case GATE_PRINT_I16:
2574 {
2575 val_i16 = *(gate_int16_t const*)values->value_ptr;
2576 text_len = gate_str_print_int16(buffer, sizeof(buffer), val_i16);
2577 ptr_text = buffer;
2578 break;
2579 }
2580 case GATE_PRINT_UI16:
2581 {
2582 val_ui16 = *(gate_uint16_t const*)values->value_ptr;
2583 text_len = gate_str_print_uint16(buffer, sizeof(buffer), val_ui16);
2584 ptr_text = buffer;
2585 break;
2586 }
2587 case GATE_PRINT_I32:
2588 {
2589 val_i32 = *(gate_int32_t const*)values->value_ptr;
2590 text_len = gate_str_print_int32(buffer, sizeof(buffer), val_i32);
2591 ptr_text = buffer;
2592 break;
2593 }
2594 case GATE_PRINT_UI32:
2595 {
2596 val_ui32 = *(gate_uint32_t const*)values->value_ptr;
2597 text_len = gate_str_print_uint32(buffer, sizeof(buffer), val_ui32);
2598 ptr_text = buffer;
2599 break;
2600 }
2601 case GATE_PRINT_I64:
2602 {
2603 val_i64 = *(gate_int64_t const*)values->value_ptr;
2604 text_len = gate_str_print_int64(buffer, sizeof(buffer), val_i64);
2605 ptr_text = buffer;
2606 break;
2607 }
2608 case GATE_PRINT_UI64:
2609 {
2610 val_ui64 = *(gate_uint64_t const*)values->value_ptr;
2611 text_len = gate_str_print_uint64(buffer, sizeof(buffer), val_ui64);
2612 ptr_text = buffer;
2613 break;
2614 }
2615 case GATE_PRINT_H8:
2616 {
2617 val_ui8 = *(gate_uint8_t const*)values->value_ptr;
2618 text_len = gate_str_print_hex(buffer, sizeof(buffer), val_ui8, 1, false);
2619 ptr_text = buffer;
2620 break;
2621 }
2622 case GATE_PRINT_H16:
2623 {
2624 val_ui16 = *(gate_uint16_t const*)values->value_ptr;
2625 text_len = gate_str_print_hex(buffer, sizeof(buffer), val_ui16, 2, false);
2626 ptr_text = buffer;
2627 break;
2628 }
2629 case GATE_PRINT_H32:
2630 {
2631 val_ui32 = *(gate_uint32_t const*)values->value_ptr;
2632 text_len = gate_str_print_hex(buffer, sizeof(buffer), val_ui32, 4, false);
2633 ptr_text = buffer;
2634 break;
2635 }
2636 case GATE_PRINT_H64:
2637 {
2638 val_ui64 = *(gate_uint64_t const*)values->value_ptr;
2639 text_len = gate_str_print_hex(buffer, sizeof(buffer), val_ui64, 8, false);
2640 ptr_text = buffer;
2641 break;
2642 }
2643
2644 case GATE_PRINT_R32:
2645 {
2646 val_r32 = *(gate_real32_t const*)values->value_ptr;
2647 text_len = gate_str_print_real(buffer, sizeof(buffer), val_r32, 0, 6, 0);
2648 ptr_text = buffer;
2649 break;
2650 }
2651 case GATE_PRINT_R64:
2652 {
2653 val_r64 = *(gate_real64_t const*)values->value_ptr;
2654 text_len = gate_str_print_real(buffer, sizeof(buffer), val_r64, 0, 6, 0);
2655 ptr_text = buffer;
2656 break;
2657 }
2658 case GATE_PRINT_ADDRESS:
2659 {
2660 text_len = gate_str_print_hex(buffer, sizeof(buffer), *(gate_uintptr_t*)values->value_ptr, sizeof(gate_uintptr_t) * 2, true);
2661 ptr_text = buffer;
2662 break;
2663 }
2664 default:
2665 {
2666 text_len = 0;
2667 continue_loop = false;
2668 break;
2669 }
2670 }
2671 if (text_len > 0)
2672 {
2673 chars_printed = gate_str_print_text(dst, dstlen, ptr_text, text_len);
2674 dst += chars_printed;
2675 dstlen -= chars_printed;
2676 used += chars_printed;
2677 }
2678 ++values;
2679 }
2680 return used;
2681 }
2682
2683
2684
2685
2686 1399 gate_bool_t gate_str_parse_digit(gate_char8_t const* src, gate_uint8_t* digit)
2687 {
2688 1399 gate_char8_t chr = *src;
2689
4/4
✓ Branch 0 taken 1281 times.
✓ Branch 1 taken 118 times.
✓ Branch 2 taken 1200 times.
✓ Branch 3 taken 81 times.
1399 if ((chr >= '0') && (chr <= '9'))
2690 {
2691 1200 *digit = chr - '0';
2692 1200 return true;
2693 }
2694 else
2695 {
2696 199 return false;
2697 }
2698 }
2699
2700 gate_size_t gate_str_parse_bool(gate_char8_t const* src, gate_size_t srclen, gate_bool_t* value)
2701 {
2702 if (srclen == 0)
2703 {
2704 *value = false;
2705 return 0;
2706 }
2707 if (gate_str_starts_with_ic(src, srclen, "false", 5))
2708 {
2709 *value = false;
2710 return 5;
2711 }
2712 if (gate_str_starts_with_ic(src, srclen, "off", 3))
2713 {
2714 *value = false;
2715 return 3;
2716 }
2717 if (gate_str_starts_with_ic(src, srclen, "0", 1))
2718 {
2719 *value = false;
2720 return 1;
2721 }
2722
2723 if (gate_str_starts_with_ic(src, srclen, "true", 4))
2724 {
2725 *value = true;
2726 return 4;
2727 }
2728 if (gate_str_starts_with_ic(src, srclen, "on", 2))
2729 {
2730 *value = true;
2731 return 2;
2732 }
2733 if (gate_str_starts_with_ic(src, srclen, "yes", 3))
2734 {
2735 *value = true;
2736 return 3;
2737 }
2738 if (gate_str_starts_with_ic(src, srclen, "1", 1))
2739 {
2740 *value = true;
2741 return 1;
2742 }
2743
2744 /* all other types of string will evaluate to true */
2745 *value = false;
2746 return srclen;
2747 }
2748
2749
2750 538 gate_size_t gate_str_parse_uint64(gate_char8_t const* src, gate_size_t srclen, gate_uint64_t* num)
2751 {
2752 538 gate_size_t ret = 0;
2753 gate_uint8_t digit;
2754 538 gate_uint64_t tmp = 0;
2755
2/2
✓ Branch 0 taken 1337 times.
✓ Branch 1 taken 348 times.
1685 while (srclen-- != 0)
2756 {
2757
2/2
✓ Branch 1 taken 190 times.
✓ Branch 2 taken 1147 times.
1337 if (!gate_str_parse_digit(src, &digit))
2758 {
2759 190 break;
2760 }
2761 1147 tmp *= 10;
2762 1147 tmp += (gate_uint64_t)digit;
2763 1147 ++src;
2764 1147 ++ret;
2765 }
2766
1/2
✓ Branch 0 taken 538 times.
✗ Branch 1 not taken.
538 if (num != NULL)
2767 {
2768 538 *num = tmp;
2769 }
2770
2771 538 return ret;
2772 }
2773 146 gate_size_t gate_str_parse_int64(gate_char8_t const* src, gate_size_t srclen, gate_int64_t* num)
2774 {
2775 146 gate_size_t ret = 0;
2776 gate_size_t tmpret;
2777 146 gate_uint64_t tmpnum = 0;
2778 146 gate_bool_t neg = false;
2779
2780 146 ret = gate_str_find_first_not_of(src, srclen, GATE_STR_WHITESPACES, GATE_STR_WHITESPACES_LENGTH, 0);
2781
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 137 times.
146 if (ret == GATE_STR_NPOS)
2782 {
2783 9 return 0;
2784 }
2785 137 src += ret;
2786 137 srclen -= ret;
2787
2788
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 135 times.
137 if (*src == '-')
2789 {
2790 2 neg = true;
2791 2 --srclen;
2792 2 ++src;
2793 2 ++ret;
2794 }
2795
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 135 times.
135 else if (*src == '+')
2796 {
2797 --srclen;
2798 ++src;
2799 ++ret;
2800 }
2801
1/2
✓ Branch 0 taken 137 times.
✗ Branch 1 not taken.
137 tmpret = gate_str_parse_uint64(src, srclen, num ? &tmpnum : NULL);
2802
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 119 times.
137 if (tmpret == 0)
2803 {
2804 18 return 0;
2805 }
2806 119 ret += tmpret;
2807
1/2
✓ Branch 0 taken 119 times.
✗ Branch 1 not taken.
119 if (num != NULL)
2808 {
2809
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 117 times.
119 if (neg)
2810 {
2811 2 *num = -((gate_int64_t)tmpnum);
2812 }
2813 else
2814 {
2815 117 *num = (gate_int64_t)tmpnum;
2816 }
2817 }
2818 119 return ret;
2819 }
2820
2821
2822 38 gate_size_t gate_str_parse_real(gate_char8_t const* src, gate_size_t srclen, gate_real64_t* num)
2823 {
2824 38 gate_int64_t int_part = 0;
2825 gate_uint8_t digit;
2826 38 gate_size_t ret = gate_str_parse_int64(src, srclen, &int_part);
2827 38 gate_bool_t const is_negative = int_part < 0;
2828 38 gate_real64_t a = 0.0;
2829
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 37 times.
38 gate_real64_t b = is_negative ? -1.0 : 1.0;
2830 38 src += ret;
2831 38 srclen -= ret;
2832
2833
4/4
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 22 times.
38 if ((srclen > 0) && (src[0] == '.'))
2834 {
2835 14 ++ret;
2836 14 ++src;
2837 14 --srclen;
2838
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 5 times.
54 while (srclen > 0)
2839 {
2840
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 40 times.
49 if (!gate_str_parse_digit(src, &digit))
2841 {
2842 9 break;
2843 }
2844 40 ++src;
2845 40 ++ret;
2846 40 --srclen;
2847 40 a *= 10.0;
2848 40 b *= 10.0;
2849 40 a += (gate_real64_t)digit;
2850 }
2851 }
2852
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 if (num)
2853 {
2854 38 *num = (gate_real64_t)int_part + a / b;
2855 }
2856 38 return ret;
2857 }
2858
2859 711 gate_bool_t gate_str_parse_hex_nibble(gate_char8_t chr, gate_uint8_t* value)
2860 {
2861
3/4
✓ Branch 0 taken 711 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 331 times.
✓ Branch 3 taken 380 times.
711 if ((chr >= '0') && (chr <= '9'))
2862 {
2863 331 *value = (gate_uint8_t)(chr - '0');
2864 331 return true;
2865 }
2866
4/4
✓ Branch 0 taken 366 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 349 times.
✓ Branch 3 taken 17 times.
380 else if ((chr >= 'A') && (chr <= 'F'))
2867 {
2868 349 *value = (gate_uint8_t)(chr - 'A' + 10);
2869 349 return true;
2870 }
2871
3/4
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
31 else if ((chr >= 'a') && (chr <= 'f'))
2872 {
2873 17 *value = (gate_uint8_t)(chr - 'a' + 10);
2874 17 return true;
2875 }
2876 else
2877 {
2878 14 return false;
2879 }
2880 }
2881 38 gate_bool_t gate_str_parse_hex_byte(gate_char8_t const chr[2], gate_uint8_t* value)
2882 {
2883 gate_uint8_t n1, n2;
2884
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
38 if (!gate_str_parse_hex_nibble(chr[0], &n1))
2885 {
2886 return false;
2887 }
2888
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
38 if (!gate_str_parse_hex_nibble(chr[1], &n2))
2889 {
2890 return false;
2891 }
2892 38 *value = n1 * 16 + n2;
2893 38 return true;
2894 }
2895 30 gate_size_t gate_str_parse_hex_int(gate_char8_t const* text, gate_size_t textlen, gate_uint64_t* num)
2896 {
2897 30 gate_size_t ret = 0;
2898 30 gate_uint64_t val = 0;
2899 gate_uint8_t n;
2900
2901
2/2
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 16 times.
111 while (textlen-- != 0)
2902 {
2903
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 81 times.
95 if (!gate_str_parse_hex_nibble(*text, &n))
2904 {
2905 14 break;
2906 }
2907 81 val *= 16;
2908 81 val += n;
2909 81 ++ret;
2910 81 ++text;
2911 }
2912
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (num != NULL)
2913 {
2914 30 *num = val;
2915 }
2916 30 return ret;
2917 }
2918 gate_size_t gate_str_parse_hex_buffer(gate_char8_t const* text, gate_size_t textlen, gate_char8_t* buffer, gate_size_t bufferlen)
2919 {
2920 gate_size_t ret = 0;
2921 gate_uint8_t tmp;
2922
2923 while ((textlen >= 2) && (bufferlen != 0))
2924 {
2925 if (!gate_str_parse_hex_byte(text, &tmp))
2926 {
2927 break;
2928 }
2929 *buffer = (char)tmp;
2930
2931 text += 2;
2932 textlen -= 2;
2933
2934 ++buffer;
2935 --bufferlen;
2936
2937 ++ret;
2938 }
2939 return ret;
2940 }
2941 gate_bool_t gate_str_parse_oct_byte(gate_char8_t const chr[3], gate_uint8_t* value)
2942 {
2943 gate_uint8_t digits[3] = GATE_INIT_EMPTY;
2944 if (gate_str_parse_digit(&chr[0], &digits[0])
2945 && gate_str_parse_digit(&chr[1], &digits[1])
2946 && gate_str_parse_digit(&chr[2], &digits[2])
2947 )
2948 {
2949 *value = (gate_uint8_t)((int)digits[0] * 64 + (int)digits[1] * 8 + (int)digits[2]);
2950 return true;
2951 }
2952 return false;
2953 }
2954 156 gate_size_t gate_str_parse_line(gate_char8_t const* text, gate_size_t textlen, gate_char8_t* buffer, gate_size_t* bufferlen)
2955 {
2956 gate_size_t pos;
2957
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 if (textlen == 0)
2958 {
2959 if ((buffer != NULL) && (bufferlen != NULL))
2960 {
2961 if (*bufferlen > 0)
2962 {
2963 buffer[0] = 0;
2964 *bufferlen = 0;
2965 }
2966 }
2967 return 0;
2968 }
2969 else
2970 {
2971 156 pos = gate_str_char_pos(text, textlen, '\n', 0);
2972
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 if (pos == GATE_STR_NPOS)
2973 {
2974 if ((buffer != NULL) && (bufferlen != NULL))
2975 {
2976 if (*bufferlen < textlen)
2977 {
2978 gate_mem_copy(buffer, text, *bufferlen);
2979 buffer[*bufferlen] = 0;
2980 return *bufferlen;
2981 }
2982 else
2983 {
2984 gate_mem_copy(buffer, text, textlen);
2985 if (*bufferlen > textlen)
2986 {
2987 buffer[textlen] = 0;
2988 }
2989 *bufferlen = textlen;
2990 }
2991 }
2992
2993 return textlen;
2994 }
2995 else
2996 {
2997
2/4
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 156 times.
✗ Branch 3 not taken.
156 if ((buffer != NULL) && (bufferlen != NULL))
2998 {
2999
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 if (*bufferlen < pos)
3000 {
3001 gate_mem_copy(buffer, text, *bufferlen);
3002 return *bufferlen;
3003 }
3004 else
3005 {
3006 156 gate_mem_copy(buffer, text, pos);
3007
1/2
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
156 if (*bufferlen > pos)
3008 {
3009 156 buffer[pos] = 0;
3010 }
3011 156 *bufferlen = pos;
3012
1/2
✓ Branch 0 taken 156 times.
✗ Branch 1 not taken.
156 if (pos > 0)
3013 {
3014
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 156 times.
156 if (buffer[pos - 1] == '\r')
3015 {
3016 buffer[pos - 1] = 0;
3017 --*bufferlen;
3018 }
3019 }
3020 }
3021 }
3022 156 return pos + 1;
3023 }
3024 }
3025 }
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036 /************************
3037 * String implementation *
3038 *************************/
3039
3040 2370 gate_string_t* gate_string_create(gate_string_t* obj, gate_char8_t const* str, gate_size_t len)
3041 {
3042
2/2
✓ Branch 0 taken 1611 times.
✓ Branch 1 taken 759 times.
2370 if (len > 0)
3043 {
3044 1611 obj->buffer = gate_mem_alloc(sizeof(gate_stringbuffer_t) + len + 1);
3045
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1611 times.
1611 if (obj->buffer == NULL)
3046 {
3047 return NULL;
3048 }
3049 1611 gate_mem_copy(&obj->buffer->data[0], str, len);
3050 1611 obj->buffer->data[len] = 0;
3051 1611 obj->str = &obj->buffer->data[0];
3052 1611 obj->length = len;
3053 1611 gate_atomic_int_init(&obj->buffer->refcount, 1);
3054 }
3055 else
3056 {
3057 759 gate_string_create_empty(obj);
3058 }
3059
3060 2370 return obj;
3061 }
3062 4768 gate_string_t* gate_string_create_static_len(gate_string_t* obj, gate_char8_t const* str, gate_size_t len)
3063 {
3064 4768 obj->str = str;
3065 4768 obj->length = len;
3066 4768 obj->buffer = NULL;
3067 4768 return obj;
3068 }
3069 1906 gate_string_t* gate_string_create_static(gate_string_t* obj, gate_char8_t const* str)
3070 {
3071
2/2
✓ Branch 0 taken 1890 times.
✓ Branch 1 taken 16 times.
1906 return gate_string_create_static_len(obj, str, (str == NULL) ? 0 : gate_str_length(str));
3072 }
3073 4453 gate_string_t* gate_string_create_empty(gate_string_t* obj)
3074 {
3075 4453 obj->str = NULL;
3076 4453 obj->length = 0;
3077 4453 obj->buffer = NULL;
3078 4453 return obj;
3079 }
3080 gate_string_t* gate_string_create_utf16(gate_string_t* obj, gate_char16_t const* str, gate_size_t len)
3081 {
3082 gate_char32_t chr32;
3083 gate_size_t processed16 = 0;
3084 gate_size_t decoded;
3085 gate_char8_t buffer[16];
3086 gate_size_t encoded;
3087 gate_size_t required8 = 0;
3088 gate_size_t used8 = 0;
3089 char* ptr = NULL;
3090
3091 while (processed16 < len)
3092 {
3093 decoded = gate_char_read_utf16(&str[processed16], len - processed16, &chr32);
3094 if (decoded == 0)
3095 {
3096 break;
3097 }
3098
3099 processed16 += decoded;
3100 encoded = gate_char_write_utf8(chr32, buffer, sizeof(buffer));
3101 required8 += encoded;
3102 }
3103
3104 if (required8 == 0)
3105 {
3106 gate_string_create_empty(obj);
3107 }
3108 else
3109 {
3110 obj->buffer = gate_mem_alloc(sizeof(gate_stringbuffer_t) + required8 + 1);
3111 if (obj->buffer == NULL)
3112 {
3113 return NULL;
3114 }
3115 processed16 = 0;
3116 ptr = &obj->buffer->data[0];
3117 while ((processed16 < len) && (used8 < required8))
3118 {
3119 decoded = gate_char_read_utf16(&str[processed16], len - processed16, &chr32);
3120 if (decoded == 0)
3121 {
3122 break;
3123 }
3124
3125 processed16 += decoded;
3126 encoded = gate_char_write_utf8(chr32, ptr, required8 - used8);
3127 used8 += encoded;
3128 ptr += encoded;
3129 }
3130 *ptr = 0;
3131
3132 obj->str = &obj->buffer->data[0];
3133 obj->length = used8;
3134 gate_atomic_int_init(&obj->buffer->refcount, 1);
3135 }
3136
3137 return obj;
3138 }
3139 1 gate_string_t* gate_string_create_utf32(gate_string_t* obj, gate_char32_t const* str, gate_size_t len)
3140 {
3141 1 gate_size_t processed32 = 0;
3142 gate_char8_t buffer[16];
3143 gate_size_t encoded;
3144 1 gate_size_t required8 = 0;
3145 1 gate_size_t used8 = 0;
3146 1 char* ptr = NULL;
3147
3148
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 while (processed32 < len)
3149 {
3150 4 encoded = gate_char_write_utf8(str[processed32], buffer, sizeof(buffer));
3151 4 ++processed32;
3152 4 required8 += encoded;
3153 }
3154
3155 1 obj->buffer = gate_mem_alloc(sizeof(gate_stringbuffer_t) + required8 + 1);
3156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (obj->buffer == NULL)
3157 {
3158 return NULL;
3159 }
3160
3161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (required8 == 0)
3162 {
3163 gate_string_create_empty(obj);
3164 }
3165 else
3166 {
3167 1 processed32 = 0;
3168 1 ptr = &obj->buffer->data[0];
3169
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
5 while ((processed32 < len) && (used8 < required8))
3170 {
3171 4 encoded = gate_char_write_utf8(str[processed32], ptr, required8 - used8);
3172 4 ++processed32;
3173 4 used8 += encoded;
3174 4 ptr += encoded;
3175 }
3176 1 *ptr = 0;
3177
3178 1 obj->str = &obj->buffer->data[0];
3179 1 obj->length = used8;
3180 1 gate_atomic_int_init(&obj->buffer->refcount, 1);
3181 }
3182
3183 1 return obj;
3184
3185 }
3186 13 gate_string_t* gate_string_create_copy(gate_string_t* obj, gate_string_t const* src)
3187 {
3188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (src == NULL)
3189 {
3190 return gate_string_create_empty(obj);
3191 }
3192 else
3193 {
3194 13 return gate_string_create(obj, src->str, src->length);
3195 }
3196 }
3197
3198
3199
3200 24581 void gate_string_release(gate_string_t* obj)
3201 {
3202
2/2
✓ Branch 0 taken 15623 times.
✓ Branch 1 taken 8958 times.
24581 if (obj->buffer != NULL)
3203 {
3204
2/2
✓ Branch 1 taken 2371 times.
✓ Branch 2 taken 13252 times.
15623 if (gate_atomic_int_dec(&obj->buffer->refcount) == 0)
3205 {
3206 2371 gate_mem_dealloc(obj->buffer);
3207 }
3208 15623 obj->buffer = NULL;
3209 }
3210 24581 obj->str = NULL;
3211 24581 obj->length = 0;
3212 24581 }
3213
3214 4359 gate_bool_t gate_string_is_empty(gate_string_t const* obj)
3215 {
3216
2/2
✓ Branch 0 taken 4339 times.
✓ Branch 1 taken 20 times.
4359 if (obj != NULL)
3217 {
3218
2/2
✓ Branch 0 taken 3814 times.
✓ Branch 1 taken 525 times.
4339 if (obj->length > 0)
3219 {
3220 3814 return false;
3221 }
3222 }
3223 545 return true;
3224 }
3225
3226 7504 gate_string_t* gate_string_duplicate(gate_string_t* dst, gate_string_t const* src)
3227 {
3228
2/2
✓ Branch 0 taken 7282 times.
✓ Branch 1 taken 222 times.
7504 if (src != NULL)
3229 {
3230 7282 dst->buffer = src->buffer;
3231 7282 dst->str = src->str;
3232 7282 dst->length = src->length;
3233
2/2
✓ Branch 0 taken 6257 times.
✓ Branch 1 taken 1025 times.
7282 if (dst->buffer != NULL)
3234 {
3235 6257 gate_atomic_int_inc(&dst->buffer->refcount);
3236 }
3237 }
3238 else
3239 {
3240 222 dst->buffer = NULL;
3241 222 dst->str = NULL;
3242 222 dst->length = 0;
3243 }
3244 7504 return dst;
3245 }
3246 3371 gate_string_t* gate_string_clone(gate_string_t* dst, gate_string_t const* src)
3247 {
3248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3371 times.
3371 if (src == NULL)
3249 {
3250 return gate_string_create_empty(dst);
3251 }
3252 else
3253 {
3254
2/2
✓ Branch 0 taken 959 times.
✓ Branch 1 taken 2412 times.
3371 if (src->buffer == NULL)
3255 {
3256 959 return gate_string_create(dst, src->str, src->length);
3257 }
3258 else
3259 {
3260 2412 dst->str = src->str;
3261 2412 dst->length = src->length;
3262 2412 dst->buffer = src->buffer;
3263 2412 gate_atomic_int_inc(&dst->buffer->refcount);
3264 2412 return dst;
3265 }
3266 }
3267 }
3268
3269 32314 gate_size_t gate_string_length(gate_string_t const* obj)
3270 {
3271
1/2
✓ Branch 0 taken 32314 times.
✗ Branch 1 not taken.
32314 return obj ? obj->length : 0;
3272 }
3273 3 gate_bool_t gate_string_like(gate_string_t const* obj, gate_string_t const* pattern)
3274 {
3275 3 return gate_str_like(obj->str, obj->length, pattern->str, pattern->length);
3276 }
3277 1 gate_bool_t gate_string_like_one_of(gate_string_t const* obj, gate_string_t const* pattern, gate_char8_t separator)
3278 {
3279 1 return gate_str_like_one_of(obj->str, obj->length, pattern->str, pattern->length, separator);
3280 }
3281
3282
3283 180 gate_size_t gate_string_count_chars(gate_string_t const* obj, gate_char8_t chr)
3284 {
3285
1/2
✓ Branch 0 taken 180 times.
✗ Branch 1 not taken.
180 if (obj)
3286 {
3287 180 return gate_str_count_chars(obj->str, obj->length, chr);
3288 }
3289 return 0;
3290 }
3291 65 gate_char8_t gate_string_char_at(gate_string_t const* str, gate_size_t index)
3292 {
3293 65 gate_char8_t ret = '\0';
3294
2/4
✓ Branch 0 taken 65 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 65 times.
✗ Branch 3 not taken.
65 if (str && (index < str->length))
3295 {
3296 65 ret = str->str[index];
3297 }
3298 65 return ret;
3299 }
3300
3301 1316 gate_size_t gate_string_char_pos(gate_string_t const* obj, gate_char8_t find, gate_size_t startat)
3302 {
3303 1316 return gate_str_char_pos(obj->str, obj->length, find, startat);
3304 }
3305 81 gate_size_t gate_string_char_pos_last(gate_string_t const* obj, gate_char8_t find)
3306 {
3307 81 return gate_str_char_pos_last(obj->str, obj->length, find);
3308 }
3309 10 gate_size_t gate_string_find_first_of(gate_string_t const* obj, gate_string_t const* chars, gate_size_t startat)
3310 {
3311 10 return gate_str_find_first_of(obj->str, obj->length, chars->str, chars->length, startat);
3312 }
3313 76 gate_size_t gate_string_find_first_not_of(gate_string_t const* obj, gate_string_t const* chars, gate_size_t startat)
3314 {
3315 76 return gate_str_find_first_not_of(obj->str, obj->length, chars->str, chars->length, startat);
3316 }
3317 2 gate_size_t gate_string_find_last_of(gate_string_t const* obj, gate_string_t const* chars)
3318 {
3319 2 return gate_str_find_last_of(obj->str, obj->length, chars->str, chars->length);
3320 }
3321 24 gate_size_t gate_string_find_last_not_of(gate_string_t const* obj, gate_string_t const* chars)
3322 {
3323 24 return gate_str_find_last_not_of(obj->str, obj->length, chars->str, chars->length);
3324 }
3325
3326 60 gate_size_t gate_string_pos(gate_string_t const* obj, gate_string_t const* find, gate_size_t startat)
3327 {
3328 60 return gate_str_pos(obj->str, obj->length, find->str, find->length, startat);
3329 }
3330 2 gate_size_t gate_string_pos_last(gate_string_t const* obj, gate_string_t const* find)
3331 {
3332 2 return gate_str_pos_last(obj->str, obj->length, find->str, find->length);
3333 }
3334
3335
3336 35 gate_size_t gate_string_parse(gate_string_t const* obj, gate_string_t const* find, gate_size_t start_at,
3337 gate_string_t* head, gate_string_t* tail, gate_bool_t separator_as_tail)
3338 {
3339 35 gate_size_t pos = gate_string_pos(obj, find, start_at);
3340
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 26 times.
35 if (pos == GATE_STR_NPOS)
3341 {
3342 9 return 0; /* nothing parsed */
3343 }
3344 else
3345 {
3346
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 if (head)
3347 {
3348 26 gate_string_substr(head, obj, 0, pos);
3349 }
3350
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 if (tail)
3351 {
3352
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 19 times.
26 if (separator_as_tail)
3353 {
3354 7 gate_string_substr(tail, obj, pos, GATE_STR_NPOS);
3355 }
3356 else
3357 {
3358 19 gate_string_substr(tail, obj, pos + find->length, GATE_STR_NPOS);
3359 }
3360 }
3361 26 return pos + gate_string_length(find); /* (head + separator) parsed */
3362 }
3363 }
3364
3365 2 gate_size_t gate_string_parse_int(gate_string_t const* obj, gate_int64_t* num)
3366 {
3367 2 return gate_str_parse_int64(obj->str, obj->length, num);
3368 }
3369
3370 120 gate_size_t gate_string_parse_uint(gate_string_t const* obj, gate_uint64_t* num)
3371 {
3372 120 return gate_str_parse_uint64(obj->str, obj->length, num);
3373 }
3374
3375 gate_size_t gate_string_parse_real(gate_string_t const* obj, gate_real64_t* num)
3376 {
3377 return gate_str_parse_real(obj->str, obj->length, num);
3378 }
3379
3380
3381 gate_size_t gate_string_parse_hex(gate_string_t const* obj, gate_uint64_t* num)
3382 {
3383 return gate_str_parse_hex_int(obj->str, obj->length, num);
3384 }
3385
3386
3387 2824 gate_char8_t const* gate_string_ptr(gate_string_t const* obj, gate_size_t index)
3388 {
3389
3/4
✓ Branch 0 taken 2824 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2811 times.
✓ Branch 3 taken 13 times.
2824 if ((obj != NULL) && (obj->str != NULL))
3390 {
3391 2811 return obj->str + index;
3392 }
3393 else
3394 {
3395 13 return NULL;
3396 }
3397 }
3398 5078 gate_string_t* gate_string_substr(gate_string_t* dst, gate_string_t const* src, gate_size_t offset, gate_size_t length)
3399 {
3400
1/2
✓ Branch 0 taken 5078 times.
✗ Branch 1 not taken.
5078 if (dst != NULL)
3401 {
3402
5/6
✓ Branch 0 taken 5078 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5027 times.
✓ Branch 3 taken 51 times.
✓ Branch 4 taken 21 times.
✓ Branch 5 taken 5006 times.
5078 if ((src == NULL) || (offset >= src->length) || (length == 0))
3403 {
3404
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
144 if (src == dst)
3405 {
3406 48 gate_string_release(dst);
3407 }
3408 else
3409 {
3410 24 dst->str = NULL;
3411 24 dst->length = 0;
3412 24 dst->buffer = NULL;
3413 }
3414 }
3415 else
3416 {
3417
2/2
✓ Branch 0 taken 2705 times.
✓ Branch 1 taken 2301 times.
5006 if (length > src->length - offset)
3418 {
3419 2705 length = src->length - offset;
3420 }
3421
3422 5006 dst->buffer = src->buffer;
3423 5006 dst->str = &src->str[offset];
3424 5006 dst->length = length;
3425
3426
4/4
✓ Branch 0 taken 3922 times.
✓ Branch 1 taken 1084 times.
✓ Branch 2 taken 2973 times.
✓ Branch 3 taken 949 times.
5006 if ((dst->buffer != NULL) && (dst != src))
3427 {
3428 2973 gate_atomic_int_inc(&dst->buffer->refcount);
3429 }
3430 }
3431 }
3432 5078 return dst;
3433 }
3434
3435 6800 int gate_string_comp(gate_string_t const* src, gate_string_t const* dst)
3436 {
3437 6800 gate_char8_t const* ptr_src = NULL;
3438 6800 gate_size_t len_src = 0;
3439 6800 gate_char8_t const* ptr_dst = NULL;
3440 6800 gate_size_t len_dst = 0;
3441
3442
1/2
✓ Branch 0 taken 6800 times.
✗ Branch 1 not taken.
6800 if (src)
3443 {
3444 6800 ptr_src = src->str;
3445 6800 len_src = src->length;
3446 }
3447
2/2
✓ Branch 0 taken 6794 times.
✓ Branch 1 taken 6 times.
6800 if (dst)
3448 {
3449 6794 ptr_dst = dst->str;
3450 6794 len_dst = dst->length;
3451 }
3452
3453 6800 return gate_str_compare(ptr_src, len_src, ptr_dst, len_dst);
3454 }
3455
3456 497 int gate_string_comp_ic(gate_string_t const* src, gate_string_t const* dst)
3457 {
3458 497 gate_char8_t const* ptr_src = NULL;
3459 497 gate_size_t len_src = 0;
3460 497 gate_char8_t const* ptr_dst = NULL;
3461 497 gate_size_t len_dst = 0;
3462
3463
1/2
✓ Branch 0 taken 497 times.
✗ Branch 1 not taken.
497 if (src)
3464 {
3465 497 ptr_src = src->str;
3466 497 len_src = src->length;
3467 }
3468
1/2
✓ Branch 0 taken 497 times.
✗ Branch 1 not taken.
497 if (dst)
3469 {
3470 497 ptr_dst = dst->str;
3471 497 len_dst = dst->length;
3472 }
3473 497 return gate_str_compare_ic(ptr_src, len_src, ptr_dst, len_dst);
3474 }
3475 955 gate_size_t gate_string_to_buffer(gate_string_t const* src, gate_char8_t* buffer, gate_size_t bufferlength)
3476 {
3477 gate_size_t ret;
3478
2/4
✓ Branch 0 taken 955 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 955 times.
955 if ((buffer == NULL) || (bufferlength == 0))
3479 {
3480 ret = 0;
3481 }
3482 else
3483 {
3484
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 952 times.
955 if (gate_string_is_empty(src))
3485 {
3486 3 buffer[0] = 0;
3487 3 ret = 0;
3488 }
3489 else
3490 {
3491 952 ret = gate_str_print_text(buffer, bufferlength, src->str, src->length);
3492 }
3493 }
3494 955 return ret;
3495 }
3496
3497
3498 10562 gate_bool_t gate_string_starts_with(gate_string_t const* obj, gate_string_t const* token)
3499 {
3500 10562 gate_size_t objlen = gate_string_length(obj);
3501 10562 gate_size_t tokenlen = gate_string_length(token);
3502
3/4
✓ Branch 0 taken 10562 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 689 times.
✓ Branch 3 taken 9873 times.
10562 if ((tokenlen == 0) || (objlen < tokenlen))
3503 {
3504 689 return false;
3505 }
3506 9873 return (gate_str_compare(obj->str, tokenlen, token->str, tokenlen) == 0);
3507 }
3508 80 gate_bool_t gate_string_starts_with_str(gate_string_t const* obj, gate_char8_t const* text)
3509 {
3510 gate_string_t tmp;
3511 80 gate_string_create_static(&tmp, text);
3512 80 return gate_string_starts_with(obj, &tmp);
3513 }
3514 2804 gate_bool_t gate_string_starts_with_char(gate_string_t const* obj, gate_char8_t chr)
3515 {
3516 2804 gate_size_t objlen = gate_string_length(obj);
3517
4/4
✓ Branch 0 taken 2802 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1316 times.
✓ Branch 3 taken 1486 times.
2804 return (objlen >= 1) && (obj->str[0] == chr);
3518 }
3519 gate_bool_t gate_string_starts_with_ic(gate_string_t const* obj, gate_string_t const* token)
3520 {
3521 gate_size_t objlen = gate_string_length(obj);
3522 gate_size_t tokenlen = gate_string_length(token);
3523 if ((tokenlen == 0) || (objlen < tokenlen))
3524 {
3525 return false;
3526 }
3527 return (gate_str_compare_ic(obj->str, tokenlen, token->str, tokenlen) == 0);
3528 }
3529 gate_bool_t gate_string_starts_with_str_ic(gate_string_t const* obj, gate_char8_t const* text)
3530 {
3531 gate_string_t tmp;
3532 gate_string_create_static(&tmp, text);
3533 return gate_string_starts_with_ic(obj, &tmp);
3534 }
3535
3536 212 gate_bool_t gate_string_ends_with(gate_string_t const* obj, gate_string_t const* token)
3537 {
3538 212 gate_size_t objlen = gate_string_length(obj);
3539 212 gate_size_t tokenlen = gate_string_length(token);
3540
3/4
✓ Branch 0 taken 212 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 210 times.
212 if ((tokenlen == 0) || (objlen < tokenlen))
3541 {
3542 2 return false;
3543 }
3544 210 return (gate_str_compare(&obj->str[objlen - tokenlen], tokenlen, token->str, tokenlen) == 0);
3545 }
3546 191 gate_bool_t gate_string_ends_with_str(gate_string_t const* obj, gate_char8_t const* text)
3547 {
3548 gate_string_t tmp;
3549 191 gate_string_create_static(&tmp, text);
3550 191 return gate_string_ends_with(obj, &tmp);
3551 }
3552 431 gate_bool_t gate_string_ends_with_char(gate_string_t const* obj, gate_char8_t chr)
3553 {
3554 431 gate_size_t objlen = gate_string_length(obj);
3555
3/4
✓ Branch 0 taken 431 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 429 times.
431 return (objlen >= 1) && (obj->str[objlen - 1] == chr);
3556 }
3557 gate_bool_t gate_string_ends_with_ic(gate_string_t const* obj, gate_string_t const* token)
3558 {
3559 gate_size_t objlen = gate_string_length(obj);
3560 gate_size_t tokenlen = gate_string_length(token);
3561 if ((tokenlen == 0) || (objlen < tokenlen))
3562 {
3563 return false;
3564 }
3565 return (gate_str_compare_ic(&obj->str[objlen - tokenlen], tokenlen, token->str, tokenlen) == 0);
3566 }
3567 gate_bool_t gate_string_ends_with_str_ic(gate_string_t const* obj, gate_char8_t const* text)
3568 {
3569 gate_string_t tmp;
3570 gate_string_create_static(&tmp, text);
3571 return gate_string_ends_with_ic(obj, &tmp);
3572 }
3573
3574 1 gate_bool_t gate_string_contains(gate_string_t const* obj, gate_string_t const* token)
3575 {
3576 1 return gate_string_pos(obj, token, 0) != GATE_STR_NPOS;
3577 }
3578
3579 1345 gate_bool_t gate_string_equals(gate_string_t const* obj, gate_string_t const* token)
3580 {
3581 1345 return gate_string_comp(obj, token) == 0;
3582 }
3583 584 gate_bool_t gate_string_equals_str(gate_string_t const* obj, gate_char8_t const* token)
3584 {
3585 gate_string_t tmp;
3586 584 gate_string_create_static(&tmp, token);
3587 584 return gate_string_equals(obj, &tmp);
3588 }
3589 8 gate_bool_t gate_string_equals_ic(gate_string_t const* obj, gate_string_t const* token)
3590 {
3591 8 return gate_string_comp_ic(obj, token) == 0;
3592 }
3593 4 gate_bool_t gate_string_equals_str_ic(gate_string_t const* obj, gate_char8_t const* token)
3594 {
3595 gate_string_t tmp;
3596 4 gate_string_create_static(&tmp, token);
3597 4 return gate_string_equals_ic(obj, &tmp);
3598 }
3599 1952 gate_string_t* gate_string_ltrim(gate_string_t* dst, gate_string_t const* src)
3600 {
3601 1952 gate_size_t pos = gate_str_find_first_not_of(src->str, src->length, GATE_STR_WHITESPACES, GATE_STR_WHITESPACES_LENGTH, 0);
3602
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 1888 times.
1952 if (pos == GATE_STR_NPOS)
3603 {
3604
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 if (dst == src)
3605 {
3606 64 gate_string_release(dst);
3607 }
3608 64 gate_string_create_empty(dst);
3609 }
3610 else
3611 {
3612 1888 dst->str = &src->str[pos];
3613 1888 dst->length = src->length - pos;
3614
2/2
✓ Branch 0 taken 1611 times.
✓ Branch 1 taken 277 times.
1888 if (dst != src)
3615 {
3616 1611 dst->buffer = src->buffer;
3617
2/2
✓ Branch 0 taken 1610 times.
✓ Branch 1 taken 1 times.
1611 if (dst->buffer != NULL)
3618 {
3619 1610 gate_atomic_int_inc(&dst->buffer->refcount);
3620 }
3621 }
3622 }
3623 1952 return dst;
3624 }
3625 135 gate_string_t* gate_string_rtrim(gate_string_t* dst, gate_string_t const* src)
3626 {
3627 135 gate_size_t pos = gate_str_find_last_not_of(src->str, src->length, GATE_STR_WHITESPACES, GATE_STR_WHITESPACES_LENGTH);
3628
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 115 times.
135 if (pos == GATE_STR_NPOS)
3629 {
3630
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (dst == src)
3631 {
3632 20 gate_string_release(dst);
3633 }
3634 20 gate_string_create_empty(dst);
3635 }
3636 else
3637 {
3638 115 dst->length = pos + 1;
3639
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 114 times.
115 if (dst != src)
3640 {
3641 1 dst->str = src->str;
3642 1 dst->buffer = src->buffer;
3643
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (dst->buffer != NULL)
3644 {
3645 gate_atomic_int_inc(&dst->buffer->refcount);
3646 }
3647 }
3648 }
3649 135 return dst;
3650 }
3651 3289 gate_string_t* gate_string_trim(gate_string_t* dst, gate_string_t const* src)
3652 {
3653 3289 gate_size_t newlength = gate_str_find_last_not_of(src->str, src->length, GATE_STR_WHITESPACES, GATE_STR_WHITESPACES_LENGTH);
3654 gate_size_t pos;
3655
3656
2/2
✓ Branch 0 taken 144 times.
✓ Branch 1 taken 3145 times.
3289 if (newlength == GATE_STR_NPOS)
3657 {
3658
1/2
✓ Branch 0 taken 144 times.
✗ Branch 1 not taken.
144 if (dst == src)
3659 {
3660 144 gate_string_release(dst);
3661 }
3662 144 return gate_string_create_empty(dst);
3663 }
3664 3145 ++newlength;
3665
3666 3145 pos = gate_str_find_first_not_of(src->str, newlength, GATE_STR_WHITESPACES, GATE_STR_WHITESPACES_LENGTH, 0);
3667
3668 3145 dst->str = &src->str[pos];
3669 3145 dst->length = newlength - pos;
3670
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3144 times.
3145 if (dst != src)
3671 {
3672 1 dst->buffer = src->buffer;
3673
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (dst->buffer != NULL)
3674 {
3675 gate_atomic_int_inc(&dst->buffer->refcount);
3676 }
3677 }
3678 3145 return dst;
3679 }
3680
3681 163 gate_string_t* gate_string_read_line(gate_string_t* line, gate_string_t const* src, gate_string_t* tail)
3682 {
3683 163 gate_string_t* ret = NULL;
3684 gate_size_t pos;
3685
2/2
✓ Branch 1 taken 154 times.
✓ Branch 2 taken 9 times.
163 if (!gate_string_is_empty(src))
3686 {
3687 154 pos = gate_str_char_pos(src->str, src->length, '\n', 0);
3688
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 154 times.
154 if (pos == GATE_STR_NPOS)
3689 {
3690 ret = gate_string_duplicate(line, src);
3691 if (tail != NULL)
3692 {
3693 if (tail == src)
3694 {
3695 gate_string_release(tail);
3696 }
3697 else
3698 {
3699 gate_string_create_empty(tail);
3700 }
3701 }
3702 }
3703 else
3704 {
3705 154 ret = gate_string_substr(line, src, 0, pos);
3706
1/2
✓ Branch 0 taken 154 times.
✗ Branch 1 not taken.
154 if (ret != NULL)
3707 {
3708
2/2
✓ Branch 1 taken 76 times.
✓ Branch 2 taken 78 times.
154 if (gate_string_ends_with_str(line, "\r"))
3709 {
3710 76 --line->length;
3711 }
3712 }
3713
1/2
✓ Branch 0 taken 154 times.
✗ Branch 1 not taken.
154 if (tail != NULL)
3714 {
3715
2/2
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 60 times.
154 if (tail == src)
3716 {
3717 94 tail->str += (pos + 1);
3718 94 tail->length -= (pos + 1);
3719 }
3720 else
3721 {
3722 60 gate_string_substr(tail, src, pos + 1, GATE_STR_NPOS);
3723 }
3724 }
3725 }
3726 }
3727 163 return ret;
3728 }
3729
3730 14 gate_string_t* gate_string_read_token(gate_string_t* token, gate_string_t const* src, gate_string_t* tail)
3731 {
3732 14 gate_string_t* ret = NULL;
3733 gate_size_t pos;
3734 gate_size_t pos2;
3735
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 if (!gate_string_is_empty(src))
3736 {
3737 14 pos = gate_str_find_first_not_of(src->str, src->length, GATE_STR_WHITESPACES, GATE_STR_WHITESPACES_LENGTH, 0);
3738
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (pos == GATE_STR_NPOS)
3739 {
3740 ret = gate_string_create_empty(token);
3741 if (tail != NULL)
3742 {
3743 gate_string_create_empty(tail);
3744 }
3745 }
3746 else
3747 {
3748 14 pos2 = gate_str_find_first_of(src->str, src->length, GATE_STR_WHITESPACES, GATE_STR_WHITESPACES_LENGTH, pos);
3749
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (pos2 == GATE_STR_NPOS)
3750 {
3751 ret = gate_string_substr(token, src, pos, src->length - pos);
3752 if (tail != NULL)
3753 {
3754 gate_string_create_empty(tail);
3755 }
3756 }
3757 else
3758 {
3759 14 ret = gate_string_substr(token, src, pos, pos2 - pos);
3760
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if (tail != NULL)
3761 {
3762 14 gate_string_substr(tail, src, pos2, src->length - pos2);
3763 }
3764 }
3765 }
3766 }
3767 14 return ret;
3768 }
3769
3770 1 gate_string_t* gate_string_to_lower(gate_string_t* dst, gate_string_t const* src)
3771 {
3772 1 gate_string_t* ret = gate_string_create_copy(dst, src);
3773
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (NULL != ret)
3774 {
3775 1 gate_str_to_lower(ret->buffer->data, ret->length);
3776 }
3777 1 return ret;
3778 }
3779 1 gate_string_t* gate_string_to_upper(gate_string_t* dst, gate_string_t const* src)
3780 {
3781 1 gate_string_t* ret = gate_string_create_copy(dst, src);
3782
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (NULL != ret)
3783 {
3784 1 gate_str_to_upper(ret->buffer->data, ret->length);
3785 }
3786 1 return ret;
3787 }
3788 1 gate_uint32_t gate_string_hash(gate_string_t const* obj)
3789 {
3790 1 return gate_str_hash(gate_string_ptr(obj, 0), gate_string_length(obj));
3791 }
3792
3793
3794
3795
3796 2007 gate_result_t gate_string_copy_constructor(void* destMem, void const* srcMem)
3797 {
3798 2007 gate_string_t* dst = (gate_string_t*)destMem;
3799 2007 gate_string_t const* src = (gate_string_t const*)srcMem;
3800
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2007 times.
2007 if (NULL == gate_string_clone(dst, src))
3801 {
3802 return GATE_RESULT_OUTOFMEMORY;
3803 }
3804 else
3805 {
3806 2007 return GATE_RESULT_OK;
3807 }
3808 }
3809 43 gate_result_t gate_string_duplicate_constructor(void* destMem, void const* srcMem)
3810 {
3811 43 gate_string_t* dst = (gate_string_t*)destMem;
3812 43 gate_string_t const* src = (gate_string_t const*)srcMem;
3813
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 43 times.
43 if (NULL == gate_string_duplicate(dst, src))
3814 {
3815 return GATE_RESULT_OUTOFMEMORY;
3816 }
3817 else
3818 {
3819 43 return GATE_RESULT_OK;
3820 }
3821 }
3822 2050 void gate_string_destructor(void* dest)
3823 {
3824 2050 gate_string_t* dst = (gate_string_t*)dest;
3825 2050 gate_string_release(dst);
3826 2050 }
3827
3828
3829
3830
3831
3832 #define GATE_CSTRBUFFER_CREATE_IMPL(char_type, buffer, textptr, length, copy_needed) \
3833 if(!buffer) return NULL; \
3834 gate_mem_clear(buffer, sizeof(*buffer)); \
3835 if(length > 0) \
3836 { \
3837 buffer->length = length; \
3838 if(!copy_needed && (textptr[length] == 0)) \
3839 { \
3840 buffer->str = textptr; \
3841 } \
3842 else if(length < (sizeof(buffer->local_buffer) / sizeof(buffer->local_buffer[0]))) \
3843 { \
3844 gate_mem_copy(buffer->local_buffer, textptr, length * sizeof(buffer->local_buffer[0])); \
3845 buffer->local_buffer[length] = 0; \
3846 buffer->str = &buffer->local_buffer[0]; \
3847 } \
3848 else \
3849 { \
3850 buffer->heap_buffer = ( char_type *)gate_mem_alloc((length + 1) * sizeof(char_type)); \
3851 if(buffer->heap_buffer) \
3852 { \
3853 gate_mem_copy(buffer->heap_buffer, textptr, length * sizeof(char_type)); \
3854 buffer->heap_buffer[length] = 0; \
3855 buffer->str = buffer->heap_buffer; \
3856 } \
3857 else \
3858 { \
3859 buffer->length = 0; \
3860 buffer = NULL; \
3861 } \
3862 } \
3863 } \
3864 return buffer
3865
3866 #define GATE_CSTRBUFFER_DESTROY_IMPL(buffer) \
3867 do { \
3868 if(!buffer) break; \
3869 if(buffer->heap_buffer && (buffer->str == buffer->heap_buffer)) \
3870 { \
3871 gate_mem_dealloc(buffer->heap_buffer); \
3872 } \
3873 gate_mem_clear(buffer, sizeof(*buffer)); \
3874 } while(0)
3875
3876 #define GATE_CSTRBUFFER_GET_IMPL(buffer) return buffer ? buffer->str : NULL
3877
3878 #define GATE_CSTRBUFFER_LENGTH_IMPL(buffer) return buffer ? buffer->length : 0
3879
3880
3881 2 gate_cstrbuffer_t* gate_cstrbuffer_create_string(gate_cstrbuffer_t* buffer, gate_string_t const* strptr, gate_bool_t copy_needed)
3882 {
3883
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (strptr)
3884 {
3885 2 return gate_cstrbuffer_create(buffer, strptr->str, strptr->length, copy_needed);
3886 }
3887 else if (buffer)
3888 {
3889 gate_mem_clear(buffer, sizeof(*buffer));
3890 }
3891 return buffer;
3892 }
3893
3894 gate_cstrbuffer_t* gate_cstrbuffer_create_wstr(gate_cstrbuffer_t* buffer, wchar_t const* textptr, gate_size_t length)
3895 {
3896 const gate_size_t required_buffer = length * 3 + 1;
3897 char* target = NULL;
3898 gate_size_t used = 0;
3899
3900 if (!buffer) return NULL;
3901 gate_mem_clear(buffer, sizeof(*buffer));
3902
3903 if (sizeof(buffer->local_buffer) <= required_buffer)
3904 {
3905 buffer->heap_buffer = (char*)gate_mem_alloc(required_buffer);
3906 if (NULL == buffer->heap_buffer)
3907 {
3908 /* allocation failed */
3909 return NULL;
3910 }
3911 target = buffer->heap_buffer;
3912 }
3913 else
3914 {
3915 target = &buffer->local_buffer[0];
3916 }
3917
3918 #if defined(GATE_TYPE_WCHAR_IS_UTF16)
3919 used = gate_str_utf16_2_utf8((gate_char16_t const*)textptr, length, target, required_buffer - 1);
3920 #else
3921 used = gate_str_utf32_2_utf8((gate_char32_t const*)textptr, length, target, required_buffer - 1);
3922 #endif
3923
3924 buffer->str = target;
3925 buffer->length = used;
3926 return buffer;
3927 }
3928
3929 9 gate_cstrbuffer_t* gate_cstrbuffer_create(gate_cstrbuffer_t* buffer, gate_char8_t const* textptr, gate_size_t length, gate_bool_t copy_needed)
3930 {
3931
9/12
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
✓ Branch 3 taken 9 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 4 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 3 times.
✓ Branch 10 taken 2 times.
✓ Branch 13 taken 2 times.
✗ Branch 14 not taken.
9 GATE_CSTRBUFFER_CREATE_IMPL(gate_char8_t, buffer, textptr, length, copy_needed);
3932 }
3933
3934 9 void gate_cstrbuffer_destroy(gate_cstrbuffer_t* buffer)
3935 {
3936
4/6
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
9 GATE_CSTRBUFFER_DESTROY_IMPL(buffer);
3937 9 }
3938
3939 12 gate_char8_t const* gate_cstrbuffer_get(gate_cstrbuffer_t const* buffer)
3940 {
3941
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 GATE_CSTRBUFFER_GET_IMPL(buffer);
3942 }
3943
3944 4 gate_size_t gate_cstrbuffer_length(gate_cstrbuffer_t const* buffer)
3945 {
3946
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 GATE_CSTRBUFFER_LENGTH_IMPL(buffer);
3947 }
3948
3949
3950
3951 2 gate_cstrbuffer16_t* gate_cstrbuffer16_create_string(gate_cstrbuffer16_t* buffer, gate_string_t const* ptr)
3952 {
3953 gate_size_t len;
3954
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (buffer)
3955 {
3956 2 gate_mem_clear(buffer, sizeof(*buffer));
3957
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 if (ptr && ptr->length)
3958 {
3959
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (ptr->length < (sizeof(buffer->local_buffer) / sizeof(buffer->local_buffer[0])))
3960 {
3961 1 len = gate_str_utf8_2_utf16(ptr->str, ptr->length, buffer->local_buffer,
3962 sizeof(buffer->local_buffer) / sizeof(buffer->local_buffer[0]) - 1);
3963 1 buffer->local_buffer[len] = 0;
3964 1 buffer->str = &buffer->local_buffer[0];
3965 1 buffer->length = len;
3966 }
3967 else
3968 {
3969 1 buffer->heap_buffer = (gate_char16_t*)gate_mem_alloc((ptr->length + 1) * sizeof(gate_char16_t));
3970
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!buffer->heap_buffer)
3971 {
3972 buffer = NULL;
3973 }
3974 else
3975 {
3976 1 buffer->length = gate_str_utf8_2_utf16(ptr->str, ptr->length, buffer->heap_buffer, ptr->length);
3977 1 buffer->heap_buffer[buffer->length] = 0;
3978 1 buffer->str = buffer->heap_buffer;
3979 }
3980 }
3981 }
3982 }
3983
3984 2 return buffer;
3985 }
3986
3987 gate_cstrbuffer16_t* gate_cstrbuffer16_create(gate_cstrbuffer16_t* buffer, gate_char16_t const* textptr, gate_size_t length, gate_bool_t copy_needed)
3988 {
3989 GATE_CSTRBUFFER_CREATE_IMPL(gate_char16_t, buffer, textptr, length, copy_needed);
3990 }
3991
3992 2 void gate_cstrbuffer16_destroy(gate_cstrbuffer16_t* buffer)
3993 {
3994
4/6
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
2 GATE_CSTRBUFFER_DESTROY_IMPL(buffer);
3995 2 }
3996
3997 1 gate_char16_t const* gate_cstrbuffer16_get(gate_cstrbuffer16_t const* buffer)
3998 {
3999
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 GATE_CSTRBUFFER_GET_IMPL(buffer);
4000 }
4001
4002 1 gate_size_t gate_cstrbuffer16_length(gate_cstrbuffer16_t const* buffer)
4003 {
4004
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 GATE_CSTRBUFFER_LENGTH_IMPL(buffer);
4005 }
4006
4007
4008
4009 gate_cstrbuffer32_t* gate_cstrbuffer32_create_string(gate_cstrbuffer32_t* buffer, gate_string_t const* ptr)
4010 {
4011 gate_size_t len;
4012 if (buffer)
4013 {
4014 gate_mem_clear(buffer, sizeof(*buffer));
4015 if (ptr && ptr->length)
4016 {
4017 if (ptr->length < (sizeof(buffer->local_buffer) / sizeof(buffer->local_buffer[0])))
4018 {
4019 len = gate_str_utf8_2_utf32(ptr->str, ptr->length, buffer->local_buffer,
4020 sizeof(buffer->local_buffer) / sizeof(buffer->local_buffer[0]) - 1);
4021 buffer->local_buffer[len] = 0;
4022 buffer->str = &buffer->local_buffer[0];
4023 buffer->length = len;
4024 }
4025 else
4026 {
4027 buffer->heap_buffer = (gate_char32_t*)gate_mem_alloc((ptr->length + 1) * sizeof(gate_char32_t));
4028 if (!buffer->heap_buffer)
4029 {
4030 buffer = NULL;
4031 }
4032 else
4033 {
4034 buffer->length = gate_str_utf8_2_utf32(ptr->str, ptr->length, buffer->heap_buffer, ptr->length);
4035 buffer->heap_buffer[buffer->length] = 0;
4036 buffer->str = buffer->heap_buffer;
4037 }
4038 }
4039 }
4040 }
4041
4042 return buffer;
4043 }
4044
4045 gate_cstrbuffer32_t* gate_cstrbuffer32_create(gate_cstrbuffer32_t* buffer, gate_char32_t const* textptr, gate_size_t length, gate_bool_t copy_needed)
4046 {
4047 GATE_CSTRBUFFER_CREATE_IMPL(gate_char32_t, buffer, textptr, length, copy_needed);
4048 }
4049
4050 void gate_cstrbuffer32_destroy(gate_cstrbuffer32_t* buffer)
4051 {
4052 GATE_CSTRBUFFER_DESTROY_IMPL(buffer);
4053 }
4054
4055 gate_char32_t const* gate_cstrbuffer32_get(gate_cstrbuffer32_t const* buffer)
4056 {
4057 GATE_CSTRBUFFER_GET_IMPL(buffer);
4058 }
4059
4060 gate_size_t gate_cstrbuffer32_length(gate_cstrbuffer32_t const* buffer)
4061 {
4062 GATE_CSTRBUFFER_LENGTH_IMPL(buffer);
4063 }
4064