GCC Code Coverage Report


Directory: src/gate/
File: src/gate/cxx_strings.cpp
Date: 2026-03-20 22:56:14
Exec Total Coverage
Lines: 499 555 89.9%
Functions: 182 188 96.8%
Branches: 93 222 41.9%

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