Plasma Engine  2.0
Loading...
Searching...
No Matches
StringBuilder.h
1#pragma once
2
3#include <Foundation/ThirdParty/utf8/utf8.h>
4
5#include <Foundation/Containers/HybridArray.h>
6#include <Foundation/Memory/MemoryUtils.h>
7#include <Foundation/Strings/FormatString.h>
8#include <Foundation/Strings/Implementation/StringBase.h>
9#include <Foundation/Strings/PathUtils.h>
10#include <Foundation/Strings/StringUtils.h>
11#include <Foundation/Strings/StringView.h>
12
13template <plUInt16 Size>
15
16template <plUInt16 Size, typename AllocatorWrapper>
17struct plHybridString;
18
19class plStreamReader;
20class plFormatString;
21
34class PL_FOUNDATION_DLL plStringBuilder : public plStringBase<plStringBuilder>
35{
36public:
39
41 plStringBuilder(const plStringBuilder& rhs); // [tested]
42
44 plStringBuilder(plStringBuilder&& rhs) noexcept;
45
47 template <plUInt16 Size>
49 : m_Data(rhs.m_Data)
50 {
51 }
52
54 template <plUInt16 Size, typename A>
56 : m_Data(rhs.m_Data)
57 {
58 }
59
60
62 template <plUInt16 Size>
64 : m_Data(std::move(rhs.m_Data))
65 {
66 }
67
69 template <plUInt16 Size, typename A>
71 : m_Data(std::move(rhs.m_Data))
72 {
73 }
74
76 plStringBuilder(plStringView sData1, plStringView sData2, plStringView sData3 = {}, plStringView sData4 = {},
77 plStringView sData5 = {}, plStringView sData6 = {}); // [tested]
78
80 /* implicit */ plStringBuilder(const char* szUTF8, plAllocator* pAllocator = plFoundation::GetDefaultAllocator()); // [tested]
81
83 /* implicit */ plStringBuilder(const wchar_t* pWChar, plAllocator* pAllocator = plFoundation::GetDefaultAllocator()); // [tested]
84
86 /* implicit */ plStringBuilder(plStringView rhs, plAllocator* pAllocator = plFoundation::GetDefaultAllocator()); // [tested]
87
89 void operator=(const plStringBuilder& rhs); // [tested]
90
92 void operator=(plStringBuilder&& rhs) noexcept;
93
95 void operator=(const char* szUTF8); // [tested]
96
98 void operator=(const wchar_t* pWChar); // [tested]
99
101 void operator=(plStringView rhs); // [tested]
102
104 template <plUInt16 Size>
106 {
107 m_Data = rhs.m_Data;
108 }
109
111 template <plUInt16 Size, typename A>
113 {
114 m_Data = rhs.m_Data;
115 }
116
118 template <plUInt16 Size>
120 {
121 m_Data = std::move(rhs.m_Data);
122 }
123
125 template <plUInt16 Size, typename A>
127 {
128 m_Data = std::move(rhs.m_Data);
129 }
130
132 plAllocator* GetAllocator() const;
133
135 void Clear(); // [tested]
136
138 const char* GetData() const; // [tested]
139
141 plUInt32 GetElementCount() const; // [tested]
142
148 plUInt32 GetCharacterCount() const; // [tested]
149
152 void ToUpper(); // [tested]
153
156 void ToLower(); // [tested]
157
166 void ChangeCharacter(iterator& ref_it, plUInt32 uiCharacter); // [tested]
167
169 void Set(plStringView sData1); // [tested]
171 void Set(plStringView sData1, plStringView sData2); // [tested]
173 void Set(plStringView sData1, plStringView sData2, plStringView sData3); // [tested]
175 void Set(plStringView sData1, plStringView sData2, plStringView sData3, plStringView sData4); // [tested]
177 void Set(plStringView sData1, plStringView sData2, plStringView sData3, plStringView sData4, plStringView sData5, plStringView sData6 = {}); // [tested]
178
180 void SetPath(plStringView sData1, plStringView sData2, plStringView sData3 = {}, plStringView sData4 = {});
181
183 void SetSubString_FromTo(const char* pStart, const char* pEnd);
184
186 void SetSubString_ElementCount(const char* pStart, plUInt32 uiElementCount);
187
189 void SetSubString_CharacterCount(const char* pStart, plUInt32 uiCharacterCount);
190
192 void Append(plUInt32 uiChar); // [tested]
193
195 void Append(const wchar_t* pData1, const wchar_t* pData2 = nullptr, const wchar_t* pData3 = nullptr, const wchar_t* pData4 = nullptr, const wchar_t* pData5 = nullptr, const wchar_t* pData6 = nullptr); // [tested]
196
198 void Append(plStringView sData1); // [tested]
200 void Append(plStringView sData1, plStringView sData2); // [tested]
202 void Append(plStringView sData1, plStringView sData2, plStringView sData3); // [tested]
204 void Append(plStringView sData1, plStringView sData2, plStringView sData3, plStringView sData4); // [tested]
206 void Append(plStringView sData1, plStringView sData2, plStringView sData3, plStringView sData4, plStringView sData5, plStringView sData6 = {}); // [tested]
207
209 void Prepend(plUInt32 uiChar); // [tested]
210
212 void Prepend(const wchar_t* pData1, const wchar_t* pData2 = nullptr, const wchar_t* pData3 = nullptr, const wchar_t* pData4 = nullptr,
213 const wchar_t* pData5 = nullptr, const wchar_t* pData6 = nullptr); // [tested]
214
216 void Prepend(plStringView sData1, plStringView sData2 = {}, plStringView sData3 = {}, plStringView sData4 = {},
217 plStringView sData5 = {}, plStringView sData6 = {}); // [tested]
218
220 void SetPrintf(const char* szUtf8Format, ...); // [tested]
221
223 void SetPrintfArgs(const char* szUtf8Format, va_list szArgs); // [tested]
224
226 void SetFormat(const plFormatString& string);
227
229 template <typename... ARGS>
230 void SetFormat(const char* szFormat, ARGS&&... args)
231 {
232 SetFormat(plFormatStringImpl<ARGS...>(szFormat, std::forward<ARGS>(args)...));
233 }
234
236 void AppendFormat(const plFormatString& string);
237
239 template <typename... ARGS>
240 void AppendFormat(const char* szFormat, ARGS&&... args)
241 {
242 AppendFormat(plFormatStringImpl<ARGS...>(szFormat, std::forward<ARGS>(args)...));
243 }
244
246 void PrependFormat(const plFormatString& string);
247
249 template <typename... ARGS>
250 void PrependFormat(const char* szFormat, ARGS&&... args)
251 {
252 PrependFormat(plFormatStringImpl<ARGS...>(szFormat, std::forward<ARGS>(args)...));
253 }
254
260 void Shrink(plUInt32 uiShrinkCharsFront, plUInt32 uiShrinkCharsBack); // [tested]
261
263 void Reserve(plUInt32 uiNumElements); // [tested]
264
265
267 void ReplaceSubString(const char* szStartPos, const char* szEndPos, plStringView sReplaceWith); // [tested]
268
270 void Insert(const char* szInsertAtPos, plStringView sTextToInsert); // [tested]
271
273 void Remove(const char* szRemoveFromPos, const char* szRemoveToPos); // [tested]
274
279 const char* ReplaceFirst(plStringView sSearchFor, plStringView sReplacement, const char* szStartSearchAt = nullptr); // [tested]
280
282 const char* ReplaceFirst_NoCase(plStringView sSearchFor, plStringView sReplacement, const char* szStartSearchAt = nullptr); // [tested]
283
287 const char* ReplaceLast(plStringView sSearchFor, plStringView sReplacement, const char* szStartSearchAt = nullptr); // [tested]
288
290 const char* ReplaceLast_NoCase(plStringView sSearchFor, plStringView sReplacement, const char* szStartSearchAt = nullptr); // [tested]
291
293 plUInt32 ReplaceAll(plStringView sSearchFor, plStringView sReplacement); // [tested]
294
296 plUInt32 ReplaceAll_NoCase(plStringView sSearchFor, plStringView sReplacement); // [tested]
297
302 const char* ReplaceWholeWord(const char* szSearchFor, plStringView sReplaceWith, plStringUtils::PL_CHARACTER_FILTER isDelimiterCB); // [tested]
303
307 const char* ReplaceWholeWord_NoCase(const char* szSearchFor, plStringView sReplaceWith, plStringUtils::PL_CHARACTER_FILTER isDelimiterCB); // [tested]
308
313 plUInt32 ReplaceWholeWordAll(const char* szSearchFor, plStringView sReplaceWith, plStringUtils::PL_CHARACTER_FILTER isDelimiterCB); // [tested]
314
318 plUInt32 ReplaceWholeWordAll_NoCase(const char* szSearchFor, plStringView sReplaceWith, plStringUtils::PL_CHARACTER_FILTER isDelimiterCB); // [tested]
319
321 void ReadAll(plStreamReader& inout_stream);
322
323 // ******* Path Functions ********
324
334 void MakeCleanPath(); // [tested]
335
340 void PathParentDirectory(plUInt32 uiLevelsUp = 1); // [tested]
341
343 void AppendPath(plStringView sPath1, plStringView sPath2 = {}, plStringView sPath3 = {}, plStringView sPath4 = {}); // [tested]
344
352 void AppendWithSeparator(plStringView sSeparator, plStringView sText1, plStringView sText2 = plStringView(), plStringView sText3 = plStringView(), plStringView sText4 = plStringView(), plStringView sText5 = plStringView(), plStringView sText6 = plStringView());
353
355 void ChangeFileName(plStringView sNewFileName); // [tested]
356
358 void ChangeFileNameAndExtension(plStringView sNewFileNameWithExtension); // [tested]
359
369 void ChangeFileExtension(plStringView sNewExtension, bool bFullExtension = false); // [tested]
370
375 void RemoveFileExtension(bool bFullExtension = false); // [tested]
376
380 plResult MakeRelativeTo(plStringView sAbsolutePathToMakeThisRelativeTo); // [tested]
381
386 void MakePathSeparatorsNative(); // [tested]
387
391 bool IsPathBelowFolder(const char* szPathToFolder); // [tested]
392
394 plUInt64 GetHeapMemoryUsage() const { return m_Data.GetHeapMemoryUsage(); }
395
399 void Trim(const char* szTrimChars = " \f\n\r\t\v"); // [tested]
400
402 void Trim(const char* szTrimCharsStart, const char* szTrimCharsEnd); // [tested]
403
407 void TrimLeft(const char* szTrimChars = " \f\n\r\t\v");
408
412 void TrimRight(const char* szTrimChars = " \f\n\r\t\v");
413
415 bool TrimWordStart(plStringView sWord); // [tested]
416
418 bool TrimWordEnd(plStringView sWord); // [tested]
419
420#if PL_ENABLED(PL_INTEROP_STL_STRINGS)
422 /* implicit */ plStringBuilder(const std::string_view& rhs, plAllocator* pAllocator = plFoundation::GetDefaultAllocator());
423
425 /* implicit */ plStringBuilder(const std::string& rhs, plAllocator* pAllocator = plFoundation::GetDefaultAllocator());
426
428 void operator=(const std::string_view& rhs);
429
431 void operator=(const std::string& rhs);
432#endif
433
434private:
437 void RemoveDoubleSlashesInPath(); // [tested]
438
439 void ChangeCharacterNonASCII(iterator& it, plUInt32 uiCharacter);
440 void AppendTerminator();
441
442 // needed for better copy construction
443 template <plUInt16 T>
444 friend struct plHybridStringBase;
445
446 friend plStreamReader;
447
449};
450
451#include <Foundation/Strings/Implementation/StringBuilder_inl.h>
Base class for all memory allocators.
Definition Allocator.h:23
Implements formating of strings with placeholders and formatting options.
Definition FormatString.h:59
Definition FormatStringImpl.h:9
static PL_ALWAYS_INLINE plAllocator * GetDefaultAllocator()
The default allocator can be used for any kind of allocation if no alignment is required.
Definition Basics.h:82
A hybrid array uses in-place storage to handle the first few elements without any allocation....
Definition HybridArray.h:12
Interface for binary in (read) streams.
Definition Stream.h:22
plStringBuilder is a class that is meant for creating and modifying strings.
Definition StringBuilder.h:35
void operator=(plHybridStringBase< Size > &&rhs)
Moves the given string into this one.
Definition StringBuilder.h:119
plStringBuilder(plHybridString< Size, A > &&rhs)
Moves the given string into this one.
Definition StringBuilder.h:70
void PrependFormat(const char *szFormat, ARGS &&... args)
Prepends a formatted string. Uses '{}' formatting placeholders, see plFormatString for details.
Definition StringBuilder.h:250
plStringBuilder(const plHybridStringBase< Size > &rhs)
Copies the given string into this one.
Definition StringBuilder.h:48
void SetFormat(const char *szFormat, ARGS &&... args)
Replaces this with a formatted string. Uses '{}' formatting placeholders, see plFormatString for deta...
Definition StringBuilder.h:230
plUInt64 GetHeapMemoryUsage() const
Returns the amount of bytes that are currently allocated on the heap.
Definition StringBuilder.h:394
void operator=(const plHybridString< Size, A > &rhs)
Copies the given string into this one.
Definition StringBuilder.h:112
void operator=(plHybridString< Size, A > &&rhs) noexcept
Moves the given string into this one.
Definition StringBuilder.h:126
plStringBuilder(plHybridStringBase< Size > &&rhs)
Moves the given string into this one.
Definition StringBuilder.h:63
plStringBuilder(const plHybridString< Size, A > &rhs)
Copies the given string into this one.
Definition StringBuilder.h:55
void operator=(const plHybridStringBase< Size > &rhs)
Copies the given string into this one.
Definition StringBuilder.h:105
void AppendFormat(const char *szFormat, ARGS &&... args)
Appends a formatted string. Uses '{}' formatting placeholders, see plFormatString for details.
Definition StringBuilder.h:240
bool(*)(plUInt32 uiChar) PL_CHARACTER_FILTER
Function Definition for a function that determines whether a (Utf32) character belongs to a certain c...
Definition StringUtils.h:209
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
A string class for storing and passing around strings.
Definition String.h:28
Definition String.h:146
Default enum for returning failure or success, instead of using a bool.
Definition Types.h:54
Base class for strings, which implements all read-only string functions.
Definition StringBase.h:14