| 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 | 3657 | 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 3652 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 3640 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 3638 times.
✓ Branch 6 taken 2738 times.
✓ Branch 7 taken 97682 times.
✓ Branch 8 taken 100420 times.
✓ Branch 9 taken 900 times.
|
101339 | 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 | 702 | 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 702 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 702 times.
✓ Branch 4 taken 562 times.
✓ Branch 5 taken 3720 times.
✓ Branch 6 taken 4282 times.
✓ Branch 7 taken 140 times.
|
4422 | 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 | 753 | 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 749 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 717 times.
✓ Branch 3 taken 32 times.
✓ Branch 4 taken 717 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 717 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 448 times.
✓ Branch 9 taken 269 times.
✓ Branch 10 taken 2 times.
✓ Branch 11 taken 446 times.
✓ Branch 13 taken 243 times.
✓ Branch 14 taken 406 times.
✓ Branch 16 taken 203 times.
✓ Branch 17 taken 203 times.
|
956 | 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 | 1506881 | gate_size_t gate_str_length(gate_char8_t const* src) | |
| 195 | { | ||
| 196 |
4/4✓ Branch 0 taken 1505757 times.
✓ Branch 1 taken 1124 times.
✓ Branch 2 taken 63384859 times.
✓ Branch 3 taken 1505757 times.
|
64891740 | 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 | 51 | gate_size_t gate_str_length_max(gate_char8_t const* src, gate_size_t capacity) | |
| 220 | { | ||
| 221 |
4/6✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 428 times.
✓ Branch 3 taken 51 times.
✓ Branch 4 taken 428 times.
✗ Branch 5 not taken.
|
479 | 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 | 3155 | gate_bool_t gate_str_is_empty(gate_char8_t const* src) | |
| 240 | { | ||
| 241 |
2/2✓ Branch 0 taken 19 times.
✓ Branch 1 taken 3136 times.
|
3155 | 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 | 31009 | 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 30693 times.
✓ Branch 1 taken 316 times.
✓ Branch 2 taken 16359 times.
✓ Branch 3 taken 60472 times.
✓ Branch 4 taken 9743 times.
✓ Branch 5 taken 50729 times.
✓ Branch 6 taken 76831 times.
✓ Branch 7 taken 4591 times.
|
81738 | 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 | 3626 | 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 3617 times.
✓ Branch 1 taken 9 times.
✓ Branch 4 taken 2287 times.
✓ Branch 5 taken 2730 times.
✓ Branch 6 taken 1124 times.
✓ Branch 7 taken 1606 times.
✓ Branch 8 taken 5017 times.
✓ Branch 9 taken 206 times.
|
5232 | 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 | 249741 | void gate_str_reverse(gate_char8_t* src, gate_size_t len) | |
| 374 | { | ||
| 375 |
3/4✓ Branch 0 taken 249741 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 250986 times.
✓ Branch 3 taken 249741 times.
|
500727 | GATE_STR_REVERSE(gate_char8_t, src, len); |
| 376 | 249741 | } | |
| 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 | 351 | int gate_str_comp(gate_char8_t const* str1, gate_char8_t const* str2) | |
| 430 | { | ||
| 431 | 351 | return gate_str_compare(str1, gate_str_length(str1), str2, gate_str_length(str2)); | |
| 432 | } | ||
| 433 | 27241 | int gate_str_compare(gate_char8_t const* str1, gate_size_t len1, gate_char8_t const* str2, gate_size_t len2) | |
| 434 | { | ||
| 435 | 27241 | gate_size_t len = len1 < len2 ? len1 : len2; | |
| 436 | 27241 | int ret = gate_str_comp_range(str1, str2, len); | |
| 437 |
2/2✓ Branch 0 taken 3472 times.
✓ Branch 1 taken 23769 times.
|
27241 | if (ret == 0) |
| 438 | { | ||
| 439 |
2/2✓ Branch 0 taken 195 times.
✓ Branch 1 taken 3277 times.
|
3472 | if (len1 < len2) ret = -1; |
| 440 |
2/2✓ Branch 0 taken 84 times.
✓ Branch 1 taken 3388 times.
|
3472 | if (len1 > len2) ret = 1; |
| 441 | } | ||
| 442 | 27241 | 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 | 3212 | 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 | 3212 | gate_size_t len = len1 < len2 ? len1 : len2; | |
| 451 | 3212 | int ret = gate_str_comp_range_ic(str1, str2, len); | |
| 452 |
2/2✓ Branch 0 taken 179 times.
✓ Branch 1 taken 3033 times.
|
3212 | if (ret == 0) |
| 453 | { | ||
| 454 |
2/2✓ Branch 0 taken 11 times.
✓ Branch 1 taken 168 times.
|
179 | if (len1 < len2) ret = -1; |
| 455 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 159 times.
|
179 | if (len1 > len2) ret = 1; |
| 456 | } | ||
| 457 | 3212 | return ret; | |
| 458 | } | ||
| 459 | |||
| 460 | 54 | 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 66 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 65 times.
✓ Branch 3 taken 1 times.
|
69 | while ((len != 0) && (likelen != 0)) |
| 463 | { | ||
| 464 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 31 times.
|
65 | if (*like == '*') |
| 465 | { | ||
| 466 | 34 | ++like; | |
| 467 | 34 | --likelen; | |
| 468 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 4 times.
|
34 | if (likelen == 0) |
| 469 | { | ||
| 470 | /* like ends with '*' -> this is always a match */ | ||
| 471 | 30 | 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 | 37 | 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 37 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
37 | if ((len == 0) && (likelen == 1)) |
| 504 | { | ||
| 505 | /* empty is like anything -> OK */ | ||
| 506 | ✗ | return *like == '*'; | |
| 507 | } | ||
| 508 | |||
| 509 | 37 | return gate_str_wildcard_match(str, len, like, likelen); | |
| 510 | } | ||
| 511 | |||
| 512 | 31 | 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 | 31 | gate_size_t startat = 0; | |
| 515 | gate_size_t pos; | ||
| 516 | gate_size_t token_len; | ||
| 517 |
1/2✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
|
33 | while (startat < likelen) |
| 518 | { | ||
| 519 | 33 | pos = gate_str_char_pos(like, likelen, like_separator, startat); | |
| 520 |
2/2✓ Branch 0 taken 31 times.
✓ Branch 1 taken 2 times.
|
33 | if (pos == GATE_STR_NPOS) |
| 521 | { | ||
| 522 | 31 | pos = likelen; | |
| 523 | } | ||
| 524 | //a;b;c | ||
| 525 | 33 | token_len = (pos - startat); | |
| 526 |
1/2✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
|
33 | if (token_len != 0) |
| 527 | { | ||
| 528 |
2/2✓ Branch 1 taken 31 times.
✓ Branch 2 taken 2 times.
|
33 | if (gate_str_like(str, len, like + startat, token_len)) |
| 529 | { | ||
| 530 | 31 | return true; | |
| 531 | } | ||
| 532 | } | ||
| 533 | 2 | startat = pos + 1; | |
| 534 | } | ||
| 535 | ✗ | return false; | |
| 536 | } | ||
| 537 | |||
| 538 | |||
| 539 | 504 | gate_size_t gate_str_find_first_of(gate_char8_t const* str, gate_size_t len, gate_char8_t const* chars, gate_size_t charcount, gate_size_t startat) | |
| 540 | { | ||
| 541 | gate_size_t ret; | ||
| 542 | gate_size_t ndx; | ||
| 543 |
5/10✓ Branch 0 taken 504 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 504 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 504 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 504 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 504 times.
|
504 | if ((str == NULL) || (len == 0) || (startat >= len) || (chars == NULL) || (charcount == 0)) |
| 544 | { | ||
| 545 | ✗ | return GATE_STR_NPOS; | |
| 546 | } | ||
| 547 | 504 | ret = startat; | |
| 548 | 504 | str += startat; | |
| 549 | 504 | len -= startat; | |
| 550 |
2/2✓ Branch 0 taken 3424 times.
✓ Branch 1 taken 390 times.
|
3814 | while (len-- > 0) |
| 551 | { | ||
| 552 |
2/2✓ Branch 0 taken 13300 times.
✓ Branch 1 taken 3310 times.
|
16610 | for (ndx = 0; ndx != charcount; ++ndx) |
| 553 | { | ||
| 554 |
2/2✓ Branch 0 taken 114 times.
✓ Branch 1 taken 13186 times.
|
13300 | if (*str == chars[ndx]) |
| 555 | { | ||
| 556 | /* first character found, that is included in 'chars' */ | ||
| 557 | 114 | return ret; | |
| 558 | } | ||
| 559 | } | ||
| 560 | 3310 | ++str; | |
| 561 | 3310 | ++ret; | |
| 562 | } | ||
| 563 | 390 | return GATE_STR_NPOS; | |
| 564 | } | ||
| 565 | |||
| 566 | 7003 | 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 6923 times.
✓ Branch 1 taken 80 times.
✓ Branch 2 taken 6919 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 6918 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 6918 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 6918 times.
|
7003 | if ((str == NULL) || (len == 0) || (startat >= len) || (chars == NULL) || (charcount == 0)) |
| 571 | { | ||
| 572 | 85 | return GATE_STR_NPOS; | |
| 573 | } | ||
| 574 | 6918 | ret = startat; | |
| 575 | 6918 | str += startat; | |
| 576 | 6918 | len -= startat; | |
| 577 |
2/2✓ Branch 0 taken 14448 times.
✓ Branch 1 taken 1 times.
|
14449 | while (len-- > 0) |
| 578 | { | ||
| 579 |
2/2✓ Branch 0 taken 36346 times.
✓ Branch 1 taken 6917 times.
|
43263 | for (ndx = 0; ndx != charcount; ++ndx) |
| 580 | { | ||
| 581 |
2/2✓ Branch 0 taken 7531 times.
✓ Branch 1 taken 28815 times.
|
36346 | if (*str == chars[ndx]) |
| 582 | { | ||
| 583 | 7531 | break; | |
| 584 | } | ||
| 585 | } | ||
| 586 |
2/2✓ Branch 0 taken 6917 times.
✓ Branch 1 taken 7531 times.
|
14448 | if (ndx == charcount) |
| 587 | { | ||
| 588 | /* first character found, that is not included in 'chars' */ | ||
| 589 | 6917 | return ret; | |
| 590 | } | ||
| 591 | 7531 | ++str; | |
| 592 | 7531 | ++ret; | |
| 593 | } | ||
| 594 | 1 | return GATE_STR_NPOS; | |
| 595 | } | ||
| 596 | 25 | 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 25 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 25 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 25 times.
|
25 | if ((str == NULL) || (len == 0) || (chars == NULL) || (charcount == 0)) |
| 601 | { | ||
| 602 | ✗ | return GATE_STR_NPOS; | |
| 603 | } | ||
| 604 | 25 | ret = len; | |
| 605 | 25 | str += len; | |
| 606 |
2/2✓ Branch 0 taken 449 times.
✓ Branch 1 taken 1 times.
|
450 | while (len-- > 0) |
| 607 | { | ||
| 608 | 449 | --str; | |
| 609 | 449 | --ret; | |
| 610 |
2/2✓ Branch 0 taken 453 times.
✓ Branch 1 taken 425 times.
|
878 | for (ndx = 0; ndx != charcount; ++ndx) |
| 611 | { | ||
| 612 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 429 times.
|
453 | if (*str == chars[ndx]) |
| 613 | { | ||
| 614 | /* last character found, that is included in 'chars' */ | ||
| 615 | 24 | return ret; | |
| 616 | } | ||
| 617 | } | ||
| 618 | } | ||
| 619 | 1 | return GATE_STR_NPOS; | |
| 620 | } | ||
| 621 | 3907 | 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 3907 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3901 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 3901 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 3901 times.
|
3907 | if ((str == NULL) || (len == 0) || (chars == NULL) || (charcount == 0)) |
| 626 | { | ||
| 627 | 6 | return GATE_STR_NPOS; | |
| 628 | } | ||
| 629 | 3901 | ret = len; | |
| 630 | 3901 | str += len; | |
| 631 |
2/2✓ Branch 0 taken 6058 times.
✓ Branch 1 taken 164 times.
|
6222 | while (len-- > 0) |
| 632 | { | ||
| 633 | 6058 | --str; | |
| 634 | 6058 | --ret; | |
| 635 |
2/2✓ Branch 0 taken 23705 times.
✓ Branch 1 taken 3737 times.
|
27442 | for (ndx = 0; ndx != charcount; ++ndx) |
| 636 | { | ||
| 637 |
2/2✓ Branch 0 taken 2321 times.
✓ Branch 1 taken 21384 times.
|
23705 | if (*str == chars[ndx]) |
| 638 | { | ||
| 639 | 2321 | break; | |
| 640 | } | ||
| 641 | } | ||
| 642 |
2/2✓ Branch 0 taken 3737 times.
✓ Branch 1 taken 2321 times.
|
6058 | if (ndx == charcount) |
| 643 | { | ||
| 644 | /* last character found, that is not included in 'chars' */ | ||
| 645 | 3737 | 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 | 3329 | 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 3318 times.
✓ Branch 1 taken 11 times.
✓ Branch 3 taken 1197 times.
✓ Branch 4 taken 2121 times.
|
3329 | 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 | 255391 | 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 255391 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 255382 times.
✓ Branch 4 taken 255272 times.
✓ Branch 5 taken 119 times.
|
255391 | 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 | 373 | gate_size_t gate_str_print_uint16(gate_char8_t* dst, gate_size_t dstlen, gate_uint16_t num) | |
| 882 | { | ||
| 883 | 373 | gate_size_t ret = 0; | |
| 884 |
2/4✓ Branch 0 taken 373 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 373 times.
✗ Branch 3 not taken.
|
373 | if ((dst != NULL) && (dstlen != 0)) |
| 885 | { | ||
| 886 | gate_char8_t* ptr; | ||
| 887 |
6/8✓ Branch 0 taken 373 times.
✓ Branch 1 taken 270 times.
✓ Branch 2 taken 643 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 187 times.
✓ Branch 5 taken 186 times.
✓ Branch 7 taken 373 times.
✗ Branch 8 not taken.
|
643 | GATE_STR_PRINT_NUM(dst, dstlen, ptr, ret, gate_uint16_t, num, gate_str_reverse); |
| 888 | } | ||
| 889 | 373 | 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 | 248577 | gate_size_t gate_str_print_uint32(gate_char8_t* dst, gate_size_t dstlen, gate_uint32_t num) | |
| 902 | { | ||
| 903 | 248577 | gate_size_t ret = 0; | |
| 904 |
2/4✓ Branch 0 taken 248577 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 248577 times.
✗ Branch 3 not taken.
|
248577 | if ((dst != NULL) && (dstlen != 0)) |
| 905 | { | ||
| 906 | gate_char8_t* ptr; | ||
| 907 |
6/8✓ Branch 0 taken 248577 times.
✓ Branch 1 taken 485702 times.
✓ Branch 2 taken 734279 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 248216 times.
✓ Branch 5 taken 361 times.
✓ Branch 7 taken 248577 times.
✗ Branch 8 not taken.
|
734279 | GATE_STR_PRINT_NUM(dst, dstlen, ptr, ret, gate_uint32_t, num, gate_str_reverse); |
| 908 | } | ||
| 909 | 248577 | return ret; | |
| 910 | } | ||
| 911 | 356 | gate_size_t gate_str_print_int32(gate_char8_t* dst, gate_size_t dstlen, gate_int32_t num) | |
| 912 | { | ||
| 913 | 356 | gate_size_t ret = 0; | |
| 914 |
2/4✓ Branch 0 taken 356 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 356 times.
✗ Branch 3 not taken.
|
356 | if ((dst != NULL) && (dstlen != 0)) |
| 915 | { | ||
| 916 | gate_uint32_t unum; | ||
| 917 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 353 times.
|
356 | GATE_STR_PRINT_INUM(ret, dst, dstlen, unum, num, gate_uint32_t, gate_str_print_uint32); |
| 918 | } | ||
| 919 | 356 | return ret; | |
| 920 | } | ||
| 921 | 1875 | gate_size_t gate_str_print_uint64(gate_char8_t* dst, gate_size_t dstlen, gate_uint64_t num) | |
| 922 | { | ||
| 923 | 1875 | gate_size_t ret = 0; | |
| 924 |
2/4✓ Branch 0 taken 1875 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1875 times.
✗ Branch 3 not taken.
|
1875 | if ((dst != NULL) && (dstlen != 0)) |
| 925 | { | ||
| 926 | gate_char8_t* ptr; | ||
| 927 |
8/8✓ Branch 0 taken 1874 times.
✓ Branch 1 taken 3578 times.
✓ Branch 2 taken 5452 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1338 times.
✓ Branch 5 taken 537 times.
✓ Branch 7 taken 1874 times.
✓ Branch 8 taken 1 times.
|
5453 | GATE_STR_PRINT_NUM(dst, dstlen, ptr, ret, gate_uint64_t, num, gate_str_reverse); |
| 928 | } | ||
| 929 | 1875 | return ret; | |
| 930 | } | ||
| 931 | 297 | gate_size_t gate_str_print_int64(gate_char8_t* dst, gate_size_t dstlen, gate_int64_t num) | |
| 932 | { | ||
| 933 | 297 | gate_size_t ret = 0; | |
| 934 |
2/4✓ Branch 0 taken 297 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 297 times.
✗ Branch 3 not taken.
|
297 | if ((dst != NULL) && (dstlen != 0)) |
| 935 | { | ||
| 936 | gate_uint64_t unum; | ||
| 937 |
2/2✓ Branch 0 taken 39 times.
✓ Branch 1 taken 258 times.
|
297 | GATE_STR_PRINT_INUM(ret, dst, dstlen, unum, num, gate_uint64_t, gate_str_print_uint64); |
| 938 | } | ||
| 939 | 297 | 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 177 times.
✓ Branch 1 taken 137 times.
|
314 | if (intlen > bytesused) |
| 1079 | { | ||
| 1080 | gate_size_t ndx; | ||
| 1081 |
3/4✓ Branch 0 taken 187 times.
✓ Branch 1 taken 177 times.
✓ Branch 2 taken 187 times.
✗ Branch 3 not taken.
|
364 | for (ndx = bytesused; (ndx < intlen) && (dstlen != 0); ++ndx) |
| 1082 | { | ||
| 1083 | 187 | *dst = '0'; | |
| 1084 | 187 | ++dst; | |
| 1085 | 187 | --dstlen; | |
| 1086 | 187 | ++ret; | |
| 1087 | } | ||
| 1088 | } | ||
| 1089 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 133 times.
|
137 | 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 | 357 | 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 | 357 | 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 | 357 | gate_char8_t* bufferptr = &buffer[0]; | |
| 1167 | 357 | gate_size_t bufferlen = sizeof(buffer) - 1; | |
| 1168 | |||
| 1169 | 357 | gate_size_t intcount = 0; | |
| 1170 | 357 | gate_size_t decimalcount = 0; | |
| 1171 | 357 | gate_size_t bufferused = 0; | |
| 1172 | gate_size_t ndx; | ||
| 1173 | |||
| 1174 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 357 times.
|
357 | if (dstlen < 3) |
| 1175 | { | ||
| 1176 | ✗ | return 0; | |
| 1177 | } | ||
| 1178 | |||
| 1179 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 357 times.
|
357 | if (gate_math_isnan(num)) |
| 1180 | { | ||
| 1181 | ✗ | return gate_str_print_text(dst, dstlen, "NaN", 3); | |
| 1182 | } | ||
| 1183 | 357 | infini = gate_math_isinfinite(num); | |
| 1184 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 357 times.
|
357 | 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 357 times.
|
357 | 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 357 times.
|
357 | 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 346 times.
|
357 | if (num < 0.0) |
| 1201 | { | ||
| 1202 | 11 | *bufferptr = '-'; | |
| 1203 | 11 | ++bufferptr; | |
| 1204 | 11 | --bufferlen; | |
| 1205 | 11 | ++bufferused; | |
| 1206 | 11 | num = -num; | |
| 1207 | } | ||
| 1208 | |||
| 1209 | 357 | intcount = gate_str_print_uint64(intbuffer, sizeof(intbuffer) - 1, (gate_uint64_t)num); | |
| 1210 | 357 | decimalnum = num - (gate_real64_t)(gate_int64_t)num; | |
| 1211 | 357 | factor = 1.0; | |
| 1212 |
3/4✓ Branch 1 taken 1800 times.
✓ Branch 2 taken 357 times.
✓ Branch 3 taken 1800 times.
✗ Branch 4 not taken.
|
2157 | while (!gate_math_iszerof((gate_real32_t)(decimalnum * factor)) && (decimalcount < sizeof(decimalbuffer) - 1)) |
| 1213 | { | ||
| 1214 | 1800 | factor *= 0.1; | |
| 1215 | 1800 | decimalnum *= 10.0; | |
| 1216 | 1800 | decimalbuffer[decimalcount] = '0' + (gate_char8_t)(gate_uint64_t)decimalnum; | |
| 1217 | 1800 | ++decimalcount; | |
| 1218 | 1800 | decimalnum -= (gate_real64_t)(gate_int64_t)decimalnum; | |
| 1219 | } | ||
| 1220 | } | ||
| 1221 | |||
| 1222 | |||
| 1223 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 356 times.
|
357 | 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 346 times.
|
356 | if (grouplen != 0) |
| 1252 | { | ||
| 1253 | 10 | ndx = gate_str_print_grouped(bufferptr, bufferlen, &intbuffer[0], intcount, grouplen); | |
| 1254 | } | ||
| 1255 | else | ||
| 1256 | { | ||
| 1257 | 346 | ndx = gate_str_print_text(bufferptr, bufferlen, &intbuffer[0], intcount); | |
| 1258 | } | ||
| 1259 | } | ||
| 1260 | 357 | bufferptr += ndx; | |
| 1261 | 357 | bufferlen -= ndx; | |
| 1262 | 357 | bufferused += ndx; | |
| 1263 | |||
| 1264 | |||
| 1265 | |||
| 1266 |
4/4✓ Branch 0 taken 8 times.
✓ Branch 1 taken 349 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 4 times.
|
357 | if ((decimallen > 0) || (decimalcount > 0)) |
| 1267 | { | ||
| 1268 | 353 | *bufferptr = '.'; | |
| 1269 | 353 | ++bufferptr; | |
| 1270 | 353 | --bufferlen; | |
| 1271 | 353 | ++bufferused; | |
| 1272 | |||
| 1273 |
2/2✓ Branch 0 taken 349 times.
✓ Branch 1 taken 4 times.
|
353 | if (decimallen > 0) |
| 1274 | { | ||
| 1275 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 253 times.
|
349 | if (decimallen > decimalcount) |
| 1276 | { | ||
| 1277 | 96 | ndx = gate_str_print_text(bufferptr, bufferlen, &decimalbuffer[0], decimalcount); | |
| 1278 |
2/2✓ Branch 0 taken 474 times.
✓ Branch 1 taken 96 times.
|
570 | for (; ndx < decimallen; ++ndx) |
| 1279 | { | ||
| 1280 | 474 | bufferptr[ndx] = '0'; | |
| 1281 | } | ||
| 1282 | } | ||
| 1283 | else | ||
| 1284 | { | ||
| 1285 | 253 | 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 | 353 | bufferptr += ndx; | |
| 1293 | 353 | bufferlen -= ndx; | |
| 1294 | 353 | bufferused += ndx; | |
| 1295 | } | ||
| 1296 | |||
| 1297 | 357 | 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 488 times.
✓ Branch 1 taken 80 times.
|
568 | if (nibble < 10) |
| 1303 | { | ||
| 1304 | 488 | return '0' + nibble; | |
| 1305 | } | ||
| 1306 | else | ||
| 1307 | { | ||
| 1308 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 56 times.
|
80 | if (uppercase) |
| 1309 | { | ||
| 1310 | 24 | return 'A' + (nibble - 10); | |
| 1311 | } | ||
| 1312 | else | ||
| 1313 | { | ||
| 1314 | 56 | 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 | 539 | gate_size_t gate_str_print_to(gate_str_printer_t printer, void* printer_param, va_list vl) | |
| 1416 | { | ||
| 1417 | 539 | gate_size_t used = 0; | |
| 1418 | 539 | 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 2461 times.
✓ Branch 1 taken 539 times.
|
3000 | while (continue_loop) |
| 1425 | { | ||
| 1426 | 2461 | int print_type = va_arg(vl, int); | |
| 1427 | 2461 | char const* ptr_text = NULL; | |
| 1428 | 2461 | gate_size_t text_len = 0; | |
| 1429 | |||
| 1430 |
17/25✓ Branch 0 taken 537 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 19 times.
✓ Branch 6 taken 1184 times.
✓ Branch 7 taken 116 times.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 16 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 3 times.
✓ Branch 13 taken 36 times.
✓ Branch 14 taken 6 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 2 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 2 times.
|
2461 | switch (print_type) |
| 1431 | { | ||
| 1432 | 537 | case GATE_PRINT_END: | |
| 1433 | { | ||
| 1434 | 537 | text_len = 0; | |
| 1435 | 537 | continue_loop = false; | |
| 1436 | 537 | 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 | 23 | case GATE_PRINT_NEWLINE: | |
| 1446 | { | ||
| 1447 | 23 | ptr_text = GATE_STR_NEWLINE; | |
| 1448 | 23 | text_len = GATE_STR_NEWLINE_LENGTH; | |
| 1449 | 23 | 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 | 19 | case GATE_PRINT_CRLF: | |
| 1464 | { | ||
| 1465 | 19 | ptr_text = GATE_STR_CRLF; | |
| 1466 | 19 | text_len = 2; | |
| 1467 | 19 | break; | |
| 1468 | } | ||
| 1469 | 1184 | case GATE_PRINT_CSTR: | |
| 1470 | { | ||
| 1471 | 1184 | char const* val_cstr = va_arg(vl, char const*); | |
| 1472 | 1184 | ptr_text = val_cstr; | |
| 1473 | 1184 | text_len = gate_str_length(val_cstr); | |
| 1474 | 1184 | break; | |
| 1475 | } | ||
| 1476 | 116 | case GATE_PRINT_STRING: | |
| 1477 | { | ||
| 1478 | 116 | gate_string_t const* val_str = va_arg(vl, gate_string_t const*); | |
| 1479 | 116 | ptr_text = gate_string_ptr(val_str, 0); | |
| 1480 | 116 | text_len = gate_string_length(val_str); | |
| 1481 | 116 | 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 | 3 | case GATE_PRINT_UI16: | |
| 1512 | { | ||
| 1513 | 3 | gate_uint16_t val_ui16 = (gate_uint16_t)va_arg(vl, int); | |
| 1514 | 3 | text_len = gate_str_print_uint16(buffer, sizeof(buffer), val_ui16); | |
| 1515 | 3 | ptr_text = buffer; | |
| 1516 | 3 | break; | |
| 1517 | } | ||
| 1518 | 36 | case GATE_PRINT_I32: | |
| 1519 | { | ||
| 1520 | 36 | gate_int32_t val_i32 = va_arg(vl, gate_int32_t); | |
| 1521 | 36 | text_len = gate_str_print_int32(buffer, sizeof(buffer), val_i32); | |
| 1522 | 36 | ptr_text = buffer; | |
| 1523 | 36 | break; | |
| 1524 | } | ||
| 1525 | 6 | case GATE_PRINT_UI32: | |
| 1526 | { | ||
| 1527 | 6 | gate_uint32_t val_ui32 = va_arg(vl, gate_uint32_t); | |
| 1528 | 6 | text_len = gate_str_print_uint32(buffer, sizeof(buffer), val_ui32); | |
| 1529 | 6 | ptr_text = buffer; | |
| 1530 | 6 | 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 | 2 | case GATE_PRINT_R64: | |
| 1582 | { | ||
| 1583 | 2 | gate_real64_t val_r64 = va_arg(vl, gate_real64_t); | |
| 1584 | 2 | text_len = gate_str_print_real(buffer, sizeof(buffer), val_r64, 0, 6, 0); | |
| 1585 | 2 | ptr_text = buffer; | |
| 1586 | 2 | 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 1919 times.
✓ Branch 1 taken 542 times.
|
2461 | if (text_len > 0) |
| 1603 | { | ||
| 1604 | 1919 | gate_size_t chars_printed = printer(printer_param, ptr_text, text_len); | |
| 1605 | 1919 | used += chars_printed; | |
| 1606 | } | ||
| 1607 | } | ||
| 1608 | 539 | 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, ¶m, 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 | 2403 | gate_bool_t gate_str_parse_digit(gate_char8_t const* src, gate_uint8_t* digit) | |
| 1852 | { | ||
| 1853 | 2403 | gate_char8_t chr = *src; | |
| 1854 |
4/4✓ Branch 0 taken 2044 times.
✓ Branch 1 taken 359 times.
✓ Branch 2 taken 1899 times.
✓ Branch 3 taken 145 times.
|
2403 | if ((chr >= '0') && (chr <= '9')) |
| 1855 | { | ||
| 1856 | 1899 | *digit = chr - '0'; | |
| 1857 | 1899 | return true; | |
| 1858 | } | ||
| 1859 | else | ||
| 1860 | { | ||
| 1861 | 504 | 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 | 816 | gate_size_t gate_str_parse_uint64(gate_char8_t const* src, gate_size_t srclen, gate_uint64_t* num) | |
| 1916 | { | ||
| 1917 | 816 | gate_size_t ret = 0; | |
| 1918 | gate_uint8_t digit; | ||
| 1919 | 816 | gate_uint64_t tmp = 0; | |
| 1920 |
2/2✓ Branch 0 taken 2025 times.
✓ Branch 1 taken 378 times.
|
2403 | while (srclen-- != 0) |
| 1921 | { | ||
| 1922 |
2/2✓ Branch 1 taken 438 times.
✓ Branch 2 taken 1587 times.
|
2025 | if (!gate_str_parse_digit(src, &digit)) |
| 1923 | { | ||
| 1924 | 438 | break; | |
| 1925 | } | ||
| 1926 | 1587 | tmp *= 10; | |
| 1927 | 1587 | tmp += (gate_uint64_t)digit; | |
| 1928 | 1587 | ++src; | |
| 1929 | 1587 | ++ret; | |
| 1930 | } | ||
| 1931 |
1/2✓ Branch 0 taken 816 times.
✗ Branch 1 not taken.
|
816 | if (num != NULL) |
| 1932 | { | ||
| 1933 | 816 | *num = tmp; | |
| 1934 | } | ||
| 1935 | |||
| 1936 | 816 | return ret; | |
| 1937 | } | ||
| 1938 | 332 | gate_size_t gate_str_parse_int64(gate_char8_t const* src, gate_size_t srclen, gate_int64_t* num) | |
| 1939 | { | ||
| 1940 | 332 | gate_size_t ret = 0; | |
| 1941 | gate_size_t tmpret; | ||
| 1942 | 332 | gate_uint64_t tmpnum = 0; | |
| 1943 | 332 | gate_bool_t neg = false; | |
| 1944 | |||
| 1945 | 332 | 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 321 times.
|
332 | if (ret == GATE_STR_NPOS) |
| 1947 | { | ||
| 1948 | 11 | return 0; | |
| 1949 | } | ||
| 1950 | 321 | src += ret; | |
| 1951 | 321 | srclen -= ret; | |
| 1952 | |||
| 1953 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 297 times.
|
321 | if (*src == '-') |
| 1954 | { | ||
| 1955 | 24 | neg = true; | |
| 1956 | 24 | --srclen; | |
| 1957 | 24 | ++src; | |
| 1958 | 24 | ++ret; | |
| 1959 | } | ||
| 1960 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 296 times.
|
297 | else if (*src == '+') |
| 1961 | { | ||
| 1962 | 1 | --srclen; | |
| 1963 | 1 | ++src; | |
| 1964 | 1 | ++ret; | |
| 1965 | } | ||
| 1966 |
1/2✓ Branch 0 taken 321 times.
✗ Branch 1 not taken.
|
321 | tmpret = gate_str_parse_uint64(src, srclen, num ? &tmpnum : NULL); |
| 1967 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 305 times.
|
321 | if (tmpret == 0) |
| 1968 | { | ||
| 1969 | 16 | return 0; | |
| 1970 | } | ||
| 1971 | 305 | ret += tmpret; | |
| 1972 |
1/2✓ Branch 0 taken 305 times.
✗ Branch 1 not taken.
|
305 | if (num != NULL) |
| 1973 | { | ||
| 1974 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 281 times.
|
305 | if (neg) |
| 1975 | { | ||
| 1976 | 24 | *num = -((gate_int64_t)tmpnum); | |
| 1977 | } | ||
| 1978 | else | ||
| 1979 | { | ||
| 1980 | 281 | *num = (gate_int64_t)tmpnum; | |
| 1981 | } | ||
| 1982 | } | ||
| 1983 | 305 | 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 11 times.
✓ Branch 1 taken 115 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 359 times.
✓ Branch 1 taken 6 times.
|
365 | while (srclen > 0) |
| 2004 | { | ||
| 2005 |
2/2✓ Branch 1 taken 66 times.
✓ Branch 2 taken 293 times.
|
359 | if (!gate_str_parse_digit(src, &digit)) |
| 2006 | { | ||
| 2007 | 66 | break; | |
| 2008 | } | ||
| 2009 | 293 | ++src; | |
| 2010 | 293 | ++ret; | |
| 2011 | 293 | --srclen; | |
| 2012 | 293 | a *= 10.0; | |
| 2013 | 293 | b *= 10.0; | |
| 2014 | 293 | 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 | 1514 | gate_bool_t gate_str_parse_hex_nibble(gate_char8_t chr, gate_uint8_t* value) | |
| 2025 | { | ||
| 2026 |
3/4✓ Branch 0 taken 1514 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 700 times.
✓ Branch 3 taken 814 times.
|
1514 | if ((chr >= '0') && (chr <= '9')) |
| 2027 | { | ||
| 2028 | 700 | *value = (gate_uint8_t)(chr - '0'); | |
| 2029 | 700 | 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 | 31 | gate_size_t gate_str_parse_hex_int(gate_char8_t const* text, gate_size_t textlen, gate_uint64_t* num) | |
| 2061 | { | ||
| 2062 | 31 | gate_size_t ret = 0; | |
| 2063 | 31 | gate_uint64_t val = 0; | |
| 2064 | gate_uint8_t n; | ||
| 2065 | |||
| 2066 |
2/2✓ Branch 0 taken 97 times.
✓ Branch 1 taken 17 times.
|
114 | while (textlen-- != 0) |
| 2067 | { | ||
| 2068 |
2/2✓ Branch 1 taken 14 times.
✓ Branch 2 taken 83 times.
|
97 | if (!gate_str_parse_hex_nibble(*text, &n)) |
| 2069 | { | ||
| 2070 | 14 | break; | |
| 2071 | } | ||
| 2072 | 83 | val *= 16; | |
| 2073 | 83 | val += n; | |
| 2074 | 83 | ++ret; | |
| 2075 | 83 | ++text; | |
| 2076 | } | ||
| 2077 |
1/2✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
|
31 | if (num != NULL) |
| 2078 | { | ||
| 2079 | 31 | *num = val; | |
| 2080 | } | ||
| 2081 | 31 | 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 | 3006 | 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 2162 times.
✓ Branch 1 taken 844 times.
|
3006 | if (len > 0) |
| 2208 | { | ||
| 2209 | 2162 | obj->buffer = gate_mem_alloc(sizeof(gate_stringbuffer_t) + len + 1); | |
| 2210 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2162 times.
|
2162 | if (obj->buffer == NULL) |
| 2211 | { | ||
| 2212 | ✗ | return NULL; | |
| 2213 | } | ||
| 2214 | 2162 | gate_mem_copy(&obj->buffer->data[0], str, len); | |
| 2215 | 2162 | obj->buffer->data[len] = 0; | |
| 2216 | 2162 | obj->str = &obj->buffer->data[0]; | |
| 2217 | 2162 | obj->length = len; | |
| 2218 | 2162 | gate_atomic_int_init(&obj->buffer->refcount, 1); | |
| 2219 | } | ||
| 2220 | else | ||
| 2221 | { | ||
| 2222 | 844 | gate_string_create_empty(obj); | |
| 2223 | } | ||
| 2224 | |||
| 2225 | 3006 | return obj; | |
| 2226 | } | ||
| 2227 | 6789 | gate_string_t* gate_string_create_static_len(gate_string_t* obj, gate_char8_t const* str, gate_size_t len) | |
| 2228 | { | ||
| 2229 | 6789 | obj->str = str; | |
| 2230 | 6789 | obj->length = len; | |
| 2231 | 6789 | obj->buffer = NULL; | |
| 2232 | 6789 | return obj; | |
| 2233 | } | ||
| 2234 | 3430 | gate_string_t* gate_string_create_static(gate_string_t* obj, gate_char8_t const* str) | |
| 2235 | { | ||
| 2236 | 3430 | gate_size_t const len = gate_str_length(str); | |
| 2237 | 3430 | return gate_string_create_static_len(obj, str, len); | |
| 2238 | } | ||
| 2239 | 5109 | gate_string_t* gate_string_create_empty(gate_string_t* obj) | |
| 2240 | { | ||
| 2241 | 5109 | obj->str = NULL; | |
| 2242 | 5109 | obj->length = 0; | |
| 2243 | 5109 | obj->buffer = NULL; | |
| 2244 | 5109 | 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 | 14 | 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 14 times.
|
14 | if (src == NULL) |
| 2355 | { | ||
| 2356 | ✗ | return gate_string_create_empty(obj); | |
| 2357 | } | ||
| 2358 | else | ||
| 2359 | { | ||
| 2360 | 14 | return gate_string_create(obj, src->str, src->length); | |
| 2361 | } | ||
| 2362 | } | ||
| 2363 | |||
| 2364 | |||
| 2365 | |||
| 2366 | 32632 | void gate_string_release(gate_string_t* obj) | |
| 2367 | { | ||
| 2368 |
2/2✓ Branch 0 taken 21776 times.
✓ Branch 1 taken 10856 times.
|
32632 | if (obj->buffer != NULL) |
| 2369 | { | ||
| 2370 |
2/2✓ Branch 1 taken 3255 times.
✓ Branch 2 taken 18521 times.
|
21776 | if (gate_atomic_int_dec(&obj->buffer->refcount) == 0) |
| 2371 | { | ||
| 2372 | 3255 | gate_mem_dealloc(obj->buffer); | |
| 2373 | } | ||
| 2374 | 21776 | obj->buffer = NULL; | |
| 2375 | } | ||
| 2376 | 32632 | obj->str = NULL; | |
| 2377 | 32632 | obj->length = 0; | |
| 2378 | 32632 | } | |
| 2379 | |||
| 2380 | 5059 | gate_bool_t gate_string_is_empty(gate_string_t const* obj) | |
| 2381 | { | ||
| 2382 |
2/2✓ Branch 0 taken 5038 times.
✓ Branch 1 taken 21 times.
|
5059 | if (obj != NULL) |
| 2383 | { | ||
| 2384 |
2/2✓ Branch 0 taken 4446 times.
✓ Branch 1 taken 592 times.
|
5038 | if (obj->length > 0) |
| 2385 | { | ||
| 2386 | 4446 | return false; | |
| 2387 | } | ||
| 2388 | } | ||
| 2389 | 613 | return true; | |
| 2390 | } | ||
| 2391 | |||
| 2392 | 9831 | gate_string_t* gate_string_duplicate(gate_string_t* dst, gate_string_t const* src) | |
| 2393 | { | ||
| 2394 |
2/2✓ Branch 0 taken 9607 times.
✓ Branch 1 taken 224 times.
|
9831 | if (src != NULL) |
| 2395 | { | ||
| 2396 | 9607 | dst->buffer = src->buffer; | |
| 2397 | 9607 | dst->str = src->str; | |
| 2398 | 9607 | dst->length = src->length; | |
| 2399 |
2/2✓ Branch 0 taken 8495 times.
✓ Branch 1 taken 1112 times.
|
9607 | if (dst->buffer != NULL) |
| 2400 | { | ||
| 2401 | 8495 | gate_atomic_int_inc(&dst->buffer->refcount); | |
| 2402 | } | ||
| 2403 | } | ||
| 2404 | else | ||
| 2405 | { | ||
| 2406 | 224 | dst->buffer = NULL; | |
| 2407 | 224 | dst->str = NULL; | |
| 2408 | 224 | dst->length = 0; | |
| 2409 | } | ||
| 2410 | 9831 | return dst; | |
| 2411 | } | ||
| 2412 | 4489 | gate_string_t* gate_string_clone(gate_string_t* dst, gate_string_t const* src) | |
| 2413 | { | ||
| 2414 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4489 times.
|
4489 | if (src == NULL) |
| 2415 | { | ||
| 2416 | ✗ | return gate_string_create_empty(dst); | |
| 2417 | } | ||
| 2418 | else | ||
| 2419 | { | ||
| 2420 |
2/2✓ Branch 0 taken 1227 times.
✓ Branch 1 taken 3262 times.
|
4489 | if (src->buffer == NULL) |
| 2421 | { | ||
| 2422 | 1227 | return gate_string_create(dst, src->str, src->length); | |
| 2423 | } | ||
| 2424 | else | ||
| 2425 | { | ||
| 2426 | 3262 | dst->str = src->str; | |
| 2427 | 3262 | dst->length = src->length; | |
| 2428 | 3262 | dst->buffer = src->buffer; | |
| 2429 | 3262 | gate_atomic_int_inc(&dst->buffer->refcount); | |
| 2430 | 3262 | return dst; | |
| 2431 | } | ||
| 2432 | } | ||
| 2433 | } | ||
| 2434 | |||
| 2435 | 41706 | gate_size_t gate_string_length(gate_string_t const* obj) | |
| 2436 | { | ||
| 2437 |
1/2✓ Branch 0 taken 41706 times.
✗ Branch 1 not taken.
|
41706 | return obj ? obj->length : 0; |
| 2438 | } | ||
| 2439 | 4 | gate_bool_t gate_string_like(gate_string_t const* obj, gate_string_t const* pattern) | |
| 2440 | { | ||
| 2441 | 4 | return gate_str_like(obj->str, obj->length, pattern->str, pattern->length); | |
| 2442 | } | ||
| 2443 | 31 | gate_bool_t gate_string_like_one_of(gate_string_t const* obj, gate_string_t const* pattern, gate_char8_t separator) | |
| 2444 | { | ||
| 2445 | 31 | return gate_str_like_one_of(obj->str, obj->length, pattern->str, pattern->length, separator); | |
| 2446 | } | ||
| 2447 | |||
| 2448 | |||
| 2449 | 220 | gate_size_t gate_string_count_chars(gate_string_t const* obj, gate_char8_t chr) | |
| 2450 | { | ||
| 2451 |
1/2✓ Branch 0 taken 220 times.
✗ Branch 1 not taken.
|
220 | if (obj) |
| 2452 | { | ||
| 2453 | 220 | return gate_str_count_chars(obj->str, obj->length, chr); | |
| 2454 | } | ||
| 2455 | ✗ | return 0; | |
| 2456 | } | ||
| 2457 | 85 | gate_char8_t gate_string_char_at(gate_string_t const* str, gate_size_t index) | |
| 2458 | { | ||
| 2459 | 85 | gate_char8_t ret = '\0'; | |
| 2460 |
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)) |
| 2461 | { | ||
| 2462 | 85 | ret = str->str[index]; | |
| 2463 | } | ||
| 2464 | 85 | return ret; | |
| 2465 | } | ||
| 2466 | |||
| 2467 | 1446 | gate_size_t gate_string_char_pos(gate_string_t const* obj, gate_char8_t find, gate_size_t startat) | |
| 2468 | { | ||
| 2469 | 1446 | return gate_str_char_pos(obj->str, obj->length, find, startat); | |
| 2470 | } | ||
| 2471 | 449 | gate_size_t gate_string_char_pos_last(gate_string_t const* obj, gate_char8_t find) | |
| 2472 | { | ||
| 2473 | 449 | return gate_str_char_pos_last(obj->str, obj->length, find); | |
| 2474 | } | ||
| 2475 | 10 | gate_size_t gate_string_find_first_of(gate_string_t const* obj, gate_string_t const* chars, gate_size_t startat) | |
| 2476 | { | ||
| 2477 | 10 | return gate_str_find_first_of(obj->str, obj->length, chars->str, chars->length, startat); | |
| 2478 | } | ||
| 2479 | 102 | gate_size_t gate_string_find_first_not_of(gate_string_t const* obj, gate_string_t const* chars, gate_size_t startat) | |
| 2480 | { | ||
| 2481 | 102 | return gate_str_find_first_not_of(obj->str, obj->length, chars->str, chars->length, startat); | |
| 2482 | } | ||
| 2483 | 2 | gate_size_t gate_string_find_last_of(gate_string_t const* obj, gate_string_t const* chars) | |
| 2484 | { | ||
| 2485 | 2 | return gate_str_find_last_of(obj->str, obj->length, chars->str, chars->length); | |
| 2486 | } | ||
| 2487 | 26 | gate_size_t gate_string_find_last_not_of(gate_string_t const* obj, gate_string_t const* chars) | |
| 2488 | { | ||
| 2489 | 26 | return gate_str_find_last_not_of(obj->str, obj->length, chars->str, chars->length); | |
| 2490 | } | ||
| 2491 | |||
| 2492 | 211 | gate_size_t gate_string_pos(gate_string_t const* obj, gate_string_t const* find, gate_size_t startat) | |
| 2493 | { | ||
| 2494 | 211 | return gate_str_pos(obj->str, obj->length, find->str, find->length, startat); | |
| 2495 | } | ||
| 2496 | 2 | gate_size_t gate_string_pos_last(gate_string_t const* obj, gate_string_t const* find) | |
| 2497 | { | ||
| 2498 | 2 | return gate_str_pos_last(obj->str, obj->length, find->str, find->length); | |
| 2499 | } | ||
| 2500 | |||
| 2501 | |||
| 2502 | 176 | gate_size_t gate_string_parse(gate_string_t const* obj, gate_string_t const* find, gate_size_t start_at, | |
| 2503 | gate_string_t* head, gate_string_t* tail, gate_bool_t separator_as_tail) | ||
| 2504 | { | ||
| 2505 | 176 | gate_size_t pos = gate_string_pos(obj, find, start_at); | |
| 2506 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 147 times.
|
176 | if (pos == GATE_STR_NPOS) |
| 2507 | { | ||
| 2508 | 29 | return 0; /* nothing parsed */ | |
| 2509 | } | ||
| 2510 | else | ||
| 2511 | { | ||
| 2512 |
1/2✓ Branch 0 taken 147 times.
✗ Branch 1 not taken.
|
147 | if (head) |
| 2513 | { | ||
| 2514 | 147 | gate_string_substr(head, obj, 0, pos); | |
| 2515 | } | ||
| 2516 |
1/2✓ Branch 0 taken 147 times.
✗ Branch 1 not taken.
|
147 | if (tail) |
| 2517 | { | ||
| 2518 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 131 times.
|
147 | if (separator_as_tail) |
| 2519 | { | ||
| 2520 | 16 | gate_string_substr(tail, obj, pos, GATE_STR_NPOS); | |
| 2521 | } | ||
| 2522 | else | ||
| 2523 | { | ||
| 2524 | 131 | gate_string_substr(tail, obj, pos + find->length, GATE_STR_NPOS); | |
| 2525 | } | ||
| 2526 | } | ||
| 2527 | 147 | return pos + gate_string_length(find); /* (head + separator) parsed */ | |
| 2528 | } | ||
| 2529 | } | ||
| 2530 | |||
| 2531 | 5 | gate_size_t gate_string_parse_int(gate_string_t const* obj, gate_int64_t* num) | |
| 2532 | { | ||
| 2533 | 5 | return gate_str_parse_int64(obj->str, obj->length, num); | |
| 2534 | } | ||
| 2535 | |||
| 2536 | 129 | gate_size_t gate_string_parse_uint(gate_string_t const* obj, gate_uint64_t* num) | |
| 2537 | { | ||
| 2538 | 129 | return gate_str_parse_uint64(obj->str, obj->length, num); | |
| 2539 | } | ||
| 2540 | |||
| 2541 | 3 | gate_size_t gate_string_parse_real(gate_string_t const* obj, gate_real64_t* num) | |
| 2542 | { | ||
| 2543 | 3 | return gate_str_parse_real(obj->str, obj->length, num); | |
| 2544 | } | ||
| 2545 | |||
| 2546 | |||
| 2547 | 1 | gate_size_t gate_string_parse_hex(gate_string_t const* obj, gate_uint64_t* num) | |
| 2548 | { | ||
| 2549 | 1 | return gate_str_parse_hex_int(obj->str, obj->length, num); | |
| 2550 | } | ||
| 2551 | |||
| 2552 | |||
| 2553 | 3819 | gate_char8_t const* gate_string_ptr(gate_string_t const* obj, gate_size_t index) | |
| 2554 | { | ||
| 2555 |
3/4✓ Branch 0 taken 3819 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3793 times.
✓ Branch 3 taken 26 times.
|
3819 | if ((obj != NULL) && (obj->str != NULL)) |
| 2556 | { | ||
| 2557 | 3793 | return obj->str + index; | |
| 2558 | } | ||
| 2559 | else | ||
| 2560 | { | ||
| 2561 | 26 | return NULL; | |
| 2562 | } | ||
| 2563 | } | ||
| 2564 | 7313 | gate_string_t* gate_string_substr(gate_string_t* dst, gate_string_t const* src, gate_size_t offset, gate_size_t length) | |
| 2565 | { | ||
| 2566 |
1/2✓ Branch 0 taken 7313 times.
✗ Branch 1 not taken.
|
7313 | if (dst != NULL) |
| 2567 | { | ||
| 2568 |
5/6✓ Branch 0 taken 7313 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7258 times.
✓ Branch 3 taken 55 times.
✓ Branch 4 taken 34 times.
✓ Branch 5 taken 7224 times.
|
7313 | if ((src == NULL) || (offset >= src->length) || (length == 0)) |
| 2569 | { | ||
| 2570 |
2/2✓ Branch 0 taken 50 times.
✓ Branch 1 taken 39 times.
|
178 | if (src == dst) |
| 2571 | { | ||
| 2572 | 50 | gate_string_release(dst); | |
| 2573 | } | ||
| 2574 | else | ||
| 2575 | { | ||
| 2576 | 39 | dst->str = NULL; | |
| 2577 | 39 | dst->length = 0; | |
| 2578 | 39 | dst->buffer = NULL; | |
| 2579 | } | ||
| 2580 | } | ||
| 2581 | else | ||
| 2582 | { | ||
| 2583 |
2/2✓ Branch 0 taken 3763 times.
✓ Branch 1 taken 3461 times.
|
7224 | if (length > src->length - offset) |
| 2584 | { | ||
| 2585 | 3763 | length = src->length - offset; | |
| 2586 | } | ||
| 2587 | |||
| 2588 | 7224 | dst->buffer = src->buffer; | |
| 2589 | 7224 | dst->str = &src->str[offset]; | |
| 2590 | 7224 | dst->length = length; | |
| 2591 | |||
| 2592 |
4/4✓ Branch 0 taken 5217 times.
✓ Branch 1 taken 2007 times.
✓ Branch 2 taken 4269 times.
✓ Branch 3 taken 948 times.
|
7224 | if ((dst->buffer != NULL) && (dst != src)) |
| 2593 | { | ||
| 2594 | 4269 | gate_atomic_int_inc(&dst->buffer->refcount); | |
| 2595 | } | ||
| 2596 | } | ||
| 2597 | } | ||
| 2598 | 7313 | return dst; | |
| 2599 | } | ||
| 2600 | |||
| 2601 | 11292 | int gate_string_comp(gate_string_t const* src, gate_string_t const* dst) | |
| 2602 | { | ||
| 2603 | 11292 | gate_char8_t const* ptr_src = NULL; | |
| 2604 | 11292 | gate_size_t len_src = 0; | |
| 2605 | 11292 | gate_char8_t const* ptr_dst = NULL; | |
| 2606 | 11292 | gate_size_t len_dst = 0; | |
| 2607 | |||
| 2608 |
1/2✓ Branch 0 taken 11292 times.
✗ Branch 1 not taken.
|
11292 | if (src) |
| 2609 | { | ||
| 2610 | 11292 | ptr_src = src->str; | |
| 2611 | 11292 | len_src = src->length; | |
| 2612 | } | ||
| 2613 |
2/2✓ Branch 0 taken 11286 times.
✓ Branch 1 taken 6 times.
|
11292 | if (dst) |
| 2614 | { | ||
| 2615 | 11286 | ptr_dst = dst->str; | |
| 2616 | 11286 | len_dst = dst->length; | |
| 2617 | } | ||
| 2618 | |||
| 2619 | 11292 | return gate_str_compare(ptr_src, len_src, ptr_dst, len_dst); | |
| 2620 | } | ||
| 2621 | |||
| 2622 | 1173 | int gate_string_comp_ic(gate_string_t const* src, gate_string_t const* dst) | |
| 2623 | { | ||
| 2624 | 1173 | gate_char8_t const* ptr_src = NULL; | |
| 2625 | 1173 | gate_size_t len_src = 0; | |
| 2626 | 1173 | gate_char8_t const* ptr_dst = NULL; | |
| 2627 | 1173 | gate_size_t len_dst = 0; | |
| 2628 | |||
| 2629 |
1/2✓ Branch 0 taken 1173 times.
✗ Branch 1 not taken.
|
1173 | if (src) |
| 2630 | { | ||
| 2631 | 1173 | ptr_src = src->str; | |
| 2632 | 1173 | len_src = src->length; | |
| 2633 | } | ||
| 2634 |
1/2✓ Branch 0 taken 1173 times.
✗ Branch 1 not taken.
|
1173 | if (dst) |
| 2635 | { | ||
| 2636 | 1173 | ptr_dst = dst->str; | |
| 2637 | 1173 | len_dst = dst->length; | |
| 2638 | } | ||
| 2639 | 1173 | return gate_str_compare_ic(ptr_src, len_src, ptr_dst, len_dst); | |
| 2640 | } | ||
| 2641 | 1185 | gate_size_t gate_string_to_buffer(gate_string_t const* src, gate_char8_t* buffer, gate_size_t bufferlength) | |
| 2642 | { | ||
| 2643 | gate_size_t ret; | ||
| 2644 |
2/4✓ Branch 0 taken 1185 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1185 times.
|
1185 | if ((buffer == NULL) || (bufferlength == 0)) |
| 2645 | { | ||
| 2646 | ✗ | ret = 0; | |
| 2647 | } | ||
| 2648 | else | ||
| 2649 | { | ||
| 2650 |
2/2✓ Branch 1 taken 7 times.
✓ Branch 2 taken 1178 times.
|
1185 | if (gate_string_is_empty(src)) |
| 2651 | { | ||
| 2652 | 7 | buffer[0] = 0; | |
| 2653 | 7 | ret = 0; | |
| 2654 | } | ||
| 2655 | else | ||
| 2656 | { | ||
| 2657 | 1178 | ret = gate_str_print_text(buffer, bufferlength, src->str, src->length); | |
| 2658 | } | ||
| 2659 | } | ||
| 2660 | 1185 | return ret; | |
| 2661 | } | ||
| 2662 | |||
| 2663 | |||
| 2664 | 13015 | gate_bool_t gate_string_starts_with(gate_string_t const* obj, gate_string_t const* token) | |
| 2665 | { | ||
| 2666 | 13015 | gate_size_t objlen = gate_string_length(obj); | |
| 2667 | 13015 | gate_size_t tokenlen = gate_string_length(token); | |
| 2668 |
3/4✓ Branch 0 taken 13015 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 733 times.
✓ Branch 3 taken 12282 times.
|
13015 | if ((tokenlen == 0) || (objlen < tokenlen)) |
| 2669 | { | ||
| 2670 | 733 | return false; | |
| 2671 | } | ||
| 2672 | 12282 | return (gate_str_compare(obj->str, tokenlen, token->str, tokenlen) == 0); | |
| 2673 | } | ||
| 2674 | 355 | gate_bool_t gate_string_starts_with_str(gate_string_t const* obj, gate_char8_t const* text) | |
| 2675 | { | ||
| 2676 | gate_string_t tmp; | ||
| 2677 | 355 | gate_string_create_static(&tmp, text); | |
| 2678 | 355 | return gate_string_starts_with(obj, &tmp); | |
| 2679 | } | ||
| 2680 | 4352 | gate_bool_t gate_string_starts_with_char(gate_string_t const* obj, gate_char8_t chr) | |
| 2681 | { | ||
| 2682 | 4352 | gate_size_t objlen = gate_string_length(obj); | |
| 2683 |
4/4✓ Branch 0 taken 4343 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 2063 times.
✓ Branch 3 taken 2280 times.
|
4352 | return (objlen >= 1) && (obj->str[0] == chr); |
| 2684 | } | ||
| 2685 | 1 | gate_bool_t gate_string_starts_with_ic(gate_string_t const* obj, gate_string_t const* token) | |
| 2686 | { | ||
| 2687 | 1 | gate_size_t objlen = gate_string_length(obj); | |
| 2688 | 1 | gate_size_t tokenlen = gate_string_length(token); | |
| 2689 |
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)) |
| 2690 | { | ||
| 2691 | ✗ | return false; | |
| 2692 | } | ||
| 2693 | 1 | return (gate_str_compare_ic(obj->str, tokenlen, token->str, tokenlen) == 0); | |
| 2694 | } | ||
| 2695 | 1 | gate_bool_t gate_string_starts_with_str_ic(gate_string_t const* obj, gate_char8_t const* text) | |
| 2696 | { | ||
| 2697 | gate_string_t tmp; | ||
| 2698 | 1 | gate_string_create_static(&tmp, text); | |
| 2699 | 1 | return gate_string_starts_with_ic(obj, &tmp); | |
| 2700 | } | ||
| 2701 | |||
| 2702 | 586 | gate_bool_t gate_string_ends_with(gate_string_t const* obj, gate_string_t const* token) | |
| 2703 | { | ||
| 2704 | 586 | gate_size_t objlen = gate_string_length(obj); | |
| 2705 | 586 | gate_size_t tokenlen = gate_string_length(token); | |
| 2706 |
3/4✓ Branch 0 taken 586 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 582 times.
|
586 | if ((tokenlen == 0) || (objlen < tokenlen)) |
| 2707 | { | ||
| 2708 | 4 | return false; | |
| 2709 | } | ||
| 2710 | 582 | return (gate_str_compare(&obj->str[objlen - tokenlen], tokenlen, token->str, tokenlen) == 0); | |
| 2711 | } | ||
| 2712 | 566 | gate_bool_t gate_string_ends_with_str(gate_string_t const* obj, gate_char8_t const* text) | |
| 2713 | { | ||
| 2714 | gate_string_t tmp; | ||
| 2715 | 566 | gate_string_create_static(&tmp, text); | |
| 2716 | 566 | return gate_string_ends_with(obj, &tmp); | |
| 2717 | } | ||
| 2718 | 430 | gate_bool_t gate_string_ends_with_char(gate_string_t const* obj, gate_char8_t chr) | |
| 2719 | { | ||
| 2720 | 430 | gate_size_t objlen = gate_string_length(obj); | |
| 2721 |
3/4✓ Branch 0 taken 430 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 426 times.
|
430 | return (objlen >= 1) && (obj->str[objlen - 1] == chr); |
| 2722 | } | ||
| 2723 | 1 | gate_bool_t gate_string_ends_with_ic(gate_string_t const* obj, gate_string_t const* token) | |
| 2724 | { | ||
| 2725 | 1 | gate_size_t objlen = gate_string_length(obj); | |
| 2726 | 1 | gate_size_t tokenlen = gate_string_length(token); | |
| 2727 |
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)) |
| 2728 | { | ||
| 2729 | ✗ | return false; | |
| 2730 | } | ||
| 2731 | 1 | return (gate_str_compare_ic(&obj->str[objlen - tokenlen], tokenlen, token->str, tokenlen) == 0); | |
| 2732 | } | ||
| 2733 | 1 | gate_bool_t gate_string_ends_with_str_ic(gate_string_t const* obj, gate_char8_t const* text) | |
| 2734 | { | ||
| 2735 | gate_string_t tmp; | ||
| 2736 | 1 | gate_string_create_static(&tmp, text); | |
| 2737 | 1 | return gate_string_ends_with_ic(obj, &tmp); | |
| 2738 | } | ||
| 2739 | |||
| 2740 | 1 | gate_bool_t gate_string_contains(gate_string_t const* obj, gate_string_t const* token) | |
| 2741 | { | ||
| 2742 | 1 | return gate_string_pos(obj, token, 0) != GATE_STR_NPOS; | |
| 2743 | } | ||
| 2744 | |||
| 2745 | 1414 | gate_bool_t gate_string_equals(gate_string_t const* obj, gate_string_t const* token) | |
| 2746 | { | ||
| 2747 | 1414 | return gate_string_comp(obj, token) == 0; | |
| 2748 | } | ||
| 2749 | 641 | gate_bool_t gate_string_equals_str(gate_string_t const* obj, gate_char8_t const* token) | |
| 2750 | { | ||
| 2751 | gate_string_t tmp; | ||
| 2752 | 641 | gate_string_create_static(&tmp, token); | |
| 2753 | 641 | return gate_string_equals(obj, &tmp); | |
| 2754 | } | ||
| 2755 | 8 | gate_bool_t gate_string_equals_ic(gate_string_t const* obj, gate_string_t const* token) | |
| 2756 | { | ||
| 2757 | 8 | return gate_string_comp_ic(obj, token) == 0; | |
| 2758 | } | ||
| 2759 | 4 | gate_bool_t gate_string_equals_str_ic(gate_string_t const* obj, gate_char8_t const* token) | |
| 2760 | { | ||
| 2761 | gate_string_t tmp; | ||
| 2762 | 4 | gate_string_create_static(&tmp, token); | |
| 2763 | 4 | return gate_string_equals_ic(obj, &tmp); | |
| 2764 | } | ||
| 2765 | 2989 | gate_string_t* gate_string_ltrim(gate_string_t* dst, gate_string_t const* src) | |
| 2766 | { | ||
| 2767 | 2989 | gate_size_t pos = gate_str_find_first_not_of(src->str, src->length, GATE_STR_WHITESPACES, GATE_STR_WHITESPACES_LENGTH, 0); | |
| 2768 |
2/2✓ Branch 0 taken 69 times.
✓ Branch 1 taken 2920 times.
|
2989 | if (pos == GATE_STR_NPOS) |
| 2769 | { | ||
| 2770 |
1/2✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
|
69 | if (dst == src) |
| 2771 | { | ||
| 2772 | 69 | gate_string_release(dst); | |
| 2773 | } | ||
| 2774 | 69 | gate_string_create_empty(dst); | |
| 2775 | } | ||
| 2776 | else | ||
| 2777 | { | ||
| 2778 | 2920 | dst->str = &src->str[pos]; | |
| 2779 | 2920 | dst->length = src->length - pos; | |
| 2780 |
2/2✓ Branch 0 taken 2496 times.
✓ Branch 1 taken 424 times.
|
2920 | if (dst != src) |
| 2781 | { | ||
| 2782 | 2496 | dst->buffer = src->buffer; | |
| 2783 |
2/2✓ Branch 0 taken 2495 times.
✓ Branch 1 taken 1 times.
|
2496 | if (dst->buffer != NULL) |
| 2784 | { | ||
| 2785 | 2495 | gate_atomic_int_inc(&dst->buffer->refcount); | |
| 2786 | } | ||
| 2787 | } | ||
| 2788 | } | ||
| 2789 | 2989 | return dst; | |
| 2790 | } | ||
| 2791 | 169 | gate_string_t* gate_string_rtrim(gate_string_t* dst, gate_string_t const* src) | |
| 2792 | { | ||
| 2793 | 169 | gate_size_t pos = gate_str_find_last_not_of(src->str, src->length, GATE_STR_WHITESPACES, GATE_STR_WHITESPACES_LENGTH); | |
| 2794 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 145 times.
|
169 | if (pos == GATE_STR_NPOS) |
| 2795 | { | ||
| 2796 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | if (dst == src) |
| 2797 | { | ||
| 2798 | 24 | gate_string_release(dst); | |
| 2799 | } | ||
| 2800 | 24 | gate_string_create_empty(dst); | |
| 2801 | } | ||
| 2802 | else | ||
| 2803 | { | ||
| 2804 | 145 | dst->length = pos + 1; | |
| 2805 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 144 times.
|
145 | if (dst != src) |
| 2806 | { | ||
| 2807 | 1 | dst->str = src->str; | |
| 2808 | 1 | dst->buffer = src->buffer; | |
| 2809 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (dst->buffer != NULL) |
| 2810 | { | ||
| 2811 | ✗ | gate_atomic_int_inc(&dst->buffer->refcount); | |
| 2812 | } | ||
| 2813 | } | ||
| 2814 | } | ||
| 2815 | 169 | return dst; | |
| 2816 | } | ||
| 2817 | 3712 | gate_string_t* gate_string_trim(gate_string_t* dst, gate_string_t const* src) | |
| 2818 | { | ||
| 2819 | 3712 | gate_size_t newlength = gate_str_find_last_not_of(src->str, src->length, GATE_STR_WHITESPACES, GATE_STR_WHITESPACES_LENGTH); | |
| 2820 | gate_size_t pos; | ||
| 2821 | |||
| 2822 |
2/2✓ Branch 0 taken 146 times.
✓ Branch 1 taken 3566 times.
|
3712 | if (newlength == GATE_STR_NPOS) |
| 2823 | { | ||
| 2824 |
1/2✓ Branch 0 taken 146 times.
✗ Branch 1 not taken.
|
146 | if (dst == src) |
| 2825 | { | ||
| 2826 | 146 | gate_string_release(dst); | |
| 2827 | } | ||
| 2828 | 146 | return gate_string_create_empty(dst); | |
| 2829 | } | ||
| 2830 | 3566 | ++newlength; | |
| 2831 | |||
| 2832 | 3566 | pos = gate_str_find_first_not_of(src->str, newlength, GATE_STR_WHITESPACES, GATE_STR_WHITESPACES_LENGTH, 0); | |
| 2833 | |||
| 2834 | 3566 | dst->str = &src->str[pos]; | |
| 2835 | 3566 | dst->length = newlength - pos; | |
| 2836 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3565 times.
|
3566 | if (dst != src) |
| 2837 | { | ||
| 2838 | 1 | dst->buffer = src->buffer; | |
| 2839 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (dst->buffer != NULL) |
| 2840 | { | ||
| 2841 | ✗ | gate_atomic_int_inc(&dst->buffer->refcount); | |
| 2842 | } | ||
| 2843 | } | ||
| 2844 | 3566 | return dst; | |
| 2845 | } | ||
| 2846 | |||
| 2847 | 279 | gate_string_t* gate_string_read_line(gate_string_t* line, gate_string_t const* src, gate_string_t* tail) | |
| 2848 | { | ||
| 2849 | 279 | gate_string_t* ret = NULL; | |
| 2850 | gate_size_t pos; | ||
| 2851 |
2/2✓ Branch 1 taken 261 times.
✓ Branch 2 taken 18 times.
|
279 | if (!gate_string_is_empty(src)) |
| 2852 | { | ||
| 2853 | 261 | pos = gate_str_char_pos(src->str, src->length, '\n', 0); | |
| 2854 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 260 times.
|
261 | if (pos == GATE_STR_NPOS) |
| 2855 | { | ||
| 2856 | 1 | ret = gate_string_duplicate(line, src); | |
| 2857 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (tail != NULL) |
| 2858 | { | ||
| 2859 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (tail == src) |
| 2860 | { | ||
| 2861 | 1 | gate_string_release(tail); | |
| 2862 | } | ||
| 2863 | else | ||
| 2864 | { | ||
| 2865 | ✗ | gate_string_create_empty(tail); | |
| 2866 | } | ||
| 2867 | } | ||
| 2868 | } | ||
| 2869 | else | ||
| 2870 | { | ||
| 2871 | 260 | ret = gate_string_substr(line, src, 0, pos); | |
| 2872 |
1/2✓ Branch 0 taken 260 times.
✗ Branch 1 not taken.
|
260 | if (ret != NULL) |
| 2873 | { | ||
| 2874 |
2/2✓ Branch 1 taken 123 times.
✓ Branch 2 taken 137 times.
|
260 | if (gate_string_ends_with_str(line, "\r")) |
| 2875 | { | ||
| 2876 | 123 | --line->length; | |
| 2877 | } | ||
| 2878 | } | ||
| 2879 |
1/2✓ Branch 0 taken 260 times.
✗ Branch 1 not taken.
|
260 | if (tail != NULL) |
| 2880 | { | ||
| 2881 |
2/2✓ Branch 0 taken 146 times.
✓ Branch 1 taken 114 times.
|
260 | if (tail == src) |
| 2882 | { | ||
| 2883 | 146 | tail->str += (pos + 1); | |
| 2884 | 146 | tail->length -= (pos + 1); | |
| 2885 | } | ||
| 2886 | else | ||
| 2887 | { | ||
| 2888 | 114 | gate_string_substr(tail, src, pos + 1, GATE_STR_NPOS); | |
| 2889 | } | ||
| 2890 | } | ||
| 2891 | } | ||
| 2892 | } | ||
| 2893 | 279 | return ret; | |
| 2894 | } | ||
| 2895 | |||
| 2896 | 14 | gate_string_t* gate_string_read_token(gate_string_t* token, gate_string_t const* src, gate_string_t* tail) | |
| 2897 | { | ||
| 2898 | 14 | gate_string_t* ret = NULL; | |
| 2899 | gate_size_t pos; | ||
| 2900 | gate_size_t pos2; | ||
| 2901 |
1/2✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
|
14 | if (!gate_string_is_empty(src)) |
| 2902 | { | ||
| 2903 | 14 | pos = gate_str_find_first_not_of(src->str, src->length, GATE_STR_WHITESPACES, GATE_STR_WHITESPACES_LENGTH, 0); | |
| 2904 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
|
14 | if (pos == GATE_STR_NPOS) |
| 2905 | { | ||
| 2906 | ✗ | ret = gate_string_create_empty(token); | |
| 2907 | ✗ | if (tail != NULL) | |
| 2908 | { | ||
| 2909 | ✗ | gate_string_create_empty(tail); | |
| 2910 | } | ||
| 2911 | } | ||
| 2912 | else | ||
| 2913 | { | ||
| 2914 | 14 | pos2 = gate_str_find_first_of(src->str, src->length, GATE_STR_WHITESPACES, GATE_STR_WHITESPACES_LENGTH, pos); | |
| 2915 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
|
14 | if (pos2 == GATE_STR_NPOS) |
| 2916 | { | ||
| 2917 | ✗ | ret = gate_string_substr(token, src, pos, src->length - pos); | |
| 2918 | ✗ | if (tail != NULL) | |
| 2919 | { | ||
| 2920 | ✗ | gate_string_create_empty(tail); | |
| 2921 | } | ||
| 2922 | } | ||
| 2923 | else | ||
| 2924 | { | ||
| 2925 | 14 | ret = gate_string_substr(token, src, pos, pos2 - pos); | |
| 2926 |
1/2✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
|
14 | if (tail != NULL) |
| 2927 | { | ||
| 2928 | 14 | gate_string_substr(tail, src, pos2, src->length - pos2); | |
| 2929 | } | ||
| 2930 | } | ||
| 2931 | } | ||
| 2932 | } | ||
| 2933 | 14 | return ret; | |
| 2934 | } | ||
| 2935 | |||
| 2936 | 1 | gate_string_t* gate_string_to_lower(gate_string_t* dst, gate_string_t const* src) | |
| 2937 | { | ||
| 2938 | 1 | gate_string_t* ret = gate_string_create_copy(dst, src); | |
| 2939 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (NULL != ret) |
| 2940 | { | ||
| 2941 | 1 | gate_str_to_lower(ret->buffer->data, ret->length); | |
| 2942 | } | ||
| 2943 | 1 | return ret; | |
| 2944 | } | ||
| 2945 | 1 | gate_string_t* gate_string_to_upper(gate_string_t* dst, gate_string_t const* src) | |
| 2946 | { | ||
| 2947 | 1 | gate_string_t* ret = gate_string_create_copy(dst, src); | |
| 2948 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (NULL != ret) |
| 2949 | { | ||
| 2950 | 1 | gate_str_to_upper(ret->buffer->data, ret->length); | |
| 2951 | } | ||
| 2952 | 1 | return ret; | |
| 2953 | } | ||
| 2954 | 2 | gate_uint32_t gate_string_hash(gate_string_t const* obj) | |
| 2955 | { | ||
| 2956 | 2 | return gate_str_hash(gate_string_ptr(obj, 0), gate_string_length(obj)); | |
| 2957 | } | ||
| 2958 | |||
| 2959 | |||
| 2960 | |||
| 2961 | |||
| 2962 | 2714 | gate_result_t gate_string_copy_constructor(void* destMem, void const* srcMem) | |
| 2963 | { | ||
| 2964 | 2714 | gate_string_t* dst = (gate_string_t*)destMem; | |
| 2965 | 2714 | gate_string_t const* src = (gate_string_t const*)srcMem; | |
| 2966 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2714 times.
|
2714 | if (NULL == gate_string_clone(dst, src)) |
| 2967 | { | ||
| 2968 | ✗ | return GATE_RESULT_OUTOFMEMORY; | |
| 2969 | } | ||
| 2970 | else | ||
| 2971 | { | ||
| 2972 | 2714 | return GATE_RESULT_OK; | |
| 2973 | } | ||
| 2974 | } | ||
| 2975 | 45 | gate_result_t gate_string_duplicate_constructor(void* destMem, void const* srcMem) | |
| 2976 | { | ||
| 2977 | 45 | gate_string_t* dst = (gate_string_t*)destMem; | |
| 2978 | 45 | gate_string_t const* src = (gate_string_t const*)srcMem; | |
| 2979 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 45 times.
|
45 | if (NULL == gate_string_duplicate(dst, src)) |
| 2980 | { | ||
| 2981 | ✗ | return GATE_RESULT_OUTOFMEMORY; | |
| 2982 | } | ||
| 2983 | else | ||
| 2984 | { | ||
| 2985 | 45 | return GATE_RESULT_OK; | |
| 2986 | } | ||
| 2987 | } | ||
| 2988 | 2759 | void gate_string_destructor(void* dest) | |
| 2989 | { | ||
| 2990 | 2759 | gate_string_t* dst = (gate_string_t*)dest; | |
| 2991 | 2759 | gate_string_release(dst); | |
| 2992 | 2759 | } | |
| 2993 |