Plasma Engine  2.0
Loading...
Searching...
No Matches
StringBuilder_inl.h
1#pragma once
2
3#include <Foundation/Strings/StringConversion.h>
4
6 : m_Data(pAllocator)
7{
8 AppendTerminator();
9}
10
12 : m_Data(rhs.GetAllocator())
13{
14 AppendTerminator();
15
16 *this = rhs;
17}
18
20 : m_Data(rhs.GetAllocator())
21{
22 AppendTerminator();
23
24 *this = std::move(rhs);
25}
26
27inline plStringBuilder::plStringBuilder(const char* szUTF8, plAllocator* pAllocator)
28 : m_Data(pAllocator)
29{
30 AppendTerminator();
31
32 *this = szUTF8;
33}
34
35inline plStringBuilder::plStringBuilder(const wchar_t* pWChar, plAllocator* pAllocator)
36 : m_Data(pAllocator)
37{
38 AppendTerminator();
39
40 *this = pWChar;
41}
42
44 : m_Data(pAllocator)
45{
46 AppendTerminator();
47
48 *this = rhs;
49}
50
52{
53 return m_Data.GetAllocator();
54}
55
56PL_ALWAYS_INLINE void plStringBuilder::operator=(const char* szUTF8)
57{
58 Set(szUTF8);
59}
60
61PL_FORCE_INLINE void plStringBuilder::operator=(const wchar_t* pWChar)
62{
63 // fine to do this, szWChar can never come from the stringbuilder's own data array
64 Clear();
65 Append(pWChar);
66}
67
68PL_ALWAYS_INLINE void plStringBuilder::operator=(const plStringBuilder& rhs)
69{
70 m_Data = rhs.m_Data;
71}
72
73PL_ALWAYS_INLINE void plStringBuilder::operator=(plStringBuilder&& rhs) noexcept
74{
75 m_Data = std::move(rhs.m_Data);
76}
77
78PL_ALWAYS_INLINE plUInt32 plStringBuilder::GetElementCount() const
79{
80 return m_Data.GetCount() - 1; // exclude the '\0' terminator
81}
82
83PL_ALWAYS_INLINE plUInt32 plStringBuilder::GetCharacterCount() const
84{
86}
87
88PL_FORCE_INLINE void plStringBuilder::Clear()
89{
90 m_Data.SetCountUninitialized(1);
91 m_Data[0] = '\0';
92}
93
94inline void plStringBuilder::Append(plUInt32 uiChar)
95{
96 char szChar[6] = {0, 0, 0, 0, 0, 0};
97 char* pChar = &szChar[0];
98
100 plUInt32 uiCharLen = (plUInt32)(pChar - szChar);
101 plUInt32 uiOldCount = m_Data.GetCount();
102 m_Data.SetCountUninitialized(uiOldCount + uiCharLen);
103 uiOldCount--;
104 for (plUInt32 i = 0; i < uiCharLen; i++)
105 {
106 m_Data[uiOldCount + i] = szChar[i];
107 }
108 m_Data[uiOldCount + uiCharLen] = '\0';
109}
110
111inline void plStringBuilder::Prepend(plUInt32 uiChar)
112{
113 char szChar[6] = {0, 0, 0, 0, 0, 0};
114 char* pChar = &szChar[0];
115
117 Prepend(szChar);
118}
119
120inline void plStringBuilder::Append(const wchar_t* pData1, const wchar_t* pData2, const wchar_t* pData3, const wchar_t* pData4, const wchar_t* pData5, const wchar_t* pData6)
121{
122 // this is a bit heavy on the stack size (6KB)
123 // but it is really only a convenience function, as one could always just use the char* Append function and convert explicitly
124 plStringUtf8 s1(pData1, m_Data.GetAllocator());
125 plStringUtf8 s2(pData2, m_Data.GetAllocator());
126 plStringUtf8 s3(pData3, m_Data.GetAllocator());
127 plStringUtf8 s4(pData4, m_Data.GetAllocator());
128 plStringUtf8 s5(pData5, m_Data.GetAllocator());
129 plStringUtf8 s6(pData6, m_Data.GetAllocator());
130
131 Append(s1.GetView(), s2.GetView(), s3.GetView(), s4.GetView(), s5.GetView(), s6.GetView());
132}
133
134inline void plStringBuilder::Prepend(const wchar_t* pData1, const wchar_t* pData2, const wchar_t* pData3, const wchar_t* pData4, const wchar_t* pData5, const wchar_t* pData6)
135{
136 // this is a bit heavy on the stack size (6KB)
137 // but it is really only a convenience function, as one could always just use the char* Append function and convert explicitly
138 plStringUtf8 s1(pData1, m_Data.GetAllocator());
139 plStringUtf8 s2(pData2, m_Data.GetAllocator());
140 plStringUtf8 s3(pData3, m_Data.GetAllocator());
141 plStringUtf8 s4(pData4, m_Data.GetAllocator());
142 plStringUtf8 s5(pData5, m_Data.GetAllocator());
143 plStringUtf8 s6(pData6, m_Data.GetAllocator());
144
145 Prepend(s1.GetView(), s2.GetView(), s3.GetView(), s4.GetView(), s5.GetView(), s6.GetView());
146}
147
148PL_ALWAYS_INLINE const char* plStringBuilder::GetData() const
149{
150 PL_ASSERT_DEBUG(!m_Data.IsEmpty(), "plStringBuilder has been corrupted, the array can never be empty.");
151
152 return &m_Data[0];
153}
154
155inline void plStringBuilder::AppendTerminator()
156{
157 // make sure the string terminates with a zero.
158 if (m_Data.IsEmpty() || (m_Data.PeekBack() != '\0'))
159 m_Data.PushBack('\0');
160}
161
163{
164 const plUInt32 uiNewStringLength = plStringUtils::ToUpperString(&m_Data[0]);
165
166 // the array stores the number of bytes, so set the count to the actually used number of bytes
167 m_Data.SetCountUninitialized(uiNewStringLength + 1);
168}
169
171{
172 const plUInt32 uiNewStringLength = plStringUtils::ToLowerString(&m_Data[0]);
173
174 // the array stores the number of bytes, so set the count to the actually used number of bytes
175 m_Data.SetCountUninitialized(uiNewStringLength + 1);
176}
177
178inline void plStringBuilder::ChangeCharacter(iterator& ref_it, plUInt32 uiCharacter)
179{
180 PL_ASSERT_DEV(ref_it.IsValid(), "The given character iterator does not point to a valid character.");
181 PL_ASSERT_DEV(ref_it.GetData() >= GetData() && ref_it.GetData() < GetData() + GetElementCount(),
182 "The given character iterator does not point into this string. It was either created from another string, or this string "
183 "has been reallocated in the mean time.");
184
185 // this is only an optimization for pure ASCII strings
186 // without it, the code below would still work
187 if (plUnicodeUtils::IsASCII(*ref_it) && plUnicodeUtils::IsASCII(uiCharacter))
188 {
189 char* pPos = const_cast<char*>(ref_it.GetData()); // yes, I know...
190 *pPos = uiCharacter & 0xFF;
191 return;
192 }
193
194 ChangeCharacterNonASCII(ref_it, uiCharacter);
195}
196
197PL_ALWAYS_INLINE void plStringBuilder::Reserve(plUInt32 uiNumElements)
198{
199 m_Data.Reserve(uiNumElements);
200}
201
202PL_ALWAYS_INLINE void plStringBuilder::Insert(const char* szInsertAtPos, plStringView sTextToInsert)
203{
204 ReplaceSubString(szInsertAtPos, szInsertAtPos, sTextToInsert);
205}
206
207PL_ALWAYS_INLINE void plStringBuilder::Remove(const char* szRemoveFromPos, const char* szRemoveToPos)
208{
209 ReplaceSubString(szRemoveFromPos, szRemoveToPos, plStringView());
210}
211
212template <typename Container>
213bool plUnicodeUtils::RepairNonUtf8Text(const char* pStartData, const char* pEndData, Container& out_result)
214{
215 if (plUnicodeUtils::IsValidUtf8(pStartData, pEndData))
216 {
217 out_result = plStringView(pStartData, pEndData);
218 return false;
219 }
220
221 out_result.Clear();
222
224 plUnicodeUtils::UtfInserter<char, decltype(fixedText)> inserter(&fixedText);
225
226 while (pStartData < pEndData)
227 {
228 const plUInt32 uiChar = plUnicodeUtils::DecodeUtf8ToUtf32(pStartData);
229 plUnicodeUtils::EncodeUtf32ToUtf8(uiChar, inserter);
230 }
231
232 PL_ASSERT_DEV(plUnicodeUtils::IsValidUtf8(fixedText.GetData(), fixedText.GetData() + fixedText.GetCount()), "Repaired text is still not a valid Utf8 string.");
233
234 out_result = plStringView(fixedText.GetData(), fixedText.GetCount());
235 return true;
236}
237
238#include <Foundation/Strings/Implementation/AllStrings_inl.h>
Base class for all memory allocators.
Definition Allocator.h:23
void PushBack(const T &value)
Pushes value at the end of the array.
Definition ArrayBase_inl.h:333
T * GetData()
Returns a pointer to the array data, or nullptr if the array is empty.
Definition ArrayBase_inl.h:423
void SetCountUninitialized(plUInt32 uiCount)
Resizes the array to have exactly uiCount elements. Extra elements might be uninitialized.
Definition ArrayBase_inl.h:155
T & PeekBack()
Returns the last element of the array.
Definition ArrayBase_inl.h:388
bool IsEmpty() const
Returns true, if the array does not contain any elements.
Definition ArrayBase_inl.h:178
plUInt32 GetCount() const
Returns the number of active elements in the array.
Definition ArrayBase_inl.h:172
plAllocator * GetAllocator() const
Returns the allocator that is used by this instance.
Definition DynamicArray.h:53
void Reserve(plUInt32 uiCapacity)
Expands the array so it can at least store the given capacity.
Definition DynamicArray_inl.h:179
A hybrid array uses in-place storage to handle the first few elements without any allocation....
Definition HybridArray.h:12
plStringBuilder is a class that is meant for creating and modifying strings.
Definition StringBuilder.h:35
void Clear()
Resets this string to be empty. Does not deallocate any previously allocated data,...
Definition StringBuilder_inl.h:88
plAllocator * GetAllocator() const
Returns the allocator that is used by this object.
Definition StringBuilder_inl.h:51
plStringBuilder(plAllocator *pAllocator=plFoundation::GetDefaultAllocator())
Initializes the string to be empty. No data is allocated, but the plStringBuilder ALWAYS creates an a...
Definition StringBuilder_inl.h:5
const char * GetData() const
Returns a char pointer to the internal Utf8 data.
Definition StringBuilder_inl.h:148
plUInt32 GetElementCount() const
Returns the number of bytes that this string takes up.
Definition StringBuilder_inl.h:78
void Append(plUInt32 uiChar)
Appends a single Utf32 character.
Definition StringBuilder_inl.h:94
void Insert(const char *szInsertAtPos, plStringView sTextToInsert)
A wrapper around ReplaceSubString. Will insert the given string at szInsertAtPos.
Definition StringBuilder_inl.h:202
void ToUpper()
Converts all characters to upper case. Might move the string data around, so all iterators to the dat...
Definition StringBuilder_inl.h:162
void Prepend(plUInt32 uiChar)
Prepends a single Utf32 character.
Definition StringBuilder_inl.h:111
void operator=(const plStringBuilder &rhs)
Copies the given string into this one.
Definition StringBuilder_inl.h:68
void Set(plStringView sData1)
Sets the string to the given string.
Definition StringBuilder.cpp:16
plUInt32 GetCharacterCount() const
Returns the number of characters of which this string consists. Might be less than GetElementCount,...
Definition StringBuilder_inl.h:83
void ReplaceSubString(const char *szStartPos, const char *szEndPos, plStringView sReplaceWith)
Replaces the string that starts at szStartPos and ends at szEndPos with the string szReplaceWith.
Definition StringBuilder.cpp:427
void Reserve(plUInt32 uiNumElements)
Reserves uiNumElements bytes.
Definition StringBuilder_inl.h:197
void Remove(const char *szRemoveFromPos, const char *szRemoveToPos)
A wrapper around ReplaceSubString. Will remove the substring which starts at szRemoveFromPos and ends...
Definition StringBuilder_inl.h:207
void ChangeCharacter(iterator &ref_it, plUInt32 uiCharacter)
Changes the single character in this string, to which the iterator currently points.
Definition StringBuilder_inl.h:178
void ToLower()
Converts all characters to lower case. Might move the string data around, so all iterators to the dat...
Definition StringBuilder_inl.h:170
A small string class that converts any other encoding to Utf8.
Definition StringConversion.h:47
static plUInt32 GetCharacterCount(const char *szUtf8, const char *pStringEnd=plUnicodeUtils::GetMaxStringEnd< char >())
Returns the number of characters (not Bytes!) in a Utf8 string (excluding the zero terminator),...
Definition StringUtils_inl.h:81
static plUInt32 ToLowerString(char *szString, const char *pStringEnd=plUnicodeUtils::GetMaxStringEnd< char >())
Converts a (UTF-8) string in-place to lower case.
Definition StringUtils.cpp:176
static plUInt32 ToUpperString(char *szString, const char *pStringEnd=plUnicodeUtils::GetMaxStringEnd< char >())
Converts a (UTF-8) string in-place to upper case.
Definition StringUtils.cpp:158
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
static plUInt32 DecodeUtf8ToUtf32(ByteIterator &ref_szUtf8Iterator)
Decodes the next character from the given Utf8 sequence to Utf32 and increments the iterator as far a...
Definition UnicodeUtils_inl.h:51
static bool IsASCII(plUInt32 uiChar)
Returns whether a character is a pure ASCII character (only the first 7 Bits are used)
Definition UnicodeUtils_inl.h:23
static void EncodeUtf32ToUtf8(plUInt32 uiUtf32, ByteIterator &ref_szUtf8Output)
Encodes the given Utf32 character to Utf8 and writes as many bytes to the output iterator,...
Definition UnicodeUtils_inl.h:92
static bool IsValidUtf8(const char *szString, const char *szStringEnd=GetMaxStringEnd< char >())
Returns false if the given string does not contain a completely valid Utf8 string.
Definition UnicodeUtils_inl.h:150
static bool RepairNonUtf8Text(const char *pStartData, const char *pEndData, Container &out_result)
Checks an array of char's, whether it is a valid Utf8 string. If not, it repairs the string,...
Definition StringBuilder_inl.h:213
[internal] Small helper class to append bytes to some arbitrary container. Used for Utf8 string build...
Definition UnicodeUtils.h:111