GCC Code Coverage Report


Directory: src/gate/
File: src/gate/strings.hpp
Date: 2026-08-06 22:46:24
Exec Total Coverage
Lines: 5 5 100.0%
Functions: 51 51 100.0%
Branches: 0 0 -%

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 /** @file
30 * @brief Strings and text primitives
31 * @ingroup gatecore_cpp
32 */
33
34 #ifndef GATE_STRINGS_HPP_INCLUDED
35 #define GATE_STRINGS_HPP_INCLUDED
36
37 #include "gate/gate_core_api.hpp"
38 #include "gate/strings.h"
39 #include "gate/gatetypes.hpp"
40
41 namespace gate
42 {
43 class StringBuilder;
44
45 /// @brief
46 class GATE_CORE_CPP_API Char
47 {
48 public:
49 static int toLower(int chr);
50 static int toUpper(int chr);
51 static bool_t isDigit(int chr);
52 static bool_t isWhitespace(int chr);
53
54 static size_t readUtf8(char_8_t const* utf8text, size_t length, char_32_t& chr32);
55 static size_t readUtf16(char_16_t const* utf16text, size_t length, char_32_t& chr32);
56 static size_t readUtf32(char_32_t const* utf32text, size_t length, char_32_t& chr32);
57
58 static size_t writeUtf8(char_32_t chr32, char_8_t* utf8text, size_t length);
59 static size_t writeUtf16(char_32_t chr32, char_16_t* utf16text, size_t length);
60 static size_t writeUtf32(char_32_t chr32, char_32_t* utf32text, size_t length);
61
62 static size_t utf8ToUtf16(char_8_t const* src, size_t srclen, char_16_t* dst, size_t dstlen);
63 static size_t utf8ToUtf32(char_8_t const* src, size_t srclen, char_32_t* dst, size_t dstlen);
64 static size_t utf16ToUtf8(char_16_t const* src, size_t srclen, char_8_t* dst, size_t dstlen);
65 static size_t utf16ToUtf32(char_16_t const* src, size_t srclen, char_32_t* dst, size_t dstlen);
66 static size_t utf32ToUtf8(char_32_t const* src, size_t srclen, char_8_t* dst, size_t dstlen);
67 static size_t utf32ToUtf16(char_32_t const* src, size_t srclen, char_16_t* dst, size_t dstlen);
68 };
69
70
71 /// @brief
72 class GATE_CORE_CPP_API String : public SafeBoolBase<String>
73 {
74 public:
75 //typedef StrToken token_t;
76
77 static size_t const npos;
78 static size_t const NPOS;
79 typedef char_8_t const* const_iterator;
80
81 public:
82 String() noexcept;
83 String(char_8_t const* str);
84 String(char_8_t const* str, size_t len);
85 String(gate_string_t const& src);
86 String(String const& src) noexcept;
87 String(StringBuilder& strbuilder);
88 String(GATE_MOVEREF(String) src) noexcept;
89 String& operator=(String const& src) noexcept;
90 ~String() noexcept;
91 String& operator=(GATE_MOVEREF(String) src) noexcept;
92
93 static String createStatic(char_8_t const* str) noexcept;
94 static String createStatic(char_8_t const* str, size_t len) noexcept;
95 static String createFrom(gate_string_t& str) noexcept; // create C++ String and release originating C object
96 static String createFrom(char_16_t const* utf16, size_t len);
97 static String createFrom(char_32_t const* utf32, size_t len);
98 static String createFilled(size_t count, char_8_t content);
99 #if !defined(GATE_COMPILER_SUPPORTS_CPP_ARRAY_SIZE_DEDUCTION)
100 template<class T> static String createStaticFrom(T str)
101 {
102 return String::createStatic(&str[0], sizeof(str) / sizeof(str[0]) - 1);
103 }
104 #else
105 14 template<unsigned N> static String createStaticFrom(char const (&str)[N])
106 {
107 14 return String::createStatic(str, N - 1);
108 }
109 #endif
110 static String createIntNum(int64_t num);
111 static String createRealNum(real64_t real, int32_t decimalCount = 3);
112 static void assign(gate_string_t& dst, String const& src);
113 static void assign(gate_string_t& dst, gate_string_t const& src);
114
115 void swap(String& that) noexcept;
116 void swap(gate_string_t& that) noexcept;
117 static void swap(gate_string_t& a, gate_string_t& b) noexcept;
118
119 size_t length() const noexcept;
120 static size_t length(char const* str) noexcept;
121 size_t size() const noexcept;
122 bool empty() const noexcept;
123 char_8_t const* c_str() const noexcept;
124 char_8_t const& at(size_t index) const noexcept;
125 gate_string_t const* c_impl() const noexcept;
126
127 const_iterator begin() const noexcept;
128 const_iterator cbegin() const noexcept;
129 const_iterator end() const noexcept;
130 const_iterator cend() const noexcept;
131
132 size_t positionOf(String const& text, size_t startAt = 0) const noexcept;
133 size_t positionOf(char const& chr, size_t startAt = 0) const noexcept;
134 size_t positionOfLast(String const& text) const noexcept;
135 size_t positionOfLast(char const& chr) const noexcept;
136 size_t findFirstOf(String const& text, size_t startAt = 0) const noexcept;
137 size_t findFirstNotOf(String const& text, size_t startAt = 0) const noexcept;
138 size_t findLastOf(String const& text) const noexcept;
139 size_t findLastNotOf(String const& text) const noexcept;
140
141 String substr(size_t offset, size_t len = GATE_STR_NPOS) const;
142 String left(size_t len) const;
143 String right(size_t len) const;
144 String readLine(String& tail) const;
145 String readLine();
146 bool_t readToken(String& token, String& tail) const;
147 String readToken();
148 String toLower() const;
149 String toUpper() const;
150
151 String ltrim() const;
152 String rtrim() const;
153 String trim() const;
154
155 String copy() const;
156 String clone() const;
157 String duplicate() const noexcept;
158 static String copy(gate_string_t const& src);
159 static String clone(gate_string_t const& src);
160 static String duplicate(gate_string_t const& src) noexcept;
161
162 size_t copyTo(char_8_t* buffer, size_t capacity) const;
163
164 int compare(String const& str) const noexcept;
165 int compareIC(String const& str) const noexcept;
166 bool_t startsWith(String const& str) const noexcept;
167 bool_t startsWith(char const& chr) const noexcept;
168 bool_t startsWithIC(String const& str) const noexcept;
169 bool_t endsWith(String const& str) const noexcept;
170 bool_t endsWithIC(String const& str) const noexcept;
171 bool_t endsWith(char const& chr) const noexcept;
172 bool_t like(String const& str) const noexcept;
173 bool_t likeOneOf(String const& str, char_8_t separator = ';') const noexcept;
174 bool_t equals(String const& str) const noexcept;
175 bool_t equalsIC(String const& str) const noexcept;
176
177 int compare(char_8_t const* str) const noexcept;
178 int compareIC(char_8_t const* str) const noexcept;
179 bool_t startsWith(char_8_t const* str) const noexcept;
180 bool_t startsWithIC(char_8_t const* str) const noexcept;
181 bool_t endsWith(char_8_t const* str) const noexcept;
182 bool_t endsWithIC(char_8_t const* str) const noexcept;
183 bool_t equals(char_8_t const* str) const noexcept;
184 bool_t equalsIC(char_8_t const* str) const noexcept;
185
186 size_t parseNum(uint64_t& num) const noexcept;
187 size_t parseNum(int64_t& num) const noexcept;
188 size_t parseNum(real64_t& num) const noexcept;
189 size_t parseNum(real32_t& num) const noexcept;
190 size_t parseHex(uint64_t& num) const noexcept;
191 String parseHex() const noexcept;
192 int64_t parseInt() const noexcept;
193 real64_t parseReal() const noexcept;
194 uint8_t parseOctByte() const noexcept;
195 bool_t parseBool() const noexcept;
196 size_t parseLine(String& line) const noexcept;
197
198 static size_t printValue(char* buffer, size_t bufferlen, int printType, void const* ptrValue);
199
200 String toHex() const;
201 static String toHex(char_8_t const* str, size_t len, bool_t upperCase = false);
202
203 bool_t operator==(String const& src) const noexcept;
204 bool_t operator!=(String const& src) const noexcept;
205 bool_t operator< (String const& src) const noexcept;
206 bool_t operator> (String const& src) const noexcept;
207 bool_t operator<=(String const& src) const noexcept;
208 bool_t operator>=(String const& src) const noexcept;
209
210 bool_t operator!() const noexcept;
211 gate_string_t const& operator*() const noexcept;
212 char_8_t const& operator[](size_t index) const noexcept;
213
214 size_t parse(String const& find, size_t startAt, String* ptrHead, String* ptrTail, bool_t separatorAsTail = false) const;
215
216 template<class N> size_t parseNum(N& num) const noexcept
217 {
218 int64_t num64 = 0;
219 size_t ret = this->parseNum(num64);
220 num = static_cast<N>(num64);
221 return ret;
222 }
223 template<class N> size_t parseHex(N& num) const noexcept
224 {
225 uint64_t num64 = 0;
226 size_t ret = this->parseHex(num64);
227 num = static_cast<N>(num64);
228 return ret;
229 }
230
231 private:
232 gate_string_t impl;
233 };
234
235 GATE_CORE_CPP_API String operator+(String const& str1, String const& str2);
236 GATE_CORE_CPP_API String operator+(String const& str1, char const* str2);
237 GATE_CORE_CPP_API String operator+(char const* str1, String const& str2);
238
239
240 /// @brief
241 class GATE_CORE_CPP_API StaticString : public String
242 {
243 public:
244 explicit StaticString(char_8_t const* str) noexcept;
245 StaticString(char_8_t const* str, size_t len) noexcept;
246 #if !defined(GATE_COMPILER_SUPPORTS_CPP_ARRAY_SIZE_DEDUCTION)
247 template<class T> StaticString(T str)
248 : String(String::createStatic(&str[0], sizeof(str) / sizeof(str[0]) - 1))
249 {
250 }
251 #else
252 398 template<unsigned N> StaticString(char const (&str)[N])
253 398 : String(String::createStatic(str, N - 1))
254 {
255 398 }
256 #endif
257 StaticString(StaticString const& src) noexcept;
258 StaticString& operator=(StaticString const& src) noexcept;
259 ~StaticString() noexcept;
260 };
261
262
263 /// @brief
264 class GATE_CORE_CPP_API StringBuilder : private NonCopyable
265 {
266 public:
267 StringBuilder(size_t capacity = 0);
268 StringBuilder(char* staticBuffer, size_t capacity, size_t alreadyUsed);
269 ~StringBuilder();
270
271 gate_strbuilder_t const* c_impl() const noexcept;
272 gate_strbuilder_t* c_impl() noexcept;
273
274 char_8_t const* ptr(size_t pos = 0) const noexcept;
275 size_t length() const noexcept;
276
277 size_t resize(size_t sz);
278 size_t append(String const& text);
279 size_t append(char const* ptr, size_t len);
280 size_t append(char_16_t const* text16, size_t textlen);
281 size_t append(char_32_t const* text32, size_t textlen);
282 size_t appendBool(bool const& b);
283 size_t append(int16_t const& num);
284 size_t append(uint16_t const& num);
285 size_t append(int32_t const& num);
286 size_t append(uint32_t const& num);
287 size_t append(int64_t const& num);
288 size_t append(uint64_t const& num);
289 size_t append(real64_t const& num, unsigned intlen = 0, unsigned decimallen = 3, unsigned grouplen = 0);
290 size_t appendHex(uint8_t const& num, bool_t upperCase = false);
291 size_t appendHex(uint16_t const& num, bool_t upperCase = false);
292 size_t appendHex(uint32_t const& num, bool_t upperCase = false);
293 size_t appendHex(uint64_t const& num, bool_t upperCase = false);
294 size_t appendHex(char const* txt, size_t len, bool_t upperCase = false);
295 size_t appendChars(size_t count, gate_char8_t chr);
296 size_t appendNewLine();
297 size_t appendCR();
298 size_t appendLF();
299 size_t appendCRLF();
300 size_t discard(size_t charCount = 1);
301 size_t discardBack(size_t charCount = 1);
302 String toString();
303 String copyString() const;
304 String getView() const;
305 size_t insert(size_t pos, String const& text);
306 size_t positionOf(String const& token, size_t startAt = 0);
307 size_t positionOf(char const& chr, size_t startAt = 0);
308 size_t replace(String const& find, String const& replaceWith, size_t startAt = 0, size_t maxReplace = GATE_STR_NPOS);
309 size_t remove(size_t fromPos, size_t length = GATE_STR_NPOS);
310
311 StringBuilder& operator<<(String const& text);
312 StringBuilder& operator<<(char const* text);
313 StringBuilder& operator<<(int16_t const& num);
314 StringBuilder& operator<<(uint16_t const& num);
315 StringBuilder& operator<<(int32_t const& num);
316 StringBuilder& operator<<(uint32_t const& num);
317 StringBuilder& operator<<(int64_t const& num);
318 StringBuilder& operator<<(uint64_t const& num);
319 StringBuilder& operator<<(real64_t const& num);
320
321 char_8_t const& operator[](size_t index) const;
322 char_8_t& operator[](size_t index);
323
324 private:
325 gate_strbuilder_t impl;
326 };
327
328
329 /// @brief
330 class GATE_CORE_CPP_API CstrBuffer : private NonCopyable
331 {
332 public:
333 CstrBuffer(char const* ptr, size_t length, bool_t copyNeeded = false);
334 CstrBuffer(wchar_t const* ptr, gate_size_t length);
335 CstrBuffer(String const& str, bool_t copyNeeded = false);
336 ~CstrBuffer() noexcept;
337
338 gate_cstrbuffer_t const* c_impl() const;
339 char const* get() const;
340 size_t length() const;
341
342 private:
343 gate_cstrbuffer_t impl;
344 };
345
346
347 class GATE_CORE_CPP_API CstrBuffer16 : private NonCopyable
348 {
349 public:
350 CstrBuffer16(char_16_t const* ptr, size_t length, bool_t copyNeeded = false);
351 CstrBuffer16(String const& str);
352 ~CstrBuffer16() noexcept;
353
354 gate_cstrbuffer16_t const* c_impl() const;
355 char_16_t const* get() const;
356 size_t length() const;
357
358 private:
359 gate_cstrbuffer16_t impl;
360 };
361
362
363 class GATE_CORE_CPP_API CstrBuffer32 : private NonCopyable
364 {
365 public:
366 CstrBuffer32(char_32_t const* ptr, size_t length, bool_t copyNeeded = false);
367 CstrBuffer32(String const& str);
368 ~CstrBuffer32() noexcept;
369
370 gate_cstrbuffer32_t const* c_impl() const;
371 char_32_t const* get() const;
372 size_t length() const;
373
374 private:
375 gate_cstrbuffer32_t impl;
376 };
377
378
379 namespace strings
380 {
381 GATE_CORE_CPP_API extern String const NewLine;
382 GATE_CORE_CPP_API extern String const Cr;
383 GATE_CORE_CPP_API extern String const Lf;
384 GATE_CORE_CPP_API extern String const CrLf;
385 GATE_CORE_CPP_API extern String const Empty;
386
387 } // end of namespace strings
388
389 } // end of namespace gate
390
391 #endif
392