GCC Code Coverage Report


Directory: src/gate/
File: src/gate/cxx_strings.cpp
Date: 2026-02-03 22:06:38
Exec Total Coverage
Lines: 499 543 91.9%
Functions: 182 186 97.8%
Branches: 93 208 44.7%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, Stefan Meislinger <sm@opengate.at> |
4 | All rights reserved. |
5 | |
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met:|
8 | |
9 | 1. Redistributions of source code must retain the above copyright notice, |
10 | this list of conditions and the following disclaimer. |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
25 | THE POSSIBILITY OF SUCH DAMAGE. |
26 +----------------------------------------------------------------------------+
27 */
28
29 #include "gate/strings.hpp"
30 #include "gate/exceptions.hpp"
31 #include "gate/memalloc.hpp"
32
33 namespace gate
34 {
35
36 1 int Char::toLower(int chr)
37 {
38 1 return gate_char_lowercase(chr);
39 }
40 1 int Char::toUpper(int chr)
41 {
42 1 return gate_char_uppercase(chr);
43 }
44 2 bool_t Char::isDigit(int chr)
45 {
46 2 return gate_char_is_digit(chr);
47 }
48 2 bool_t Char::isWhitespace(int chr)
49 {
50 2 return gate_char_is_whitespace(chr);
51 }
52
53 1 size_t Char::readUtf8(char_8_t const* utf8text, size_t length, char_32_t& chr32)
54 {
55 1 return gate_char_read_utf8(utf8text, length, &chr32);
56 }
57 1 size_t Char::readUtf16(char_16_t const* utf16text, size_t length, char_32_t& chr32)
58 {
59 1 return gate_char_read_utf16(utf16text, length, &chr32);
60 }
61 1 size_t Char::readUtf32(char_32_t const* utf32text, size_t length, char_32_t& chr32)
62 {
63 1 return gate_char_read_utf32(utf32text, length, &chr32);
64 }
65
66 1 size_t Char::writeUtf8(char_32_t chr32, char_8_t* utf8text, size_t length)
67 {
68 1 return gate_char_write_utf8(chr32, utf8text, length);
69 }
70 1 size_t Char::writeUtf16(char_32_t chr32, char_16_t* utf16text, size_t length)
71 {
72 1 return gate_char_write_utf16(chr32, utf16text, length);
73 }
74 1 size_t Char::writeUtf32(char_32_t chr32, char_32_t* utf32text, size_t length)
75 {
76 1 return gate_char_write_utf32(chr32, utf32text, length);
77 }
78
79
80 size_t const String::npos = GATE_STR_NPOS;
81 size_t const String::NPOS = GATE_STR_NPOS;
82
83 3222 String::String() noexcept
84 {
85 3222 gate_string_create_empty(&this->impl);
86 3222 }
87 982 String::String(char_8_t const* str)
88 {
89
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 982 times.
982 if (NULL == gate_string_create(&this->impl, str, gate_str_length(str)))
90 {
91 GATEXX_RAISE_ERROR(results::OutOfMemory);
92 }
93 982 }
94 214 String::String(char_8_t const* str, size_t len)
95 {
96
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 214 times.
214 if (NULL == gate_string_create(&this->impl, str, len))
97 {
98 GATEXX_RAISE_ERROR(results::OutOfMemory);
99 }
100 214 }
101 554 String::String(String const& src) noexcept
102 {
103 554 gate_string_duplicate(&this->impl, &src.impl);
104 554 }
105 23 String::String(gate_string_t const& src)
106 {
107
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
23 if (NULL == gate_string_clone(&this->impl, &src))
108 {
109 GATEXX_RAISE_ERROR(results::OutOfMemory);
110 }
111 23 }
112 1 String::String(StringBuilder& strbuilder)
113 {
114
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 gate_string_create_empty(&this->impl);
115
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 String tmp = strbuilder.toString();
116 1 this->swap(tmp);
117 1 }
118
119
120
121 17 String& String::operator=(String const& src) noexcept
122 {
123
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 if (this != &src)
124 {
125 34 String that(src);
126 17 this->swap(that);
127 }
128 17 return *this;
129 }
130
131 9986 String::~String() noexcept
132 {
133 4993 gate_string_release(&this->impl);
134 4993 }
135
136 #if defined(GATE_COMPILER_SUPPORTS_CPP_MOVEREFS)
137 1 String::String(String&& src) noexcept
138 {
139 1 gate_string_create_empty(&this->impl);
140 1 gate::swapRefs(this->impl, src.impl);
141 1 }
142
143 419 String& String::operator=(String&& src) noexcept
144 {
145 419 gate::swapRefs(this->impl, src.impl);
146 419 return *this;
147 }
148 #endif
149
150 497 String String::createStatic(char_8_t const* str) noexcept
151 {
152 497 String tmp;
153 497 gate_string_create_static(&tmp.impl, str);
154 497 return tmp;
155 }
156 262 String String::createStatic(char_8_t const* str, size_t len) noexcept
157 {
158 262 String tmp;
159 262 gate_string_create_static_len(&tmp.impl, str, len);
160 262 return tmp;
161 }
162 406 String String::createFrom(gate_string_t& str) noexcept
163 {
164 406 String ret;
165 406 ret.impl = str;
166 406 gate_string_create_empty(&str);
167 406 return ret;
168 }
169 1 String String::createFrom(char_16_t const* utf16, size_t len)
170 {
171 1 String ret;
172
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 if (NULL == gate_string_create_utf16(&ret.impl, utf16, len))
173 {
174 GATEXX_RAISE_ERROR(results::OutOfMemory);
175 }
176 1 return ret;
177 }
178 1 String String::createFrom(char_32_t const* utf32, size_t len)
179 {
180 1 String ret;
181
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 if (NULL == gate_string_create_utf32(&ret.impl, utf32, len))
182 {
183 GATEXX_RAISE_ERROR(results::OutOfMemory);
184 }
185 1 return ret;
186 }
187
188 1 String String::createFilled(size_t count, char_8_t content)
189 {
190
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 StringBuilder builder(count);
191
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 builder.appendChars(count, content);
192
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return builder.toString();
193 }
194
195
196 1 String String::createIntNum(int64_t num)
197 {
198 char buffer[64];
199
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 size_t len = gate_str_print_int64(buffer, sizeof(buffer), num);
200
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return String(buffer, len);
201 }
202 1 String String::createRealNum(real64_t num, int32_t decimalCount)
203 {
204 char buffer[64];
205
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 size_t len = gate_str_print_real(buffer, sizeof(buffer), num, 0, decimalCount, 0);
206
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return String(buffer, len);
207 }
208
209 57 void String::assign(gate_string_t& dst, gate_string_t const& src)
210 {
211 57 gate_string_t tmp = GATE_STRING_INIT_EMPTY;
212
2/4
✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 57 times.
57 if (NULL == gate_string_clone(&tmp, &src))
213 {
214 GATEXX_RAISE_ERROR(results::OutOfMemory);
215 }
216
1/2
✓ Branch 1 taken 57 times.
✗ Branch 2 not taken.
57 gate_string_release(&dst);
217 57 dst = tmp;
218 57 }
219
220 5 void String::assign(gate_string_t& dst, String const& src)
221 {
222 5 String::assign(dst, *src.c_impl());
223 5 }
224
225
226
227 23 void String::swap(String& that) noexcept
228 {
229 23 gate::swapRefsNoExcept(this->impl, that.impl);
230 23 }
231 5 void String::swap(gate_string_t& that) noexcept
232 {
233 5 gate::swapRefsNoExcept(this->impl, that);
234 5 }
235 1 void String::swap(gate_string_t& a, gate_string_t& b) noexcept
236 {
237 1 gate::swapRefsNoExcept(a, b);
238 1 }
239
240
241
242 1235 size_t String::size() const noexcept
243 {
244 1235 return gate_string_length(&this->impl);
245 }
246 3125 size_t String::length() const noexcept
247 {
248 3125 return gate_string_length(&this->impl);
249 }
250 205 bool String::empty() const noexcept
251 {
252 205 return this->size() == 0;
253 }
254 1752 char_8_t const* String::c_str() const noexcept
255 {
256 1752 return gate_string_ptr(&this->impl, 0);
257 }
258
259 1 char_8_t const& String::at(size_t index) const noexcept
260 {
261 1 return *gate_string_ptr(&this->impl, index);
262 }
263
264 1013 gate_string_t const* String::c_impl() const noexcept
265 {
266 1013 return &this->impl;
267 }
268
269 1 String::const_iterator String::cbegin() const noexcept
270 {
271 1 return gate_string_ptr(&this->impl, 0);
272 }
273 4 String::const_iterator String::cend() const noexcept
274 {
275 4 return gate_string_ptr(&this->impl, 0) + gate_string_length(&this->impl);
276 }
277
278 1 String::const_iterator String::begin() const noexcept
279 {
280 1 return this->cbegin();
281 }
282 4 String::const_iterator String::end() const noexcept
283 {
284 4 return this->cend();
285 }
286
287 4 size_t String::positionOf(String const& text, size_t startAt) const noexcept
288 {
289 4 return gate_string_pos(&this->impl, text.c_impl(), startAt);
290 }
291 1 size_t String::positionOf(char const& chr, size_t startAt) const noexcept
292 {
293 1 return gate_string_char_pos(&this->impl, chr, startAt);
294 }
295
296 2 size_t String::positionOfLast(String const& text) const noexcept
297 {
298 2 size_t pos = gate_string_pos_last(&this->impl, text.c_impl());
299 2 return pos;
300 }
301 1 size_t String::positionOfLast(char const& chr) const noexcept
302 {
303 1 return gate_string_char_pos_last(&this->impl, chr);
304 }
305
306
307 1 size_t String::findFirstOf(String const& text, size_t startAt) const noexcept
308 {
309 1 return gate_string_find_first_of(&this->impl, text.c_impl(), startAt);
310 }
311 1 size_t String::findFirstNotOf(String const& text, size_t startAt) const noexcept
312 {
313 1 return gate_string_find_first_not_of(&this->impl, text.c_impl(), startAt);
314 }
315 1 size_t String::findLastOf(String const& text) const noexcept
316 {
317 1 return gate_string_find_last_of(&this->impl, text.c_impl());
318 }
319 1 size_t String::findLastNotOf(String const& text) const noexcept
320 {
321 1 return gate_string_find_last_not_of(&this->impl, text.c_impl());
322 }
323
324 4 String String::substr(size_t offset, size_t len) const
325 {
326 4 String ret;
327
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 gate_string_substr(&ret.impl, &this->impl, offset, len);
328 4 return ret;
329 }
330 2 String String::left(size_t len) const
331 {
332 2 return this->substr(0, len);
333 }
334 2 String String::right(size_t len) const
335 {
336 2 size_t sz = this->size();
337
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (len >= sz)
338 {
339 1 return *this;
340 }
341 else
342 {
343 1 return this->substr(sz - len, len);
344 }
345 }
346 2 String String::readLine(String& tail) const
347 {
348 gate_string_t str_line;
349 gate_string_t str_tail;
350
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
2 if (NULL == gate_string_read_line(&str_line, &this->impl, &str_tail))
351 {
352 GATEXX_RAISE_ERROR(results::OutOfMemory);
353 }
354 2 tail = String::duplicate(str_tail);
355
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 gate_string_release(&str_tail);
356
357 2 String ret = String::duplicate(str_line);
358
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 gate_string_release(&str_line);
359 4 return ret;
360 }
361 1 String String::readLine()
362 {
363 2 String tail;
364
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 String ret = this->readLine(tail);
365 1 this->swap(tail);
366 2 return ret;
367 }
368 1 String String::toLower() const
369 {
370 1 gate_string_t dst = GATE_STRING_INIT_EMPTY;
371
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 if (NULL == gate_string_to_lower(&dst, &this->impl))
372 {
373 GATEXX_RAISE_ERROR(results::OutOfMemory);
374 }
375 2 return String::createFrom(dst);
376 }
377 1 String String::toUpper() const
378 {
379 1 gate_string_t dst = GATE_STRING_INIT_EMPTY;
380
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 if (NULL == gate_string_to_upper(&dst, &this->impl))
381 {
382 GATEXX_RAISE_ERROR(results::OutOfMemory);
383 }
384 2 return String::createFrom(dst);
385 }
386
387
388
389 1 String String::ltrim() const
390 {
391 1 String ret;
392
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 gate_string_ltrim(&ret.impl, &this->impl);
393 1 return ret;
394 }
395 1 String String::rtrim() const
396 {
397 1 String ret;
398
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 gate_string_rtrim(&ret.impl, &this->impl);
399 1 return ret;
400 }
401 1 String String::trim() const
402 {
403 1 String ret;
404
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 gate_string_trim(&ret.impl, &this->impl);
405 1 return ret;
406 }
407
408 9 String String::copy(gate_string_t const& src)
409 {
410 9 String ret;
411
2/4
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
9 if (NULL == gate_string_create_copy(&ret.impl, &src))
412 {
413 GATEXX_RAISE_ERROR(results::OutOfMemory);
414 }
415 9 return ret;
416 }
417 10 String String::clone(gate_string_t const& src)
418 {
419 10 String ret;
420
2/4
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
10 if (NULL == gate_string_clone(&ret.impl, &src))
421 {
422 GATEXX_RAISE_ERROR(results::OutOfMemory);
423 }
424 10 return ret;
425
426 }
427 506 String String::duplicate(gate_string_t const& src) noexcept
428 {
429 506 String ret;
430 506 gate_string_duplicate(&ret.impl, &src);
431 506 return ret;
432 }
433
434
435 2 String String::copy() const
436 {
437 2 return String::copy(this->impl);
438 }
439 9 String String::clone() const
440 {
441 9 return String::clone(this->impl);
442 }
443 1 String String::duplicate() const noexcept
444 {
445 1 return String::duplicate(this->impl);
446 }
447
448
449
450 14 size_t String::copyTo(char_8_t* buffer, size_t capacity) const
451 {
452
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
14 if ((buffer == NULL) || (capacity == 0))
453 {
454 return 0;
455 }
456 14 size_t sz = this->size();
457
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14 times.
14 if (capacity <= sz)
458 {
459 sz = capacity - 1;
460 }
461 14 gate_mem_copy(buffer, this->impl.str, sz);
462 14 buffer[sz] = 0;
463 14 return sz;
464 }
465
466
467 1620 int String::compare(String const& str) const noexcept
468 {
469 1620 return gate_str_compare(this->impl.str, this->impl.length, str.impl.str, str.impl.length);
470 }
471 3 int String::compareIC(String const& str) const noexcept
472 {
473 3 return gate_str_compare_ic(this->impl.str, this->impl.length, str.impl.str, str.impl.length);
474 }
475 1 bool_t String::startsWith(String const& str) const noexcept
476 {
477 1 return gate_string_starts_with(&this->impl, &str.impl);
478 }
479 2 bool_t String::startsWith(char const& chr) const noexcept
480 {
481 2 return gate_string_starts_with_char(&this->impl, chr);
482 }
483 bool_t String::startsWithIC(String const& str) const noexcept
484 {
485 return gate_string_starts_with_ic(&this->impl, str.c_impl());
486 }
487 1 bool_t String::endsWith(String const& str) const noexcept
488 {
489 1 return gate_string_ends_with(&this->impl, &str.impl);
490 }
491 2 bool_t String::endsWith(char const& chr) const noexcept
492 {
493 2 return gate_string_ends_with_char(&this->impl, chr);
494 }
495 bool_t String::endsWithIC(String const& str) const noexcept
496 {
497 return gate_string_ends_with_ic(&this->impl, str.c_impl());
498 }
499 2 bool_t String::equals(String const& str) const noexcept
500 {
501 2 return gate_string_equals(&this->impl, &str.impl);
502 }
503 2 bool_t String::equalsIC(String const& str) const noexcept
504 {
505 2 return gate_string_equals_ic(&this->impl, &str.impl);
506 }
507 3 bool_t String::like(String const& pattern) const noexcept
508 {
509 3 return gate_string_like(&this->impl, &pattern.impl);
510 }
511 1 bool_t String::likeOneOf(String const& pattern, char_8_t separator) const noexcept
512 {
513 1 return gate_string_like_one_of(&this->impl, &pattern.impl, separator);
514 }
515
516 97 size_t String::length(char const* str) noexcept
517 {
518 97 return gate_str_length(str);
519 }
520
521 1 int String::compare(char_8_t const* str) const noexcept
522 {
523 1 return this->compare(String::createStatic(str));
524 }
525 1 int String::compareIC(char_8_t const* str) const noexcept
526 {
527 1 return this->compareIC(String::createStatic(str));
528 }
529 2 bool_t String::startsWith(char_8_t const* str) const noexcept
530 {
531 2 return gate_string_starts_with_str(&this->impl, str);
532 }
533 1 bool_t String::startsWithIC(char_8_t const* str) const noexcept
534 {
535 1 return gate_string_starts_with_str_ic(&this->impl, str);
536 }
537 1 bool_t String::endsWith(char_8_t const* str) const noexcept
538 {
539 1 return gate_string_ends_with_str(&this->impl, str);
540 }
541 1 bool_t String::endsWithIC(char_8_t const* str) const noexcept
542 {
543 1 return gate_string_ends_with_str_ic(&this->impl, str);
544 }
545 1 bool_t String::equals(char_8_t const* str) const noexcept
546 {
547 1 return this->equals(String::createStatic(str));
548 }
549 1 bool_t String::equalsIC(char_8_t const* str) const noexcept
550 {
551 1 return this->equalsIC(String::createStatic(str));
552 }
553
554 1 size_t String::parseNum(uint64_t& num) const noexcept
555 {
556 1 return gate_string_parse_uint(&this->impl, &num);
557 }
558 2 size_t String::parseNum(int64_t& num) const noexcept
559 {
560 2 return gate_string_parse_int(&this->impl, &num);
561 }
562 3 size_t String::parseNum(real64_t& num) const noexcept
563 {
564 3 return gate_string_parse_real(&this->impl, &num);
565 }
566 2 size_t String::parseNum(real32_t& num) const noexcept
567 {
568 2 real64_t r64 = 0.0;
569 2 size_t ret = this->parseNum(r64);
570 2 num = static_cast<real32_t>(r64);
571 2 return ret;
572 }
573
574 1 size_t String::parseHex(uint64_t& num) const noexcept
575 {
576 1 return gate_string_parse_hex(&this->impl, &num);
577 }
578
579 1 String String::parseHex() const noexcept
580 {
581 1 char const* const ptr = this->c_str();
582 1 size_t const len = this->length();
583
584 char buffer[GATE_MAX_COPYBUFFER_LENGTH];
585 1 char* ptr_buffer = &buffer[0];
586 1 size_t bufferlen = sizeof(buffer);
587 gate_size_t bufferused;
588 gate_string_t str;
589 bool_t str_created;
590
591
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (len / 2 >= bufferlen)
592 {
593 bufferlen = len / 2;
594 ptr_buffer = static_cast<char*>(gate_mem_alloc(bufferlen));
595 if (ptr_buffer == NULL)
596 {
597 GATEXX_RAISE_ERROR(results::OutOfMemory);
598 }
599 }
600
601 1 bufferused = gate_str_parse_hex_buffer(ptr, len, ptr_buffer, bufferlen);
602 1 str_created = (NULL != gate_string_create(&str, ptr_buffer, bufferused));
603
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if(ptr_buffer != NULL && (ptr_buffer != &buffer[0]))
604 {
605 gate_mem_dealloc(ptr_buffer);
606 }
607
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!str_created)
608 {
609 GATEXX_RAISE_ERROR(results::OutOfMemory);
610 }
611 1 return String::createFrom(str);
612 }
613
614 uint8_t String::parseOctByte() const noexcept
615 {
616 uint8_t ret = 0;
617 size_t const len = this->length();
618 if (len >= 3)
619 {
620 gate_str_parse_oct_byte(this->c_str(), &ret);
621 }
622 return ret;
623 }
624
625 1 int64_t String::parseInt() const noexcept
626 {
627 1 int64_t ret = 0;
628 1 this->parseNum(ret);
629 1 return ret;
630 }
631
632 1 real64_t String::parseReal() const noexcept
633 {
634 1 real64_t ret = 0.0;
635 1 this->parseNum(ret);
636 1 return ret;
637 }
638
639 8 bool_t String::parseBool() const noexcept
640 {
641 8 gate_bool_t ret = false;
642 8 gate_str_parse_bool(this->c_str(), this->length(), &ret);
643 8 return ret;
644 }
645
646 9 size_t String::printValue(char* buffer, size_t bufferlen, int printType, void const* ptrValue)
647 {
648 gate_print_value_t pv;
649 9 pv.value_type = printType;
650 9 pv.value_ptr = ptrValue;
651
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
18 return gate_str_print_values(buffer, bufferlen, &pv, 1);
652 }
653
654
655
656 2 size_t String::parse(String const& find, size_t startAt, String* ptrHead, String* ptrTail, bool_t separatorAsTail) const
657 {
658 2 gate_string_t head = GATE_STRING_INIT_EMPTY;
659 2 gate_string_t tail = GATE_STRING_INIT_EMPTY;
660
661
1/2
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 size_t ret = gate_string_parse(&this->impl, find.c_impl(), startAt, &head, &tail, separatorAsTail);
662
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (ptrHead)
663 {
664 2 *ptrHead = String::duplicate(head);
665 }
666
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (ptrTail)
667 {
668 2 *ptrTail = String::duplicate(tail);
669 }
670
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 gate_string_release(&tail);
671
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 gate_string_release(&head);
672 2 return ret;
673 }
674
675 1 String String::toHex(char_8_t const* str, size_t len, bool_t upperCase)
676 {
677
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 StringBuilder builder(len * 2);
678
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 builder.appendHex(str, len, upperCase);
679
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return builder.toString();
680 }
681
682 1 String String::toHex() const
683 {
684 1 return String::toHex(this->c_str(), this->length());
685 }
686
687
688
689 450 bool_t String::operator==(String const& src) const noexcept
690 {
691 450 return this->compare(src) == 0;
692 }
693 3 bool_t String::operator!=(String const& src) const noexcept
694 {
695 3 return this->compare(src) != 0;
696 }
697 1163 bool_t String::operator< (String const& src) const noexcept
698 {
699 1163 return this->compare(src) < 0;
700 }
701 1 bool_t String::operator> (String const& src) const noexcept
702 {
703 1 return this->compare(src) > 0;
704 }
705 1 bool_t String::operator<=(String const& src) const noexcept
706 {
707 1 return this->compare(src) <= 0;
708 }
709 1 bool_t String::operator>=(String const& src) const noexcept
710 {
711 1 return this->compare(src) >= 0;
712 }
713 1 bool_t String::operator!() const noexcept
714 {
715 1 return this->empty();
716 }
717 1 gate_string_t const& String::operator*() const noexcept
718 {
719 1 return this->impl;
720 }
721
722 2 char_8_t const& String::operator[](size_t index) const noexcept
723 {
724 static char_8_t empty_char_bytes[2] = GATE_INIT_EMPTY;
725 2 char_8_t const* ptr = gate_string_ptr(&this->impl, index);
726
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (ptr)
727 {
728 2 return *ptr;
729 }
730 else
731 {
732 return empty_char_bytes[0];
733 }
734 }
735
736 2 String operator+(String const& str1, String const& str2)
737 {
738
1/2
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
4 StringBuilder builder(str1.length() + str2.length() + 2);
739
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 builder << str1 << str2;
740
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return builder.toString();
741 }
742 19 String operator+(String const& str1, char const* str2)
743 {
744
1/2
✓ Branch 3 taken 19 times.
✗ Branch 4 not taken.
38 StringBuilder builder(str1.length() + String::length(str2));
745
2/4
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
19 builder << str1 << str2;
746
1/2
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
38 return builder.toString();
747 }
748 76 String operator+(char const* str1, String const& str2)
749 {
750
1/2
✓ Branch 3 taken 76 times.
✗ Branch 4 not taken.
152 StringBuilder builder(String::length(str1) + str2.length() + 2);
751
2/4
✓ Branch 1 taken 76 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 76 times.
✗ Branch 5 not taken.
76 builder << str1 << str2;
752
1/2
✓ Branch 1 taken 76 times.
✗ Branch 2 not taken.
152 return builder.toString();
753 }
754
755
756
757 102 StaticString::StaticString(char_8_t const* str) noexcept
758 102 : String(String::createStatic(str))
759 {
760 102 }
761 3 StaticString::StaticString(char_8_t const* str, size_t len) noexcept
762 3 : String(String::createStatic(str, len))
763 {
764 3 }
765 1 StaticString::StaticString(StaticString const& src) noexcept
766 1 : String(src)
767 {
768 1 }
769 1 StaticString& StaticString::operator=(StaticString const& src) noexcept
770 {
771 1 String::operator=(src);
772 1 return *this;
773 }
774 285 StaticString::~StaticString() noexcept
775 {
776 285 }
777
778
779
780
781
782 123 StringBuilder::StringBuilder(size_t capacity)
783 {
784 123 gate_strbuilder_create(&this->impl, capacity);
785 123 }
786 1 StringBuilder::StringBuilder(char* staticBuffer, size_t capacity, size_t alreadyUsed)
787 {
788 1 gate_strbuilder_create_static(&this->impl, staticBuffer, capacity, alreadyUsed);
789 1 }
790
791 248 StringBuilder::~StringBuilder()
792 {
793 124 gate_strbuilder_release(&this->impl);
794 124 }
795
796 1 gate_strbuilder_t const* StringBuilder::c_impl() const noexcept
797 {
798 1 return &this->impl;
799 }
800 16 gate_strbuilder_t* StringBuilder::c_impl() noexcept
801 {
802 16 return &this->impl;
803 }
804
805 1 char_8_t const* StringBuilder::ptr(size_t pos) const noexcept
806 {
807 1 return gate_strbuilder_ptr(&this->impl, pos);
808 }
809 6 size_t StringBuilder::length() const noexcept
810 {
811 6 return gate_strbuilder_length(&this->impl);
812 }
813
814 1 size_t StringBuilder::resize(size_t sz)
815 {
816 1 return gate_strbuilder_resize(&this->impl, sz);
817 }
818 223 size_t StringBuilder::append(String const& text)
819 {
820 223 return gate_strbuilder_append_text(&this->impl, text.c_str(), text.length());
821 }
822 12 size_t StringBuilder::append(char const* ptr, size_t len)
823 {
824 12 return gate_strbuilder_append_text(&this->impl, ptr, len);
825 }
826 1 size_t StringBuilder::append(char_16_t const* text16, size_t textlen)
827 {
828 1 return gate_strbuilder_append_text16(&this->impl, text16, textlen);
829 }
830 1 size_t StringBuilder::append(char_32_t const* text32, size_t textlen)
831 {
832 1 return gate_strbuilder_append_text32(&this->impl, text32, textlen);
833 }
834
835 1 size_t StringBuilder::append(int16_t const& num)
836 {
837 1 return gate_strbuilder_append_int16(&this->impl, num);
838 }
839 1 size_t StringBuilder::append(uint16_t const& num)
840 {
841 1 return gate_strbuilder_append_uint16(&this->impl, num);
842 }
843 2 size_t StringBuilder::append(int32_t const& num)
844 {
845 2 return gate_strbuilder_append_int32(&this->impl, num);
846 }
847 3 size_t StringBuilder::append(uint32_t const& num)
848 {
849 3 return gate_strbuilder_append_uint32(&this->impl, num);
850 }
851 1 size_t StringBuilder::append(int64_t const& num)
852 {
853 1 return gate_strbuilder_append_int64(&this->impl, num);
854 }
855 3 size_t StringBuilder::append(uint64_t const& num)
856 {
857 3 return gate_strbuilder_append_uint64(&this->impl, num);
858 }
859 1 size_t StringBuilder::append(real64_t const& num, unsigned intlen, unsigned decimallen, unsigned grouplen)
860 {
861 1 return gate_strbuilder_append_real(&this->impl, num, intlen, decimallen, grouplen);
862 }
863 4 size_t StringBuilder::appendHex(uint8_t const& num, bool_t upperCase)
864 {
865 char buffer[8];
866
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 size_t buffer_used = gate_str_print_hex_byte(buffer, sizeof(buffer), num, upperCase);
867
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
8 return this->append(buffer, buffer_used);
868 }
869 2 size_t StringBuilder::appendHex(uint16_t const& num, bool_t upperCase)
870 {
871 char buffer[8];
872
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 size_t buffer_used = gate_str_print_hex_num(buffer, sizeof(buffer), num, upperCase);
873
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return this->append(buffer, buffer_used);
874 }
875 2 size_t StringBuilder::appendHex(uint32_t const& num, bool_t upperCase)
876 {
877 char buffer[16];
878
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 size_t buffer_used = gate_str_print_hex_num(buffer, sizeof(buffer), num, upperCase);
879
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return this->append(buffer, buffer_used);
880 }
881 1 size_t StringBuilder::appendHex(uint64_t const& num, bool_t upperCase)
882 {
883 char buffer[24];
884
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 size_t buffer_used = gate_str_print_hex_num(buffer, sizeof(buffer), num, upperCase);
885
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return this->append(buffer, buffer_used);
886 }
887 1 size_t StringBuilder::appendHex(char const* txt, size_t len, bool_t upperCase)
888 {
889 1 size_t ret = 0;
890
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 while (len-- > 0)
891 {
892
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 ret += this->appendHex(static_cast<uint8_t>(*txt), upperCase);
893 4 ++txt;
894 }
895 1 return ret;
896 }
897
898
899 1 size_t StringBuilder::appendChars(size_t count, gate_char8_t chr)
900 {
901 1 return gate_strbuilder_append_chars(&this->impl, count, chr);
902 }
903
904 5 size_t StringBuilder::appendNewLine()
905 {
906 5 return this->append(strings::NewLine);
907 }
908 2 size_t StringBuilder::discard(size_t charCount)
909 {
910 2 return gate_strbuilder_discard(&this->impl, charCount);
911 }
912 1 size_t StringBuilder::discardBack(size_t charCount)
913 {
914 1 return gate_strbuilder_discard_back(&this->impl, charCount);
915 }
916 123 String StringBuilder::toString()
917 {
918 gate_string_t tmp;
919
2/4
✓ Branch 1 taken 123 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 123 times.
123 if (NULL == gate_strbuilder_to_string(&this->impl, &tmp))
920 {
921 GATEXX_RAISE_ERROR(results::OutOfMemory);
922 }
923 123 String ret = String::duplicate(tmp);
924
1/2
✓ Branch 1 taken 123 times.
✗ Branch 2 not taken.
123 gate_string_release(&tmp);
925 246 return ret;
926 }
927 4 String StringBuilder::copyString() const
928 {
929 gate_string_t tmp;
930
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
4 if (NULL == gate_strbuilder_copy_to_string(&this->impl, &tmp))
931 {
932 GATEXX_RAISE_ERROR(results::OutOfMemory);
933 }
934 4 String ret = String::duplicate(tmp);
935
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 gate_string_release(&tmp);
936 8 return ret;
937 }
938
939 4 String StringBuilder::getView() const
940 {
941 return String::createStatic(
942 gate_strbuilder_ptr(&this->impl, 0),
943 gate_strbuilder_length(&this->impl)
944 4 );
945 }
946
947 1 size_t StringBuilder::insert(size_t pos, String const& text)
948 {
949 1 return gate_strbuilder_insert(&this->impl, pos, text.c_str(), text.length());
950 }
951
952
953 1 size_t StringBuilder::positionOf(String const& token, size_t startAt)
954 {
955 1 return gate_strbuilder_pos(&this->impl, token.c_impl(), startAt);
956 }
957
958 1 size_t StringBuilder::positionOf(char const& chr, size_t startAt)
959 {
960 1 return gate_strbuilder_char_pos(&this->impl, chr, startAt);
961 }
962
963 1 size_t StringBuilder::replace(String const& find, String const& replaceWith, size_t startAt, size_t maxReplace)
964 {
965 1 return gate_strbuilder_replace_string(&this->impl, find.c_impl(), replaceWith.c_impl(), startAt, maxReplace);
966 }
967
968 1 size_t StringBuilder::remove(size_t fromPos, size_t length)
969 {
970 1 return gate_strbuilder_remove(&this->impl, fromPos, length);
971 }
972
973 105 StringBuilder& StringBuilder::operator<<(String const& text) { this->append(text); return *this; }
974
1/2
✓ Branch 2 taken 105 times.
✗ Branch 3 not taken.
105 StringBuilder& StringBuilder::operator<<(char const* text) { this->append(String(text)); return *this; }
975 1 StringBuilder& StringBuilder::operator<<(int16_t const& num) { this->append(num); return *this; }
976 1 StringBuilder& StringBuilder::operator<<(uint16_t const& num) { this->append(num); return *this; }
977 2 StringBuilder& StringBuilder::operator<<(int32_t const& num) { this->append(num); return *this; }
978 3 StringBuilder& StringBuilder::operator<<(uint32_t const& num) { this->append(num); return *this; }
979 1 StringBuilder& StringBuilder::operator<<(int64_t const& num) { this->append(num); return *this; }
980 3 StringBuilder& StringBuilder::operator<<(uint64_t const& num) { this->append(num); return *this; }
981 1 StringBuilder& StringBuilder::operator<<(real64_t const& num) { this->append(num); return *this; }
982
983 static char_8_t const empty_dummy = '\0';
984 char_8_t const& StringBuilder::operator[](size_t index) const
985 {
986 gate_char8_t const* ptr = gate_strbuilder_ptr(&this->impl, index);
987 if (ptr == NULL)
988 {
989 return empty_dummy;
990 }
991 else
992 {
993 return *ptr;
994 }
995 }
996 1 char_8_t& StringBuilder::operator[](size_t index)
997 {
998 1 gate_char8_t const* ptr = gate_strbuilder_ptr(&this->impl, index);
999 1 return *const_cast<gate_char8_t*>(ptr);
1000 }
1001
1002
1003 1 CstrBuffer::CstrBuffer(char const* ptr, size_t length, bool_t copyNeeded)
1004 {
1005
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (NULL == gate_cstrbuffer_create(&this->impl, ptr, length, copyNeeded))
1006 {
1007 GATEXX_RAISE_ERROR(results::OutOfMemory);
1008 }
1009 1 }
1010 1 CstrBuffer::CstrBuffer(wchar_t const* ptr, gate_size_t length)
1011 {
1012
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (NULL == gate_cstrbuffer_create_wstr(&this->impl, ptr, length))
1013 {
1014 GATEXX_RAISE_ERROR(results::OutOfMemory);
1015 }
1016 1 }
1017
1018 1 CstrBuffer::CstrBuffer(String const& str, bool_t copyNeeded)
1019 {
1020
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (NULL == gate_cstrbuffer_create_string(&this->impl, str.c_impl(), copyNeeded))
1021 {
1022 GATEXX_RAISE_ERROR(results::OutOfMemory);
1023 }
1024 1 }
1025 6 CstrBuffer::~CstrBuffer() noexcept
1026 {
1027 3 gate_cstrbuffer_destroy(&this->impl);
1028 3 }
1029 1 gate_cstrbuffer_t const* CstrBuffer::c_impl() const
1030 {
1031 1 return &this->impl;
1032 }
1033 2 char const* CstrBuffer::get() const
1034 {
1035 2 return gate_cstrbuffer_get(&this->impl);
1036 }
1037 1 size_t CstrBuffer::length() const
1038 {
1039 1 return gate_cstrbuffer_length(&this->impl);
1040 }
1041
1042
1043 1 CstrBuffer16::CstrBuffer16(char_16_t const* ptr, size_t length, bool_t copyNeeded)
1044 {
1045
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (NULL == gate_cstrbuffer16_create(&this->impl, ptr, length, copyNeeded))
1046 {
1047 GATEXX_RAISE_ERROR(results::OutOfMemory);
1048 }
1049 1 }
1050 1 CstrBuffer16::CstrBuffer16(String const& str)
1051 {
1052
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (NULL == gate_cstrbuffer16_create_string(&this->impl, str.c_impl()))
1053 {
1054 GATEXX_RAISE_ERROR(results::OutOfMemory);
1055 }
1056 1 }
1057 4 CstrBuffer16::~CstrBuffer16() noexcept
1058 {
1059 2 gate_cstrbuffer16_destroy(&this->impl);
1060 2 }
1061 1 gate_cstrbuffer16_t const* CstrBuffer16::c_impl() const
1062 {
1063 1 return &this->impl;
1064 }
1065 1 char_16_t const* CstrBuffer16::get() const
1066 {
1067 1 return gate_cstrbuffer16_get(&this->impl);
1068 }
1069 1 size_t CstrBuffer16::length() const
1070 {
1071 1 return gate_cstrbuffer16_length(&this->impl);
1072 }
1073
1074
1075 1 CstrBuffer32::CstrBuffer32(char_32_t const* ptr, size_t length, bool_t copyNeeded)
1076 {
1077
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (NULL == gate_cstrbuffer32_create(&this->impl, ptr, length, copyNeeded))
1078 {
1079 GATEXX_RAISE_ERROR(results::OutOfMemory);
1080 }
1081 1 }
1082 1 CstrBuffer32::CstrBuffer32(String const& str)
1083 {
1084
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (NULL == gate_cstrbuffer32_create_string(&this->impl, str.c_impl()))
1085 {
1086 GATEXX_RAISE_ERROR(results::OutOfMemory);
1087 }
1088 1 }
1089 4 CstrBuffer32::~CstrBuffer32() noexcept
1090 {
1091 2 gate_cstrbuffer32_destroy(&this->impl);
1092 2 }
1093 1 gate_cstrbuffer32_t const* CstrBuffer32::c_impl() const
1094 {
1095 1 return &this->impl;
1096 }
1097 1 char_32_t const* CstrBuffer32::get() const
1098 {
1099 1 return gate_cstrbuffer32_get(&this->impl);
1100 }
1101 1 size_t CstrBuffer32::length() const
1102 {
1103 1 return gate_cstrbuffer32_length(&this->impl);
1104 }
1105
1106
1107
1108
1109 namespace strings
1110 {
1111 String const NewLine = String::createStatic(GATE_STR_NEWLINE, GATE_STR_NEWLINE_LENGTH);
1112 String const Cr = String::createStatic(GATE_STR_CR, 1);
1113 String const Lf = String::createStatic(GATE_STR_LF, 1);
1114 String const CrLf = String::createStatic(GATE_STR_CRLF, 2);
1115 String const Empty = String();
1116
1117 } // end of namespace strings
1118
1119
1120
1121
1122 } // end of namespace gate
1123