TGUI 1.13
Loading...
Searching...
No Matches
StringView.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2026 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#ifndef TGUI_STRING_VIEW_HPP
26#define TGUI_STRING_VIEW_HPP
27
28#include <TGUI/Config.hpp>
29
30#include <algorithm> // equal, min
31#include <cctype> // tolower
32#include <string>
33#include <string_view>
34
36
37namespace tgui
38{
39 inline namespace literals
40 {
41 inline namespace string_view_literals
42 {
43 // Allow using operator ""sv
44 // Note that this only affects code placed inside the tgui namespace.
45 using namespace std::literals::string_view_literals;
46 } // namespace string_view_literals
47 } // namespace literals
48
49 using StringView = std::u32string_view;
50 using CharStringView = std::string_view;
51
60 [[nodiscard]] inline bool viewEqualIgnoreCase(CharStringView view1, CharStringView view2)
61 {
62 return std::equal(view1.begin(),
63 view1.end(),
64 view2.begin(),
65 view2.end(),
66 [](char char1, char char2)
67 {
68 if (char1 == char2)
69 return true;
70 return std::tolower(static_cast<unsigned char>(char1))
71 == std::tolower(static_cast<unsigned char>(char2));
72 });
73 }
74
83 [[nodiscard]] inline bool viewEqualIgnoreCase(StringView view1, StringView view2)
84 {
85 return std::equal(view1.cbegin(),
86 view1.cend(),
87 view2.cbegin(),
88 view2.cend(),
89 [](char32_t char1, char32_t char2)
90 {
91 if (char1 == char2)
92 return true;
93 if ((char1 < 128) && (char2 < 128))
94 return std::tolower(static_cast<unsigned char>(char1))
95 == std::tolower(static_cast<unsigned char>(char2));
96 return false;
97 });
98 }
99
100#if defined(__cpp_lib_starts_ends_with) && (__cpp_lib_starts_ends_with >= 201711L)
109 [[nodiscard]] inline bool viewStartsWith(CharStringView viewToLookInto, CharStringView viewToLookFor)
110 {
111 return viewToLookInto.starts_with(viewToLookFor);
112 }
113
122 [[nodiscard]] inline bool viewStartsWith(CharStringView viewToLookInto, char charToLookFor)
123 {
124 return viewToLookInto.starts_with(charToLookFor);
125 }
126
135 [[nodiscard]] inline bool viewEndsWith(CharStringView viewToLookInto, CharStringView viewToLookFor)
136 {
137 return viewToLookInto.ends_with(viewToLookFor);
138 }
139
148 [[nodiscard]] inline bool viewEndsWith(CharStringView viewToLookInto, char charToLookFor)
149 {
150 return viewToLookInto.ends_with(charToLookFor);
151 }
152
161 [[nodiscard]] inline bool viewStartsWith(StringView viewToLookInto, StringView viewToLookFor)
162 {
163 return viewToLookInto.starts_with(viewToLookFor);
164 }
165
174 [[nodiscard]] inline bool viewStartsWith(StringView viewToLookInto, char32_t charToLookFor)
175 {
176 return viewToLookInto.starts_with(charToLookFor);
177 }
178
187 [[nodiscard]] inline bool viewEndsWith(StringView viewToLookInto, StringView viewToLookFor)
188 {
189 return viewToLookInto.ends_with(viewToLookFor);
190 }
191
200 [[nodiscard]] inline bool viewEndsWith(StringView viewToLookInto, char32_t charToLookFor)
201 {
202 return viewToLookInto.ends_with(charToLookFor);
203 }
204#else
213 [[nodiscard]] inline bool viewStartsWith(CharStringView viewToLookInto, CharStringView viewToLookFor)
214 {
215 if (viewToLookFor.length() > viewToLookInto.length())
216 return false;
217
218 return viewToLookInto.substr(0, viewToLookFor.length()) == viewToLookFor;
219 }
220
229 [[nodiscard]] inline bool viewStartsWith(CharStringView viewToLookInto, char charToLookFor)
230 {
231 return !viewToLookInto.empty() && (viewToLookInto.front() == charToLookFor);
232 }
233
242 [[nodiscard]] inline bool viewEndsWith(CharStringView viewToLookInto, CharStringView viewToLookFor)
243 {
244 if (viewToLookFor.length() > viewToLookInto.length())
245 return false;
246
247 return CharStringView(viewToLookInto.data() + (viewToLookInto.length() - viewToLookFor.length()), viewToLookFor.length())
248 .compare(viewToLookFor)
249 == 0;
250 }
251
260 [[nodiscard]] inline bool viewEndsWith(CharStringView viewToLookInto, char charToLookFor)
261 {
262 return !viewToLookInto.empty() && (viewToLookInto.back() == charToLookFor);
263 }
264
273 [[nodiscard]] inline bool viewStartsWith(StringView viewToLookInto, StringView viewToLookFor)
274 {
275 if (viewToLookFor.length() > viewToLookInto.length())
276 return false;
277
278 return viewToLookInto.substr(0, viewToLookFor.length()) == viewToLookFor;
279 }
280
289 [[nodiscard]] inline bool viewStartsWith(StringView viewToLookInto, char32_t charToLookFor)
290 {
291 return !viewToLookInto.empty() && (viewToLookInto.front() == charToLookFor);
292 }
293
302 [[nodiscard]] inline bool viewEndsWith(StringView viewToLookInto, StringView viewToLookFor)
303 {
304 if (viewToLookFor.length() > viewToLookInto.length())
305 return false;
306
307 return StringView(viewToLookInto.data() + (viewToLookInto.length() - viewToLookFor.length()), viewToLookFor.length())
308 .compare(viewToLookFor)
309 == 0;
310 }
311
320 [[nodiscard]] inline bool viewEndsWith(StringView viewToLookInto, char32_t charToLookFor)
321 {
322 return !viewToLookInto.empty() && (viewToLookInto.back() == charToLookFor);
323 }
324#endif
325} // namespace tgui
326
327#endif // TGUI_STRING_VIEW_HPP
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37
bool viewStartsWith(CharStringView viewToLookInto, CharStringView viewToLookFor)
Checks whether the view starts with the given substring.
Definition StringView.hpp:213
bool viewEqualIgnoreCase(CharStringView view1, CharStringView view2)
Returns whether two view are equal if letters would have been lowercase.
Definition StringView.hpp:60
bool viewEndsWith(CharStringView viewToLookInto, CharStringView viewToLookFor)
Checks whether the view ends with the given substring.
Definition StringView.hpp:242