Plasma Engine  2.0
Loading...
Searching...
No Matches
StringIterator.h
1#pragma once
2
3#ifndef PL_INCLUDING_BASICS_H
4# error "Please don't include StringIterator.h directly, but instead include Foundation/Basics.h"
5#endif
6
10{
11 using iterator_category = std::bidirectional_iterator_tag;
12 using value_type = plUInt32;
13 using difference_type = std::ptrdiff_t;
14 using pointer = const char*;
15 using reference = plUInt32;
16
17 PL_DECLARE_POD_TYPE();
18
20 PL_ALWAYS_INLINE plStringIterator() = default; // [tested]
21
23 PL_FORCE_INLINE explicit plStringIterator(const char* pStartPtr, const char* pEndPtr, const char* pCurPtr)
24 {
25 m_pStartPtr = pStartPtr;
26 m_pEndPtr = pEndPtr;
27 m_pCurPtr = pCurPtr;
28 }
29
31 PL_ALWAYS_INLINE bool IsValid() const { return m_pCurPtr != nullptr && m_pCurPtr != m_pEndPtr; } // [tested]
32
34 PL_ALWAYS_INLINE plUInt32 GetCharacter() const { return IsValid() ? plUnicodeUtils::ConvertUtf8ToUtf32(m_pCurPtr) : plUInt32(0); } // [tested]
35
37 PL_ALWAYS_INLINE plUInt32 operator*() const { return GetCharacter(); } // [tested]
38
40 PL_ALWAYS_INLINE const char* GetData() const { return m_pCurPtr; } // [tested]
41
43 PL_ALWAYS_INLINE bool operator==(const plStringIterator& it2) const { return (m_pCurPtr == it2.m_pCurPtr); } // [tested]
44 PL_ADD_DEFAULT_OPERATOR_NOTEQUAL(const plStringIterator&);
45
47 PL_ALWAYS_INLINE plUInt32 Advance()
48 {
49 const char* pPrevElement = m_pCurPtr;
50
51 if (m_pCurPtr < m_pEndPtr)
52 {
54 }
55
56 return static_cast<plUInt32>(m_pCurPtr - pPrevElement);
57 }
58
60 PL_ALWAYS_INLINE plStringIterator& operator++() // [tested]
61 {
62 if (m_pCurPtr < m_pEndPtr)
63 {
65 }
66
67 return *this;
68 }
69
71 PL_ALWAYS_INLINE plStringIterator& operator--() // [tested]
72 {
73 if (m_pStartPtr < m_pCurPtr)
74 {
75 plUnicodeUtils::MoveToPriorUtf8(m_pCurPtr, m_pStartPtr).AssertSuccess();
76 }
77
78 return *this;
79 }
80
82 PL_ALWAYS_INLINE plStringIterator operator++(int) // [tested]
83 {
84 plStringIterator tmp = *this;
85 ++(*this);
86 return tmp;
87 }
88
90 PL_ALWAYS_INLINE plStringIterator operator--(int) // [tested]
91 {
92 plStringIterator tmp = *this;
93 --(*this);
94 return tmp;
95 }
96
98 PL_FORCE_INLINE void operator+=(difference_type d) // [tested]
99 {
100 while (d > 0)
101 {
102 ++(*this);
103 --d;
104 }
105 while (d < 0)
106 {
107 --(*this);
108 ++d;
109 }
110 }
111
113 PL_FORCE_INLINE void operator-=(difference_type d) // [tested]
114 {
115 while (d > 0)
116 {
117 --(*this);
118 --d;
119 }
120 while (d < 0)
121 {
122 ++(*this);
123 ++d;
124 }
125 }
126
128 PL_ALWAYS_INLINE plStringIterator operator+(difference_type d) const // [tested]
129 {
130 plStringIterator it = *this;
131 it += d;
132 return it;
133 }
134
136 PL_ALWAYS_INLINE plStringIterator operator-(difference_type d) const // [tested]
137 {
138 plStringIterator it = *this;
139 it -= d;
140 return it;
141 }
142
146 void SetCurrentPosition(const char* szCurPos)
147 {
148 PL_ASSERT_DEV((szCurPos >= m_pStartPtr) && (szCurPos <= m_pEndPtr), "New position must still be inside the iterator's range.");
149
150 m_pCurPtr = szCurPos;
151 }
152
153private:
154 const char* m_pStartPtr = nullptr;
155 const char* m_pEndPtr = nullptr;
156 const char* m_pCurPtr = nullptr;
157};
158
159
163{
164 using iterator_category = std::bidirectional_iterator_tag;
165 using value_type = plUInt32;
166 using difference_type = std::ptrdiff_t;
167 using pointer = const char*;
168 using reference = plUInt32;
169
170 PL_DECLARE_POD_TYPE();
171
173 PL_ALWAYS_INLINE plStringReverseIterator() = default; // [tested]
174
176 PL_FORCE_INLINE explicit plStringReverseIterator(const char* pStartPtr, const char* pEndPtr, const char* pCurPtr) // [tested]
177 {
178 m_pStartPtr = pStartPtr;
179 m_pEndPtr = pEndPtr;
180 m_pCurPtr = pCurPtr;
181
182 if (m_pStartPtr >= m_pEndPtr)
183 {
184 m_pCurPtr = nullptr;
185 }
186 else if (m_pCurPtr == m_pEndPtr)
187 {
188 plUnicodeUtils::MoveToPriorUtf8(m_pCurPtr, m_pStartPtr).AssertSuccess();
189 }
190 }
191
193 PL_ALWAYS_INLINE bool IsValid() const { return (m_pCurPtr != nullptr); } // [tested]
194
196 PL_ALWAYS_INLINE plUInt32 GetCharacter() const { return IsValid() ? plUnicodeUtils::ConvertUtf8ToUtf32(m_pCurPtr) : plUInt32(0); } // [tested]
197
199 PL_ALWAYS_INLINE plUInt32 operator*() const { return GetCharacter(); } // [tested]
200
202 PL_ALWAYS_INLINE const char* GetData() const { return m_pCurPtr; } // [tested]
203
205 PL_ALWAYS_INLINE bool operator==(const plStringReverseIterator& it2) const { return (m_pCurPtr == it2.m_pCurPtr); } // [tested]
206 PL_ADD_DEFAULT_OPERATOR_NOTEQUAL(const plStringReverseIterator&);
207
209 PL_FORCE_INLINE plStringReverseIterator& operator++() // [tested]
210 {
211 if (m_pCurPtr != nullptr && m_pStartPtr < m_pCurPtr)
212 plUnicodeUtils::MoveToPriorUtf8(m_pCurPtr, m_pStartPtr).AssertSuccess();
213 else
214 m_pCurPtr = nullptr;
215
216 return *this;
217 }
218
220 PL_FORCE_INLINE plStringReverseIterator& operator--() // [tested]
221 {
222 if (m_pCurPtr != nullptr)
223 {
224 const char* szOldPos = m_pCurPtr;
226
227 if (m_pCurPtr == m_pEndPtr)
228 m_pCurPtr = szOldPos;
229 }
230 else
231 {
232 // Set back to the first character.
233 m_pCurPtr = m_pStartPtr;
234 }
235 return *this;
236 }
237
239 PL_ALWAYS_INLINE plStringReverseIterator operator++(int) // [tested]
240 {
241 plStringReverseIterator tmp = *this;
242 ++(*this);
243 return tmp;
244 }
245
247 PL_ALWAYS_INLINE plStringReverseIterator operator--(int) // [tested]
248 {
249 plStringReverseIterator tmp = *this;
250 --(*this);
251 return tmp;
252 }
253
255 PL_FORCE_INLINE void operator+=(difference_type d) // [tested]
256 {
257 while (d > 0)
258 {
259 ++(*this);
260 --d;
261 }
262 while (d < 0)
263 {
264 --(*this);
265 ++d;
266 }
267 }
268
270 PL_FORCE_INLINE void operator-=(difference_type d) // [tested]
271 {
272 while (d > 0)
273 {
274 --(*this);
275 --d;
276 }
277 while (d < 0)
278 {
279 ++(*this);
280 ++d;
281 }
282 }
283
285 PL_ALWAYS_INLINE plStringReverseIterator operator+(difference_type d) const // [tested]
286 {
287 plStringReverseIterator it = *this;
288 it += d;
289 return it;
290 }
291
293 PL_ALWAYS_INLINE plStringReverseIterator operator-(difference_type d) const // [tested]
294 {
295 plStringReverseIterator it = *this;
296 it -= d;
297 return it;
298 }
299
303 PL_FORCE_INLINE void SetCurrentPosition(const char* szCurPos)
304 {
305 PL_ASSERT_DEV((szCurPos == nullptr) || ((szCurPos >= m_pStartPtr) && (szCurPos < m_pEndPtr)), "New position must still be inside the iterator's range.");
306
307 m_pCurPtr = szCurPos;
308 }
309
310private:
311 const char* m_pStartPtr = nullptr;
312 const char* m_pEndPtr = nullptr;
313 const char* m_pCurPtr = nullptr;
314};
static plUInt32 ConvertUtf8ToUtf32(const char *pFirstChar)
Converts the UTF-8 character that starts at pFirstChar into a UTF-32 character.
Definition UnicodeUtils_inl.h:124
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
static plResult MoveToPriorUtf8(const char *&ref_szUtf8, const char *szUtf8Start, plUInt32 uiNumCharacters=1)
Moves the given string pointer backwards to the previous Utf8 character sequence.
Definition UnicodeUtils_inl.h:241
void AssertSuccess(const char *szMsg=nullptr, const char *szDetails=nullptr) const
Asserts that the function succeeded. In case of failure, the program will terminate.
Definition Status.cpp:7
STL forward iterator used by all string classes. Iterates over unicode characters....
Definition StringIterator.h:10
PL_ALWAYS_INLINE plStringIterator operator-(difference_type d) const
Returns an iterator that is advanced backwards by d characters.
Definition StringIterator.h:136
PL_ALWAYS_INLINE const char * GetData() const
Returns the address the iterator currently points to.
Definition StringIterator.h:40
PL_ALWAYS_INLINE plUInt32 operator*() const
Returns the currently pointed to character in Utf32 encoding.
Definition StringIterator.h:37
PL_ALWAYS_INLINE plUInt32 Advance()
Advances the iterated to the next character, same as operator++, but returns how many bytes were cons...
Definition StringIterator.h:47
PL_ALWAYS_INLINE plStringIterator operator+(difference_type d) const
Returns an iterator that is advanced forwards by d characters.
Definition StringIterator.h:128
PL_ALWAYS_INLINE plUInt32 GetCharacter() const
Returns the currently pointed to character in Utf32 encoding.
Definition StringIterator.h:34
PL_ALWAYS_INLINE plStringIterator & operator++()
Move to the next Utf8 character.
Definition StringIterator.h:60
PL_ALWAYS_INLINE plStringIterator operator--(int)
Move to the previous Utf8 character.
Definition StringIterator.h:90
PL_ALWAYS_INLINE plStringIterator()=default
Constructs an invalid iterator.
void SetCurrentPosition(const char *szCurPos)
Allows to set the 'current' iteration position to a different value.
Definition StringIterator.h:146
PL_FORCE_INLINE plStringIterator(const char *pStartPtr, const char *pEndPtr, const char *pCurPtr)
Constructs either a begin or end iterator for the given string.
Definition StringIterator.h:23
PL_ALWAYS_INLINE bool operator==(const plStringIterator &it2) const
Checks whether the two iterators point to the same element.
Definition StringIterator.h:43
PL_FORCE_INLINE void operator-=(difference_type d)
Moves the iterator backwards by d characters. Does not move it beyond the range's start.
Definition StringIterator.h:113
PL_ALWAYS_INLINE bool IsValid() const
Checks whether this iterator points to a valid element. Invalid iterators either point to m_pEndPtr o...
Definition StringIterator.h:31
PL_ALWAYS_INLINE plStringIterator & operator--()
Move to the previous Utf8 character.
Definition StringIterator.h:71
PL_ALWAYS_INLINE plStringIterator operator++(int)
Move to the next Utf8 character.
Definition StringIterator.h:82
PL_FORCE_INLINE void operator+=(difference_type d)
Advances the iterator forwards by d characters. Does not move it beyond the range's end.
Definition StringIterator.h:98
STL reverse iterator used by all string classes. Iterates over unicode characters....
Definition StringIterator.h:163
PL_ALWAYS_INLINE plUInt32 GetCharacter() const
Returns the currently pointed to character in Utf32 encoding.
Definition StringIterator.h:196
PL_ALWAYS_INLINE plStringReverseIterator operator-(difference_type d) const
Returns an iterator that is advanced backwards by d characters.
Definition StringIterator.h:293
PL_FORCE_INLINE plStringReverseIterator & operator++()
Move to the next Utf8 character.
Definition StringIterator.h:209
PL_ALWAYS_INLINE bool operator==(const plStringReverseIterator &it2) const
Checks whether the two iterators point to the same element.
Definition StringIterator.h:205
PL_ALWAYS_INLINE plStringReverseIterator operator+(difference_type d) const
Returns an iterator that is advanced forwards by d characters.
Definition StringIterator.h:285
PL_FORCE_INLINE void SetCurrentPosition(const char *szCurPos)
Allows to set the 'current' iteration position to a different value.
Definition StringIterator.h:303
PL_ALWAYS_INLINE plUInt32 operator*() const
Returns the currently pointed to character in Utf32 encoding.
Definition StringIterator.h:199
PL_ALWAYS_INLINE plStringReverseIterator operator--(int)
Move to the previous Utf8 character.
Definition StringIterator.h:247
PL_FORCE_INLINE void operator+=(difference_type d)
Advances the iterator forwards by d characters. Does not move it beyond the range's end.
Definition StringIterator.h:255
PL_FORCE_INLINE void operator-=(difference_type d)
Moves the iterator backwards by d characters. Does not move it beyond the range's start.
Definition StringIterator.h:270
PL_ALWAYS_INLINE const char * GetData() const
Returns the address the iterator currently points to.
Definition StringIterator.h:202
PL_ALWAYS_INLINE plStringReverseIterator()=default
Constructs an invalid iterator.
PL_ALWAYS_INLINE bool IsValid() const
Checks whether this iterator points to a valid element.
Definition StringIterator.h:193
PL_FORCE_INLINE plStringReverseIterator & operator--()
Move to the previous Utf8 character.
Definition StringIterator.h:220
PL_FORCE_INLINE plStringReverseIterator(const char *pStartPtr, const char *pEndPtr, const char *pCurPtr)
Constructs either a rbegin or rend iterator for the given string.
Definition StringIterator.h:176
PL_ALWAYS_INLINE plStringReverseIterator operator++(int)
Move to the next Utf8 character.
Definition StringIterator.h:239