Plasma Engine  2.0
Loading...
Searching...
No Matches
StringView_inl.h
1#pragma once
2
3PL_ALWAYS_INLINE constexpr plStringView::plStringView() = default;
4
5PL_ALWAYS_INLINE plStringView::plStringView(char* pStart)
6 : m_pStart(pStart)
7 , m_uiElementCount(plStringUtils::GetStringElementCount(pStart))
8{
9}
10
11template <typename T>
12constexpr PL_ALWAYS_INLINE plStringView::plStringView(T pStart, typename std::enable_if<std::is_same<T, const char*>::value, int>::type*)
13 : m_pStart(pStart)
14 , m_uiElementCount(plStringUtils::GetStringElementCount(pStart))
15{
16}
17
18template <typename T>
19constexpr PL_ALWAYS_INLINE plStringView::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*)
20{
21 m_pStart = str;
22 m_uiElementCount = plStringUtils::GetStringElementCount(m_pStart);
23}
24
25constexpr PL_ALWAYS_INLINE plStringView::plStringView(const char* pStart, const char* pEnd)
26{
27 PL_ASSERT_DEBUG(pStart <= pEnd, "Invalid pointers to construct a string view from.");
28
29 m_pStart = pStart;
30 m_uiElementCount = static_cast<plUInt32>(pEnd - pStart);
31}
32
33constexpr PL_ALWAYS_INLINE plStringView::plStringView(const char* pStart, plUInt32 uiLength)
34 : m_pStart(pStart)
35 , m_uiElementCount(uiLength)
36{
37}
38
39template <size_t N>
40constexpr PL_ALWAYS_INLINE plStringView::plStringView(const char (&str)[N])
41 : m_pStart(str)
42 , m_uiElementCount(N - 1)
43{
44 static_assert(N > 0, "Not a string literal");
45}
46
47template <size_t N>
48constexpr PL_ALWAYS_INLINE plStringView::plStringView(char (&str)[N])
49{
50 m_pStart = str;
51 m_uiElementCount = plStringUtils::GetStringElementCount(str, str + N);
52}
53
55{
56 if (!IsValid())
57 return;
58
59 const char* pEnd = m_pStart + m_uiElementCount;
60 plUnicodeUtils::MoveToNextUtf8(m_pStart, pEnd).IgnoreResult(); // if it fails, the string is just empty
61 m_uiElementCount = static_cast<plUInt32>(pEnd - m_pStart);
62}
63
64inline void plStringView::operator+=(plUInt32 d)
65{
66 const char* pEnd = m_pStart + m_uiElementCount;
67 plUnicodeUtils::MoveToNextUtf8(m_pStart, pEnd, d).IgnoreResult(); // if it fails, the string is just empty
68 m_uiElementCount = static_cast<plUInt32>(pEnd - m_pStart);
69}
70
71PL_ALWAYS_INLINE bool plStringView::IsValid() const
72{
73 return (m_pStart != nullptr) && (m_uiElementCount > 0);
74}
75
76PL_ALWAYS_INLINE void plStringView::SetStartPosition(const char* szCurPos)
77{
78 PL_ASSERT_DEV((szCurPos >= m_pStart) && (szCurPos <= m_pStart + m_uiElementCount), "New start position must still be inside the view's range.");
79
80 const char* pEnd = m_pStart + m_uiElementCount;
81 m_pStart = szCurPos;
82 m_uiElementCount = static_cast<plUInt32>(pEnd - m_pStart);
83}
84
85PL_ALWAYS_INLINE bool plStringView::IsEmpty() const
86{
87 return m_uiElementCount == 0;
88}
89
90PL_ALWAYS_INLINE bool plStringView::IsEqual(plStringView sOther) const
91{
92 return plStringUtils::IsEqual(m_pStart, sOther.GetStartPointer(), m_pStart + m_uiElementCount, sOther.GetEndPointer());
93}
94
95PL_ALWAYS_INLINE bool plStringView::IsEqual_NoCase(plStringView sOther) const
96{
97 return plStringUtils::IsEqual_NoCase(m_pStart, sOther.GetStartPointer(), m_pStart + m_uiElementCount, sOther.GetEndPointer());
98}
99
100PL_ALWAYS_INLINE bool plStringView::StartsWith(plStringView sStartsWith) const
101{
102 return plStringUtils::StartsWith(m_pStart, sStartsWith.GetStartPointer(), m_pStart + m_uiElementCount, sStartsWith.GetEndPointer());
103}
104
105PL_ALWAYS_INLINE bool plStringView::StartsWith_NoCase(plStringView sStartsWith) const
106{
107 return plStringUtils::StartsWith_NoCase(m_pStart, sStartsWith.GetStartPointer(), m_pStart + m_uiElementCount, sStartsWith.GetEndPointer());
108}
109
110PL_ALWAYS_INLINE bool plStringView::EndsWith(plStringView sEndsWith) const
111{
112 return plStringUtils::EndsWith(m_pStart, sEndsWith.GetStartPointer(), m_pStart + m_uiElementCount, sEndsWith.GetEndPointer());
113}
114
115PL_ALWAYS_INLINE bool plStringView::EndsWith_NoCase(plStringView sEndsWith) const
116{
117 return plStringUtils::EndsWith_NoCase(m_pStart, sEndsWith.GetStartPointer(), m_pStart + m_uiElementCount, sEndsWith.GetEndPointer());
118}
119
120PL_ALWAYS_INLINE void plStringView::Trim(const char* szTrimChars)
121{
122 return Trim(szTrimChars, szTrimChars);
123}
124
125PL_ALWAYS_INLINE void plStringView::Trim(const char* szTrimCharsStart, const char* szTrimCharsEnd)
126{
127 if (IsValid())
128 {
129 const char* pEnd = m_pStart + m_uiElementCount;
130 plStringUtils::Trim(m_pStart, pEnd, szTrimCharsStart, szTrimCharsEnd);
131 m_uiElementCount = static_cast<plUInt32>(pEnd - m_pStart);
132 }
133}
134
135constexpr PL_ALWAYS_INLINE plStringView operator"" _plsv(const char* pString, size_t uiLen)
136{
137 return plStringView(pString, static_cast<plUInt32>(uiLen));
138}
139
140template <typename Container>
141void plStringView::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
142{
143 ref_output.Clear();
144
145 if (IsEmpty())
146 return;
147
148 const plUInt32 uiParams = 6;
149
150 const plStringView seps[uiParams] = {szSeparator1, szSeparator2, szSeparator3, szSeparator4, szSeparator5, szSeparator6};
151
152 const char* szReadPos = GetStartPointer();
153
154 while (true)
155 {
156 const char* szFoundPos = plUnicodeUtils::GetMaxStringEnd<char>();
157 plUInt32 uiFoundSeparator = 0;
158
159 for (plUInt32 i = 0; i < uiParams; ++i)
160 {
161 const char* szFound = plStringUtils::FindSubString(szReadPos, seps[i].GetStartPointer(), GetEndPointer(), seps[i].GetEndPointer());
162
163 if ((szFound != nullptr) && (szFound < szFoundPos))
164 {
165 szFoundPos = szFound;
166 uiFoundSeparator = i;
167 }
168 }
169
170 // nothing found
171 if (szFoundPos == plUnicodeUtils::GetMaxStringEnd<char>())
172 {
173 const plUInt32 uiLen = plStringUtils::GetStringElementCount(szReadPos, GetEndPointer());
174
175 if (bReturnEmptyStrings || (uiLen > 0))
176 ref_output.PushBack(plStringView(szReadPos, szReadPos + uiLen));
177
178 return;
179 }
180
181 if (bReturnEmptyStrings || (szFoundPos > szReadPos))
182 ref_output.PushBack(plStringView(szReadPos, szFoundPos));
183
184 szReadPos = szFoundPos + seps[uiFoundSeparator].GetElementCount();
185 }
186}
187
188PL_ALWAYS_INLINE bool operator==(plStringView lhs, plStringView rhs)
189{
190 return lhs.IsEqual(rhs);
191}
192
193#if PL_DISABLED(PL_USE_CPP20_OPERATORS)
194
195PL_ALWAYS_INLINE bool operator!=(plStringView lhs, plStringView rhs)
196{
197 return !lhs.IsEqual(rhs);
198}
199
200#endif
201
202#if PL_ENABLED(PL_USE_CPP20_OPERATORS)
203
204PL_ALWAYS_INLINE std::strong_ordering operator<=>(plStringView lhs, plStringView rhs)
205{
206 return lhs.Compare(rhs) <=> 0;
207}
208
209#else
210
211PL_ALWAYS_INLINE bool operator<(plStringView lhs, plStringView rhs)
212{
213 return lhs.Compare(rhs) < 0;
214}
215
216PL_ALWAYS_INLINE bool operator<=(plStringView lhs, plStringView rhs)
217{
218 return lhs.Compare(rhs) <= 0;
219}
220
221PL_ALWAYS_INLINE bool operator>(plStringView lhs, plStringView rhs)
222{
223 return lhs.Compare(rhs) > 0;
224}
225
226PL_ALWAYS_INLINE bool operator>=(plStringView lhs, plStringView rhs)
227{
228 return lhs.Compare(rhs) >= 0;
229}
230
231#endif
Helper functions to work with UTF-8 strings (which include pure ASCII strings)
Definition StringUtils.h:11
static bool IsEqual(const char *pString1, const char *pString2, const char *pString1End=plUnicodeUtils::GetMaxStringEnd< char >(), const char *pString2End=plUnicodeUtils::GetMaxStringEnd< char >())
Returns true, if the two given strings are identical (bitwise).
Definition StringUtils_inl.h:125
static bool IsEqual_NoCase(const char *pString1, const char *pString2, const char *pString1End=plUnicodeUtils::GetMaxStringEnd< char >(), const char *pString2End=plUnicodeUtils::GetMaxStringEnd< char >())
Returns true, if the two given strings are identical (case-insensitive).
Definition StringUtils_inl.h:136
static bool EndsWith_NoCase(const char *szString, const char *szEndsWith, const char *pStringEnd=plUnicodeUtils::GetMaxStringEnd< char >(), const char *szEndsWithEnd=plUnicodeUtils::GetMaxStringEnd< char >())
Returns true if szString ends with the string given in szEndsWith. Ignores case.
Definition StringUtils.cpp:517
static const char * FindSubString(const char *szSource, const char *szStringToFind, const char *pSourceEnd=plUnicodeUtils::GetMaxStringEnd< char >(), const char *szStringToFindEnd=plUnicodeUtils::GetMaxStringEnd< char >())
Searches for the first occurrence of szStringToFind in szSource.
Definition StringUtils.cpp:550
static bool EndsWith(const char *szString, const char *szEndsWith, const char *pStringEnd=plUnicodeUtils::GetMaxStringEnd< char >(), const char *szEndsWithEnd=plUnicodeUtils::GetMaxStringEnd< char >())
Returns true if szString ends with the string given in szEndsWith.
Definition StringUtils.cpp:501
static bool StartsWith(const char *szString, const char *szStartsWith, const char *pStringEnd=plUnicodeUtils::GetMaxStringEnd< char >(), const char *szStartsWithEnd=plUnicodeUtils::GetMaxStringEnd< char >())
Returns true if szString starts with the string given in szStartsWith.
Definition StringUtils.cpp:453
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 void Trim(const char *&ref_pString, const char *&ref_pStringEnd, const char *szTrimCharsStart, const char *szTrimCharsEnd)
Removes all characters at the start and end of the string that match the respective characters and up...
Definition StringUtils.cpp:794
static bool StartsWith_NoCase(const char *szString, const char *szStartsWith, const char *pStringEnd=plUnicodeUtils::GetMaxStringEnd< char >(), const char *szStartsWithEnd=plUnicodeUtils::GetMaxStringEnd< char >())
Returns true if szString starts with the string given in szStartsWith. Ignores case.
Definition StringUtils.cpp:477
plStringView represent a read-only sub-string of a larger string, as it can store a dedicated string ...
Definition StringView.h:34
bool StartsWith(plStringView sStartsWith) const
Returns true, if this string starts with the given string.
Definition StringView_inl.h:100
plUInt32 GetElementCount() const
Returns the number of bytes from the start position up to its end.
Definition StringView.h:93
void operator++()
Advances the start to the next character, unless the end of the range was reached.
Definition StringView_inl.h:54
void operator+=(plUInt32 d)
Advances the start forwards by d characters. Does not move it beyond the range's end.
Definition StringView_inl.h:64
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
Fills the given container with plStringView's which represent each found substring....
Definition StringView_inl.h:141
void Trim(const char *szTrimChars)
Removes all characters from the start and end that appear in the given strings by adjusting the begin...
Definition StringView_inl.h:120
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
bool EndsWith(plStringView sEndsWith) const
Returns true, if this string ends with the given string.
Definition StringView_inl.h:110
bool EndsWith_NoCase(plStringView sEndsWith) const
Returns true, if this string ends with the given string. Case insensitive.
Definition StringView_inl.h:115
bool StartsWith_NoCase(plStringView sStartsWith) const
Returns true, if this string starts with the given string. Case insensitive.
Definition StringView_inl.h:105
bool IsValid() const
Returns true, if the current string pointed to is non empty.
Definition StringView_inl.h:71
const char * GetStartPointer() const
Returns the start of the view range.
Definition StringView.h:102
bool IsEqual_NoCase(plStringView sOther) const
Compares this string view with the other string view for equality.
Definition StringView_inl.h:95
void SetStartPosition(const char *szCurPos)
Allows to set the start position to a different value.
Definition StringView_inl.h:76
constexpr plStringView()
Default constructor creates an invalid view.
bool IsEmpty() const
Returns whether the string is an empty string.
Definition StringView_inl.h:85
plInt32 Compare(plStringView sOther) const
Compares this string with the other one. Returns 0 for equality, -1 if this string is 'smaller',...
Definition StringView.cpp:31
bool IsEqual(plStringView sOther) const
Compares this string view with the other string view for equality.
Definition StringView_inl.h:90
static constexpr T * GetMaxStringEnd()
[internal] Returns the max string end pointer for the given type
Definition UnicodeUtils_inl.h:261
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
PL_ALWAYS_INLINE void IgnoreResult()
Used to silence compiler warnings, when success or failure doesn't matter.
Definition Types.h:69