GCC Code Coverage Report


Directory: src/gate/
File: src/gate/strings.c
Date: 2026-10-19 17:06:30
Exec Total Coverage
Lines: 1103 1395 79.1%
Functions: 129 181 71.3%
Branches: 580 1080 53.7%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, 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 GATE_STR_CHAR_POS(str, len, find, startat) \
37 gate_size_t ret; \
38 if((str == NULL) || (len == 0) || (startat >= len)) \
39 { \
40 return GATE_STR_NPOS; \
41 } \
42 ret = startat; \
43 str += startat; \
44 len -= startat; \
45 while(len-- > 0) \
46 { \
47 if(*str == find) \
48 { \
49 return ret; \
50 } \
51 ++str; \
52 ++ret; \
53 } \
54 return GATE_STR_NPOS;
55
56 5890 gate_size_t gate_str_char_pos(gate_char8_t const* str, gate_size_t len, gate_char8_t find, gate_size_t startat)
57 {
58
10/10
✓ Branch 0 taken 5885 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5866 times.
✓ Branch 3 taken 19 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 5864 times.
✓ Branch 6 taken 4064 times.
✓ Branch 7 taken 131213 times.
✓ Branch 8 taken 135277 times.
✓ Branch 9 taken 1800 times.
137103 GATE_STR_CHAR_POS(str, len, find, startat);
59 }
60 ✗ gate_size_t gate_str16_char_pos(gate_char16_t const* str, gate_size_t len, gate_char16_t find, gate_size_t startat)
61 {
62 ✗ GATE_STR_CHAR_POS(str, len, find, startat);
63 }
64 ✗ gate_size_t gate_str32_char_pos(gate_char32_t const* str, gate_size_t len, gate_char32_t find, gate_size_t startat)
65 {
66 ✗ GATE_STR_CHAR_POS(str, len, find, startat);
67 }
68
69 #define GATE_STR_CHAR_POS_LAST(str, len, find) \
70 gate_size_t ret; \
71 if((str == NULL) || (len == 0)) \
72 { \
73 return GATE_STR_NPOS; \
74 } \
75 ret = len; \
76 str += len; \
77 while(len-- != 0) \
78 { \
79 --ret; \
80 --str; \
81 if(*str == find) \
82 { \
83 return ret; \
84 } \
85 } \
86 return GATE_STR_NPOS;
87
88 712 gate_size_t gate_str_char_pos_last(gate_char8_t const* str, gate_size_t len, gate_char8_t find)
89 {
90
6/8
✓ Branch 0 taken 712 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 712 times.
✓ Branch 4 taken 570 times.
✓ Branch 5 taken 3860 times.
✓ Branch 6 taken 4430 times.
✓ Branch 7 taken 142 times.
4572 GATE_STR_CHAR_POS_LAST(str, len, find);
91 }
92 ✗ gate_size_t gate_str16_char_pos_last(gate_char16_t const* str, gate_size_t len, gate_char16_t find)
93 {
94 ✗ GATE_STR_CHAR_POS_LAST(str, len, find);
95 }
96 ✗ gate_size_t gate_str32_char_pos_last(gate_char32_t const* str, gate_size_t len, gate_char32_t find)
97 {
98 ✗ GATE_STR_CHAR_POS_LAST(str, len, find);
99 }
100
101
102
103
104
105 #define GATE_STR_POS(charfinder, rangecomparer, str, len, find, findlen, startat) \
106 gate_size_t ret = startat; \
107 if((str == NULL) || (len == 0) || (find == NULL) || (findlen == 0) || (findlen > len) || (startat > len - findlen)) \
108 { \
109 return GATE_STR_NPOS; \
110 } \
111 str += startat; \
112 len -= startat; \
113 for(;;) \
114 { \
115 gate_size_t pos = charfinder(str, len, *find, 0); \
116 if(pos == GATE_STR_NPOS) \
117 { \
118 return pos; \
119 } \
120 ret += pos; \
121 str += pos; \
122 len -= pos; \
123 if(rangecomparer(str, find, findlen) == 0) \
124 { \
125 break; \
126 } \
127 ++str; \
128 --len; \
129 ++ret; \
130 } \
131 return ret;
132
133
134 796 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)
135 {
136
14/16
✓ Branch 0 taken 792 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 760 times.
✓ Branch 3 taken 32 times.
✓ Branch 4 taken 760 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 760 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 491 times.
✓ Branch 9 taken 269 times.
✓ Branch 10 taken 4 times.
✓ Branch 11 taken 487 times.
✓ Branch 13 taken 249 times.
✓ Branch 14 taken 449 times.
✓ Branch 16 taken 238 times.
✓ Branch 17 taken 211 times.
1007 GATE_STR_POS(gate_str_char_pos, gate_str_comp_range, str, len, find, findlen, startat);
137 }
138 ✗ 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)
139 {
140 ✗ GATE_STR_POS(gate_str16_char_pos, gate_str16_comp_range, str, len, find, findlen, startat);
141 }
142 ✗ 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)
143 {
144 ✗ GATE_STR_POS(gate_str32_char_pos, gate_str32_comp_range, str, len, find, findlen, startat);
145 }
146
147
148 #define GATE_STR_POS_LAST(rangecomparer, str, len, find, findlen) \
149 if((str == NULL) || (len == 0) || (find == NULL) || (findlen == 0) || (findlen > len)) \
150 { \
151 return GATE_STR_NPOS; \
152 } \
153 str += (len - findlen); \
154 len -= (findlen - 1); \
155 while(len-- > 0) \
156 { \
157 if(rangecomparer(str, find, findlen) == 0) \
158 { \
159 return len; \
160 } \
161 --str; \
162 } \
163 return GATE_STR_NPOS;
164
165
166 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)
167 {
168
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);
169 }
170 ✗ 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)
171 {
172 ✗ GATE_STR_POS_LAST(gate_str16_comp_range, str, len, find, findlen);
173 }
174 ✗ 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)
175 {
176 ✗ GATE_STR_POS_LAST(gate_str32_comp_range, str, len, find, findlen);
177 }
178
179
180
181
182 #define GATE_STR_LEN(src) \
183 gate_size_t ret = 0; \
184 if(src != NULL) \
185 { \
186 while(*src != 0) \
187 { \
188 ++src; \
189 ++ret; \
190 } \
191 } \
192 return ret;
193
194 1515251 gate_size_t gate_str_length(gate_char8_t const* src)
195 {
196
4/4
✓ Branch 0 taken 1514126 times.
✓ Branch 1 taken 1125 times.
✓ Branch 2 taken 63689846 times.
✓ Branch 3 taken 1514126 times.
65205097 GATE_STR_LEN(src);
197 }
198 ✗ gate_size_t gate_str16_length(gate_char16_t const* src)
199 {
200 ✗ GATE_STR_LEN(src);
201 }
202 20 gate_size_t gate_str32_length(gate_char32_t const* src)
203 {
204
3/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 64 times.
✓ Branch 3 taken 20 times.
84 GATE_STR_LEN(src);
205 }
206
207 #define GATE_STR_LEN_MAX(src, capacity) \
208 gate_size_t ret = 0; \
209 if(src != NULL) \
210 { \
211 while((*src != 0) && (capacity-- != 0)) \
212 { \
213 ++src; \
214 ++ret; \
215 } \
216 } \
217 return ret;
218
219 52 gate_size_t gate_str_length_max(gate_char8_t const* src, gate_size_t capacity)
220 {
221
4/6
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 437 times.
✓ Branch 3 taken 52 times.
✓ Branch 4 taken 437 times.
✗ Branch 5 not taken.
489 GATE_STR_LEN_MAX(src, capacity);
222 }
223
224 ✗ gate_size_t gate_str16_length_max(gate_char16_t const* src, gate_size_t capacity)
225 {
226 ✗ GATE_STR_LEN_MAX(src, capacity);
227 }
228
229 ✗ gate_size_t gate_str32_length_max(gate_char32_t const* src, gate_size_t capacity)
230 {
231 ✗ GATE_STR_LEN_MAX(src, capacity);
232 }
233
234 #define GATE_STR_IS_EMPTY(src) \
235 if(src == NULL) return true; \
236 return *src == 0
237
238
239 3249 gate_bool_t gate_str_is_empty(gate_char8_t const* src)
240 {
241
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 3230 times.
3249 GATE_STR_IS_EMPTY(src);
242 }
243 ✗ gate_bool_t gate_str16_is_empty(gate_char16_t const* src)
244 {
245 ✗ GATE_STR_IS_EMPTY(src);
246 }
247 ✗ gate_bool_t gate_str32_is_empty(gate_char32_t const* src)
248 {
249 ✗ GATE_STR_IS_EMPTY(src);
250 }
251
252
253 #define GATE_STR_COMP_RANGE(str1, str2, len) \
254 if(str1 != str2) \
255 { \
256 while(len-- > 0) \
257 { \
258 if(*str1 < *str2) return -1; \
259 if(*str1 > *str2) return +1; \
260 ++str1; \
261 ++str2; \
262 } \
263 } \
264 return 0;
265
266
267 39595 int gate_str_comp_range(gate_char8_t const* str1, gate_char8_t const* str2, gate_size_t len)
268 {
269
8/8
✓ Branch 0 taken 39273 times.
✓ Branch 1 taken 322 times.
✓ Branch 2 taken 20541 times.
✓ Branch 3 taken 71558 times.
✓ Branch 4 taken 12640 times.
✓ Branch 5 taken 58918 times.
✓ Branch 6 taken 92099 times.
✓ Branch 7 taken 6092 times.
98513 GATE_STR_COMP_RANGE(str1, str2, len);
270 }
271 1 int gate_str16_comp_range(gate_char16_t const* str1, gate_char16_t const* str2, gate_size_t len)
272 {
273
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);
274 }
275 3 int gate_str32_comp_range(gate_char32_t const* str1, gate_char32_t const* str2, gate_size_t len)
276 {
277
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);
278 }
279
280
281
282
283 #define GATE_STR_COMP_RANGE_IC(str1, str2, len) \
284 if(str1 != str2) \
285 { \
286 while(len-- > 0) \
287 { \
288 gate_char8_t const c1 = (gate_char8_t)gate_char_lowercase(*str1); \
289 gate_char8_t const c2 = (gate_char8_t)gate_char_lowercase(*str2); \
290 if(c1 < c2) return -1; \
291 if(c1 > c2) return +1; \
292 ++str1; \
293 ++str2; \
294 } \
295 } \
296 return 0;
297
298 3721 int gate_str_comp_range_ic(gate_char8_t const* str1, gate_char8_t const* str2, gate_size_t len)
299 {
300
8/8
✓ Branch 0 taken 3712 times.
✓ Branch 1 taken 9 times.
✓ Branch 4 taken 2341 times.
✓ Branch 5 taken 2940 times.
✓ Branch 6 taken 1154 times.
✓ Branch 7 taken 1786 times.
✓ Branch 8 taken 5281 times.
✓ Branch 9 taken 217 times.
5507 GATE_STR_COMP_RANGE_IC(str1, str2, len);
301 }
302 ✗ int gate_str16_comp_range_ic(gate_char16_t const* str1, gate_char16_t const* str2, gate_size_t len)
303 {
304 ✗ GATE_STR_COMP_RANGE_IC(str1, str2, len);
305 }
306 ✗ int gate_str32_comp_range_ic(gate_char32_t const* str1, gate_char32_t const* str2, gate_size_t len)
307 {
308 ✗ GATE_STR_COMP_RANGE_IC(str1, str2, len);
309 }
310
311 #define GATE_STR_TO_LOWER(cast_type, str, len) \
312 while(len-- > 0) \
313 { \
314 *str = (cast_type)gate_char_lowercase(*str); \
315 ++str; \
316 }
317
318 1 void gate_str_to_lower(gate_char8_t* str, gate_size_t len)
319 {
320
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 1 times.
7 GATE_STR_TO_LOWER(gate_char8_t, str, len);
321 1 }
322 ✗ void gate_str16_to_lower(gate_char16_t* str, gate_size_t len)
323 {
324 ✗ GATE_STR_TO_LOWER(gate_char16_t, str, len);
325 ✗ }
326 ✗ void gate_str32_to_lower(gate_char32_t* str, gate_size_t len)
327 {
328 ✗ GATE_STR_TO_LOWER(gate_char32_t, str, len);
329 ✗ }
330
331
332
333
334 #define GATE_STR_TO_UPPER(cast_type, str, len) \
335 while(len-- > 0) \
336 { \
337 *str = (cast_type)gate_char_uppercase(*str); \
338 ++str; \
339 }
340
341 1 void gate_str_to_upper(gate_char8_t* str, gate_size_t len)
342 {
343
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 1 times.
7 GATE_STR_TO_UPPER(gate_char8_t, str, len);
344 1 }
345 ✗ void gate_str16_to_upper(gate_char16_t* str, gate_size_t len)
346 {
347 ✗ GATE_STR_TO_UPPER(gate_char16_t, str, len);
348 ✗ }
349 ✗ void gate_str32_to_upper(gate_char32_t* str, gate_size_t len)
350 {
351 ✗ GATE_STR_TO_UPPER(gate_char32_t, str, len);
352 ✗ }
353
354
355
356 #define GATE_STR_REVERSE(type, src, len) \
357 if(len > 1) \
358 {\
359 type* dst = src + len - 1; \
360 len /= 2; \
361 while(len != 0) \
362 { \
363 type chr = *src; \
364 *src = *dst; \
365 *dst = chr; \
366 ++src; \
367 --dst; \
368 --len; \
369 } \
370 }
371
372
373 251050 void gate_str_reverse(gate_char8_t* src, gate_size_t len)
374 {
375
3/4
✓ Branch 0 taken 251050 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 252323 times.
✓ Branch 3 taken 251050 times.
503373 GATE_STR_REVERSE(gate_char8_t, src, len);
376 251050 }
377 ✗ void gate_str16_reverse(gate_char16_t* src, gate_size_t len)
378 {
379 ✗ GATE_STR_REVERSE(gate_char16_t, src, len);
380 ✗ }
381 ✗ void gate_str32_reverse(gate_char32_t* src, gate_size_t len)
382 {
383 ✗ GATE_STR_REVERSE(gate_char32_t, src, len);
384 ✗ }
385
386 #define GATE_STR_IS_NUMERIC(str, len) \
387 gate_size_t ret = 0; \
388 for(; len != 0; --len, ++str) \
389 { \
390 if(gate_char_is_digit((int)*str)) \
391 { \
392 ++ret; \
393 } \
394 else \
395 { \
396 break; \
397 } \
398 } \
399 return ret;
400
401 ✗ gate_size_t gate_str_is_numeric(gate_char8_t const* str, gate_size_t len)
402 {
403 ✗ GATE_STR_IS_NUMERIC(str, len)
404 }
405 ✗ gate_size_t gate_str16_is_numeric(gate_char16_t const* str, gate_size_t len)
406 {
407 ✗ GATE_STR_IS_NUMERIC(str, len)
408 }
409 ✗ gate_size_t gate_str32_is_numeric(gate_char32_t const* str, gate_size_t len)
410 {
411 ✗ GATE_STR_IS_NUMERIC(str, len)
412 }
413
414
415
416 220 gate_size_t gate_str_count_chars(gate_char8_t const* str, gate_size_t len, gate_char8_t chr)
417 {
418 220 gate_size_t count = 0;
419
2/2
✓ Branch 0 taken 2410 times.
✓ Branch 1 taken 220 times.
2630 while (len-- != 0)
420 {
421
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2404 times.
2410 if (*str == chr)
422 {
423 6 ++count;
424 }
425 2410 ++str;
426 }
427 220 return count;
428 }
429 355 int gate_str_comp(gate_char8_t const* str1, gate_char8_t const* str2)
430 {
431 355 return gate_str_compare(str1, gate_str_length(str1), str2, gate_str_length(str2));
432 }
433 34245 int gate_str_compare(gate_char8_t const* str1, gate_size_t len1, gate_char8_t const* str2, gate_size_t len2)
434 {
435 34245 gate_size_t len = len1 < len2 ? len1 : len2;
436 34245 int ret = gate_str_comp_range(str1, str2, len);
437
2/2
✓ Branch 0 taken 4847 times.
✓ Branch 1 taken 29398 times.
34245 if (ret == 0)
438 {
439
2/2
✓ Branch 0 taken 195 times.
✓ Branch 1 taken 4652 times.
4847 if (len1 < len2) ret = -1;
440
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 4760 times.
4847 if (len1 > len2) ret = 1;
441 }
442 34245 return ret;
443 }
444 16 int gate_str_comp_ic(gate_char8_t const* str1, gate_char8_t const* str2)
445 {
446 16 return gate_str_compare_ic(str1, gate_str_length(str1), str2, gate_str_length(str2));
447 }
448 3307 int gate_str_compare_ic(gate_char8_t const* str1, gate_size_t len1, gate_char8_t const* str2, gate_size_t len2)
449 {
450 3307 gate_size_t len = len1 < len2 ? len1 : len2;
451 3307 int ret = gate_str_comp_range_ic(str1, str2, len);
452
2/2
✓ Branch 0 taken 190 times.
✓ Branch 1 taken 3117 times.
3307 if (ret == 0)
453 {
454
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 179 times.
190 if (len1 < len2) ret = -1;
455
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 170 times.
190 if (len1 > len2) ret = 1;
456 }
457 3307 return ret;
458 }
459
460 56 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)
461 {
462
4/4
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 67 times.
✓ Branch 3 taken 1 times.
71 while ((len != 0) && (likelen != 0))
463 {
464
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 31 times.
67 if (*like == '*')
465 {
466 36 ++like;
467 36 --likelen;
468
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 4 times.
36 if (likelen == 0)
469 {
470 /* like ends with '*' -> this is always a match */
471 32 return true;
472 }
473
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 2 times.
19 while (len != 0)
474 {
475 /* start a new match attempt at each position in str */
476
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 15 times.
17 if (gate_str_wildcard_match(str, len, like, likelen))
477 {
478 2 return true;
479 }
480 15 ++str;
481 15 --len;
482 }
483 2 return false;
484 }
485
486
3/4
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 16 times.
31 if ((*like == '?') || (*str == *like))
487 {
488 15 ++str;
489 15 --len;
490 15 ++like;
491 15 --likelen;
492 }
493 else
494 {
495 16 return false;
496 }
497 }
498 4 return (len == likelen);
499 }
500
501 39 gate_bool_t gate_str_like(gate_char8_t const* str, gate_size_t len, gate_char8_t const* like, gate_size_t likelen)
502 {
503
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 39 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
39 if ((len == 0) && (likelen == 1))
504 {
505 /* empty is like anything -> OK */
506 ✗ return *like == '*';
507 }
508
509 39 return gate_str_wildcard_match(str, len, like, likelen);
510 }
511
512 33 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)
513 {
514 33 gate_size_t startat = 0;
515 gate_size_t pos;
516 gate_size_t token_len;
517
1/2
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
35 while (startat < likelen)
518 {
519 35 pos = gate_str_char_pos(like, likelen, like_separator, startat);
520
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 2 times.
35 if (pos == GATE_STR_NPOS)
521 {
522 33 pos = likelen;
523 }
524 //a;b;c
525 35 token_len = (pos - startat);
526
1/2
✓ Branch 0 taken 35 times.
✗ Branch 1 not taken.
35 if (token_len != 0)
527 {
528
2/2
✓ Branch 1 taken 33 times.
✓ Branch 2 taken 2 times.
35 if (gate_str_like(str, len, like + startat, token_len))
529 {
530 33 return true;
531 }
532 }
533 2 startat = pos + 1;
534 }
535 ✗ return false;
536 }
537
538
539 869 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)
540 {
541 gate_size_t ret;
542 gate_size_t ndx;
543
5/10
✓ Branch 0 taken 869 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 869 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 869 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 869 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 869 times.
869 if ((str == NULL) || (len == 0) || (startat >= len) || (chars == NULL) || (charcount == 0))
544 {
545 ✗ return GATE_STR_NPOS;
546 }
547 869 ret = startat;
548 869 str += startat;
549 869 len -= startat;
550
2/2
✓ Branch 0 taken 5635 times.
✓ Branch 1 taken 688 times.
6323 while (len-- > 0)
551 {
552
2/2
✓ Branch 0 taken 21943 times.
✓ Branch 1 taken 5454 times.
27397 for (ndx = 0; ndx != charcount; ++ndx)
553 {
554
2/2
✓ Branch 0 taken 181 times.
✓ Branch 1 taken 21762 times.
21943 if (*str == chars[ndx])
555 {
556 /* first character found, that is included in 'chars' */
557 181 return ret;
558 }
559 }
560 5454 ++str;
561 5454 ++ret;
562 }
563 688 return GATE_STR_NPOS;
564 }
565
566 10893 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)
567 {
568 gate_size_t ret;
569 gate_size_t ndx;
570
8/10
✓ Branch 0 taken 10746 times.
✓ Branch 1 taken 147 times.
✓ Branch 2 taken 10742 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 10741 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 10741 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 10741 times.
10893 if ((str == NULL) || (len == 0) || (startat >= len) || (chars == NULL) || (charcount == 0))
571 {
572 152 return GATE_STR_NPOS;
573 }
574 10741 ret = startat;
575 10741 str += startat;
576 10741 len -= startat;
577
2/2
✓ Branch 0 taken 24226 times.
✓ Branch 1 taken 1 times.
24227 while (len-- > 0)
578 {
579
2/2
✓ Branch 0 taken 59135 times.
✓ Branch 1 taken 10740 times.
69875 for (ndx = 0; ndx != charcount; ++ndx)
580 {
581
2/2
✓ Branch 0 taken 13486 times.
✓ Branch 1 taken 45649 times.
59135 if (*str == chars[ndx])
582 {
583 13486 break;
584 }
585 }
586
2/2
✓ Branch 0 taken 10740 times.
✓ Branch 1 taken 13486 times.
24226 if (ndx == charcount)
587 {
588 /* first character found, that is not included in 'chars' */
589 10740 return ret;
590 }
591 13486 ++str;
592 13486 ++ret;
593 }
594 1 return GATE_STR_NPOS;
595 }
596 28 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)
597 {
598 gate_size_t ret;
599 gate_size_t ndx;
600
4/8
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 28 times.
28 if ((str == NULL) || (len == 0) || (chars == NULL) || (charcount == 0))
601 {
602 ✗ return GATE_STR_NPOS;
603 }
604 28 ret = len;
605 28 str += len;
606
2/2
✓ Branch 0 taken 506 times.
✓ Branch 1 taken 1 times.
507 while (len-- > 0)
607 {
608 506 --str;
609 506 --ret;
610
2/2
✓ Branch 0 taken 510 times.
✓ Branch 1 taken 479 times.
989 for (ndx = 0; ndx != charcount; ++ndx)
611 {
612
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 483 times.
510 if (*str == chars[ndx])
613 {
614 /* last character found, that is included in 'chars' */
615 27 return ret;
616 }
617 }
618 }
619 1 return GATE_STR_NPOS;
620 }
621 4978 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)
622 {
623 gate_size_t ret;
624 gate_size_t ndx;
625
5/8
✓ Branch 0 taken 4978 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4972 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 4972 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 4972 times.
4978 if ((str == NULL) || (len == 0) || (chars == NULL) || (charcount == 0))
626 {
627 6 return GATE_STR_NPOS;
628 }
629 4972 ret = len;
630 4972 str += len;
631
2/2
✓ Branch 0 taken 7131 times.
✓ Branch 1 taken 164 times.
7295 while (len-- > 0)
632 {
633 7131 --str;
634 7131 --ret;
635
2/2
✓ Branch 0 taken 27979 times.
✓ Branch 1 taken 4808 times.
32787 for (ndx = 0; ndx != charcount; ++ndx)
636 {
637
2/2
✓ Branch 0 taken 2323 times.
✓ Branch 1 taken 25656 times.
27979 if (*str == chars[ndx])
638 {
639 2323 break;
640 }
641 }
642
2/2
✓ Branch 0 taken 4808 times.
✓ Branch 1 taken 2323 times.
7131 if (ndx == charcount)
643 {
644 /* last character found, that is not included in 'chars' */
645 4808 return ret;
646 }
647 }
648 164 return GATE_STR_NPOS;
649
650 }
651 2 gate_uint32_t gate_str_hash(gate_char8_t const* str, gate_size_t len)
652 {
653 2 gate_uint32_t hash = 5381;
654
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 while (len-- != 0)
655 {
656 8 hash += (hash << 5) + (gate_uint32_t)(gate_uint8_t) * (str++);
657 }
658 2 return hash;
659 }
660
661 ✗ gate_size_t gate_str_replace(gate_char8_t* str, gate_size_t len, gate_char8_t find, gate_char8_t replace)
662 {
663 ✗ gate_size_t counter = 0;
664 ✗ for (; len != 0; --len, ++str)
665 {
666 ✗ if (*str == find)
667 {
668 ✗ *str = replace;
669 ✗ ++counter;
670 }
671 }
672 ✗ return counter;
673 }
674
675
676
677
678 4869 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)
679 {
680
4/4
✓ Branch 0 taken 4857 times.
✓ Branch 1 taken 12 times.
✓ Branch 3 taken 1294 times.
✓ Branch 4 taken 3563 times.
4869 return (startlen > len) ? false : (0 == gate_str_comp_range(str, starttoken, startlen));
681 }
682 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)
683 {
684
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));
685 }
686
687 473 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)
688 {
689
4/4
✓ Branch 0 taken 391 times.
✓ Branch 1 taken 82 times.
✓ Branch 3 taken 28 times.
✓ Branch 4 taken 363 times.
473 return (startlen > len) ? false : (0 == gate_str_comp_range_ic(str, starttoken, startlen));
690 }
691 23 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)
692 {
693
3/4
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 15 times.
23 return (endlen > len) ? false : (0 == gate_str_comp_range_ic(str + len - endlen, endtoken, endlen));
694 }
695
696
697 ✗ 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)
698 {
699 ✗ return (startlen > len) ? false : (0 == gate_str16_comp_range(str, starttoken, startlen));
700 }
701 ✗ 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)
702 {
703 ✗ return (endlen > len) ? false : (0 == gate_str16_comp_range(str + len - endlen, endtoken, endlen));
704 }
705 ✗ 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)
706 {
707 ✗ return (startlen > len) ? false : (0 == gate_str32_comp_range(str, starttoken, startlen));
708 }
709 ✗ 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)
710 {
711 ✗ return (endlen > len) ? false : (0 == gate_str32_comp_range(str + len - endlen, endtoken, endlen));
712 }
713
714
715
716 ✗ int gate_str16_comp(gate_char16_t const* str1, gate_char16_t const* str2)
717 {
718 ✗ return gate_str16_compare(str1, gate_str16_length(str1), str2, gate_str16_length(str2));
719 }
720 1 int gate_str16_compare(gate_char16_t const* str1, gate_size_t len1, gate_char16_t const* str2, gate_size_t len2)
721 {
722 1 gate_size_t len = len1 < len2 ? len1 : len2;
723 1 int ret = gate_str16_comp_range(str1, str2, len);
724
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (ret == 0)
725 {
726
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (len1 < len2) ret = -1;
727
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (len1 > len2) ret = 1;
728 }
729 1 return ret;
730 }
731 ✗ int gate_str16_comp_ic(gate_char16_t const* str1, gate_char16_t const* str2)
732 {
733 ✗ return gate_str16_compare_ic(str1, gate_str16_length(str1), str2, gate_str16_length(str2));
734 }
735 ✗ int gate_str16_compare_ic(gate_char16_t const* str1, gate_size_t len1, gate_char16_t const* str2, gate_size_t len2)
736 {
737 ✗ gate_size_t len = len1 < len2 ? len1 : len2;
738 ✗ int ret = gate_str16_comp_range_ic(str1, str2, len);
739 ✗ if (ret == 0)
740 {
741 ✗ if (len1 < len2) ret = -1;
742 ✗ if (len1 > len2) ret = 1;
743 }
744 ✗ return ret;
745 }
746
747
748 3 int gate_str32_comp(gate_char32_t const* str1, gate_char32_t const* str2)
749 {
750 3 return gate_str32_compare(str1, gate_str32_length(str1), str2, gate_str32_length(str2));
751 }
752 3 int gate_str32_compare(gate_char32_t const* str1, gate_size_t len1, gate_char32_t const* str2, gate_size_t len2)
753 {
754 3 gate_size_t len = len1 < len2 ? len1 : len2;
755 3 int ret = gate_str32_comp_range(str1, str2, len);
756
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (ret == 0)
757 {
758
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (len1 < len2) ret = -1;
759
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (len1 > len2) ret = 1;
760 }
761 3 return ret;
762 }
763 ✗ int gate_str32_comp_ic(gate_char32_t const* str1, gate_char32_t const* str2)
764 {
765 ✗ return gate_str32_compare_ic(str1, gate_str32_length(str1), str2, gate_str32_length(str2));
766 }
767 ✗ int gate_str32_compare_ic(gate_char32_t const* str1, gate_size_t len1, gate_char32_t const* str2, gate_size_t len2)
768 {
769 ✗ gate_size_t len = len1 < len2 ? len1 : len2;
770 ✗ int ret = gate_str32_comp_range_ic(str1, str2, len);
771 ✗ if (ret == 0)
772 {
773 ✗ if (len1 < len2) ret = -1;
774 ✗ if (len1 > len2) ret = 1;
775 }
776 ✗ return ret;
777 }
778
779
780 #define GATE_STR_PRINT_TXT(dst, dstlen, txt, txtlen) \
781 if((dstlen) == 0) \
782 { \
783 return 0; \
784 } \
785 if((txtlen) >= (dstlen)) \
786 { \
787 txtlen = dstlen - 1; \
788 } \
789 if((txtlen) != 0) \
790 { \
791 gate_mem_copy((dst), (txt), (txtlen) * sizeof( (dst)[0] )); \
792 } \
793 dst[txtlen] = 0; \
794 return txtlen;
795
796 256820 gate_size_t gate_str_print_text(gate_char8_t* dst, gate_size_t dstlen, gate_char8_t const* txt, gate_size_t txtlen)
797 {
798
5/6
✗ Branch 0 not taken.
✓ Branch 1 taken 256820 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 256811 times.
✓ Branch 4 taken 256701 times.
✓ Branch 5 taken 119 times.
256820 GATE_STR_PRINT_TXT(dst, dstlen, txt, txtlen);
799 }
800 ✗ gate_size_t gate_str16_print_text(gate_char16_t* dst, gate_size_t dstlen, gate_char16_t const* txt, gate_size_t txtlen)
801 {
802 ✗ GATE_STR_PRINT_TXT(dst, dstlen, txt, txtlen);
803 }
804 ✗ gate_size_t gate_str32_print_text(gate_char32_t* dst, gate_size_t dstlen, gate_char32_t const* txt, gate_size_t txtlen)
805 {
806 ✗ GATE_STR_PRINT_TXT(dst, dstlen, txt, txtlen);
807 }
808
809 9 gate_size_t gate_str_print_wtext(gate_char8_t* dst, gate_size_t dstlen, wchar_t const* txt, gate_size_t txtlen)
810 {
811 #if defined(GATE_TYPE_WCHAR_IS_UTF16)
812 return gate_str_utf16_2_utf8((gate_char16_t const*)txt, txtlen, dst, dstlen);
813 #else
814 9 return gate_str_utf32_2_utf8((gate_char32_t const*)txt, txtlen, dst, dstlen);
815 #endif
816 }
817 ✗ gate_size_t gate_str16_print_wtext(gate_char16_t* dst, gate_size_t dstlen, wchar_t const* txt, gate_size_t txtlen)
818 {
819 #if defined(GATE_TYPE_WCHAR_IS_UTF16)
820 return gate_str16_print_text(dst, dstlen, txt, txtlen);
821 #else
822 ✗ return gate_str_utf32_2_utf16((gate_char32_t const*)txt, txtlen, dst, dstlen);
823 #endif
824 }
825 ✗ gate_size_t gate_str32_print_wtext(gate_char32_t* dst, gate_size_t dstlen, wchar_t const* txt, gate_size_t txtlen)
826 {
827 #if defined(GATE_TYPE_WCHAR_IS_UTF16)
828 return gate_str_utf16_2_utf32((gate_char16_t const*)txt, txtlen, dst, dstlen);
829 #else
830 ✗ return gate_str32_print_text(dst, dstlen, txt, txtlen);
831 #endif
832
833 }
834
835
836
837
838 #define GATE_STR_PRINT_NUM(buffer, bufferlen, ptr, bufferused, numtype, num, reverter) \
839 { \
840 ptr = &buffer[0]; \
841 bufferused = 0; \
842 while(bufferlen != 0) \
843 { \
844 *ptr = ('0' + (gate_char8_t)(num % (numtype)10)); \
845 num /= (numtype)10; \
846 ++bufferused; \
847 ++ptr; \
848 --bufferlen; \
849 if(num == 0) break; \
850 } \
851 if(bufferused >= 2) \
852 { \
853 reverter(&buffer[0], bufferused); \
854 } \
855 if(bufferlen != 0) \
856 { \
857 *ptr = 0; \
858 } \
859 }
860
861
862 #define GATE_STR_PRINT_INUM(retvar, buffer, bufferlen, unum, num, uint_type, uint_func) \
863 { \
864 retvar = 0; \
865 if(num < 0) \
866 { \
867 *buffer = '-'; \
868 ++buffer; \
869 --bufferlen; \
870 ++retvar; \
871 unum = (uint_type)-num; \
872 } \
873 else \
874 { \
875 unum = (uint_type)num; \
876 } \
877 retvar += uint_func(buffer, bufferlen, unum) ; \
878 }
879
880
881 407 gate_size_t gate_str_print_uint16(gate_char8_t* dst, gate_size_t dstlen, gate_uint16_t num)
882 {
883 407 gate_size_t ret = 0;
884
2/4
✓ Branch 0 taken 407 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 407 times.
✗ Branch 3 not taken.
407 if ((dst != NULL) && (dstlen != 0))
885 {
886 gate_char8_t* ptr;
887
6/8
✓ Branch 0 taken 407 times.
✓ Branch 1 taken 305 times.
✓ Branch 2 taken 712 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 212 times.
✓ Branch 5 taken 195 times.
✓ Branch 7 taken 407 times.
✗ Branch 8 not taken.
712 GATE_STR_PRINT_NUM(dst, dstlen, ptr, ret, gate_uint16_t, num, gate_str_reverse);
888 }
889 407 return ret;
890 }
891 10 gate_size_t gate_str_print_int16(gate_char8_t* dst, gate_size_t dstlen, gate_int16_t num)
892 {
893 10 gate_size_t ret = 0;
894
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 if ((dst != NULL) && (dstlen != 0))
895 {
896 gate_uint16_t unum;
897
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10 GATE_STR_PRINT_INUM(ret, dst, dstlen, unum, num, gate_uint16_t, gate_str_print_uint16);
898 }
899 10 return ret;
900 }
901 249831 gate_size_t gate_str_print_uint32(gate_char8_t* dst, gate_size_t dstlen, gate_uint32_t num)
902 {
903 249831 gate_size_t ret = 0;
904
2/4
✓ Branch 0 taken 249831 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 249831 times.
✗ Branch 3 not taken.
249831 if ((dst != NULL) && (dstlen != 0))
905 {
906 gate_char8_t* ptr;
907
6/8
✓ Branch 0 taken 249831 times.
✓ Branch 1 taken 491214 times.
✓ Branch 2 taken 741045 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 249448 times.
✓ Branch 5 taken 383 times.
✓ Branch 7 taken 249831 times.
✗ Branch 8 not taken.
741045 GATE_STR_PRINT_NUM(dst, dstlen, ptr, ret, gate_uint32_t, num, gate_str_reverse);
908 }
909 249831 return ret;
910 }
911 413 gate_size_t gate_str_print_int32(gate_char8_t* dst, gate_size_t dstlen, gate_int32_t num)
912 {
913 413 gate_size_t ret = 0;
914
2/4
✓ Branch 0 taken 413 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 413 times.
✗ Branch 3 not taken.
413 if ((dst != NULL) && (dstlen != 0))
915 {
916 gate_uint32_t unum;
917
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 408 times.
413 GATE_STR_PRINT_INUM(ret, dst, dstlen, unum, num, gate_uint32_t, gate_str_print_uint32);
918 }
919 413 return ret;
920 }
921 1938 gate_size_t gate_str_print_uint64(gate_char8_t* dst, gate_size_t dstlen, gate_uint64_t num)
922 {
923 1938 gate_size_t ret = 0;
924
2/4
✓ Branch 0 taken 1938 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1938 times.
✗ Branch 3 not taken.
1938 if ((dst != NULL) && (dstlen != 0))
925 {
926 gate_char8_t* ptr;
927
8/8
✓ Branch 0 taken 1937 times.
✓ Branch 1 taken 3652 times.
✓ Branch 2 taken 5589 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1390 times.
✓ Branch 5 taken 548 times.
✓ Branch 7 taken 1937 times.
✓ Branch 8 taken 1 times.
5590 GATE_STR_PRINT_NUM(dst, dstlen, ptr, ret, gate_uint64_t, num, gate_str_reverse);
928 }
929 1938 return ret;
930 }
931 329 gate_size_t gate_str_print_int64(gate_char8_t* dst, gate_size_t dstlen, gate_int64_t num)
932 {
933 329 gate_size_t ret = 0;
934
2/4
✓ Branch 0 taken 329 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 329 times.
✗ Branch 3 not taken.
329 if ((dst != NULL) && (dstlen != 0))
935 {
936 gate_uint64_t unum;
937
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 289 times.
329 GATE_STR_PRINT_INUM(ret, dst, dstlen, unum, num, gate_uint64_t, gate_str_print_uint64);
938 }
939 329 return ret;
940 }
941
942
943 ✗ gate_size_t gate_str16_print_uint16(gate_char16_t* dst, gate_size_t dstlen, gate_uint16_t num)
944 {
945 ✗ gate_size_t ret = 0;
946 ✗ if ((dst != NULL) && (dstlen != 0))
947 {
948 gate_char16_t* ptr;
949 ✗ GATE_STR_PRINT_NUM(dst, dstlen, ptr, ret, gate_uint16_t, num, gate_str16_reverse);
950 }
951 ✗ return ret;
952 }
953 ✗ gate_size_t gate_str16_print_int16(gate_char16_t* dst, gate_size_t dstlen, gate_int16_t num)
954 {
955 ✗ gate_size_t ret = 0;
956 ✗ if ((dst != NULL) && (dstlen != 0))
957 {
958 gate_uint16_t unum;
959 ✗ GATE_STR_PRINT_INUM(ret, dst, dstlen, unum, num, gate_uint16_t, gate_str16_print_uint16);
960 }
961 ✗ return ret;
962 }
963 ✗ gate_size_t gate_str16_print_uint32(gate_char16_t* dst, gate_size_t dstlen, gate_uint32_t num)
964 {
965 ✗ gate_size_t ret = 0;
966 ✗ if ((dst != NULL) && (dstlen != 0))
967 {
968 gate_char16_t* ptr;
969 ✗ GATE_STR_PRINT_NUM(dst, dstlen, ptr, ret, gate_uint32_t, num, gate_str16_reverse);
970 }
971 ✗ return ret;
972 }
973 ✗ gate_size_t gate_str16_print_int32(gate_char16_t* dst, gate_size_t dstlen, gate_int32_t num)
974 {
975 ✗ gate_size_t ret = 0;
976 ✗ if ((dst != NULL) && (dstlen != 0))
977 {
978 gate_uint32_t unum;
979 ✗ GATE_STR_PRINT_INUM(ret, dst, dstlen, unum, num, gate_uint32_t, gate_str16_print_uint32);
980 }
981 ✗ return ret;
982 }
983 ✗ gate_size_t gate_str16_print_uint64(gate_char16_t* dst, gate_size_t dstlen, gate_uint64_t num)
984 {
985 ✗ gate_size_t ret = 0;
986 ✗ if ((dst != NULL) && (dstlen != 0))
987 {
988 gate_char16_t* ptr;
989 ✗ GATE_STR_PRINT_NUM(dst, dstlen, ptr, ret, gate_uint64_t, num, gate_str16_reverse);
990 }
991 ✗ return ret;
992 }
993 ✗ gate_size_t gate_str16_print_int64(gate_char16_t* dst, gate_size_t dstlen, gate_int64_t num)
994 {
995 ✗ gate_size_t ret = 0;
996 ✗ if ((dst != NULL) && (dstlen != 0))
997 {
998 gate_uint64_t unum;
999 ✗ GATE_STR_PRINT_INUM(ret, dst, dstlen, unum, num, gate_uint64_t, gate_str16_print_uint64);
1000 }
1001 ✗ return ret;
1002 }
1003
1004
1005 ✗ gate_size_t gate_str32_print_uint16(gate_char32_t* dst, gate_size_t dstlen, gate_uint16_t num)
1006 {
1007 ✗ gate_size_t ret = 0;
1008 ✗ if ((dst != NULL) && (dstlen != 0))
1009 {
1010 gate_char32_t* ptr;
1011 ✗ GATE_STR_PRINT_NUM(dst, dstlen, ptr, ret, gate_uint16_t, num, gate_str32_reverse);
1012 }
1013 ✗ return ret;
1014 }
1015 ✗ gate_size_t gate_str32_print_int16(gate_char32_t* dst, gate_size_t dstlen, gate_int16_t num)
1016 {
1017 ✗ gate_size_t ret = 0;
1018 ✗ if ((dst != NULL) && (dstlen != 0))
1019 {
1020 gate_uint16_t unum;
1021 ✗ GATE_STR_PRINT_INUM(ret, dst, dstlen, unum, num, gate_uint16_t, gate_str32_print_uint16);
1022 }
1023 ✗ return ret;
1024 }
1025 ✗ gate_size_t gate_str32_print_uint32(gate_char32_t* dst, gate_size_t dstlen, gate_uint32_t num)
1026 {
1027 ✗ gate_size_t ret = 0;
1028 ✗ if ((dst != NULL) && (dstlen != 0))
1029 {
1030 gate_char32_t* ptr;
1031 ✗ GATE_STR_PRINT_NUM(dst, dstlen, ptr, ret, gate_uint32_t, num, gate_str32_reverse);
1032 }
1033 ✗ return ret;
1034 }
1035 ✗ gate_size_t gate_str32_print_int32(gate_char32_t* dst, gate_size_t dstlen, gate_int32_t num)
1036 {
1037 ✗ gate_size_t ret = 0;
1038 ✗ if ((dst != NULL) && (dstlen != 0))
1039 {
1040 gate_uint32_t unum;
1041 ✗ GATE_STR_PRINT_INUM(ret, dst, dstlen, unum, num, gate_uint32_t, gate_str32_print_uint32);
1042 }
1043 ✗ return ret;
1044 }
1045 ✗ gate_size_t gate_str32_print_uint64(gate_char32_t* dst, gate_size_t dstlen, gate_uint64_t num)
1046 {
1047 ✗ gate_size_t ret = 0;
1048 ✗ if ((dst != NULL) && (dstlen != 0))
1049 {
1050 gate_char32_t* ptr;
1051 ✗ GATE_STR_PRINT_NUM(dst, dstlen, ptr, ret, gate_uint64_t, num, gate_str32_reverse);
1052 }
1053 ✗ return ret;
1054 }
1055 ✗ gate_size_t gate_str32_print_int64(gate_char32_t* dst, gate_size_t dstlen, gate_int64_t num)
1056 {
1057 ✗ gate_size_t ret = 0;
1058 ✗ if ((dst != NULL) && (dstlen != 0))
1059 {
1060 gate_uint64_t unum;
1061 ✗ GATE_STR_PRINT_INUM(ret, dst, dstlen, unum, num, gate_uint64_t, gate_str32_print_uint64);
1062 }
1063 ✗ return ret;
1064 }
1065
1066
1067
1068
1069 504 gate_size_t gate_str_print_uint(gate_char8_t* dst, gate_size_t dstlen, gate_uint64_t num, gate_size_t intlen)
1070 {
1071 504 gate_size_t ret = 0;
1072 gate_char8_t buffer[24];
1073 504 gate_char8_t* ptr = &buffer[0];
1074 504 gate_size_t bytesused = gate_str_print_uint64(ptr, sizeof(buffer), num);
1075
1076
2/2
✓ Branch 0 taken 314 times.
✓ Branch 1 taken 190 times.
504 if (intlen > 0)
1077 {
1078
2/2
✓ Branch 0 taken 154 times.
✓ Branch 1 taken 160 times.
314 if (intlen > bytesused)
1079 {
1080 gate_size_t ndx;
1081
3/4
✓ Branch 0 taken 164 times.
✓ Branch 1 taken 154 times.
✓ Branch 2 taken 164 times.
✗ Branch 3 not taken.
318 for (ndx = bytesused; (ndx < intlen) && (dstlen != 0); ++ndx)
1082 {
1083 164 *dst = '0';
1084 164 ++dst;
1085 164 --dstlen;
1086 164 ++ret;
1087 }
1088 }
1089
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 156 times.
160 else if (bytesused > intlen)
1090 {
1091 4 ptr += (bytesused - intlen);
1092 4 bytesused -= intlen;
1093 }
1094 }
1095
1096
2/2
✓ Branch 0 taken 502 times.
✓ Branch 1 taken 2 times.
504 if (dstlen >= bytesused)
1097 {
1098 502 gate_mem_copy(dst, ptr, bytesused);
1099 502 ret += bytesused;
1100 502 dst += bytesused;
1101 502 dstlen -= bytesused;
1102 }
1103
1104
2/2
✓ Branch 0 taken 447 times.
✓ Branch 1 taken 57 times.
504 if (dstlen != 0)
1105 {
1106 447 *dst = '\0';
1107 }
1108 504 return ret;
1109
1110 }
1111
1112 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)
1113 {
1114 11 gate_size_t ret = 0;
1115 11 gate_size_t count = 0;
1116 11 gate_size_t mod = 0;
1117 gate_size_t len;
1118 gate_size_t ndx;
1119
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 if (grouplen != 0)
1120 {
1121 11 count = srclen / grouplen;
1122 11 mod = srclen % grouplen;
1123 }
1124
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 3 times.
11 if (mod != 0)
1125 {
1126 8 len = gate_str_print_text(&bufferptr[ret], bufferlen - ret, src, mod);
1127 8 ret += len;
1128
1129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (count != 0)
1130 {
1131 ✗ bufferptr[ret] = ' ';
1132 ✗ ++ret;
1133 }
1134
1135 8 src += mod;
1136 8 srclen -= mod;
1137 }
1138
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 11 times.
15 for (ndx = 0; ndx != count; )
1139 {
1140 4 ++ndx;
1141 4 len = gate_str_print_text(&bufferptr[ret], bufferlen - ret, src, grouplen);
1142 4 ret += len;
1143
1144
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (ndx != count)
1145 {
1146 1 bufferptr[ret] = ' ';
1147 1 ++ret;
1148 }
1149
1150 4 src += mod;
1151 4 srclen -= mod;
1152 }
1153 11 return ret;
1154 }
1155
1156 388 gate_size_t gate_str_print_real(gate_char8_t* dst, gate_size_t dstlen, gate_real64_t num,
1157 unsigned intlen, unsigned decimallen, unsigned grouplen)
1158 {
1159 388 gate_size_t ret = 0;
1160 gate_real64_t decimalnum;
1161 gate_real64_t factor;
1162 int infini;
1163 gate_char8_t intbuffer[64];
1164 gate_char8_t decimalbuffer[64];
1165 gate_char8_t buffer[64];
1166 388 gate_char8_t* bufferptr = &buffer[0];
1167 388 gate_size_t bufferlen = sizeof(buffer) - 1;
1168
1169 388 gate_size_t intcount = 0;
1170 388 gate_size_t decimalcount = 0;
1171 388 gate_size_t bufferused = 0;
1172 gate_size_t ndx;
1173
1174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 388 times.
388 if (dstlen < 3)
1175 {
1176 ✗ return 0;
1177 }
1178
1179
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 388 times.
388 if (gate_math_isnan(num))
1180 {
1181 ✗ return gate_str_print_text(dst, dstlen, "NaN", 3);
1182 }
1183 388 infini = gate_math_isinfinite(num);
1184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 388 times.
388 if (infini > 0)
1185 {
1186 ✗ return gate_str_print_text(dst, dstlen, "Inf+", 4);
1187 }
1188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 388 times.
388 else if (infini < 0)
1189 {
1190 ✗ return gate_str_print_text(dst, dstlen, "Inf-", 4);
1191 }
1192
1193
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 388 times.
388 if (gate_math_iszero(num))
1194 {
1195 ✗ intbuffer[0] = '0';
1196 ✗ intcount = 1;
1197 }
1198 else
1199 {
1200
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 377 times.
388 if (num < 0.0)
1201 {
1202 11 *bufferptr = '-';
1203 11 ++bufferptr;
1204 11 --bufferlen;
1205 11 ++bufferused;
1206 11 num = -num;
1207 }
1208
1209 388 intcount = gate_str_print_uint64(intbuffer, sizeof(intbuffer) - 1, (gate_uint64_t)num);
1210 388 decimalnum = num - (gate_real64_t)(gate_int64_t)num;
1211 388 factor = 1.0;
1212
3/4
✓ Branch 1 taken 1944 times.
✓ Branch 2 taken 388 times.
✓ Branch 3 taken 1944 times.
✗ Branch 4 not taken.
2332 while (!gate_math_iszerof((gate_real32_t)(decimalnum * factor)) && (decimalcount < sizeof(decimalbuffer) - 1))
1213 {
1214 1944 factor *= 0.1;
1215 1944 decimalnum *= 10.0;
1216 1944 decimalbuffer[decimalcount] = '0' + (gate_char8_t)(gate_uint64_t)decimalnum;
1217 1944 ++decimalcount;
1218 1944 decimalnum -= (gate_real64_t)(gate_int64_t)decimalnum;
1219 }
1220 }
1221
1222
1223
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 387 times.
388 if (intlen > 0)
1224 {
1225 1 ret = 0;
1226
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (intlen > intcount)
1227 {
1228 ✗ for (ndx = 0; ndx < (intlen - intcount); ++ndx)
1229 {
1230 ✗ *dst = '0';
1231 ✗ ++dst;
1232 ✗ --dstlen;
1233 ✗ ++ret;
1234 }
1235 ✗ ndx = gate_str_print_text(bufferptr, bufferlen, intbuffer, intcount);
1236 }
1237 else
1238 {
1239
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (grouplen != 0)
1240 {
1241 1 ndx = gate_str_print_grouped(bufferptr, bufferlen, &intbuffer[intcount - intlen], intlen, grouplen);
1242 }
1243 else
1244 {
1245 ✗ ndx = gate_str_print_text(bufferptr, bufferlen, &intbuffer[intcount - intlen], intlen);
1246 }
1247 }
1248 }
1249 else
1250 {
1251
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 377 times.
387 if (grouplen != 0)
1252 {
1253 10 ndx = gate_str_print_grouped(bufferptr, bufferlen, &intbuffer[0], intcount, grouplen);
1254 }
1255 else
1256 {
1257 377 ndx = gate_str_print_text(bufferptr, bufferlen, &intbuffer[0], intcount);
1258 }
1259 }
1260 388 bufferptr += ndx;
1261 388 bufferlen -= ndx;
1262 388 bufferused += ndx;
1263
1264
1265
1266
4/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 380 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 4 times.
388 if ((decimallen > 0) || (decimalcount > 0))
1267 {
1268 384 *bufferptr = '.';
1269 384 ++bufferptr;
1270 384 --bufferlen;
1271 384 ++bufferused;
1272
1273
2/2
✓ Branch 0 taken 380 times.
✓ Branch 1 taken 4 times.
384 if (decimallen > 0)
1274 {
1275
2/2
✓ Branch 0 taken 107 times.
✓ Branch 1 taken 273 times.
380 if (decimallen > decimalcount)
1276 {
1277 107 ndx = gate_str_print_text(bufferptr, bufferlen, &decimalbuffer[0], decimalcount);
1278
2/2
✓ Branch 0 taken 529 times.
✓ Branch 1 taken 107 times.
636 for (; ndx < decimallen; ++ndx)
1279 {
1280 529 bufferptr[ndx] = '0';
1281 }
1282 }
1283 else
1284 {
1285 273 ndx = gate_str_print_text(bufferptr, bufferlen, &decimalbuffer[0], decimallen);
1286 }
1287 }
1288 else
1289 {
1290 4 ndx = gate_str_print_text(bufferptr, bufferlen, &decimalbuffer[0], decimalcount);
1291 }
1292 384 bufferptr += ndx;
1293 384 bufferlen -= ndx;
1294 384 bufferused += ndx;
1295 }
1296
1297 388 return gate_str_print_text(dst, dstlen, &buffer[0], bufferused);
1298 }
1299
1300 568 gate_char8_t gate_str_print_hex_nibble(gate_uint8_t nibble, gate_bool_t uppercase)
1301 {
1302
2/2
✓ Branch 0 taken 495 times.
✓ Branch 1 taken 73 times.
568 if (nibble < 10)
1303 {
1304 495 return '0' + nibble;
1305 }
1306 else
1307 {
1308
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 49 times.
73 if (uppercase)
1309 {
1310 24 return 'A' + (nibble - 10);
1311 }
1312 else
1313 {
1314 49 return 'a' + (nibble - 10);
1315 }
1316 }
1317 }
1318
1319 278 gate_size_t gate_str_print_hex_byte(gate_char8_t* dst, gate_size_t dstlen, gate_uint8_t num, gate_bool_t uppercase)
1320 {
1321
2/4
✓ Branch 0 taken 278 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 278 times.
278 if ((dst == NULL) || (dstlen < 2))
1322 {
1323 ✗ return 0;
1324 }
1325 278 dst[0] = gate_str_print_hex_nibble((gate_uint8_t)((num >> 4) & 0x0f), uppercase);
1326 278 dst[1] = gate_str_print_hex_nibble((gate_uint8_t)((num) & 0x0f), uppercase);
1327
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 257 times.
278 if (dstlen > 2)
1328 {
1329 21 dst[2] = '\0';
1330 }
1331 278 return 2;
1332 }
1333 85 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)
1334 {
1335 85 gate_size_t cnt = 8;
1336 gate_uint8_t bytes[8];
1337 85 gate_uint8_t* ptr = &bytes[0];
1338
2/2
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 2 times.
85 if (numlen < 8)
1339 {
1340 83 ptr += 8 - numlen;
1341 83 cnt -= 8 - numlen;
1342 }
1343 85 bytes[0] = (gate_uint8_t)((num >> 56) & 0xff);
1344 85 bytes[1] = (gate_uint8_t)((num >> 48) & 0xff);
1345 85 bytes[2] = (gate_uint8_t)((num >> 40) & 0xff);
1346 85 bytes[3] = (gate_uint8_t)((num >> 32) & 0xff);
1347 85 bytes[4] = (gate_uint8_t)((num >> 24) & 0xff);
1348 85 bytes[5] = (gate_uint8_t)((num >> 16) & 0xff);
1349 85 bytes[6] = (gate_uint8_t)((num >> 8) & 0xff);
1350 85 bytes[7] = (gate_uint8_t)((num) & 0xff);
1351
2/4
✓ Branch 0 taken 85 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 85 times.
85 if ((dst == NULL) || (dstlen < cnt * 2))
1352 {
1353 ✗ return 0;
1354 }
1355
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 21 times.
85 if (dstlen > cnt * 2)
1356 {
1357 64 dst[cnt * 2] = 0;
1358 }
1359 85 dstlen = cnt * 2;
1360
2/2
✓ Branch 0 taken 173 times.
✓ Branch 1 taken 85 times.
258 while (cnt != 0)
1361 {
1362 173 gate_str_print_hex_byte(dst, 2, *ptr, uppercase);
1363 173 dst += 2;
1364 173 ++ptr;
1365 173 --cnt;
1366 }
1367 85 return dstlen;
1368 }
1369 8 gate_size_t gate_str_print_hex_num(gate_char8_t* dst, gate_size_t dstlen, gate_uint64_t num, gate_bool_t uppercase)
1370 {
1371 8 gate_size_t numlen = 8;
1372
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 5 times.
8 if (num < 256) { numlen = 1; }
1373
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 else if (num < 65536) { numlen = 2; }
1374
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 else if (num < 16777216) { numlen = 3; }
1375
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 else if (num < 4294967296) { numlen = 4; }
1376 ✗ else if (num < 4294967296 * 256) { numlen = 5; }
1377 ✗ else if (num < 4294967296 * 256 * 256) { numlen = 6; }
1378 ✗ else if (num < 4294967296 * 256 * 256 * 256) { numlen = 7; }
1379
1380 8 return gate_str_print_hex(dst, dstlen, num, numlen, uppercase);
1381 }
1382 38 gate_size_t gate_str_print_hex_uint16(gate_char8_t* dst, gate_size_t dstlen, gate_uint16_t num, gate_bool_t uppercase)
1383 {
1384 38 return gate_str_print_hex(dst, dstlen, num, 2, uppercase);
1385 }
1386 7 gate_size_t gate_str_print_hex_uint32(gate_char8_t* dst, gate_size_t dstlen, gate_uint32_t num, gate_bool_t uppercase)
1387 {
1388 7 return gate_str_print_hex(dst, dstlen, num, 4, uppercase);
1389 }
1390 ✗ gate_size_t gate_str_print_hex_uint64(gate_char8_t* dst, gate_size_t dstlen, gate_uint64_t num, gate_bool_t uppercase)
1391 {
1392 ✗ return gate_str_print_hex(dst, dstlen, num, 8, uppercase);
1393 }
1394
1395 ✗ 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)
1396 {
1397 ✗ gate_size_t ret = 0;
1398 ✗ gate_uint8_t const* ptr = (gate_uint8_t const*)buffer;
1399 ✗ while ((dstlen > 1) && (bufferlen != 0))
1400 {
1401 ✗ gate_str_print_hex_byte(dst, dstlen, *ptr, uppercase);
1402 ✗ dst += 2;
1403 ✗ dstlen -= 2;
1404 ✗ ret += 2;
1405 ✗ ++ptr;
1406 ✗ --bufferlen;
1407 }
1408 ✗ if (dstlen != 0)
1409 {
1410 ✗ *dst = 0;
1411 }
1412 ✗ return ret;
1413 }
1414
1415 553 gate_size_t gate_str_print_to(gate_str_printer_t printer, void* printer_param, va_list vl)
1416 {
1417 553 gate_size_t used = 0;
1418 553 gate_bool_t continue_loop = true;
1419 char buffer[512];
1420
1421 /* INFO: types small than [int] are converted to [int]
1422 https://stackoverflow.com/questions/23983471/char-is-promoted-to-int-when-passed-through-in-c
1423 */
1424
2/2
✓ Branch 0 taken 2528 times.
✓ Branch 1 taken 553 times.
3081 while (continue_loop)
1425 {
1426 2528 int print_type = va_arg(vl, int);
1427 2528 char const* ptr_text = NULL;
1428 2528 gate_size_t text_len = 0;
1429
1430
17/25
✓ Branch 0 taken 551 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 29 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 17 times.
✓ Branch 6 taken 1208 times.
✓ Branch 7 taken 125 times.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 16 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 10 times.
✓ Branch 13 taken 39 times.
✓ Branch 14 taken 8 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 494 times.
✓ Branch 17 taken 16 times.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✓ Branch 22 taken 6 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 2 times.
2528 switch (print_type)
1431 {
1432 551 case GATE_PRINT_END:
1433 {
1434 551 text_len = 0;
1435 551 continue_loop = false;
1436 551 break;
1437 }
1438 3 case GATE_PRINT_CHAR:
1439 {
1440 3 buffer[0] = (char)va_arg(vl, int);
1441 3 ptr_text = &buffer[0];
1442 3 text_len = 1;
1443 3 break;
1444 }
1445 29 case GATE_PRINT_NEWLINE:
1446 {
1447 29 ptr_text = GATE_STR_NEWLINE;
1448 29 text_len = GATE_STR_NEWLINE_LENGTH;
1449 29 break;
1450 }
1451 1 case GATE_PRINT_CR:
1452 {
1453 1 ptr_text = GATE_STR_CR;
1454 1 text_len = 1;
1455 1 break;
1456 }
1457 1 case GATE_PRINT_LF:
1458 {
1459 1 ptr_text = GATE_STR_LF;
1460 1 text_len = 1;
1461 1 break;
1462 }
1463 17 case GATE_PRINT_CRLF:
1464 {
1465 17 ptr_text = GATE_STR_CRLF;
1466 17 text_len = 2;
1467 17 break;
1468 }
1469 1208 case GATE_PRINT_CSTR:
1470 {
1471 1208 char const* val_cstr = va_arg(vl, char const*);
1472 1208 ptr_text = val_cstr;
1473 1208 text_len = gate_str_length(val_cstr);
1474 1208 break;
1475 }
1476 125 case GATE_PRINT_STRING:
1477 {
1478 125 gate_string_t const* val_str = va_arg(vl, gate_string_t const*);
1479 125 ptr_text = gate_string_ptr(val_str, 0);
1480 125 text_len = gate_string_length(val_str);
1481 125 break;
1482 }
1483 2 case GATE_PRINT_BOOL:
1484 {
1485 2 gate_bool_t val_bool = (gate_bool_t)va_arg(vl, int);
1486
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 ptr_text = val_bool ? "true" : "false";
1487
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 text_len = val_bool ? 4 : 5;
1488 2 break;
1489 }
1490 ✗ case GATE_PRINT_I8:
1491 {
1492 ✗ gate_int8_t val_i8 = (gate_int8_t)va_arg(vl, int);
1493 ✗ text_len = gate_str_print_int16(buffer, sizeof(buffer), val_i8);
1494 ✗ ptr_text = buffer;
1495 ✗ break;
1496 }
1497 16 case GATE_PRINT_UI8:
1498 {
1499 16 gate_uint8_t val_ui8 = (gate_uint8_t)va_arg(vl, int);
1500 16 text_len = gate_str_print_uint16(buffer, sizeof(buffer), val_ui8);
1501 16 ptr_text = buffer;
1502 16 break;
1503 }
1504 ✗ case GATE_PRINT_I16:
1505 {
1506 ✗ gate_int16_t val_i16 = (gate_int16_t)va_arg(vl, int);
1507 ✗ text_len = gate_str_print_int16(buffer, sizeof(buffer), val_i16);
1508 ✗ ptr_text = buffer;
1509 ✗ break;
1510 }
1511 10 case GATE_PRINT_UI16:
1512 {
1513 10 gate_uint16_t val_ui16 = (gate_uint16_t)va_arg(vl, int);
1514 10 text_len = gate_str_print_uint16(buffer, sizeof(buffer), val_ui16);
1515 10 ptr_text = buffer;
1516 10 break;
1517 }
1518 39 case GATE_PRINT_I32:
1519 {
1520 39 gate_int32_t val_i32 = va_arg(vl, gate_int32_t);
1521 39 text_len = gate_str_print_int32(buffer, sizeof(buffer), val_i32);
1522 39 ptr_text = buffer;
1523 39 break;
1524 }
1525 8 case GATE_PRINT_UI32:
1526 {
1527 8 gate_uint32_t val_ui32 = va_arg(vl, gate_uint32_t);
1528 8 text_len = gate_str_print_uint32(buffer, sizeof(buffer), val_ui32);
1529 8 ptr_text = buffer;
1530 8 break;
1531 }
1532 ✗ case GATE_PRINT_I64:
1533 {
1534 ✗ gate_int64_t val_i64 = va_arg(vl, gate_int64_t);
1535 ✗ text_len = gate_str_print_int64(buffer, sizeof(buffer), val_i64);
1536 ✗ ptr_text = buffer;
1537 ✗ break;
1538 }
1539 494 case GATE_PRINT_UI64:
1540 {
1541 494 gate_uint64_t val_ui64 = va_arg(vl, gate_uint64_t);
1542 494 text_len = gate_str_print_uint64(buffer, sizeof(buffer), val_ui64);
1543 494 ptr_text = buffer;
1544 494 break;
1545 }
1546 16 case GATE_PRINT_H8:
1547 {
1548 16 gate_uint8_t val_ui8 = (gate_uint8_t)va_arg(vl, unsigned);
1549 16 text_len = gate_str_print_hex_byte(buffer, sizeof(buffer), val_ui8, false);
1550 16 ptr_text = buffer;
1551 16 break;
1552 }
1553 ✗ case GATE_PRINT_H16:
1554 {
1555 ✗ gate_uint16_t val_ui16 = (gate_uint16_t)va_arg(vl, unsigned);
1556 ✗ text_len = gate_str_print_hex(buffer, sizeof(buffer), val_ui16, 2, false);
1557 ✗ ptr_text = buffer;
1558 ✗ break;
1559 }
1560 ✗ case GATE_PRINT_H32:
1561 {
1562 ✗ gate_uint32_t val_ui32 = va_arg(vl, gate_uint32_t);
1563 ✗ text_len = gate_str_print_hex(buffer, sizeof(buffer), val_ui32, 4, false);
1564 ✗ ptr_text = buffer;
1565 ✗ break;
1566 }
1567 ✗ case GATE_PRINT_H64:
1568 {
1569 ✗ gate_uint64_t val_ui64 = va_arg(vl, gate_uint64_t);
1570 ✗ text_len = gate_str_print_hex(buffer, sizeof(buffer), val_ui64, 8, false);
1571 ✗ ptr_text = buffer;
1572 ✗ break;
1573 }
1574 ✗ case GATE_PRINT_R32:
1575 {
1576 ✗ gate_real32_t val_r32 = (gate_real32_t)va_arg(vl, double);
1577 ✗ text_len = gate_str_print_real(buffer, sizeof(buffer), val_r32, 0, 6, 0);
1578 ✗ ptr_text = buffer;
1579 ✗ break;
1580 }
1581 6 case GATE_PRINT_R64:
1582 {
1583 6 gate_real64_t val_r64 = va_arg(vl, gate_real64_t);
1584 6 text_len = gate_str_print_real(buffer, sizeof(buffer), val_r64, 0, 6, 0);
1585 6 ptr_text = buffer;
1586 6 break;
1587 }
1588 ✗ case GATE_PRINT_ADDRESS:
1589 {
1590 ✗ void* val_ptr = va_arg(vl, void*);
1591 ✗ text_len = gate_str_print_hex(buffer, sizeof(buffer), (gate_uint64_t)(gate_uintptr_t)val_ptr, sizeof(val_ptr) * 2, true);
1592 ✗ ptr_text = buffer;
1593 ✗ break;
1594 }
1595 2 default:
1596 {
1597 2 text_len = 0;
1598 2 continue_loop = false;
1599 2 break;
1600 }
1601 }
1602
2/2
✓ Branch 0 taken 1972 times.
✓ Branch 1 taken 556 times.
2528 if (text_len > 0)
1603 {
1604 1972 gate_size_t chars_printed = printer(printer_param, ptr_text, text_len);
1605 1972 used += chars_printed;
1606 }
1607 }
1608 553 return used;
1609 }
1610
1611 struct gate_str_printer_param
1612 {
1613 gate_char8_t* dst;
1614 gate_size_t dstlen;
1615 };
1616
1617 1373 static gate_size_t GATE_CALL gate_str_printer_callback(void* printer_param, gate_char8_t const* data, gate_size_t data_len)
1618 {
1619 1373 struct gate_str_printer_param* param = (struct gate_str_printer_param*)printer_param;
1620 1373 gate_size_t len_printed = gate_str_print_text(param->dst, param->dstlen, data, data_len);
1621 1373 param->dst += len_printed;
1622 1373 param->dstlen -= len_printed;
1623 1373 return len_printed;
1624 }
1625
1626 460 gate_size_t gate_str_print(gate_char8_t* dst, gate_size_t dstlen, ...)
1627 {
1628 struct gate_str_printer_param param;
1629 460 gate_size_t ret = 0;
1630 va_list vl;
1631
1632 460 va_start(vl, dstlen);
1633
1634 460 param.dst = dst;
1635 460 param.dstlen = dstlen;
1636 460 ret = gate_str_print_to(&gate_str_printer_callback, &param, vl);
1637
1638 460 va_end(vl);
1639 460 return ret;
1640 }
1641
1642 30 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)
1643 {
1644 30 gate_size_t used = 0;
1645 30 gate_bool_t continue_loop = true;
1646 gate_bool_t val_bool;
1647 gate_string_t const* val_str;
1648 gate_int8_t val_i8;
1649 gate_uint8_t val_ui8;
1650 gate_int16_t val_i16;
1651 gate_uint16_t val_ui16;
1652 gate_int32_t val_i32;
1653 gate_uint32_t val_ui32;
1654 gate_int64_t val_i64;
1655 gate_uint64_t val_ui64;
1656 gate_real32_t val_r32;
1657 gate_real64_t val_r64;
1658 30 char const* ptr_text = NULL;
1659 char buffer[512];
1660 gate_size_t text_len;
1661 gate_size_t chars_printed;
1662
1663
3/4
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
60 while ((values_count-- != 0) && continue_loop)
1664 {
1665 30 ptr_text = NULL;
1666 30 text_len = 0;
1667
20/25
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3 times.
✓ Branch 9 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 11 taken 2 times.
✓ Branch 12 taken 2 times.
✓ Branch 13 taken 2 times.
✓ Branch 14 taken 2 times.
✓ Branch 15 taken 2 times.
✓ Branch 16 taken 2 times.
✓ Branch 17 taken 1 times.
✓ Branch 18 taken 1 times.
✓ Branch 19 taken 1 times.
✓ Branch 20 taken 1 times.
✓ Branch 21 taken 1 times.
✓ Branch 22 taken 1 times.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
30 switch (values->value_type)
1668 {
1669 ✗ case GATE_PRINT_END:
1670 {
1671 ✗ text_len = 0;
1672 ✗ continue_loop = false;
1673 ✗ break;
1674 }
1675 ✗ case GATE_PRINT_CHAR:
1676 {
1677 ✗ ptr_text = (char const*)values->value_ptr;
1678 ✗ text_len = 1;
1679 ✗ break;
1680 }
1681 1 case GATE_PRINT_NEWLINE:
1682 {
1683 1 ptr_text = GATE_STR_NEWLINE;
1684 1 text_len = GATE_STR_NEWLINE_LENGTH;
1685 1 break;
1686 }
1687 1 case GATE_PRINT_CR:
1688 {
1689 1 ptr_text = GATE_STR_CR;
1690 1 text_len = 1;
1691 1 break;
1692 }
1693 1 case GATE_PRINT_LF:
1694 {
1695 1 ptr_text = GATE_STR_LF;
1696 1 text_len = 1;
1697 1 break;
1698 }
1699 1 case GATE_PRINT_CRLF:
1700 {
1701 1 ptr_text = GATE_STR_CRLF;
1702 1 text_len = 2;
1703 1 break;
1704 }
1705 1 case GATE_PRINT_CSTR:
1706 {
1707 1 ptr_text = (char const*)values->value_ptr;
1708 1 text_len = gate_str_length(ptr_text);
1709 1 break;
1710 }
1711 ✗ case GATE_PRINT_STRING:
1712 {
1713 ✗ val_str = (gate_string_t const*)values->value_ptr;
1714 ✗ ptr_text = val_str->str;
1715 ✗ text_len = val_str->length;
1716 ✗ break;
1717 }
1718 3 case GATE_PRINT_BOOL:
1719 {
1720 3 val_bool = *(gate_bool_t const*)values->value_ptr;
1721
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 ptr_text = val_bool ? "true" : "false";
1722
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 text_len = val_bool ? 4 : 5;
1723 3 break;
1724 }
1725 2 case GATE_PRINT_I8:
1726 {
1727 2 val_i8 = *(gate_int8_t const*)values->value_ptr;
1728 2 text_len = gate_str_print_int16(buffer, sizeof(buffer), val_i8);
1729 2 ptr_text = buffer;
1730 2 break;
1731 }
1732 2 case GATE_PRINT_UI8:
1733 {
1734 2 val_ui8 = *(gate_uint8_t const*)values->value_ptr;
1735 2 text_len = gate_str_print_uint16(buffer, sizeof(buffer), val_ui8);
1736 2 ptr_text = buffer;
1737 2 break;
1738 }
1739 2 case GATE_PRINT_I16:
1740 {
1741 2 val_i16 = *(gate_int16_t const*)values->value_ptr;
1742 2 text_len = gate_str_print_int16(buffer, sizeof(buffer), val_i16);
1743 2 ptr_text = buffer;
1744 2 break;
1745 }
1746 2 case GATE_PRINT_UI16:
1747 {
1748 2 val_ui16 = *(gate_uint16_t const*)values->value_ptr;
1749 2 text_len = gate_str_print_uint16(buffer, sizeof(buffer), val_ui16);
1750 2 ptr_text = buffer;
1751 2 break;
1752 }
1753 2 case GATE_PRINT_I32:
1754 {
1755 2 val_i32 = *(gate_int32_t const*)values->value_ptr;
1756 2 text_len = gate_str_print_int32(buffer, sizeof(buffer), val_i32);
1757 2 ptr_text = buffer;
1758 2 break;
1759 }
1760 2 case GATE_PRINT_UI32:
1761 {
1762 2 val_ui32 = *(gate_uint32_t const*)values->value_ptr;
1763 2 text_len = gate_str_print_uint32(buffer, sizeof(buffer), val_ui32);
1764 2 ptr_text = buffer;
1765 2 break;
1766 }
1767 2 case GATE_PRINT_I64:
1768 {
1769 2 val_i64 = *(gate_int64_t const*)values->value_ptr;
1770 2 text_len = gate_str_print_int64(buffer, sizeof(buffer), val_i64);
1771 2 ptr_text = buffer;
1772 2 break;
1773 }
1774 2 case GATE_PRINT_UI64:
1775 {
1776 2 val_ui64 = *(gate_uint64_t const*)values->value_ptr;
1777 2 text_len = gate_str_print_uint64(buffer, sizeof(buffer), val_ui64);
1778 2 ptr_text = buffer;
1779 2 break;
1780 }
1781 1 case GATE_PRINT_H8:
1782 {
1783 1 val_ui8 = *(gate_uint8_t const*)values->value_ptr;
1784 1 text_len = gate_str_print_hex(buffer, sizeof(buffer), val_ui8, 1, false);
1785 1 ptr_text = buffer;
1786 1 break;
1787 }
1788 1 case GATE_PRINT_H16:
1789 {
1790 1 val_ui16 = *(gate_uint16_t const*)values->value_ptr;
1791 1 text_len = gate_str_print_hex(buffer, sizeof(buffer), val_ui16, 2, false);
1792 1 ptr_text = buffer;
1793 1 break;
1794 }
1795 1 case GATE_PRINT_H32:
1796 {
1797 1 val_ui32 = *(gate_uint32_t const*)values->value_ptr;
1798 1 text_len = gate_str_print_hex(buffer, sizeof(buffer), val_ui32, 4, false);
1799 1 ptr_text = buffer;
1800 1 break;
1801 }
1802 1 case GATE_PRINT_H64:
1803 {
1804 1 val_ui64 = *(gate_uint64_t const*)values->value_ptr;
1805 1 text_len = gate_str_print_hex(buffer, sizeof(buffer), val_ui64, 8, false);
1806 1 ptr_text = buffer;
1807 1 break;
1808 }
1809 1 case GATE_PRINT_R32:
1810 {
1811 1 val_r32 = *(gate_real32_t const*)values->value_ptr;
1812 1 text_len = gate_str_print_real(buffer, sizeof(buffer), val_r32, 0, 6, 0);
1813 1 ptr_text = buffer;
1814 1 break;
1815 }
1816 1 case GATE_PRINT_R64:
1817 {
1818 1 val_r64 = *(gate_real64_t const*)values->value_ptr;
1819 1 text_len = gate_str_print_real(buffer, sizeof(buffer), val_r64, 0, 6, 0);
1820 1 ptr_text = buffer;
1821 1 break;
1822 }
1823 ✗ case GATE_PRINT_ADDRESS:
1824 {
1825 ✗ text_len = gate_str_print_hex(buffer, sizeof(buffer), *(gate_uintptr_t*)values->value_ptr, sizeof(gate_uintptr_t) * 2, true);
1826 ✗ ptr_text = buffer;
1827 ✗ break;
1828 }
1829 ✗ default:
1830 {
1831 ✗ text_len = 0;
1832 ✗ continue_loop = false;
1833 ✗ break;
1834 }
1835 }
1836
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (text_len > 0)
1837 {
1838 30 chars_printed = gate_str_print_text(dst, dstlen, ptr_text, text_len);
1839 30 dst += chars_printed;
1840 30 dstlen -= chars_printed;
1841 30 used += chars_printed;
1842 }
1843 30 ++values;
1844 }
1845 30 return used;
1846 }
1847
1848
1849
1850
1851 2870 gate_bool_t gate_str_parse_digit(gate_char8_t const* src, gate_uint8_t* digit)
1852 {
1853 2870 gate_char8_t chr = *src;
1854
4/4
✓ Branch 0 taken 2498 times.
✓ Branch 1 taken 372 times.
✓ Branch 2 taken 2352 times.
✓ Branch 3 taken 146 times.
2870 if ((chr >= '0') && (chr <= '9'))
1855 {
1856 2352 *digit = chr - '0';
1857 2352 return true;
1858 }
1859 else
1860 {
1861 518 return false;
1862 }
1863 }
1864
1865 9 gate_size_t gate_str_parse_bool(gate_char8_t const* src, gate_size_t srclen, gate_bool_t* value)
1866 {
1867
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
9 if (srclen == 0)
1868 {
1869 1 *value = false;
1870 1 return 0;
1871 }
1872
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 7 times.
8 if (gate_str_starts_with_ic(src, srclen, "false", 5))
1873 {
1874 1 *value = false;
1875 1 return 5;
1876 }
1877
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 6 times.
7 if (gate_str_starts_with_ic(src, srclen, "off", 3))
1878 {
1879 1 *value = false;
1880 1 return 3;
1881 }
1882
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 5 times.
6 if (gate_str_starts_with_ic(src, srclen, "0", 1))
1883 {
1884 1 *value = false;
1885 1 return 1;
1886 }
1887
1888
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
5 if (gate_str_starts_with_ic(src, srclen, "true", 4))
1889 {
1890 1 *value = true;
1891 1 return 4;
1892 }
1893
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
4 if (gate_str_starts_with_ic(src, srclen, "on", 2))
1894 {
1895 1 *value = true;
1896 1 return 2;
1897 }
1898
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
3 if (gate_str_starts_with_ic(src, srclen, "yes", 3))
1899 {
1900 1 *value = true;
1901 1 return 3;
1902 }
1903
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
2 if (gate_str_starts_with_ic(src, srclen, "1", 1))
1904 {
1905 1 *value = true;
1906 1 return 1;
1907 }
1908
1909 /* all other types of string will evaluate to true */
1910 1 *value = false;
1911 1 return srclen;
1912 }
1913
1914
1915 1008 gate_size_t gate_str_parse_uint64(gate_char8_t const* src, gate_size_t srclen, gate_uint64_t* num)
1916 {
1917 1008 gate_size_t ret = 0;
1918 gate_uint8_t digit;
1919 1008 gate_uint64_t tmp = 0;
1920
2/2
✓ Branch 0 taken 2473 times.
✓ Branch 1 taken 556 times.
3029 while (srclen-- != 0)
1921 {
1922
2/2
✓ Branch 1 taken 452 times.
✓ Branch 2 taken 2021 times.
2473 if (!gate_str_parse_digit(src, &digit))
1923 {
1924 452 break;
1925 }
1926 2021 tmp *= 10;
1927 2021 tmp += (gate_uint64_t)digit;
1928 2021 ++src;
1929 2021 ++ret;
1930 }
1931
1/2
✓ Branch 0 taken 1008 times.
✗ Branch 1 not taken.
1008 if (num != NULL)
1932 {
1933 1008 *num = tmp;
1934 }
1935
1936 1008 return ret;
1937 }
1938 333 gate_size_t gate_str_parse_int64(gate_char8_t const* src, gate_size_t srclen, gate_int64_t* num)
1939 {
1940 333 gate_size_t ret = 0;
1941 gate_size_t tmpret;
1942 333 gate_uint64_t tmpnum = 0;
1943 333 gate_bool_t neg = false;
1944
1945 333 ret = gate_str_find_first_not_of(src, srclen, GATE_STR_WHITESPACES, GATE_STR_WHITESPACES_LENGTH, 0);
1946
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 322 times.
333 if (ret == GATE_STR_NPOS)
1947 {
1948 11 return 0;
1949 }
1950 322 src += ret;
1951 322 srclen -= ret;
1952
1953
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 300 times.
322 if (*src == '-')
1954 {
1955 22 neg = true;
1956 22 --srclen;
1957 22 ++src;
1958 22 ++ret;
1959 }
1960
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 299 times.
300 else if (*src == '+')
1961 {
1962 1 --srclen;
1963 1 ++src;
1964 1 ++ret;
1965 }
1966
1/2
✓ Branch 0 taken 322 times.
✗ Branch 1 not taken.
322 tmpret = gate_str_parse_uint64(src, srclen, num ? &tmpnum : NULL);
1967
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 306 times.
322 if (tmpret == 0)
1968 {
1969 16 return 0;
1970 }
1971 306 ret += tmpret;
1972
1/2
✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
306 if (num != NULL)
1973 {
1974
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 284 times.
306 if (neg)
1975 {
1976 22 *num = -((gate_int64_t)tmpnum);
1977 }
1978 else
1979 {
1980 284 *num = (gate_int64_t)tmpnum;
1981 }
1982 }
1983 306 return ret;
1984 }
1985
1986
1987 126 gate_size_t gate_str_parse_real(gate_char8_t const* src, gate_size_t srclen, gate_real64_t* num)
1988 {
1989 126 gate_int64_t int_part = 0;
1990 gate_uint8_t digit;
1991 126 gate_size_t ret = gate_str_parse_int64(src, srclen, &int_part);
1992 126 gate_bool_t const is_negative = int_part < 0;
1993 126 gate_real64_t a = 0.0;
1994
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 116 times.
126 gate_real64_t b = is_negative ? -1.0 : 1.0;
1995 126 src += ret;
1996 126 srclen -= ret;
1997
1998
4/4
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 72 times.
✓ Branch 3 taken 52 times.
126 if ((srclen > 0) && (src[0] == '.'))
1999 {
2000 72 ++ret;
2001 72 ++src;
2002 72 --srclen;
2003
2/2
✓ Branch 0 taken 378 times.
✓ Branch 1 taken 6 times.
384 while (srclen > 0)
2004 {
2005
2/2
✓ Branch 1 taken 66 times.
✓ Branch 2 taken 312 times.
378 if (!gate_str_parse_digit(src, &digit))
2006 {
2007 66 break;
2008 }
2009 312 ++src;
2010 312 ++ret;
2011 312 --srclen;
2012 312 a *= 10.0;
2013 312 b *= 10.0;
2014 312 a += (gate_real64_t)digit;
2015 }
2016 }
2017
1/2
✓ Branch 0 taken 126 times.
✗ Branch 1 not taken.
126 if (num)
2018 {
2019 126 *num = (gate_real64_t)int_part + a / b;
2020 }
2021 126 return ret;
2022 }
2023
2024 1515 gate_bool_t gate_str_parse_hex_nibble(gate_char8_t chr, gate_uint8_t* value)
2025 {
2026
3/4
✓ Branch 0 taken 1515 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 701 times.
✓ Branch 3 taken 814 times.
1515 if ((chr >= '0') && (chr <= '9'))
2027 {
2028 701 *value = (gate_uint8_t)(chr - '0');
2029 701 return true;
2030 }
2031
4/4
✓ Branch 0 taken 800 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 777 times.
✓ Branch 3 taken 23 times.
814 else if ((chr >= 'A') && (chr <= 'F'))
2032 {
2033 777 *value = (gate_uint8_t)(chr - 'A' + 10);
2034 777 return true;
2035 }
2036
3/4
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
37 else if ((chr >= 'a') && (chr <= 'f'))
2037 {
2038 23 *value = (gate_uint8_t)(chr - 'a' + 10);
2039 23 return true;
2040 }
2041 else
2042 {
2043 14 return false;
2044 }
2045 }
2046 68 gate_bool_t gate_str_parse_hex_byte(gate_char8_t const chr[2], gate_uint8_t* value)
2047 {
2048 gate_uint8_t n1, n2;
2049
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 68 times.
68 if (!gate_str_parse_hex_nibble(chr[0], &n1))
2050 {
2051 ✗ return false;
2052 }
2053
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 68 times.
68 if (!gate_str_parse_hex_nibble(chr[1], &n2))
2054 {
2055 ✗ return false;
2056 }
2057 68 *value = n1 * 16 + n2;
2058 68 return true;
2059 }
2060 32 gate_size_t gate_str_parse_hex_int(gate_char8_t const* text, gate_size_t textlen, gate_uint64_t* num)
2061 {
2062 32 gate_size_t ret = 0;
2063 32 gate_uint64_t val = 0;
2064 gate_uint8_t n;
2065
2066
2/2
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 18 times.
116 while (textlen-- != 0)
2067 {
2068
2/2
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 84 times.
98 if (!gate_str_parse_hex_nibble(*text, &n))
2069 {
2070 14 break;
2071 }
2072 84 val *= 16;
2073 84 val += n;
2074 84 ++ret;
2075 84 ++text;
2076 }
2077
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (num != NULL)
2078 {
2079 32 *num = val;
2080 }
2081 32 return ret;
2082 }
2083 5 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)
2084 {
2085 5 gate_size_t ret = 0;
2086 gate_uint8_t tmp;
2087
2088
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
21 while ((textlen >= 2) && (bufferlen != 0))
2089 {
2090
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
16 if (!gate_str_parse_hex_byte(text, &tmp))
2091 {
2092 ✗ break;
2093 }
2094 16 *buffer = (char)tmp;
2095
2096 16 text += 2;
2097 16 textlen -= 2;
2098
2099 16 ++buffer;
2100 16 --bufferlen;
2101
2102 16 ++ret;
2103 }
2104 5 return ret;
2105 }
2106 2 gate_bool_t gate_str_parse_oct_byte(gate_char8_t const chr[3], gate_uint8_t* value)
2107 {
2108 2 gate_uint8_t digits[3] = GATE_INIT_EMPTY;
2109
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (gate_str_parse_digit(&chr[0], &digits[0])
2110
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 && gate_str_parse_digit(&chr[1], &digits[1])
2111
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 && gate_str_parse_digit(&chr[2], &digits[2])
2112 )
2113 {
2114 2 *value = (gate_uint8_t)((int)digits[0] * 64 + (int)digits[1] * 8 + (int)digits[2]);
2115 2 return true;
2116 }
2117 ✗ return false;
2118 }
2119 159 gate_size_t gate_str_parse_line(gate_char8_t const* text, gate_size_t textlen, gate_char8_t* buffer, gate_size_t* bufferlen)
2120 {
2121 gate_size_t pos;
2122
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 158 times.
159 if (textlen == 0)
2123 {
2124
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if ((buffer != NULL) && (bufferlen != NULL))
2125 {
2126
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (*bufferlen > 0)
2127 {
2128 1 buffer[0] = 0;
2129 1 *bufferlen = 0;
2130 }
2131 }
2132 1 return 0;
2133 }
2134 else
2135 {
2136 158 pos = gate_str_char_pos(text, textlen, '\n', 0);
2137
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 157 times.
158 if (pos == GATE_STR_NPOS)
2138 {
2139
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if ((buffer != NULL) && (bufferlen != NULL))
2140 {
2141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (*bufferlen < textlen)
2142 {
2143 ✗ gate_mem_copy(buffer, text, *bufferlen);
2144 ✗ buffer[*bufferlen] = 0;
2145 ✗ return *bufferlen;
2146 }
2147 else
2148 {
2149 1 gate_mem_copy(buffer, text, textlen);
2150
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (*bufferlen > textlen)
2151 {
2152 1 buffer[textlen] = 0;
2153 }
2154 1 *bufferlen = textlen;
2155 }
2156 }
2157
2158 1 return textlen;
2159 }
2160 else
2161 {
2162
2/4
✓ Branch 0 taken 157 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 157 times.
✗ Branch 3 not taken.
157 if ((buffer != NULL) && (bufferlen != NULL))
2163 {
2164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 157 times.
157 if (*bufferlen < pos)
2165 {
2166 ✗ gate_mem_copy(buffer, text, *bufferlen);
2167 ✗ return *bufferlen;
2168 }
2169 else
2170 {
2171 157 gate_mem_copy(buffer, text, pos);
2172
1/2
✓ Branch 0 taken 157 times.
✗ Branch 1 not taken.
157 if (*bufferlen > pos)
2173 {
2174 157 buffer[pos] = 0;
2175 }
2176 157 *bufferlen = pos;
2177
1/2
✓ Branch 0 taken 157 times.
✗ Branch 1 not taken.
157 if (pos > 0)
2178 {
2179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 157 times.
157 if (buffer[pos - 1] == '\r')
2180 {
2181 ✗ buffer[pos - 1] = 0;
2182 ✗ --*bufferlen;
2183 }
2184 }
2185 }
2186 }
2187 157 return pos + 1;
2188 }
2189 }
2190 }
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201 /************************
2202 * String implementation *
2203 *************************/
2204
2205 3427 gate_string_t* gate_string_create(gate_string_t* obj, gate_char8_t const* str, gate_size_t len)
2206 {
2207
2/2
✓ Branch 0 taken 2309 times.
✓ Branch 1 taken 1118 times.
3427 if (len > 0)
2208 {
2209 2309 obj->buffer = gate_mem_alloc(sizeof(gate_stringbuffer_t) + len + 1);
2210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2309 times.
2309 if (obj->buffer == NULL)
2211 {
2212 ✗ return NULL;
2213 }
2214 2309 gate_mem_copy(&obj->buffer->data[0], str, len);
2215 2309 obj->buffer->data[len] = 0;
2216 2309 obj->str = &obj->buffer->data[0];
2217 2309 obj->length = len;
2218 2309 gate_atomic_int_init(&obj->buffer->refcount, 1);
2219 }
2220 else
2221 {
2222 1118 gate_string_create_empty(obj);
2223 }
2224
2225 3427 return obj;
2226 }
2227 7733 gate_string_t* gate_string_create_static_len(gate_string_t* obj, gate_char8_t const* str, gate_size_t len)
2228 {
2229 7733 obj->str = str;
2230 7733 obj->length = len;
2231 7733 obj->buffer = NULL;
2232 7733 return obj;
2233 }
2234 4327 gate_string_t* gate_string_create_static(gate_string_t* obj, gate_char8_t const* str)
2235 {
2236 4327 gate_size_t const len = gate_str_length(str);
2237 4327 return gate_string_create_static_len(obj, str, len);
2238 }
2239 5899 gate_string_t* gate_string_create_empty(gate_string_t* obj)
2240 {
2241 5899 obj->str = NULL;
2242 5899 obj->length = 0;
2243 5899 obj->buffer = NULL;
2244 5899 return obj;
2245 }
2246 1 gate_string_t* gate_string_create_utf16(gate_string_t* obj, gate_char16_t const* str, gate_size_t len)
2247 {
2248 gate_char32_t chr32;
2249 1 gate_size_t processed16 = 0;
2250 gate_size_t decoded;
2251 gate_char8_t buffer[16];
2252 gate_size_t encoded;
2253 1 gate_size_t required8 = 0;
2254 1 gate_size_t used8 = 0;
2255 1 char* ptr = NULL;
2256
2257
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 while (processed16 < len)
2258 {
2259 5 decoded = gate_char_read_utf16(&str[processed16], len - processed16, &chr32);
2260
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (decoded == 0)
2261 {
2262 ✗ break;
2263 }
2264
2265 5 processed16 += decoded;
2266 5 encoded = gate_char_write_utf8(chr32, buffer, sizeof(buffer));
2267 5 required8 += encoded;
2268 }
2269
2270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (required8 == 0)
2271 {
2272 ✗ gate_string_create_empty(obj);
2273 }
2274 else
2275 {
2276 1 obj->buffer = gate_mem_alloc(sizeof(gate_stringbuffer_t) + required8 + 1);
2277
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (obj->buffer == NULL)
2278 {
2279 ✗ return NULL;
2280 }
2281 1 processed16 = 0;
2282 1 ptr = &obj->buffer->data[0];
2283
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
6 while ((processed16 < len) && (used8 < required8))
2284 {
2285 5 decoded = gate_char_read_utf16(&str[processed16], len - processed16, &chr32);
2286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 if (decoded == 0)
2287 {
2288 ✗ break;
2289 }
2290
2291 5 processed16 += decoded;
2292 5 encoded = gate_char_write_utf8(chr32, ptr, required8 - used8);
2293 5 used8 += encoded;
2294 5 ptr += encoded;
2295 }
2296 1 *ptr = 0;
2297
2298 1 obj->str = &obj->buffer->data[0];
2299 1 obj->length = used8;
2300 1 gate_atomic_int_init(&obj->buffer->refcount, 1);
2301 }
2302
2303 1 return obj;
2304 }
2305 2 gate_string_t* gate_string_create_utf32(gate_string_t* obj, gate_char32_t const* str, gate_size_t len)
2306 {
2307 2 gate_size_t processed32 = 0;
2308 gate_char8_t buffer[16];
2309 gate_size_t encoded;
2310 2 gate_size_t required8 = 0;
2311 2 gate_size_t used8 = 0;
2312 2 char* ptr = NULL;
2313
2314
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2 times.
11 while (processed32 < len)
2315 {
2316 9 encoded = gate_char_write_utf8(str[processed32], buffer, sizeof(buffer));
2317 9 ++processed32;
2318 9 required8 += encoded;
2319 }
2320
2321 2 obj->buffer = gate_mem_alloc(sizeof(gate_stringbuffer_t) + required8 + 1);
2322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (obj->buffer == NULL)
2323 {
2324 ✗ return NULL;
2325 }
2326
2327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (required8 == 0)
2328 {
2329 ✗ gate_string_create_empty(obj);
2330 }
2331 else
2332 {
2333 2 processed32 = 0;
2334 2 ptr = &obj->buffer->data[0];
2335
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
11 while ((processed32 < len) && (used8 < required8))
2336 {
2337 9 encoded = gate_char_write_utf8(str[processed32], ptr, required8 - used8);
2338 9 ++processed32;
2339 9 used8 += encoded;
2340 9 ptr += encoded;
2341 }
2342 2 *ptr = 0;
2343
2344 2 obj->str = &obj->buffer->data[0];
2345 2 obj->length = used8;
2346 2 gate_atomic_int_init(&obj->buffer->refcount, 1);
2347 }
2348
2349 2 return obj;
2350
2351 }
2352 19 gate_string_t* gate_string_create_copy(gate_string_t* obj, gate_string_t const* src)
2353 {
2354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (src == NULL)
2355 {
2356 ✗ return gate_string_create_empty(obj);
2357 }
2358 else
2359 {
2360 19 return gate_string_create(obj, src->str, src->length);
2361 }
2362 }
2363
2364
2365
2366 57965 void gate_string_release(gate_string_t* obj)
2367 {
2368
2/2
✓ Branch 0 taken 41632 times.
✓ Branch 1 taken 16333 times.
57965 if (obj->buffer != NULL)
2369 {
2370
2/2
✓ Branch 1 taken 4115 times.
✓ Branch 2 taken 37517 times.
41632 if (gate_atomic_int_dec(&obj->buffer->refcount) == 0)
2371 {
2372 4115 gate_mem_dealloc(obj->buffer);
2373 }
2374 41632 obj->buffer = NULL;
2375 }
2376 57965 obj->str = NULL;
2377 57965 obj->length = 0;
2378 57965 }
2379
2380 6187 gate_bool_t gate_string_is_empty(gate_string_t const* obj)
2381 {
2382
4/4
✓ Branch 0 taken 6164 times.
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 937 times.
✓ Branch 3 taken 5227 times.
6187 return ((obj == NULL) || (obj->length == 0));
2383 }
2384
2385 5 gate_bool_t gate_string_is_terminated(gate_string_t const* obj)
2386 {
2387 /* NOTICE: general string requirement in GATE framework:
2388 every string needs to be overallocated to ensure one NUL-byte can be appended
2389 */
2390 5 return (obj->str[obj->length] == 0);
2391 }
2392
2393 19904 gate_string_t* gate_string_duplicate(gate_string_t* dst, gate_string_t const* src)
2394 {
2395
2/2
✓ Branch 0 taken 19476 times.
✓ Branch 1 taken 428 times.
19904 if (src != NULL)
2396 {
2397 19476 dst->buffer = src->buffer;
2398 19476 dst->str = src->str;
2399 19476 dst->length = src->length;
2400
2/2
✓ Branch 0 taken 16775 times.
✓ Branch 1 taken 2701 times.
19476 if (dst->buffer != NULL)
2401 {
2402 16775 gate_atomic_int_inc(&dst->buffer->refcount);
2403 }
2404 }
2405 else
2406 {
2407 428 dst->buffer = NULL;
2408 428 dst->str = NULL;
2409 428 dst->length = 0;
2410 }
2411 19904 return dst;
2412 }
2413 8963 gate_string_t* gate_string_clone(gate_string_t* dst, gate_string_t const* src)
2414 {
2415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8963 times.
8963 if (src == NULL)
2416 {
2417 ✗ return gate_string_create_empty(dst);
2418 }
2419 else
2420 {
2421
2/2
✓ Branch 0 taken 1569 times.
✓ Branch 1 taken 7394 times.
8963 if (src->buffer == NULL)
2422 {
2423 1569 return gate_string_create(dst, src->str, src->length);
2424 }
2425 else
2426 {
2427 7394 dst->str = src->str;
2428 7394 dst->length = src->length;
2429 7394 dst->buffer = src->buffer;
2430 7394 gate_atomic_int_inc(&dst->buffer->refcount);
2431 7394 return dst;
2432 }
2433 }
2434 }
2435
2436 52384 gate_size_t gate_string_length(gate_string_t const* obj)
2437 {
2438
1/2
✓ Branch 0 taken 52384 times.
✗ Branch 1 not taken.
52384 return obj ? obj->length : 0;
2439 }
2440 4 gate_bool_t gate_string_like(gate_string_t const* obj, gate_string_t const* pattern)
2441 {
2442 4 return gate_str_like(obj->str, obj->length, pattern->str, pattern->length);
2443 }
2444 33 gate_bool_t gate_string_like_one_of(gate_string_t const* obj, gate_string_t const* pattern, gate_char8_t separator)
2445 {
2446 33 return gate_str_like_one_of(obj->str, obj->length, pattern->str, pattern->length, separator);
2447 }
2448
2449
2450 220 gate_size_t gate_string_count_chars(gate_string_t const* obj, gate_char8_t chr)
2451 {
2452
1/2
✓ Branch 0 taken 220 times.
✗ Branch 1 not taken.
220 if (obj)
2453 {
2454 220 return gate_str_count_chars(obj->str, obj->length, chr);
2455 }
2456 ✗ return 0;
2457 }
2458 85 gate_char8_t gate_string_char_at(gate_string_t const* str, gate_size_t index)
2459 {
2460 85 gate_char8_t ret = '\0';
2461
2/4
✓ Branch 0 taken 85 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 85 times.
✗ Branch 3 not taken.
85 if (str && (index < str->length))
2462 {
2463 85 ret = str->str[index];
2464 }
2465 85 return ret;
2466 }
2467
2468 2896 gate_size_t gate_string_char_pos(gate_string_t const* obj, gate_char8_t find, gate_size_t startat)
2469 {
2470 2896 return gate_str_char_pos(obj->str, obj->length, find, startat);
2471 }
2472 453 gate_size_t gate_string_char_pos_last(gate_string_t const* obj, gate_char8_t find)
2473 {
2474 453 return gate_str_char_pos_last(obj->str, obj->length, find);
2475 }
2476 10 gate_size_t gate_string_find_first_of(gate_string_t const* obj, gate_string_t const* chars, gate_size_t startat)
2477 {
2478 10 return gate_str_find_first_of(obj->str, obj->length, chars->str, chars->length, startat);
2479 }
2480 104 gate_size_t gate_string_find_first_not_of(gate_string_t const* obj, gate_string_t const* chars, gate_size_t startat)
2481 {
2482 104 return gate_str_find_first_not_of(obj->str, obj->length, chars->str, chars->length, startat);
2483 }
2484 3 gate_size_t gate_string_find_last_of(gate_string_t const* obj, gate_string_t const* chars)
2485 {
2486 3 return gate_str_find_last_of(obj->str, obj->length, chars->str, chars->length);
2487 }
2488 30 gate_size_t gate_string_find_last_not_of(gate_string_t const* obj, gate_string_t const* chars)
2489 {
2490 30 return gate_str_find_last_not_of(obj->str, obj->length, chars->str, chars->length);
2491 }
2492
2493 251 gate_size_t gate_string_pos(gate_string_t const* obj, gate_string_t const* find, gate_size_t startat)
2494 {
2495 251 return gate_str_pos(obj->str, obj->length, find->str, find->length, startat);
2496 }
2497 2 gate_size_t gate_string_pos_last(gate_string_t const* obj, gate_string_t const* find)
2498 {
2499 2 return gate_str_pos_last(obj->str, obj->length, find->str, find->length);
2500 }
2501
2502
2503 192 gate_size_t gate_string_parse(gate_string_t const* obj, gate_string_t const* find, gate_size_t start_at,
2504 gate_string_t* head, gate_string_t* tail, gate_bool_t separator_as_tail)
2505 {
2506 192 gate_size_t pos = gate_string_pos(obj, find, start_at);
2507
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 157 times.
192 if (pos == GATE_STR_NPOS)
2508 {
2509 35 return 0; /* nothing parsed */
2510 }
2511 else
2512 {
2513
1/2
✓ Branch 0 taken 157 times.
✗ Branch 1 not taken.
157 if (head)
2514 {
2515 157 gate_string_substr(head, obj, 0, pos);
2516 }
2517
1/2
✓ Branch 0 taken 157 times.
✗ Branch 1 not taken.
157 if (tail)
2518 {
2519
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 135 times.
157 if (separator_as_tail)
2520 {
2521 22 gate_string_substr(tail, obj, pos, GATE_STR_NPOS);
2522 }
2523 else
2524 {
2525 135 gate_string_substr(tail, obj, pos + find->length, GATE_STR_NPOS);
2526 }
2527 }
2528 157 return pos + gate_string_length(find); /* (head + separator) parsed */
2529 }
2530 }
2531
2532 6 gate_size_t gate_string_parse_int(gate_string_t const* obj, gate_int64_t* num)
2533 {
2534 6 return gate_str_parse_int64(obj->str, obj->length, num);
2535 }
2536
2537 131 gate_size_t gate_string_parse_uint(gate_string_t const* obj, gate_uint64_t* num)
2538 {
2539 131 return gate_str_parse_uint64(obj->str, obj->length, num);
2540 }
2541
2542 3 gate_size_t gate_string_parse_real(gate_string_t const* obj, gate_real64_t* num)
2543 {
2544 3 return gate_str_parse_real(obj->str, obj->length, num);
2545 }
2546
2547
2548 1 gate_size_t gate_string_parse_hex(gate_string_t const* obj, gate_uint64_t* num)
2549 {
2550 1 return gate_str_parse_hex_int(obj->str, obj->length, num);
2551 }
2552
2553
2554 5045 gate_char8_t const* gate_string_ptr(gate_string_t const* obj, gate_size_t index)
2555 {
2556
3/4
✓ Branch 0 taken 5045 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5012 times.
✓ Branch 3 taken 33 times.
5045 if ((obj != NULL) && (obj->str != NULL))
2557 {
2558 5012 return obj->str + index;
2559 }
2560 else
2561 {
2562 33 return NULL;
2563 }
2564 }
2565 12535 gate_string_t* gate_string_substr(gate_string_t* dst, gate_string_t const* src, gate_size_t offset, gate_size_t length)
2566 {
2567
1/2
✓ Branch 0 taken 12535 times.
✗ Branch 1 not taken.
12535 if (dst != NULL)
2568 {
2569
5/6
✓ Branch 0 taken 12535 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12413 times.
✓ Branch 3 taken 122 times.
✓ Branch 4 taken 42 times.
✓ Branch 5 taken 12371 times.
12535 if ((src == NULL) || (offset >= src->length) || (length == 0))
2570 {
2571
2/2
✓ Branch 0 taken 117 times.
✓ Branch 1 taken 47 times.
328 if (src == dst)
2572 {
2573 117 gate_string_release(dst);
2574 }
2575 else
2576 {
2577 47 dst->str = NULL;
2578 47 dst->length = 0;
2579 47 dst->buffer = NULL;
2580 }
2581 }
2582 else
2583 {
2584
2/2
✓ Branch 0 taken 6750 times.
✓ Branch 1 taken 5621 times.
12371 if (length > src->length - offset)
2585 {
2586 6750 length = src->length - offset;
2587 }
2588
2589 12371 dst->buffer = src->buffer;
2590 12371 dst->str = &src->str[offset];
2591 12371 dst->length = length;
2592
2593
4/4
✓ Branch 0 taken 10345 times.
✓ Branch 1 taken 2026 times.
✓ Branch 2 taken 8379 times.
✓ Branch 3 taken 1966 times.
12371 if ((dst->buffer != NULL) && (dst != src))
2594 {
2595 8379 gate_atomic_int_inc(&dst->buffer->refcount);
2596 }
2597 }
2598 }
2599 12535 return dst;
2600 }
2601
2602 16515 int gate_string_comp(gate_string_t const* src, gate_string_t const* dst)
2603 {
2604 16515 gate_char8_t const* ptr_src = NULL;
2605 16515 gate_size_t len_src = 0;
2606 16515 gate_char8_t const* ptr_dst = NULL;
2607 16515 gate_size_t len_dst = 0;
2608
2609
1/2
✓ Branch 0 taken 16515 times.
✗ Branch 1 not taken.
16515 if (src)
2610 {
2611 16515 ptr_src = src->str;
2612 16515 len_src = src->length;
2613 }
2614
2/2
✓ Branch 0 taken 16509 times.
✓ Branch 1 taken 6 times.
16515 if (dst)
2615 {
2616 16509 ptr_dst = dst->str;
2617 16509 len_dst = dst->length;
2618 }
2619
2620 16515 return gate_str_compare(ptr_src, len_src, ptr_dst, len_dst);
2621 }
2622
2623 1218 int gate_string_comp_ic(gate_string_t const* src, gate_string_t const* dst)
2624 {
2625 1218 gate_char8_t const* ptr_src = NULL;
2626 1218 gate_size_t len_src = 0;
2627 1218 gate_char8_t const* ptr_dst = NULL;
2628 1218 gate_size_t len_dst = 0;
2629
2630
1/2
✓ Branch 0 taken 1218 times.
✗ Branch 1 not taken.
1218 if (src)
2631 {
2632 1218 ptr_src = src->str;
2633 1218 len_src = src->length;
2634 }
2635
1/2
✓ Branch 0 taken 1218 times.
✗ Branch 1 not taken.
1218 if (dst)
2636 {
2637 1218 ptr_dst = dst->str;
2638 1218 len_dst = dst->length;
2639 }
2640 1218 return gate_str_compare_ic(ptr_src, len_src, ptr_dst, len_dst);
2641 }
2642 1236 gate_size_t gate_string_to_buffer(gate_string_t const* src, gate_char8_t* buffer, gate_size_t bufferlength)
2643 {
2644 gate_size_t ret;
2645
2/4
✓ Branch 0 taken 1236 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1236 times.
1236 if ((buffer == NULL) || (bufferlength == 0))
2646 {
2647 ✗ ret = 0;
2648 }
2649 else
2650 {
2651
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 1228 times.
1236 if (gate_string_is_empty(src))
2652 {
2653 8 buffer[0] = 0;
2654 8 ret = 0;
2655 }
2656 else
2657 {
2658 1228 ret = gate_str_print_text(buffer, bufferlength, src->str, src->length);
2659 }
2660 }
2661 1236 return ret;
2662 }
2663
2664
2665 14490 gate_bool_t gate_string_starts_with(gate_string_t const* obj, gate_string_t const* token)
2666 {
2667 14490 gate_size_t objlen = gate_string_length(obj);
2668 14490 gate_size_t tokenlen = gate_string_length(token);
2669
3/4
✓ Branch 0 taken 14490 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 740 times.
✓ Branch 3 taken 13750 times.
14490 if ((tokenlen == 0) || (objlen < tokenlen))
2670 {
2671 740 return false;
2672 }
2673 13750 return (gate_str_compare(obj->str, tokenlen, token->str, tokenlen) == 0);
2674 }
2675 359 gate_bool_t gate_string_starts_with_str(gate_string_t const* obj, gate_char8_t const* text)
2676 {
2677 gate_string_t tmp;
2678 359 gate_string_create_static(&tmp, text);
2679 359 return gate_string_starts_with(obj, &tmp);
2680 }
2681 7774 gate_bool_t gate_string_starts_with_char(gate_string_t const* obj, gate_char8_t chr)
2682 {
2683 7774 gate_size_t objlen = gate_string_length(obj);
2684
4/4
✓ Branch 0 taken 7765 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 3854 times.
✓ Branch 3 taken 3911 times.
7774 return (objlen >= 1) && (obj->str[0] == chr);
2685 }
2686 1 gate_bool_t gate_string_starts_with_ic(gate_string_t const* obj, gate_string_t const* token)
2687 {
2688 1 gate_size_t objlen = gate_string_length(obj);
2689 1 gate_size_t tokenlen = gate_string_length(token);
2690
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if ((tokenlen == 0) || (objlen < tokenlen))
2691 {
2692 ✗ return false;
2693 }
2694 1 return (gate_str_compare_ic(obj->str, tokenlen, token->str, tokenlen) == 0);
2695 }
2696 1 gate_bool_t gate_string_starts_with_str_ic(gate_string_t const* obj, gate_char8_t const* text)
2697 {
2698 gate_string_t tmp;
2699 1 gate_string_create_static(&tmp, text);
2700 1 return gate_string_starts_with_ic(obj, &tmp);
2701 }
2702
2703 667 gate_bool_t gate_string_ends_with(gate_string_t const* obj, gate_string_t const* token)
2704 {
2705 667 gate_size_t objlen = gate_string_length(obj);
2706 667 gate_size_t tokenlen = gate_string_length(token);
2707
3/4
✓ Branch 0 taken 667 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 663 times.
667 if ((tokenlen == 0) || (objlen < tokenlen))
2708 {
2709 4 return false;
2710 }
2711 663 return (gate_str_compare(&obj->str[objlen - tokenlen], tokenlen, token->str, tokenlen) == 0);
2712 }
2713 647 gate_bool_t gate_string_ends_with_str(gate_string_t const* obj, gate_char8_t const* text)
2714 {
2715 gate_string_t tmp;
2716 647 gate_string_create_static(&tmp, text);
2717 647 return gate_string_ends_with(obj, &tmp);
2718 }
2719 794 gate_bool_t gate_string_ends_with_char(gate_string_t const* obj, gate_char8_t chr)
2720 {
2721 794 gate_size_t objlen = gate_string_length(obj);
2722
3/4
✓ Branch 0 taken 794 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 46 times.
✓ Branch 3 taken 748 times.
794 return (objlen >= 1) && (obj->str[objlen - 1] == chr);
2723 }
2724 59 gate_bool_t gate_string_ends_with_ic(gate_string_t const* obj, gate_string_t const* token)
2725 {
2726 59 gate_size_t objlen = gate_string_length(obj);
2727 59 gate_size_t tokenlen = gate_string_length(token);
2728
3/4
✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 51 times.
59 if ((tokenlen == 0) || (objlen < tokenlen))
2729 {
2730 8 return false;
2731 }
2732 51 return (gate_str_compare_ic(&obj->str[objlen - tokenlen], tokenlen, token->str, tokenlen) == 0);
2733 }
2734 59 gate_bool_t gate_string_ends_with_str_ic(gate_string_t const* obj, gate_char8_t const* text)
2735 {
2736 gate_string_t tmp;
2737 59 gate_string_create_static(&tmp, text);
2738 59 return gate_string_ends_with_ic(obj, &tmp);
2739 }
2740
2741 1 gate_bool_t gate_string_contains(gate_string_t const* obj, gate_string_t const* token)
2742 {
2743 1 return gate_string_pos(obj, token, 0) != GATE_STR_NPOS;
2744 }
2745
2746 2583 gate_bool_t gate_string_equals(gate_string_t const* obj, gate_string_t const* token)
2747 {
2748 2583 return gate_string_comp(obj, token) == 0;
2749 }
2750 663 gate_bool_t gate_string_equals_str(gate_string_t const* obj, gate_char8_t const* token)
2751 {
2752 gate_string_t tmp;
2753 663 gate_string_create_static(&tmp, token);
2754 663 return gate_string_equals(obj, &tmp);
2755 }
2756 16 gate_bool_t gate_string_equals_ic(gate_string_t const* obj, gate_string_t const* token)
2757 {
2758 16 return gate_string_comp_ic(obj, token) == 0;
2759 }
2760 12 gate_bool_t gate_string_equals_str_ic(gate_string_t const* obj, gate_char8_t const* token)
2761 {
2762 gate_string_t tmp;
2763 12 gate_string_create_static(&tmp, token);
2764 12 return gate_string_equals_ic(obj, &tmp);
2765 }
2766 5851 gate_string_t* gate_string_ltrim(gate_string_t* dst, gate_string_t const* src)
2767 {
2768 5851 gate_size_t pos = gate_str_find_first_not_of(src->str, src->length, GATE_STR_WHITESPACES, GATE_STR_WHITESPACES_LENGTH, 0);
2769
2/2
✓ Branch 0 taken 136 times.
✓ Branch 1 taken 5715 times.
5851 if (pos == GATE_STR_NPOS)
2770 {
2771
1/2
✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
136 if (dst == src)
2772 {
2773 136 gate_string_release(dst);
2774 }
2775 136 gate_string_create_empty(dst);
2776 }
2777 else
2778 {
2779 5715 dst->str = &src->str[pos];
2780 5715 dst->length = src->length - pos;
2781
2/2
✓ Branch 0 taken 4970 times.
✓ Branch 1 taken 745 times.
5715 if (dst != src)
2782 {
2783 4970 dst->buffer = src->buffer;
2784
2/2
✓ Branch 0 taken 4969 times.
✓ Branch 1 taken 1 times.
4970 if (dst->buffer != NULL)
2785 {
2786 4969 gate_atomic_int_inc(&dst->buffer->refcount);
2787 }
2788 }
2789 }
2790 5851 return dst;
2791 }
2792 211 gate_string_t* gate_string_rtrim(gate_string_t* dst, gate_string_t const* src)
2793 {
2794 211 gate_size_t pos = gate_str_find_last_not_of(src->str, src->length, GATE_STR_WHITESPACES, GATE_STR_WHITESPACES_LENGTH);
2795
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 187 times.
211 if (pos == GATE_STR_NPOS)
2796 {
2797
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (dst == src)
2798 {
2799 24 gate_string_release(dst);
2800 }
2801 24 gate_string_create_empty(dst);
2802 }
2803 else
2804 {
2805 187 dst->length = pos + 1;
2806
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 186 times.
187 if (dst != src)
2807 {
2808 1 dst->str = src->str;
2809 1 dst->buffer = src->buffer;
2810
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (dst->buffer != NULL)
2811 {
2812 ✗ gate_atomic_int_inc(&dst->buffer->refcount);
2813 }
2814 }
2815 }
2816 211 return dst;
2817 }
2818 4737 gate_string_t* gate_string_trim(gate_string_t* dst, gate_string_t const* src)
2819 {
2820 4737 gate_size_t newlength = gate_str_find_last_not_of(src->str, src->length, GATE_STR_WHITESPACES, GATE_STR_WHITESPACES_LENGTH);
2821 gate_size_t pos;
2822
2823
2/2
✓ Branch 0 taken 146 times.
✓ Branch 1 taken 4591 times.
4737 if (newlength == GATE_STR_NPOS)
2824 {
2825
1/2
✓ Branch 0 taken 146 times.
✗ Branch 1 not taken.
146 if (dst == src)
2826 {
2827 146 gate_string_release(dst);
2828 }
2829 146 return gate_string_create_empty(dst);
2830 }
2831 4591 ++newlength;
2832
2833 4591 pos = gate_str_find_first_not_of(src->str, newlength, GATE_STR_WHITESPACES, GATE_STR_WHITESPACES_LENGTH, 0);
2834
2835 4591 dst->str = &src->str[pos];
2836 4591 dst->length = newlength - pos;
2837
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4590 times.
4591 if (dst != src)
2838 {
2839 1 dst->buffer = src->buffer;
2840
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (dst->buffer != NULL)
2841 {
2842 ✗ gate_atomic_int_inc(&dst->buffer->refcount);
2843 }
2844 }
2845 4591 return dst;
2846 }
2847
2848 367 gate_string_t* gate_string_read_line(gate_string_t* line, gate_string_t const* src, gate_string_t* tail)
2849 {
2850 367 gate_string_t* ret = NULL;
2851 gate_size_t pos;
2852
2/2
✓ Branch 1 taken 342 times.
✓ Branch 2 taken 25 times.
367 if (!gate_string_is_empty(src))
2853 {
2854 342 pos = gate_str_char_pos(src->str, src->length, '\n', 0);
2855
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 341 times.
342 if (pos == GATE_STR_NPOS)
2856 {
2857 1 ret = gate_string_duplicate(line, src);
2858
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (tail != NULL)
2859 {
2860
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (tail == src)
2861 {
2862 1 gate_string_release(tail);
2863 }
2864 else
2865 {
2866 ✗ gate_string_create_empty(tail);
2867 }
2868 }
2869 }
2870 else
2871 {
2872 341 ret = gate_string_substr(line, src, 0, pos);
2873
1/2
✓ Branch 0 taken 341 times.
✗ Branch 1 not taken.
341 if (ret != NULL)
2874 {
2875
2/2
✓ Branch 1 taken 204 times.
✓ Branch 2 taken 137 times.
341 if (gate_string_ends_with_str(line, "\r"))
2876 {
2877 204 --line->length;
2878 }
2879 }
2880
1/2
✓ Branch 0 taken 341 times.
✗ Branch 1 not taken.
341 if (tail != NULL)
2881 {
2882
2/2
✓ Branch 0 taken 227 times.
✓ Branch 1 taken 114 times.
341 if (tail == src)
2883 {
2884 227 tail->str += (pos + 1);
2885 227 tail->length -= (pos + 1);
2886 }
2887 else
2888 {
2889 114 gate_string_substr(tail, src, pos + 1, GATE_STR_NPOS);
2890 }
2891 }
2892 }
2893 }
2894 367 return ret;
2895 }
2896
2897 14 gate_string_t* gate_string_read_token(gate_string_t* token, gate_string_t const* src, gate_string_t* tail)
2898 {
2899 14 gate_string_t* ret = NULL;
2900 gate_size_t pos;
2901 gate_size_t pos2;
2902
1/2
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
14 if (!gate_string_is_empty(src))
2903 {
2904 14 pos = gate_str_find_first_not_of(src->str, src->length, GATE_STR_WHITESPACES, GATE_STR_WHITESPACES_LENGTH, 0);
2905
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (pos == GATE_STR_NPOS)
2906 {
2907 ✗ ret = gate_string_create_empty(token);
2908 ✗ if (tail != NULL)
2909 {
2910 ✗ gate_string_create_empty(tail);
2911 }
2912 }
2913 else
2914 {
2915 14 pos2 = gate_str_find_first_of(src->str, src->length, GATE_STR_WHITESPACES, GATE_STR_WHITESPACES_LENGTH, pos);
2916
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (pos2 == GATE_STR_NPOS)
2917 {
2918 ✗ ret = gate_string_substr(token, src, pos, src->length - pos);
2919 ✗ if (tail != NULL)
2920 {
2921 ✗ gate_string_create_empty(tail);
2922 }
2923 }
2924 else
2925 {
2926 14 ret = gate_string_substr(token, src, pos, pos2 - pos);
2927
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if (tail != NULL)
2928 {
2929 14 gate_string_substr(tail, src, pos2, src->length - pos2);
2930 }
2931 }
2932 }
2933 }
2934 14 return ret;
2935 }
2936
2937 1 gate_string_t* gate_string_to_lower(gate_string_t* dst, gate_string_t const* src)
2938 {
2939 1 gate_string_t* ret = gate_string_create_copy(dst, src);
2940
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (NULL != ret)
2941 {
2942 1 gate_str_to_lower(ret->buffer->data, ret->length);
2943 }
2944 1 return ret;
2945 }
2946 1 gate_string_t* gate_string_to_upper(gate_string_t* dst, gate_string_t const* src)
2947 {
2948 1 gate_string_t* ret = gate_string_create_copy(dst, src);
2949
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (NULL != ret)
2950 {
2951 1 gate_str_to_upper(ret->buffer->data, ret->length);
2952 }
2953 1 return ret;
2954 }
2955 2 gate_uint32_t gate_string_hash(gate_string_t const* obj)
2956 {
2957 2 return gate_str_hash(gate_string_ptr(obj, 0), gate_string_length(obj));
2958 }
2959
2960
2961 5 gate_result_t gate_string_create_copy_constructor(void* destMem, void const* srcMem)
2962 {
2963 5 gate_string_t* dst = (gate_string_t*)destMem;
2964 5 gate_string_t const* src = (gate_string_t const*)srcMem;
2965
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 if (NULL == gate_string_create_copy(dst, src))
2966 {
2967 ✗ return GATE_RESULT_OUTOFMEMORY;
2968 }
2969 else
2970 {
2971 5 return GATE_RESULT_OK;
2972 }
2973 }
2974 5614 gate_result_t gate_string_copy_constructor(void* destMem, void const* srcMem)
2975 {
2976 5614 gate_string_t* dst = (gate_string_t*)destMem;
2977 5614 gate_string_t const* src = (gate_string_t const*)srcMem;
2978
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5614 times.
5614 if (NULL == gate_string_clone(dst, src))
2979 {
2980 ✗ return GATE_RESULT_OUTOFMEMORY;
2981 }
2982 else
2983 {
2984 5614 return GATE_RESULT_OK;
2985 }
2986 }
2987 110 gate_result_t gate_string_duplicate_constructor(void* destMem, void const* srcMem)
2988 {
2989 110 gate_string_t* dst = (gate_string_t*)destMem;
2990 110 gate_string_t const* src = (gate_string_t const*)srcMem;
2991
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 110 times.
110 if (NULL == gate_string_duplicate(dst, src))
2992 {
2993 ✗ return GATE_RESULT_OUTOFMEMORY;
2994 }
2995 else
2996 {
2997 110 return GATE_RESULT_OK;
2998 }
2999 }
3000 5732 void gate_string_destructor(void* dest)
3001 {
3002 5732 gate_string_t* dst = (gate_string_t*)dest;
3003 5732 gate_string_release(dst);
3004 5732 }
3005