25#ifndef TGUI_STRING_VIEW_HPP
26#define TGUI_STRING_VIEW_HPP
28#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
32 #if TGUI_COMPILED_WITH_CPP_VER >= 17
33 #include <string_view>
41#if TGUI_COMPILED_WITH_CPP_VER >= 17
44 inline namespace literals
46 inline namespace string_view_literals
50 using namespace std::literals::string_view_literals;
56TGUI_MODULE_EXPORT
namespace tgui
58#if TGUI_COMPILED_WITH_CPP_VER >= 17
59 using StringView = std::u32string_view;
60 using CharStringView = std::string_view;
62 template <
typename Type>
68 template<
typename Type>
69 using TypeIdentity_t =
typename TypeIdentity<Type>::type;
71 template <
typename CharType>
75 using const_iterator =
const CharType*;
77 constexpr StringViewImpl() =
default;
78 constexpr StringViewImpl(
const StringViewImpl& other) =
default;
80 constexpr StringViewImpl(
const CharType* str, std::size_t strLength) :
86 constexpr StringViewImpl(
const CharType* str) :
88 m_length(std::char_traits<CharType>::length(str))
92 constexpr StringViewImpl(
const std::basic_string<CharType>& str) :
94 m_length(str.length())
98 TGUI_NODISCARD
constexpr const_iterator begin() const noexcept
102 TGUI_NODISCARD
constexpr const_iterator cbegin() const noexcept
107 TGUI_NODISCARD
constexpr const_iterator end() const noexcept
109 return m_string +
static_cast<std::ptrdiff_t
>(m_length);
111 TGUI_NODISCARD
constexpr const_iterator cend() const noexcept
113 return m_string +
static_cast<std::ptrdiff_t
>(m_length);
116 TGUI_NODISCARD
constexpr const CharType& operator[](std::size_t index)
const
118 return m_string[index];
121 TGUI_NODISCARD
constexpr const CharType& front()
const
126 TGUI_NODISCARD
constexpr const CharType& back()
const
128 return m_string[m_length-1];
131 TGUI_NODISCARD
constexpr const CharType* data() const noexcept
136 TGUI_NODISCARD
constexpr std::size_t size() const noexcept
140 TGUI_NODISCARD
constexpr std::size_t length() const noexcept
145 TGUI_NODISCARD
constexpr bool empty() const noexcept
147 return (m_length == 0);
150 TGUI_NODISCARD
constexpr StringViewImpl substr(std::size_t pos = 0, std::size_t count = std::u32string::npos)
const
152 if (count != std::u32string::npos)
153 return StringViewImpl(&m_string[pos], count);
155 return StringViewImpl(&m_string[pos], m_length - pos);
158 TGUI_NODISCARD
constexpr int compare(StringViewImpl strView)
const noexcept
160 const std::size_t rlen = std::min(length(), strView.length());
161 const int ret = std::char_traits<CharType>::compare(data(), strView.data(), rlen);
165 if (length() < strView.length())
167 else if (length() > strView.length())
173 TGUI_NODISCARD
constexpr std::size_t find(StringViewImpl strView, std::size_t pos = 0) const noexcept
175 if (empty() || (strView.length() > m_length))
176 return std::u32string::npos;
181 for (std::size_t i = pos; i <= m_length - strView.length(); ++i)
183 if (m_string[i] != strView[0])
187 for (std::size_t j = 1; j < strView.length(); ++j)
189 if (m_string[i+j] != strView[j])
200 return std::u32string::npos;
203 TGUI_NODISCARD
constexpr std::size_t find(CharType ch, std::size_t pos = 0) const noexcept
205 return find(StringViewImpl(&ch, 1), pos);
207 TGUI_NODISCARD
constexpr std::size_t find(
const CharType* str, std::size_t pos, std::size_t count)
const
209 return find(StringViewImpl(str, count), pos);
211 TGUI_NODISCARD
constexpr std::size_t find(
const CharType* str, std::size_t pos = 0)
const
213 return find(StringViewImpl(str), pos);
217 const CharType* m_string =
nullptr;
218 std::size_t m_length = 0;
221 template <
typename CharType>
222 TGUI_NODISCARD
constexpr bool operator==(StringViewImpl<CharType> lhs, TypeIdentity_t<StringViewImpl<CharType>> rhs)
noexcept
224 return lhs.compare(rhs) == 0;
227 template <
typename CharType>
228 TGUI_NODISCARD
constexpr bool operator!=(StringViewImpl<CharType> lhs, TypeIdentity_t<StringViewImpl<CharType>> rhs)
noexcept
230 return lhs.compare(rhs) != 0;
233 template <
typename CharType>
234 TGUI_NODISCARD
constexpr bool operator<(StringViewImpl<CharType> lhs, TypeIdentity_t<StringViewImpl<CharType>> rhs)
noexcept
236 return lhs.compare(rhs) < 0;
239 template <
typename CharType>
240 TGUI_NODISCARD
constexpr bool operator<=(StringViewImpl<CharType> lhs, TypeIdentity_t<StringViewImpl<CharType>> rhs)
noexcept
242 return lhs.compare(rhs) <= 0;
245 template <
typename CharType>
246 TGUI_NODISCARD
constexpr bool operator>(StringViewImpl<CharType> lhs, TypeIdentity_t<StringViewImpl<CharType>> rhs)
noexcept
248 return lhs.compare(rhs) > 0;
251 template <
typename CharType>
252 TGUI_NODISCARD
constexpr bool operator>=(StringViewImpl<CharType> lhs, TypeIdentity_t<StringViewImpl<CharType>> rhs)
noexcept
254 return lhs.compare(rhs) >= 0;
257 using StringView = StringViewImpl<char32_t>;
258 using CharStringView = StringViewImpl<char>;
262#if defined(__clang__)
263# pragma clang diagnostic push
264# pragma clang diagnostic ignored "-Wuser-defined-literals"
265#elif defined(__GNUC__)
266# pragma GCC diagnostic push
267# pragma GCC diagnostic ignored "-Wliteral-suffix"
268#elif defined (_MSC_VER)
269# pragma warning(push)
270# pragma warning(disable: 4455)
272 inline namespace literals
274 inline namespace string_view_literals
276 inline constexpr StringViewImpl<char>
operator""sv(
const char* str,
size_t len)
noexcept
278 return StringViewImpl<char>{str, len};
281 inline constexpr StringViewImpl<wchar_t>
operator""sv(
const wchar_t* str,
size_t len)
noexcept
283 return StringViewImpl<wchar_t>{str, len};
286 inline constexpr StringViewImpl<char16_t>
operator""sv(
const char16_t* str,
size_t len)
noexcept
288 return StringViewImpl<char16_t>{str, len};
291 inline constexpr StringViewImpl<char32_t>
operator""sv(
const char32_t* str,
size_t len)
noexcept
293 return StringViewImpl<char32_t>{str, len};
297#if defined(__clang__)
298# pragma clang diagnostic pop
299#elif defined(__GNUC__)
300# pragma GCC diagnostic pop
301#elif defined (_MSC_VER)
317 return std::equal(view1.begin(), view1.end(), view2.begin(), view2.end(),
318 [](
char char1,
char char2)
323 return std::tolower(static_cast<unsigned char>(char1)) == std::tolower(static_cast<unsigned char>(char2));
338 return std::equal(view1.begin(), view1.end(), view2.begin(), view2.end(),
339 [](
char32_t char1,
char32_t char2)
343 else if ((char1 < 128) && (char2 < 128))
344 return std::tolower(static_cast<unsigned char>(char1)) == std::tolower(static_cast<unsigned char>(char2));
351#if TGUI_COMPILED_WITH_CPP_VER >= 17 && defined(__cpp_lib_starts_ends_with) && (__cpp_lib_starts_ends_with >= 201711L)
360 TGUI_NODISCARD
inline bool viewStartsWith(CharStringView viewToLookInto, CharStringView viewToLookFor)
362 return viewToLookInto.starts_with(viewToLookFor);
373 TGUI_NODISCARD
inline bool viewStartsWith(CharStringView viewToLookInto,
char charToLookFor)
375 return viewToLookInto.starts_with(charToLookFor);
386 TGUI_NODISCARD
inline bool viewEndsWith(CharStringView viewToLookInto, CharStringView viewToLookFor)
388 return viewToLookInto.ends_with(viewToLookFor);
399 TGUI_NODISCARD
inline bool viewEndsWith(CharStringView viewToLookInto,
char charToLookFor)
401 return viewToLookInto.ends_with(charToLookFor);
412 TGUI_NODISCARD
inline bool viewStartsWith(StringView viewToLookInto, StringView viewToLookFor)
414 return viewToLookInto.starts_with(viewToLookFor);
425 TGUI_NODISCARD
inline bool viewStartsWith(StringView viewToLookInto,
char32_t charToLookFor)
427 return viewToLookInto.starts_with(charToLookFor);
438 TGUI_NODISCARD
inline bool viewEndsWith(StringView viewToLookInto, StringView viewToLookFor)
440 return viewToLookInto.ends_with(viewToLookFor);
451 TGUI_NODISCARD
inline bool viewEndsWith(StringView viewToLookInto,
char32_t charToLookFor)
453 return viewToLookInto.ends_with(charToLookFor);
464 TGUI_NODISCARD
inline bool viewStartsWith(CharStringView viewToLookInto, CharStringView viewToLookFor)
466 if (viewToLookFor.length() > viewToLookInto.length())
469 return viewToLookInto.substr(0, viewToLookFor.length()) == viewToLookFor;
480 TGUI_NODISCARD
inline bool viewStartsWith(CharStringView viewToLookInto,
char charToLookFor)
482 return !viewToLookInto.empty() && (viewToLookInto.front() == charToLookFor);
493 TGUI_NODISCARD
inline bool viewEndsWith(CharStringView viewToLookInto, CharStringView viewToLookFor)
495 if (viewToLookFor.length() > viewToLookInto.length())
498 return CharStringView(viewToLookInto.data() + (viewToLookInto.length() - viewToLookFor.length()), viewToLookFor.length()).compare(viewToLookFor) == 0;
509 TGUI_NODISCARD
inline bool viewEndsWith(CharStringView viewToLookInto,
char charToLookFor)
511 return !viewToLookInto.empty() && (viewToLookInto.back() == charToLookFor);
522 TGUI_NODISCARD
inline bool viewStartsWith(StringView viewToLookInto, StringView viewToLookFor)
524 if (viewToLookFor.length() > viewToLookInto.length())
527 return viewToLookInto.substr(0, viewToLookFor.length()) == viewToLookFor;
538 TGUI_NODISCARD
inline bool viewStartsWith(StringView viewToLookInto,
char32_t charToLookFor)
540 return !viewToLookInto.empty() && (viewToLookInto.front() == charToLookFor);
551 TGUI_NODISCARD
inline bool viewEndsWith(StringView viewToLookInto, StringView viewToLookFor)
553 if (viewToLookFor.length() > viewToLookInto.length())
556 return StringView(viewToLookInto.data() + (viewToLookInto.length() - viewToLookFor.length()), viewToLookFor.length()).compare(viewToLookFor) == 0;
567 TGUI_NODISCARD
inline bool viewEndsWith(StringView viewToLookInto,
char32_t charToLookFor)
569 return !viewToLookInto.empty() && (viewToLookInto.back() == charToLookFor);
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
bool viewStartsWith(CharStringView viewToLookInto, CharStringView viewToLookFor)
Checks whether the view starts with the given substring.
Definition StringView.hpp:464
bool viewEqualIgnoreCase(CharStringView view1, CharStringView view2)
Returns whether two view are equal if letters would have been lowercase.
Definition StringView.hpp:315
bool viewEndsWith(CharStringView viewToLookInto, CharStringView viewToLookFor)
Checks whether the view ends with the given substring.
Definition StringView.hpp:493