Plasma Engine  2.0
Loading...
Searching...
No Matches
StringView.h
1#pragma once
2
3#ifndef PL_INCLUDING_BASICS_H
4# error "Please don't include StringView.h directly, but instead include Foundation/Basics.h"
5#endif
6
7#include <Foundation/Strings/StringUtils.h>
8
9#include <Foundation/Strings/Implementation/StringIterator.h>
10
11#include <type_traits>
12
13#if PL_ENABLED(PL_INTEROP_STL_STRINGS)
14# include <string_view>
15#endif
16
19{
20};
21
22class plStringBuilder;
23
33class PL_FOUNDATION_DLL plStringView : public plThisIsAString
34{
35public:
36 PL_DECLARE_POD_TYPE();
37
42
44 constexpr plStringView();
45
47 plStringView(char* pStart);
48
50 template <typename T> // T is always const char*
51 constexpr plStringView(T pStart, typename std::enable_if<std::is_same<T, const char*>::value, int>::type* = 0); // [tested]
52
54 template <typename T>
55 constexpr PL_ALWAYS_INLINE plStringView(const T&& str, typename std::enable_if<std::is_same<T, const char*>::value == false && std::is_convertible<T, const char*>::value, int>::type* = 0); // [tested]
56
58 constexpr plStringView(const char* pStart, const char* pEnd); // [tested]
59
61 constexpr plStringView(const char* pStart, plUInt32 uiLength);
62
64 template <size_t N>
65 constexpr plStringView(const char (&str)[N]);
66
68 template <size_t N>
69 constexpr plStringView(char (&str)[N]);
70
72 void operator++(); // [tested]
73
75 void operator+=(plUInt32 d); // [tested]
76
78 plUInt32 GetCharacter() const; // [tested]
79
81 bool IsValid() const; // [tested]
82
88 const char* GetData(plStringBuilder& ref_sTempStorage) const; // [tested]
89
93 plUInt32 GetElementCount() const { return m_uiElementCount; } // [tested]
94
98 void SetStartPosition(const char* szCurPos); // [tested]
99
102 const char* GetStartPointer() const { return m_pStart; } // [tested]
103
108 const char* GetEndPointer() const { return m_pStart + m_uiElementCount; } // [tested]
109
111 bool IsEmpty() const; // [tested]
112
114 bool IsEqual(plStringView sOther) const;
115
117 bool IsEqual_NoCase(plStringView sOther) const;
118
120 bool IsEqualN(plStringView sOther, plUInt32 uiCharsToCompare) const; // [tested]
121
123 bool IsEqualN_NoCase(plStringView sOther, plUInt32 uiCharsToCompare) const; // [tested]
124
126 plInt32 Compare(plStringView sOther) const; // [tested]
127
130 plInt32 CompareN(plStringView sOther, plUInt32 uiCharsToCompare) const; // [tested]
131
133 plInt32 Compare_NoCase(plStringView sOther) const; // [tested]
134
137 plInt32 CompareN_NoCase(plStringView sOther, plUInt32 uiCharsToCompare) const; // [tested]
138
140 bool StartsWith(plStringView sStartsWith) const; // [tested]
141
143 bool StartsWith_NoCase(plStringView sStartsWith) const; // [tested]
144
146 bool EndsWith(plStringView sEndsWith) const; // [tested]
147
149 bool EndsWith_NoCase(plStringView sEndsWith) const; // [tested]
150
152 const char* ComputeCharacterPosition(plUInt32 uiCharacterIndex) const;
153
156 const char* FindSubString(plStringView sStringToFind, const char* szStartSearchAt = nullptr) const; // [tested]
157
160 const char* FindSubString_NoCase(plStringView sStringToFind, const char* szStartSearchAt = nullptr) const; // [tested]
161
164 const char* FindLastSubString(plStringView sStringToFind, const char* szStartSearchAt = nullptr) const; // [tested]
165
168 const char* FindLastSubString_NoCase(plStringView sStringToFind, const char* szStartSearchAt = nullptr) const; // [tested]
169
172 const char* FindWholeWord(const char* szSearchFor, plStringUtils::PL_CHARACTER_FILTER isDelimiterCB, const char* szStartSearchAt = nullptr) const; // [tested]
173
176 const char* FindWholeWord_NoCase(const char* szSearchFor, plStringUtils::PL_CHARACTER_FILTER isDelimiterCB, const char* szStartSearchAt = nullptr) const; // [tested]
177
178
186 void Shrink(plUInt32 uiShrinkCharsFront, plUInt32 uiShrinkCharsBack); // [tested]
187
189 plStringView GetShrunk(plUInt32 uiShrinkCharsFront, plUInt32 uiShrinkCharsBack = 0) const; // [tested]
190
196 plStringView GetSubString(plUInt32 uiFirstCharacter, plUInt32 uiNumCharacters) const; // [tested]
197
199 void ChopAwayFirstCharacterUtf8(); // [tested]
200
204 void ChopAwayFirstCharacterAscii(); // [tested]
205
207 void Trim(const char* szTrimChars); // [tested]
208
210 void Trim(const char* szTrimCharsStart, const char* szTrimCharsEnd); // [tested]
211
213 bool TrimWordStart(plStringView sWord); // [tested]
214
216 bool TrimWordEnd(plStringView sWord); // [tested]
217
223 template <typename Container>
224 void Split(bool bReturnEmptyStrings, Container& ref_output, const char* szSeparator1, const char* szSeparator2 = nullptr, const char* szSeparator3 = nullptr, const char* szSeparator4 = nullptr, const char* szSeparator5 = nullptr, const char* szSeparator6 = nullptr) const; // [tested]
225
230 iterator GetIteratorFront() const;
231
236 reverse_iterator GetIteratorBack() const;
237
238 // ******* Path Functions ********
239
241 bool HasAnyExtension() const; // [tested]
242
246 bool HasExtension(plStringView sExtension) const; // [tested]
247
252 plStringView GetFileExtension(bool bFullExtension = false) const; // [tested]
253
257 plStringView GetFileName() const; // [tested]
258
262 plStringView GetFileNameAndExtension() const; // [tested]
263
271 plStringView GetFileDirectory() const; // [tested]
272
274 bool IsAbsolutePath() const; // [tested]
275
277 bool IsRelativePath() const; // [tested]
278
280 bool IsRootedPath() const; // [tested]
281
289 plStringView GetRootedPathRootName() const; // [tested]
290
291#if PL_ENABLED(PL_INTEROP_STL_STRINGS)
293 plStringView(const std::string_view& rhs);
294
296 plStringView(const std::string& rhs);
297
299 operator std::string_view() const;
300
302 std::string_view GetAsStdView() const;
303#endif
304
305private:
306 const char* m_pStart = nullptr;
307 plUInt32 m_uiElementCount = 0;
308};
309
314constexpr plStringView operator"" _plsv(const char* pString, size_t uiLen);
315
316PL_ALWAYS_INLINE typename plStringView::iterator begin(plStringView sContainer)
317{
318 return typename plStringView::iterator(sContainer.GetStartPointer(), sContainer.GetEndPointer(), sContainer.GetStartPointer());
319}
320
321PL_ALWAYS_INLINE typename plStringView::const_iterator cbegin(plStringView sContainer)
322{
323 return typename plStringView::const_iterator(sContainer.GetStartPointer(), sContainer.GetEndPointer(), sContainer.GetStartPointer());
324}
325
326PL_ALWAYS_INLINE typename plStringView::iterator end(plStringView sContainer)
327{
328 return typename plStringView::iterator(sContainer.GetStartPointer(), sContainer.GetEndPointer(), sContainer.GetEndPointer());
329}
330
331PL_ALWAYS_INLINE typename plStringView::const_iterator cend(plStringView sContainer)
332{
333 return typename plStringView::const_iterator(sContainer.GetStartPointer(), sContainer.GetEndPointer(), sContainer.GetEndPointer());
334}
335
336
337PL_ALWAYS_INLINE typename plStringView::reverse_iterator rbegin(plStringView sContainer)
338{
339 return typename plStringView::reverse_iterator(sContainer.GetStartPointer(), sContainer.GetEndPointer(), sContainer.GetEndPointer());
340}
341
342PL_ALWAYS_INLINE typename plStringView::const_reverse_iterator crbegin(plStringView sContainer)
343{
344 return typename plStringView::const_reverse_iterator(sContainer.GetStartPointer(), sContainer.GetEndPointer(), sContainer.GetEndPointer());
345}
346
347PL_ALWAYS_INLINE typename plStringView::reverse_iterator rend(plStringView sContainer)
348{
349 return typename plStringView::reverse_iterator(sContainer.GetStartPointer(), sContainer.GetEndPointer(), nullptr);
350}
351
352PL_ALWAYS_INLINE typename plStringView::const_reverse_iterator crend(plStringView sContainer)
353{
354 return typename plStringView::const_reverse_iterator(sContainer.GetStartPointer(), sContainer.GetEndPointer(), nullptr);
355}
356
357#include <Foundation/Strings/Implementation/StringView_inl.h>
plStringBuilder is a class that is meant for creating and modifying strings.
Definition StringBuilder.h:35
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
plUInt32 GetElementCount() const
Returns the number of bytes from the start position up to its end.
Definition StringView.h:93
constexpr plStringView(T pStart, typename std::enable_if< std::is_same< T, const char * >::value, int >::type *=0)
Creates a string view starting at the given position, ending at the next '\0' terminator.
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
constexpr plStringView(char(&str)[N])
Construct a string view from a fixed size buffer.
const char * GetStartPointer() const
Returns the start of the view range.
Definition StringView.h:102
constexpr plStringView()
Default constructor creates an invalid view.
constexpr plStringView(const char(&str)[N])
Construct a string view from a string literal.
STL forward iterator used by all string classes. Iterates over unicode characters....
Definition StringIterator.h:10
STL reverse iterator used by all string classes. Iterates over unicode characters....
Definition StringIterator.h:163
Base class which marks a class as containing string data.
Definition StringView.h:19