Plasma Engine  2.0
Loading...
Searching...
No Matches
String_inl.h
1#pragma once
2
3template <plUInt16 Size>
5 : m_Data(pAllocator)
6{
7 Clear();
8}
9
10template <plUInt16 Size>
12 : m_Data(pAllocator)
13{
14 *this = rhs;
15}
16
17template <plUInt16 Size>
19 : m_Data(pAllocator)
20{
21 operator=(std::move(rhs));
22}
23
24template <plUInt16 Size>
26 : m_Data(pAllocator)
27{
28 *this = rhs;
29}
30
31template <plUInt16 Size>
33 : m_Data(pAllocator)
34{
35 *this = rhs;
36}
37
38template <plUInt16 Size>
40 : m_Data(pAllocator)
41{
42 *this = rhs;
43}
44
45template <plUInt16 Size>
47
48template <plUInt16 Size>
50{
51 m_Data.SetCountUninitialized(1);
52 m_Data[0] = '\0';
53}
54
55template <plUInt16 Size>
56PL_ALWAYS_INLINE const char* plHybridStringBase<Size>::GetData() const
57{
58 PL_ASSERT_DEBUG(!m_Data.IsEmpty(), "plHybridString has been corrupted, the array can never be empty. This can happen when you access a "
59 "string that was previously std::move'd into another string.");
60
61 return &m_Data[0];
62}
63
64template <plUInt16 Size>
65PL_ALWAYS_INLINE plUInt32 plHybridStringBase<Size>::GetElementCount() const
66{
67 return m_Data.GetCount() - 1;
68}
69
70template <plUInt16 Size>
71PL_ALWAYS_INLINE plUInt32 plHybridStringBase<Size>::GetCharacterCount() const
72{
73 return plStringUtils::GetCharacterCount(GetData());
74}
75
76template <plUInt16 Size>
77void plHybridStringBase<Size>::operator=(const char* szString)
78{
79 plUInt32 uiElementCount = plStringUtils::GetStringElementCount(szString);
80
81 if (szString + uiElementCount < m_Data.GetData() || szString >= m_Data.GetData() + m_Data.GetCount())
82 {
83 // source string is outside our own memory, so no overlapped copy
84 }
85 else
86 {
87 // source string overlaps with our own memory -> we can't increase the size of our memory, as that might invalidate the source data
88 PL_ASSERT_DEBUG(uiElementCount < m_Data.GetCount(), "Invalid copy of overlapping string data.");
89 }
90
91 m_Data.SetCountUninitialized(uiElementCount + 1);
92 plStringUtils::Copy(&m_Data[0], uiElementCount + 1, szString);
93}
94
95template <plUInt16 Size>
97{
98 if (this == &rhs)
99 return;
100
101 m_Data = rhs.m_Data;
102}
103
104template <plUInt16 Size>
106{
107 if (this == &rhs)
108 return;
109
110 m_Data = std::move(rhs.m_Data);
111}
112
113template <plUInt16 Size>
114void plHybridStringBase<Size>::operator=(const wchar_t* szString)
115{
116 plStringUtf8 sConversion(szString, m_Data.GetAllocator());
117 *this = sConversion.GetData();
118}
119
120template <plUInt16 Size>
122{
123 PL_ASSERT_DEBUG(rhs.GetStartPointer() < m_Data.GetData() || rhs.GetStartPointer() >= m_Data.GetData() + m_Data.GetCount(),
124 "Can't assign string a value that points to ourself!");
125
126 m_Data.SetCountUninitialized(rhs.GetElementCount() + 1);
127 plStringUtils::Copy(&m_Data[0], m_Data.GetCount(), rhs.GetStartPointer(), rhs.GetEndPointer());
128}
129
130template <plUInt16 Size>
131plStringView plHybridStringBase<Size>::GetSubString(plUInt32 uiFirstCharacter, plUInt32 uiNumCharacters) const
132{
133 const char* szStart = GetData();
134 if (plUnicodeUtils::MoveToNextUtf8(szStart, uiFirstCharacter).Failed())
135 return {}; // szStart was moved too far, the result is just an empty string
136
137 const char* szEnd = szStart;
138 plUnicodeUtils::MoveToNextUtf8(szEnd, uiNumCharacters).IgnoreResult(); // if it fails, szEnd just points to the end of this string
139
140 return plStringView(szStart, szEnd);
141}
142
143template <plUInt16 Size>
145{
146 return GetSubString(0, uiNumCharacters);
147}
148
149template <plUInt16 Size>
150plStringView plHybridStringBase<Size>::GetLast(plUInt32 uiNumCharacters) const
151{
152 const plUInt32 uiMaxCharacterCount = GetCharacterCount();
153 PL_ASSERT_DEV(uiNumCharacters < uiMaxCharacterCount, "The string only contains {0} characters, cannot return the last {1} characters.",
154 uiMaxCharacterCount, uiNumCharacters);
155 return GetSubString(uiMaxCharacterCount - uiNumCharacters, uiNumCharacters);
156}
157
158
159template <plUInt16 Size, typename A>
161 : plHybridStringBase<Size>(A::GetAllocator())
162{
163}
164
165template <plUInt16 Size, typename A>
166PL_ALWAYS_INLINE plHybridString<Size, A>::plHybridString(plAllocator* pAllocator)
167 : plHybridStringBase<Size>(pAllocator)
168{
169}
170
171template <plUInt16 Size, typename A>
173 : plHybridStringBase<Size>(other, A::GetAllocator())
174{
175}
176
177template <plUInt16 Size, typename A>
179 : plHybridStringBase<Size>(other, A::GetAllocator())
180{
181}
182
183template <plUInt16 Size, typename A>
185 : plHybridStringBase<Size>(std::move(other), A::GetAllocator())
186{
187}
188
189template <plUInt16 Size, typename A>
191 : plHybridStringBase<Size>(std::move(other), A::GetAllocator())
192{
193}
194
195template <plUInt16 Size, typename A>
196PL_ALWAYS_INLINE plHybridString<Size, A>::plHybridString(const char* rhs)
197 : plHybridStringBase<Size>(rhs, A::GetAllocator())
198{
199}
200
201template <plUInt16 Size, typename A>
202PL_ALWAYS_INLINE plHybridString<Size, A>::plHybridString(const wchar_t* rhs)
203 : plHybridStringBase<Size>(rhs, A::GetAllocator())
204{
205}
206
207template <plUInt16 Size, typename A>
208PL_ALWAYS_INLINE plHybridString<Size, A>::plHybridString(const plStringView& rhs)
209 : plHybridStringBase<Size>(rhs, A::GetAllocator())
210{
211}
212
213template <plUInt16 Size, typename A>
214PL_ALWAYS_INLINE void plHybridString<Size, A>::operator=(const plHybridString<Size, A>& rhs)
215{
217}
218
219template <plUInt16 Size, typename A>
220PL_ALWAYS_INLINE void plHybridString<Size, A>::operator=(const plHybridStringBase<Size>& rhs)
221{
223}
224
225template <plUInt16 Size, typename A>
227{
229}
230
231template <plUInt16 Size, typename A>
233{
235}
236
237template <plUInt16 Size, typename A>
238PL_ALWAYS_INLINE void plHybridString<Size, A>::operator=(const char* rhs)
239{
241}
242
243template <plUInt16 Size, typename A>
244PL_ALWAYS_INLINE void plHybridString<Size, A>::operator=(const wchar_t* rhs)
245{
247}
248
249template <plUInt16 Size, typename A>
250PL_ALWAYS_INLINE void plHybridString<Size, A>::operator=(const plStringView& rhs)
251{
253}
254
255#if PL_ENABLED(PL_INTEROP_STL_STRINGS)
256
257template <plUInt16 Size>
258plHybridStringBase<Size>::plHybridStringBase(const std::string_view& rhs, plAllocator* pAllocator)
259{
260 *this = rhs;
261}
262
263template <plUInt16 Size>
264plHybridStringBase<Size>::plHybridStringBase(const std::string& rhs, plAllocator* pAllocator)
265{
266 *this = rhs;
267}
268
269template <plUInt16 Size>
270void plHybridStringBase<Size>::operator=(const std::string_view& rhs)
271{
272 if (rhs.empty())
273 {
274 Clear();
275 }
276 else
277 {
278 m_Data.SetCountUninitialized(((plUInt32)rhs.size() + 1));
279 plStringUtils::Copy(&m_Data[0], m_Data.GetCount(), rhs.data(), rhs.data() + rhs.size());
280 }
281}
282
283template <plUInt16 Size>
284void plHybridStringBase<Size>::operator=(const std::string& rhs)
285{
286 *this = std::string_view(rhs);
287}
288
289template <plUInt16 Size, typename A>
290PL_ALWAYS_INLINE plHybridString<Size, A>::plHybridString(const std::string_view& rhs)
291 : plHybridStringBase<Size>(rhs, A::GetAllocator())
292{
293}
294
295template <plUInt16 Size, typename A>
296PL_ALWAYS_INLINE plHybridString<Size, A>::plHybridString(const std::string& rhs)
297 : plHybridStringBase<Size>(rhs, A::GetAllocator())
298{
299}
300
301template <plUInt16 Size, typename A>
302PL_ALWAYS_INLINE void plHybridString<Size, A>::operator=(const std::string_view& rhs)
303{
305}
306
307template <plUInt16 Size, typename A>
308PL_ALWAYS_INLINE void plHybridString<Size, A>::operator=(const std::string& rhs)
309{
311}
312
313#endif
314
315#include <Foundation/Strings/Implementation/AllStrings_inl.h>
Base class for all memory allocators.
Definition Allocator.h:23
A small string class that converts any other encoding to Utf8.
Definition StringConversion.h:47
static constexpr plUInt32 GetStringElementCount(const T *pString)
Returns the number of elements of type T that the string contains, until it hits an element that is z...
Definition StringUtils_inl.h:45
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 Copy(char *szDest, plUInt32 uiDstSize, const char *szSource, const char *pSourceEnd=plUnicodeUtils::GetMaxStringEnd< char >())
Copies the string from szSource into the given buffer, which can hold at least uiDstSize bytes.
Definition StringUtils.cpp:362
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
plUInt32 GetElementCount() const
Returns the number of bytes from the start position up to its end.
Definition StringView.h:93
const char * GetEndPointer() const
Returns the end of the view range. This will point to the byte AFTER the last character.
Definition StringView.h:108
const char * GetStartPointer() const
Returns the start of the view range.
Definition StringView.h:102
static plResult MoveToNextUtf8(const char *&ref_szUtf8, plUInt32 uiNumCharacters=1)
Moves the given string pointer ahead to the next Utf8 character sequence.
Definition UnicodeUtils_inl.h:201
A string class for storing and passing around strings.
Definition String.h:28
plHybridStringBase(plAllocator *pAllocator)
Creates an empty string.
Definition String_inl.h:4
const char * GetData() const
Returns a pointer to the internal Utf8 string.
Definition String_inl.h:56
plUInt32 GetElementCount() const
Returns the amount of bytes that this string takes (excluding the '\0' terminator).
Definition String_inl.h:65
plStringView GetFirst(plUInt32 uiNumCharacters) const
Returns a view to the sub-string containing the first uiNumCharacters characters of this string.
Definition String_inl.h:144
plUInt32 GetCharacterCount() const
Returns the number of characters in this string. Might be less than GetElementCount,...
Definition String_inl.h:71
~plHybridStringBase()
Destructor.
plStringView GetSubString(plUInt32 uiFirstCharacter, plUInt32 uiNumCharacters) const
Returns a view to a sub-string of this string, starting at character uiFirstCharacter,...
Definition String_inl.h:131
void Clear()
Resets this string to an empty string.
Definition String_inl.h:49
void operator=(const plHybridStringBase &rhs)
Copies the data from rhs.
Definition String_inl.h:96
plStringView GetLast(plUInt32 uiNumCharacters) const
Returns a view to the sub-string containing the last uiNumCharacters characters of this string.
Definition String_inl.h:150
Definition String.h:146
PL_ALWAYS_INLINE void IgnoreResult()
Used to silence compiler warnings, when success or failure doesn't matter.
Definition Types.h:69