GCC Code Coverage Report


Directory: src/gate/
File: src/gate/cxx_strings.cpp
Date: 2026-05-04 21:11:01
Exec Total Coverage
Lines: 500 556 89.9%
Functions: 182 188 96.8%
Branches: 95 224 42.4%

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 3297 String::String() noexcept
84 {
85 3297 gate_string_create_empty(&this->impl);
86 3297 }
87 1036 String::String(char_8_t const* str)
88 {
89
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1036 times.
1036 if (NULL == gate_string_create(&this->impl, str, gate_str_length(str)))
90 {
91 GATEXX_RAISE_ERROR(results::OutOfMemory);
92 }
93 1036 }
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 577 String::String(String const& src) noexcept
102 {
103 577 gate_string_duplicate(&this->impl, &src.impl);
104 577 }
105 25 String::String(gate_string_t const& src)
106 {
107
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 25 times.
25 if (NULL == gate_string_clone(&this->impl, &src))
108 {
109 GATEXX_RAISE_ERROR(results::OutOfMemory);
110 }
111 25 }
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 10294 String::~String() noexcept
132 {
133 5147 gate_string_release(&this->impl);
134 5147 }
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 474 String& String::operator=(String&& src) noexcept
144 {
145 474 gate::swapRefs(this->impl, src.impl);
146 474 return *this;
147 }
148 #endif
149
150 490 String String::createStatic(char_8_t const* str) noexcept
151 {
152 490 String tmp;
153 490 gate_string_create_static(&tmp.impl, str);
154 490 return tmp;
155 }
156 274 String String::createStatic(char_8_t const* str, size_t len) noexcept
157 {
158 274 String tmp;
159 274 gate_string_create_static_len(&tmp.impl, str, len);
160 274 return tmp;
161 }
162 416 String String::createFrom(gate_string_t& str) noexcept
163 {
164 416 String ret;
165 416 ret.impl = str;
166 416 gate_string_create_empty(&str);
167 416 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 1244 size_t String::size() const noexcept
243 {
244 1244 return gate_string_length(&this->impl);
245 }
246 3133 size_t String::length() const noexcept
247 {
248 3133 return gate_string_length(&this->impl);
249 }
250 211 bool String::empty() const noexcept
251 {
252 211 return this->size() == 0;
253 }
254 1791 char_8_t const* String::c_str() const noexcept
255 {
256 1791 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 1030 gate_string_t const* String::c_impl() const noexcept
265 {
266 1030 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 bool_t String::readToken(String& token, String& tail) const
369 {
370 gate_string_t strToken;
371 gate_string_t strTail;
372 if (NULL == gate_string_read_token(&strToken, &this->impl, &strTail))
373 {
374 return false;
375 }
376 else
377 {
378 token = String::createFrom(strToken);
379 tail = String::createFrom(strTail);
380 return true;
381 }
382 }
383
384 String String::readToken()
385 {
386 gate_string_t strToken;
387 gate_string_t strTail;
388 if (NULL == gate_string_read_token(&strToken, &this->impl, &strTail))
389 {
390 GATEXX_RAISE_EXCEPTION(results::OutOfMemory, NULL, 0);
391 }
392 gate_string_release(&this->impl);
393 gate_mem_copy(&this->impl, &strTail, sizeof(gate_string_t));
394 return String::createFrom(strToken);
395 }
396
397
398 1 String String::toLower() const
399 {
400 1 gate_string_t dst = GATE_STRING_INIT_EMPTY;
401
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))
402 {
403 GATEXX_RAISE_ERROR(results::OutOfMemory);
404 }
405 2 return String::createFrom(dst);
406 }
407 1 String String::toUpper() const
408 {
409 1 gate_string_t dst = GATE_STRING_INIT_EMPTY;
410
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))
411 {
412 GATEXX_RAISE_ERROR(results::OutOfMemory);
413 }
414 2 return String::createFrom(dst);
415 }
416
417
418
419 1 String String::ltrim() const
420 {
421 1 String ret;
422
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 gate_string_ltrim(&ret.impl, &this->impl);
423 1 return ret;
424 }
425 1 String String::rtrim() const
426 {
427 1 String ret;
428
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 gate_string_rtrim(&ret.impl, &this->impl);
429 1 return ret;
430 }
431 1 String String::trim() const
432 {
433 1 String ret;
434
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 gate_string_trim(&ret.impl, &this->impl);
435 1 return ret;
436 }
437
438 9 String String::copy(gate_string_t const& src)
439 {
440 9 String ret;
441
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))
442 {
443 GATEXX_RAISE_ERROR(results::OutOfMemory);
444 }
445 9 return ret;
446 }
447 10 String String::clone(gate_string_t const& src)
448 {
449 10 String ret;
450
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))
451 {
452 GATEXX_RAISE_ERROR(results::OutOfMemory);
453 }
454 10 return ret;
455
456 }
457 507 String String::duplicate(gate_string_t const& src) noexcept
458 {
459 507 String ret;
460 507 gate_string_duplicate(&ret.impl, &src);
461 507 return ret;
462 }
463
464
465 2 String String::copy() const
466 {
467 2 return String::copy(this->impl);
468 }
469 9 String String::clone() const
470 {
471 9 return String::clone(this->impl);
472 }
473 1 String String::duplicate() const noexcept
474 {
475 1 return String::duplicate(this->impl);
476 }
477
478
479
480 17 size_t String::copyTo(char_8_t* buffer, size_t capacity) const
481 {
482
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 17 times.
17 if ((buffer == NULL) || (capacity == 0))
483 {
484 return 0;
485 }
486 17 size_t sz = this->size();
487
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (capacity <= sz)
488 {
489 sz = capacity - 1;
490 }
491
492
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 3 times.
17 if (sz > 0)
493 {
494 14 gate_mem_copy(buffer, this->impl.str, sz);
495 }
496 17 buffer[sz] = 0;
497 17 return sz;
498 }
499
500
501 1672 int String::compare(String const& str) const noexcept
502 {
503 1672 return gate_str_compare(this->impl.str, this->impl.length, str.impl.str, str.impl.length);
504 }
505 3 int String::compareIC(String const& str) const noexcept
506 {
507 3 return gate_str_compare_ic(this->impl.str, this->impl.length, str.impl.str, str.impl.length);
508 }
509 1 bool_t String::startsWith(String const& str) const noexcept
510 {
511 1 return gate_string_starts_with(&this->impl, &str.impl);
512 }
513 2 bool_t String::startsWith(char const& chr) const noexcept
514 {
515 2 return gate_string_starts_with_char(&this->impl, chr);
516 }
517 bool_t String::startsWithIC(String const& str) const noexcept
518 {
519 return gate_string_starts_with_ic(&this->impl, str.c_impl());
520 }
521 1 bool_t String::endsWith(String const& str) const noexcept
522 {
523 1 return gate_string_ends_with(&this->impl, &str.impl);
524 }
525 2 bool_t String::endsWith(char const& chr) const noexcept
526 {
527 2 return gate_string_ends_with_char(&this->impl, chr);
528 }
529 bool_t String::endsWithIC(String const& str) const noexcept
530 {
531 return gate_string_ends_with_ic(&this->impl, str.c_impl());
532 }
533 2 bool_t String::equals(String const& str) const noexcept
534 {
535 2 return gate_string_equals(&this->impl, &str.impl);
536 }
537 2 bool_t String::equalsIC(String const& str) const noexcept
538 {
539 2 return gate_string_equals_ic(&this->impl, &str.impl);
540 }
541 3 bool_t String::like(String const& pattern) const noexcept
542 {
543 3 return gate_string_like(&this->impl, &pattern.impl);
544 }
545 1 bool_t String::likeOneOf(String const& pattern, char_8_t separator) const noexcept
546 {
547 1 return gate_string_like_one_of(&this->impl, &pattern.impl, separator);
548 }
549
550 98 size_t String::length(char const* str) noexcept
551 {
552 98 return gate_str_length(str);
553 }
554
555 1 int String::compare(char_8_t const* str) const noexcept
556 {
557 1 return this->compare(String::createStatic(str));
558 }
559 1 int String::compareIC(char_8_t const* str) const noexcept
560 {
561 1 return this->compareIC(String::createStatic(str));
562 }
563 2 bool_t String::startsWith(char_8_t const* str) const noexcept
564 {
565 2 return gate_string_starts_with_str(&this->impl, str);
566 }
567 1 bool_t String::startsWithIC(char_8_t const* str) const noexcept
568 {
569 1 return gate_string_starts_with_str_ic(&this->impl, str);
570 }
571 1 bool_t String::endsWith(char_8_t const* str) const noexcept
572 {
573 1 return gate_string_ends_with_str(&this->impl, str);
574 }
575 1 bool_t String::endsWithIC(char_8_t const* str) const noexcept
576 {
577 1 return gate_string_ends_with_str_ic(&this->impl, str);
578 }
579 1 bool_t String::equals(char_8_t const* str) const noexcept
580 {
581 1 return this->equals(String::createStatic(str));
582 }
583 1 bool_t String::equalsIC(char_8_t const* str) const noexcept
584 {
585 1 return this->equalsIC(String::createStatic(str));
586 }
587
588 1 size_t String::parseNum(uint64_t& num) const noexcept
589 {
590 1 return gate_string_parse_uint(&this->impl, &num);
591 }
592 2 size_t String::parseNum(int64_t& num) const noexcept
593 {
594 2 return gate_string_parse_int(&this->impl, &num);
595 }
596 3 size_t String::parseNum(real64_t& num) const noexcept
597 {
598 3 return gate_string_parse_real(&this->impl, &num);
599 }
600 2 size_t String::parseNum(real32_t& num) const noexcept
601 {
602 2 real64_t r64 = 0.0;
603 2 size_t ret = this->parseNum(r64);
604 2 num = static_cast<real32_t>(r64);
605 2 return ret;
606 }
607
608 1 size_t String::parseHex(uint64_t& num) const noexcept
609 {
610 1 return gate_string_parse_hex(&this->impl, &num);
611 }
612
613 1 String String::parseHex() const noexcept
614 {
615 1 char const* const ptr = this->c_str();
616 1 size_t const len = this->length();
617
618 char buffer[GATE_MAX_COPYBUFFER_LENGTH];
619 1 char* ptr_buffer = &buffer[0];
620 1 size_t bufferlen = sizeof(buffer);
621 gate_size_t bufferused;
622 gate_string_t str;
623 bool_t str_created;
624
625
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (len / 2 >= bufferlen)
626 {
627 bufferlen = len / 2;
628 ptr_buffer = static_cast<char*>(gate_mem_alloc(bufferlen));
629 if (ptr_buffer == NULL)
630 {
631 GATEXX_RAISE_ERROR(results::OutOfMemory);
632 }
633 }
634
635 1 bufferused = gate_str_parse_hex_buffer(ptr, len, ptr_buffer, bufferlen);
636 1 str_created = (NULL != gate_string_create(&str, ptr_buffer, bufferused));
637
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]))
638 {
639 gate_mem_dealloc(ptr_buffer);
640 }
641
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!str_created)
642 {
643 GATEXX_RAISE_ERROR(results::OutOfMemory);
644 }
645 1 return String::createFrom(str);
646 }
647
648 uint8_t String::parseOctByte() const noexcept
649 {
650 uint8_t ret = 0;
651 size_t const len = this->length();
652 if (len >= 3)
653 {
654 gate_str_parse_oct_byte(this->c_str(), &ret);
655 }
656 return ret;
657 }
658
659 1 int64_t String::parseInt() const noexcept
660 {
661 1 int64_t ret = 0;
662 1 this->parseNum(ret);
663 1 return ret;
664 }
665
666 1 real64_t String::parseReal() const noexcept
667 {
668 1 real64_t ret = 0.0;
669 1 this->parseNum(ret);
670 1 return ret;
671 }
672
673 8 bool_t String::parseBool() const noexcept
674 {
675 8 gate_bool_t ret = false;
676 8 gate_str_parse_bool(this->c_str(), this->length(), &ret);
677 8 return ret;
678 }
679
680 9 size_t String::printValue(char* buffer, size_t bufferlen, int printType, void const* ptrValue)
681 {
682 gate_print_value_t pv;
683 9 pv.value_type = printType;
684 9 pv.value_ptr = ptrValue;
685
1/2
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
18 return gate_str_print_values(buffer, bufferlen, &pv, 1);
686 }
687
688
689
690 2 size_t String::parse(String const& find, size_t startAt, String* ptrHead, String* ptrTail, bool_t separatorAsTail) const
691 {
692 2 gate_string_t head = GATE_STRING_INIT_EMPTY;
693 2 gate_string_t tail = GATE_STRING_INIT_EMPTY;
694
695
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);
696
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (ptrHead)
697 {
698 2 *ptrHead = String::duplicate(head);
699 }
700
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (ptrTail)
701 {
702 2 *ptrTail = String::duplicate(tail);
703 }
704
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 gate_string_release(&tail);
705
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 gate_string_release(&head);
706 2 return ret;
707 }
708
709 1 String String::toHex(char_8_t const* str, size_t len, bool_t upperCase)
710 {
711
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 StringBuilder builder(len * 2);
712
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 builder.appendHex(str, len, upperCase);
713
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return builder.toString();
714 }
715
716 1 String String::toHex() const
717 {
718 1 return String::toHex(this->c_str(), this->length());
719 }
720
721
722
723 454 bool_t String::operator==(String const& src) const noexcept
724 {
725 454 return this->compare(src) == 0;
726 }
727 3 bool_t String::operator!=(String const& src) const noexcept
728 {
729 3 return this->compare(src) != 0;
730 }
731 1211 bool_t String::operator< (String const& src) const noexcept
732 {
733 1211 return this->compare(src) < 0;
734 }
735 1 bool_t String::operator> (String const& src) const noexcept
736 {
737 1 return this->compare(src) > 0;
738 }
739 1 bool_t String::operator<=(String const& src) const noexcept
740 {
741 1 return this->compare(src) <= 0;
742 }
743 1 bool_t String::operator>=(String const& src) const noexcept
744 {
745 1 return this->compare(src) >= 0;
746 }
747 3 bool_t String::operator!() const noexcept
748 {
749 3 return this->empty();
750 }
751 1 gate_string_t const& String::operator*() const noexcept
752 {
753 1 return this->impl;
754 }
755
756 2 char_8_t const& String::operator[](size_t index) const noexcept
757 {
758 static char_8_t empty_char_bytes[2] = GATE_INIT_EMPTY;
759 2 char_8_t const* ptr = gate_string_ptr(&this->impl, index);
760
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (ptr)
761 {
762 2 return *ptr;
763 }
764 else
765 {
766 return empty_char_bytes[0];
767 }
768 }
769
770 2 String operator+(String const& str1, String const& str2)
771 {
772
1/2
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
4 StringBuilder builder(str1.length() + str2.length() + 2);
773
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 builder << str1 << str2;
774
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return builder.toString();
775 }
776 20 String operator+(String const& str1, char const* str2)
777 {
778
1/2
✓ Branch 3 taken 20 times.
✗ Branch 4 not taken.
40 StringBuilder builder(str1.length() + String::length(str2));
779
2/4
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 20 times.
✗ Branch 5 not taken.
20 builder << str1 << str2;
780
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
40 return builder.toString();
781 }
782 76 String operator+(char const* str1, String const& str2)
783 {
784
1/2
✓ Branch 3 taken 76 times.
✗ Branch 4 not taken.
152 StringBuilder builder(String::length(str1) + str2.length() + 2);
785
2/4
✓ Branch 1 taken 76 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 76 times.
✗ Branch 5 not taken.
76 builder << str1 << str2;
786
1/2
✓ Branch 1 taken 76 times.
✗ Branch 2 not taken.
152 return builder.toString();
787 }
788
789
790
791 102 StaticString::StaticString(char_8_t const* str) noexcept
792 102 : String(String::createStatic(str))
793 {
794 102 }
795 4 StaticString::StaticString(char_8_t const* str, size_t len) noexcept
796 4 : String(String::createStatic(str, len))
797 {
798 4 }
799 1 StaticString::StaticString(StaticString const& src) noexcept
800 1 : String(src)
801 {
802 1 }
803 1 StaticString& StaticString::operator=(StaticString const& src) noexcept
804 {
805 1 String::operator=(src);
806 1 return *this;
807 }
808 291 StaticString::~StaticString() noexcept
809 {
810 291 }
811
812
813
814
815
816 124 StringBuilder::StringBuilder(size_t capacity)
817 {
818 124 gate_strbuilder_create(&this->impl, capacity);
819 124 }
820 1 StringBuilder::StringBuilder(char* staticBuffer, size_t capacity, size_t alreadyUsed)
821 {
822 1 gate_strbuilder_create_static(&this->impl, staticBuffer, capacity, alreadyUsed);
823 1 }
824
825 250 StringBuilder::~StringBuilder()
826 {
827 125 gate_strbuilder_release(&this->impl);
828 125 }
829
830 1 gate_strbuilder_t const* StringBuilder::c_impl() const noexcept
831 {
832 1 return &this->impl;
833 }
834 16 gate_strbuilder_t* StringBuilder::c_impl() noexcept
835 {
836 16 return &this->impl;
837 }
838
839 1 char_8_t const* StringBuilder::ptr(size_t pos) const noexcept
840 {
841 1 return gate_strbuilder_ptr(&this->impl, pos);
842 }
843 6 size_t StringBuilder::length() const noexcept
844 {
845 6 return gate_strbuilder_length(&this->impl);
846 }
847
848 1 size_t StringBuilder::resize(size_t sz)
849 {
850 1 return gate_strbuilder_resize(&this->impl, sz);
851 }
852 225 size_t StringBuilder::append(String const& text)
853 {
854 225 return gate_strbuilder_append_text(&this->impl, text.c_str(), text.length());
855 }
856 12 size_t StringBuilder::append(char const* ptr, size_t len)
857 {
858 12 return gate_strbuilder_append_text(&this->impl, ptr, len);
859 }
860 1 size_t StringBuilder::append(char_16_t const* text16, size_t textlen)
861 {
862 1 return gate_strbuilder_append_text16(&this->impl, text16, textlen);
863 }
864 1 size_t StringBuilder::append(char_32_t const* text32, size_t textlen)
865 {
866 1 return gate_strbuilder_append_text32(&this->impl, text32, textlen);
867 }
868
869 1 size_t StringBuilder::append(int16_t const& num)
870 {
871 1 return gate_strbuilder_append_int16(&this->impl, num);
872 }
873 1 size_t StringBuilder::append(uint16_t const& num)
874 {
875 1 return gate_strbuilder_append_uint16(&this->impl, num);
876 }
877 2 size_t StringBuilder::append(int32_t const& num)
878 {
879 2 return gate_strbuilder_append_int32(&this->impl, num);
880 }
881 3 size_t StringBuilder::append(uint32_t const& num)
882 {
883 3 return gate_strbuilder_append_uint32(&this->impl, num);
884 }
885 1 size_t StringBuilder::append(int64_t const& num)
886 {
887 1 return gate_strbuilder_append_int64(&this->impl, num);
888 }
889 3 size_t StringBuilder::append(uint64_t const& num)
890 {
891 3 return gate_strbuilder_append_uint64(&this->impl, num);
892 }
893 1 size_t StringBuilder::append(real64_t const& num, unsigned intlen, unsigned decimallen, unsigned grouplen)
894 {
895 1 return gate_strbuilder_append_real(&this->impl, num, intlen, decimallen, grouplen);
896 }
897 4 size_t StringBuilder::appendHex(uint8_t const& num, bool_t upperCase)
898 {
899 char buffer[8];
900
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);
901
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
8 return this->append(buffer, buffer_used);
902 }
903 2 size_t StringBuilder::appendHex(uint16_t const& num, bool_t upperCase)
904 {
905 char buffer[8];
906
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);
907
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return this->append(buffer, buffer_used);
908 }
909 2 size_t StringBuilder::appendHex(uint32_t const& num, bool_t upperCase)
910 {
911 char buffer[16];
912
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);
913
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return this->append(buffer, buffer_used);
914 }
915 1 size_t StringBuilder::appendHex(uint64_t const& num, bool_t upperCase)
916 {
917 char buffer[24];
918
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);
919
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return this->append(buffer, buffer_used);
920 }
921 1 size_t StringBuilder::appendHex(char const* txt, size_t len, bool_t upperCase)
922 {
923 1 size_t ret = 0;
924
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 while (len-- > 0)
925 {
926
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 ret += this->appendHex(static_cast<uint8_t>(*txt), upperCase);
927 4 ++txt;
928 }
929 1 return ret;
930 }
931
932
933 1 size_t StringBuilder::appendChars(size_t count, gate_char8_t chr)
934 {
935 1 return gate_strbuilder_append_chars(&this->impl, count, chr);
936 }
937
938 5 size_t StringBuilder::appendNewLine()
939 {
940 5 return this->append(strings::NewLine);
941 }
942 2 size_t StringBuilder::discard(size_t charCount)
943 {
944 2 return gate_strbuilder_discard(&this->impl, charCount);
945 }
946 1 size_t StringBuilder::discardBack(size_t charCount)
947 {
948 1 return gate_strbuilder_discard_back(&this->impl, charCount);
949 }
950 124 String StringBuilder::toString()
951 {
952 gate_string_t tmp;
953
2/4
✓ Branch 1 taken 124 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 124 times.
124 if (NULL == gate_strbuilder_to_string(&this->impl, &tmp))
954 {
955 GATEXX_RAISE_ERROR(results::OutOfMemory);
956 }
957 124 String ret = String::duplicate(tmp);
958
1/2
✓ Branch 1 taken 124 times.
✗ Branch 2 not taken.
124 gate_string_release(&tmp);
959 248 return ret;
960 }
961 4 String StringBuilder::copyString() const
962 {
963 gate_string_t tmp;
964
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))
965 {
966 GATEXX_RAISE_ERROR(results::OutOfMemory);
967 }
968 4 String ret = String::duplicate(tmp);
969
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 gate_string_release(&tmp);
970 8 return ret;
971 }
972
973 4 String StringBuilder::getView() const
974 {
975 return String::createStatic(
976 gate_strbuilder_ptr(&this->impl, 0),
977 gate_strbuilder_length(&this->impl)
978 4 );
979 }
980
981 1 size_t StringBuilder::insert(size_t pos, String const& text)
982 {
983 1 return gate_strbuilder_insert(&this->impl, pos, text.c_str(), text.length());
984 }
985
986
987 1 size_t StringBuilder::positionOf(String const& token, size_t startAt)
988 {
989 1 return gate_strbuilder_pos(&this->impl, token.c_impl(), startAt);
990 }
991
992 1 size_t StringBuilder::positionOf(char const& chr, size_t startAt)
993 {
994 1 return gate_strbuilder_char_pos(&this->impl, chr, startAt);
995 }
996
997 1 size_t StringBuilder::replace(String const& find, String const& replaceWith, size_t startAt, size_t maxReplace)
998 {
999 1 return gate_strbuilder_replace_string(&this->impl, find.c_impl(), replaceWith.c_impl(), startAt, maxReplace);
1000 }
1001
1002 1 size_t StringBuilder::remove(size_t fromPos, size_t length)
1003 {
1004 1 return gate_strbuilder_remove(&this->impl, fromPos, length);
1005 }
1006
1007 106 StringBuilder& StringBuilder::operator<<(String const& text) { this->append(text); return *this; }
1008
1/2
✓ Branch 2 taken 106 times.
✗ Branch 3 not taken.
106 StringBuilder& StringBuilder::operator<<(char const* text) { this->append(String(text)); return *this; }
1009 1 StringBuilder& StringBuilder::operator<<(int16_t const& num) { this->append(num); return *this; }
1010 1 StringBuilder& StringBuilder::operator<<(uint16_t const& num) { this->append(num); return *this; }
1011 2 StringBuilder& StringBuilder::operator<<(int32_t const& num) { this->append(num); return *this; }
1012 3 StringBuilder& StringBuilder::operator<<(uint32_t const& num) { this->append(num); return *this; }
1013 1 StringBuilder& StringBuilder::operator<<(int64_t const& num) { this->append(num); return *this; }
1014 3 StringBuilder& StringBuilder::operator<<(uint64_t const& num) { this->append(num); return *this; }
1015 1 StringBuilder& StringBuilder::operator<<(real64_t const& num) { this->append(num); return *this; }
1016
1017 static char_8_t const empty_dummy = '\0';
1018 char_8_t const& StringBuilder::operator[](size_t index) const
1019 {
1020 gate_char8_t const* ptr = gate_strbuilder_ptr(&this->impl, index);
1021 if (ptr == NULL)
1022 {
1023 return empty_dummy;
1024 }
1025 else
1026 {
1027 return *ptr;
1028 }
1029 }
1030 1 char_8_t& StringBuilder::operator[](size_t index)
1031 {
1032 1 gate_char8_t const* ptr = gate_strbuilder_ptr(&this->impl, index);
1033 1 return *const_cast<gate_char8_t*>(ptr);
1034 }
1035
1036
1037 1 CstrBuffer::CstrBuffer(char const* ptr, size_t length, bool_t copyNeeded)
1038 {
1039
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (NULL == gate_cstrbuffer_create(&this->impl, ptr, length, copyNeeded))
1040 {
1041 GATEXX_RAISE_ERROR(results::OutOfMemory);
1042 }
1043 1 }
1044 1 CstrBuffer::CstrBuffer(wchar_t const* ptr, gate_size_t length)
1045 {
1046
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (NULL == gate_cstrbuffer_create_wstr(&this->impl, ptr, length))
1047 {
1048 GATEXX_RAISE_ERROR(results::OutOfMemory);
1049 }
1050 1 }
1051
1052 1 CstrBuffer::CstrBuffer(String const& str, bool_t copyNeeded)
1053 {
1054
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (NULL == gate_cstrbuffer_create_string(&this->impl, str.c_impl(), copyNeeded))
1055 {
1056 GATEXX_RAISE_ERROR(results::OutOfMemory);
1057 }
1058 1 }
1059 6 CstrBuffer::~CstrBuffer() noexcept
1060 {
1061 3 gate_cstrbuffer_destroy(&this->impl);
1062 3 }
1063 1 gate_cstrbuffer_t const* CstrBuffer::c_impl() const
1064 {
1065 1 return &this->impl;
1066 }
1067 2 char const* CstrBuffer::get() const
1068 {
1069 2 return gate_cstrbuffer_get(&this->impl);
1070 }
1071 1 size_t CstrBuffer::length() const
1072 {
1073 1 return gate_cstrbuffer_length(&this->impl);
1074 }
1075
1076
1077 1 CstrBuffer16::CstrBuffer16(char_16_t const* ptr, size_t length, bool_t copyNeeded)
1078 {
1079
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (NULL == gate_cstrbuffer16_create(&this->impl, ptr, length, copyNeeded))
1080 {
1081 GATEXX_RAISE_ERROR(results::OutOfMemory);
1082 }
1083 1 }
1084 1 CstrBuffer16::CstrBuffer16(String const& str)
1085 {
1086
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (NULL == gate_cstrbuffer16_create_string(&this->impl, str.c_impl()))
1087 {
1088 GATEXX_RAISE_ERROR(results::OutOfMemory);
1089 }
1090 1 }
1091 4 CstrBuffer16::~CstrBuffer16() noexcept
1092 {
1093 2 gate_cstrbuffer16_destroy(&this->impl);
1094 2 }
1095 1 gate_cstrbuffer16_t const* CstrBuffer16::c_impl() const
1096 {
1097 1 return &this->impl;
1098 }
1099 1 char_16_t const* CstrBuffer16::get() const
1100 {
1101 1 return gate_cstrbuffer16_get(&this->impl);
1102 }
1103 1 size_t CstrBuffer16::length() const
1104 {
1105 1 return gate_cstrbuffer16_length(&this->impl);
1106 }
1107
1108
1109 1 CstrBuffer32::CstrBuffer32(char_32_t const* ptr, size_t length, bool_t copyNeeded)
1110 {
1111
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (NULL == gate_cstrbuffer32_create(&this->impl, ptr, length, copyNeeded))
1112 {
1113 GATEXX_RAISE_ERROR(results::OutOfMemory);
1114 }
1115 1 }
1116 1 CstrBuffer32::CstrBuffer32(String const& str)
1117 {
1118
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (NULL == gate_cstrbuffer32_create_string(&this->impl, str.c_impl()))
1119 {
1120 GATEXX_RAISE_ERROR(results::OutOfMemory);
1121 }
1122 1 }
1123 4 CstrBuffer32::~CstrBuffer32() noexcept
1124 {
1125 2 gate_cstrbuffer32_destroy(&this->impl);
1126 2 }
1127 1 gate_cstrbuffer32_t const* CstrBuffer32::c_impl() const
1128 {
1129 1 return &this->impl;
1130 }
1131 1 char_32_t const* CstrBuffer32::get() const
1132 {
1133 1 return gate_cstrbuffer32_get(&this->impl);
1134 }
1135 1 size_t CstrBuffer32::length() const
1136 {
1137 1 return gate_cstrbuffer32_length(&this->impl);
1138 }
1139
1140
1141
1142
1143 namespace strings
1144 {
1145 String const NewLine = String::createStatic(GATE_STR_NEWLINE, GATE_STR_NEWLINE_LENGTH);
1146 String const Cr = String::createStatic(GATE_STR_CR, 1);
1147 String const Lf = String::createStatic(GATE_STR_LF, 1);
1148 String const CrLf = String::createStatic(GATE_STR_CRLF, 2);
1149 String const Empty = String();
1150
1151 } // end of namespace strings
1152
1153
1154
1155
1156 } // end of namespace gate
1157