TGUI  1.6.1
Loading...
Searching...
No Matches
String.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 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_HPP
26#define TGUI_STRING_HPP
27
28#include <TGUI/Config.hpp>
29#include <TGUI/StringView.hpp>
30#include <TGUI/Utf.hpp>
31
32#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
33 #include <string>
34 #include <vector>
35 #include <cstring>
36 #include <locale>
37 #include <iomanip>
38 #include <ostream>
39 #include <sstream>
40 #include <type_traits>
41 #include <initializer_list>
42#endif
43
44#if TGUI_HAS_WINDOW_BACKEND_SFML
45 #include <SFML/System/String.hpp>
46#endif
47
49
50TGUI_MODULE_EXPORT namespace tgui
51{
57 TGUI_NODISCARD TGUI_API bool isWhitespace(char character);
58
64 TGUI_NODISCARD TGUI_API bool isWhitespace(char32_t character);
65
71 TGUI_NODISCARD TGUI_API bool isAlpha(char32_t character);
72
78 TGUI_NODISCARD TGUI_API bool isDigit(char32_t character);
79
80
95 class TGUI_API String
96 {
97 private:
98 std::u32string m_string;
99
100#if TGUI_COMPILED_WITH_CPP_VER >= 17
101 // Helper to check if template parameter is a string_view
102 template <typename StringViewType>
103 using IsStringViewType = std::enable_if_t<
104 std::is_same_v<StringViewType, std::string_view>
105#if defined(__cpp_lib_char8_t) && (__cpp_lib_char8_t >= 201811L)
106 || std::is_same_v<StringViewType, std::u8string_view>
107#endif
108 || std::is_same_v<StringViewType, std::wstring_view>
109 || std::is_same_v<StringViewType, std::u16string_view>
110 || std::is_same_v<StringViewType, std::u32string_view>,
111 void>;
112#endif
113
114 public:
115
116#if TGUI_COMPILED_WITH_CPP_VER >= 17
117 static constexpr auto npos = std::u32string_view::npos;
118#else
119 static const decltype(std::u32string::npos) npos;
120#endif
121
122 using iterator = std::u32string::iterator;
123 using const_iterator = std::u32string::const_iterator;
124 using reverse_iterator = std::u32string::reverse_iterator;
125 using const_reverse_iterator = std::u32string::const_reverse_iterator;
126
127 using value_type = char32_t;
128 using reference = char32_t&;
129 using const_reference = const char32_t&;
130
131 public:
132
140 TGUI_NODISCARD bool attemptToInt(int& result) const;
141
149 TGUI_NODISCARD bool attemptToUInt(unsigned int& result) const;
150
158 TGUI_NODISCARD bool attemptToFloat(float& result) const;
159
167 TGUI_NODISCARD int toInt(int defaultValue = 0) const;
168
176 TGUI_NODISCARD unsigned int toUInt(unsigned int defaultValue = 0) const;
177
185 TGUI_NODISCARD float toFloat(float defaultValue = 0) const;
186
192 TGUI_NODISCARD String trim() const;
193
199 TGUI_NODISCARD String toLower() const;
200
206 TGUI_NODISCARD String toUpper() const;
207
215 TGUI_NODISCARD bool equalIgnoreCase(const String& other) const;
216
217#ifndef TGUI_REMOVE_DEPRECATED_CODE
227 TGUI_DEPRECATED("Use starts_with instead") TGUI_NODISCARD bool startsWith(const String& substring) const;
228#endif
229
237 TGUI_NODISCARD bool startsWithIgnoreCase(const String& substring) const;
238
239#ifndef TGUI_REMOVE_DEPRECATED_CODE
249 TGUI_DEPRECATED("Use ends_with instead") TGUI_NODISCARD bool endsWith(const String& substring) const;
250#endif
251
259 TGUI_NODISCARD bool endsWithIgnoreCase(const String& substring) const;
260
269 String& replace(const String& searchFor, const String& replaceWith);
270
276 void remove(const String& substring);
277
284 TGUI_NODISCARD std::vector<String> split(const String& delimiter, bool trim = false) const;
285
292 TGUI_NODISCARD static String join(const std::vector<String>& segments, const String& separator);
293
301 template <typename T>
302 TGUI_NODISCARD static String fromNumber(T value)
303 {
304 // If the value is a floating point then we can't use std::to_string because its result depends on the locale.
305 // If the value is an 8-bit type (e.g. uint8_t and int8_t) then using std::ostringstream results in the wrong result,
306 // as it will be interpreted as a character instead of a number.
307 TGUI_IF_CONSTEXPR (std::is_integral<T>::value)
308 {
309 return {std::to_string(value)};
310 }
311 else
312 {
313 std::ostringstream oss;
314 oss.imbue(std::locale::classic());
315 oss << value;
316 return {oss.str()};
317 }
318 }
319
328 template <typename T>
329 TGUI_NODISCARD static String fromNumberRounded(T value, unsigned int decimals)
330 {
331 // Precision is ignored by std::ostringstream for integers, so we use std::to_string for integers instead.
332 // If the value is an 8-bit type (e.g. uint8_t and int8_t) then using std::ostringstream results in the wrong result,
333 // as it will be interpreted as a character instead of a number. Which is why this separate branch for integers exists.
334 TGUI_IF_CONSTEXPR (std::is_integral<T>::value)
335 return {std::to_string(value)};
336 else
337 {
338 std::ostringstream oss;
339 oss.imbue(std::locale::classic());
340 oss << std::fixed << std::setprecision(static_cast<int>(decimals));
341 oss << value;
342 return {oss.str()};
343 }
344 }
345
347 public:
348
349 String() = default;
350
351 String(const std::string& str);
352 String(const std::wstring& str);
353 String(const std::u16string& str);
354 String(const std::u32string& str);
355
356 String(std::u32string&& str) :
357 m_string{std::move(str)}
358 {
359 }
360
361 String(char ansiChar);
362 String(wchar_t wideChar);
363 String(char16_t utfChar);
364 String(char32_t utfChar);
365
366 String(const char* str);
367 String(const wchar_t* str);
368 String(const char16_t* str);
369 String(const char32_t* str) :
370 m_string{str}
371 {
372 }
373
374 // Constructor to initialize the string from a number (integer or float)
375 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
376 explicit String(T number) :
377 String{fromNumber(number)}
378 {
379 }
380
381 String(std::size_t count, char ch);
382 String(std::size_t count, wchar_t ch);
383 String(std::size_t count, char16_t ch);
384 String(std::size_t count, char32_t ch);
385
386 String(const std::string& str, std::size_t pos);
387 String(const std::wstring& str, std::size_t pos);
388 String(const std::u16string& str, std::size_t pos);
389 String(const std::u32string& str, std::size_t pos);
390
391 String(const std::string& str, std::size_t pos, std::size_t count);
392 String(const std::wstring& str, std::size_t pos, std::size_t count);
393 String(const std::u16string& str, std::size_t pos, std::size_t count);
394 String(const std::u32string& str, std::size_t pos, std::size_t count);
395
396 String(const char* str, std::size_t count);
397 String(const wchar_t* str, std::size_t count);
398 String(const char16_t* str, std::size_t count);
399 String(const char32_t* str, std::size_t count);
400
401 explicit String(std::initializer_list<char> chars);
402 explicit String(std::initializer_list<wchar_t> chars);
403 explicit String(std::initializer_list<char16_t> chars);
404 explicit String(std::initializer_list<char32_t> chars);
405
406 // Constructors using iterators have to be explicit to prevent {"1", "2"} to be ambiguous between String and std::vector<String>.
407 // The reason these constructors were considered a candicate to clang is due to a private constructor in the iterator class.
408 // We can't accept string_view::const_iterator iterators, because it would lead to the same kind of ambiguity even when the
409 // constructor is marked explicit (in GCC but not MSVC if we define the function normally, in MSVC but not GCC if we use a template).
410 explicit String(std::string::const_iterator first, std::string::const_iterator last);
411 explicit String(std::wstring::const_iterator first, std::wstring::const_iterator last);
412 explicit String(std::u16string::const_iterator first, std::u16string::const_iterator last);
413 explicit String(std::u32string::const_iterator first, std::u32string::const_iterator last);
414
415#if TGUI_COMPILED_WITH_CPP_VER >= 17
416 template <typename StringViewType, typename = IsStringViewType<StringViewType>>
417 explicit String(const StringViewType& stringView) :
418 String(stringView.data(), stringView.size())
419 {
420 }
421
422 template <typename StringViewType, typename = IsStringViewType<StringViewType>>
423 explicit String(const StringViewType& stringView, std::size_t pos, std::size_t count) :
424 String(stringView.data() + pos, count)
425 {
426 }
427#else
428 template <typename CharType>
429 explicit String(StringViewImpl<CharType> stringView) :
430 String(stringView.data(), stringView.size())
431 {
432 }
433
434 template <typename CharType>
435 explicit String(StringViewImpl<CharType> stringView, std::size_t pos, std::size_t count) :
436 String(stringView.data() + pos, count)
437 {
438 }
439#endif
440
441#if TGUI_HAS_WINDOW_BACKEND_SFML
442 // This constructor has to be explicit or it will cause MSVC to no longer compile code that performs sf::String + std::string
443 explicit String(const sf::String& str)
444 : m_string{reinterpret_cast<const char32_t*>(str.toUtf32().c_str())}
445 {
446 }
447
448 explicit operator sf::String() const
449 {
450 return sf::String::fromUtf32(m_string.begin(), m_string.end());
451 }
452#endif
453
454 explicit operator std::string() const;
455 explicit operator std::wstring() const;
456 explicit operator std::u16string() const;
457 explicit operator const std::u32string&() const
458 {
459 return m_string;
460 }
461
462 operator StringView() const noexcept
463 {
464#if TGUI_COMPILED_WITH_CPP_VER >= 17
465 return m_string;
466#else
467 return StringView(m_string.data(), m_string.length());
468#endif
469 }
470
471 TGUI_NODISCARD std::string toStdString() const;
472 TGUI_NODISCARD std::wstring toWideString() const;
473 TGUI_NODISCARD std::u16string toUtf16() const;
474 TGUI_NODISCARD const std::u32string& toUtf32() const
475 {
476 return m_string;
477 }
478
479 String& assign(std::size_t count, char ch);
480 String& assign(std::size_t count, wchar_t ch);
481 String& assign(std::size_t count, char16_t ch);
482 String& assign(std::size_t count, char32_t ch);
483
484 String& assign(StringView sv);
485 String& assign(const char32_t* str);
486 String& assign(const std::u32string& str);
487 String& assign(const String& str);
488
489 String& assign(StringView sv, std::size_t pos, std::size_t count = npos);
490 String& assign(const std::string& str, std::size_t pos, std::size_t count = npos);
491 String& assign(const std::wstring& str, std::size_t pos, std::size_t count = npos);
492 String& assign(const std::u16string& str, std::size_t pos, std::size_t count = npos);
493 String& assign(const std::u32string& str, std::size_t pos, std::size_t count = npos);
494 String& assign(const String& str, std::size_t pos, std::size_t count = npos);
495
496 String& assign(std::u32string&& str);
497 String& assign(String&& str);
498
499 String& assign(const char* str, std::size_t count);
500 String& assign(const wchar_t* str, std::size_t count);
501 String& assign(const char16_t* str, std::size_t count);
502 String& assign(const char32_t* str, std::size_t count);
503
504 String& assign(std::initializer_list<char> chars);
505 String& assign(std::initializer_list<wchar_t> chars);
506 String& assign(std::initializer_list<char16_t> chars);
507 String& assign(std::initializer_list<char32_t> chars);
508
509 String& assign(std::string::const_iterator first, std::string::const_iterator last);
510 String& assign(std::wstring::const_iterator first, std::wstring::const_iterator last);
511 String& assign(std::u16string::const_iterator first, std::u16string::const_iterator last);
512 String& assign(std::u32string::const_iterator first, std::u32string::const_iterator last);
513 String& assign(StringView::const_iterator first, StringView::const_iterator last);
514
515 TGUI_NODISCARD reference at(std::size_t pos);
516 TGUI_NODISCARD const_reference at(std::size_t pos) const;
517
518 TGUI_NODISCARD const_reference operator [](std::size_t index) const;
519 TGUI_NODISCARD reference operator [](std::size_t index);
520
521 TGUI_NODISCARD reference front();
522 TGUI_NODISCARD const_reference front() const;
523
524 TGUI_NODISCARD reference back();
525 TGUI_NODISCARD const_reference back() const;
526
527 TGUI_NODISCARD const char32_t* data() const noexcept
528 {
529 return m_string.data();
530 }
531
532 TGUI_NODISCARD char32_t* data() noexcept
533 {
534#if __cplusplus >= 201703L
535 return m_string.data();
536#else
537 return &m_string[0];
538#endif
539 }
540
541 TGUI_NODISCARD const char32_t* c_str() const noexcept
542 {
543 return m_string.c_str();
544 }
545
546 TGUI_NODISCARD iterator begin() noexcept;
547 TGUI_NODISCARD const_iterator begin() const noexcept;
548 TGUI_NODISCARD const_iterator cbegin() const noexcept;
549
550 TGUI_NODISCARD iterator end() noexcept;
551 TGUI_NODISCARD const_iterator end() const noexcept;
552 TGUI_NODISCARD const_iterator cend() const noexcept;
553
554 TGUI_NODISCARD reverse_iterator rbegin() noexcept;
555 TGUI_NODISCARD const_reverse_iterator rbegin() const noexcept;
556 TGUI_NODISCARD const_reverse_iterator crbegin() const noexcept;
557
558 TGUI_NODISCARD reverse_iterator rend() noexcept;
559 TGUI_NODISCARD const_reverse_iterator rend() const noexcept;
560 TGUI_NODISCARD const_reverse_iterator crend() const noexcept;
561
562 TGUI_NODISCARD bool empty() const noexcept
563 {
564 return m_string.empty();
565 }
566
567 TGUI_NODISCARD std::size_t size() const noexcept
568 {
569 return m_string.size();
570 }
571
572 TGUI_NODISCARD std::size_t length() const noexcept
573 {
574 return m_string.length();
575 }
576
577 TGUI_NODISCARD std::size_t max_size() const noexcept;
578
579 void reserve(std::size_t newCap);
580 TGUI_NODISCARD std::size_t capacity() const noexcept;
581 void shrink_to_fit();
582
583 void clear() noexcept;
584
585 String& insert(std::size_t index, std::size_t count, char ch);
586 String& insert(std::size_t index, std::size_t count, wchar_t ch);
587 String& insert(std::size_t index, std::size_t count, char16_t ch);
588 String& insert(std::size_t index, std::size_t count, char32_t ch);
589
590 String& insert(std::size_t index, StringView sv);
591 String& insert(std::size_t index, const char32_t* str);
592 String& insert(std::size_t index, const std::u32string& str);
593 String& insert(std::size_t index, const String& str);
594
595 String& insert(std::size_t index, StringView sv, std::size_t pos, std::size_t count = npos);
596 String& insert(std::size_t index, const std::string& str, std::size_t pos, std::size_t count = npos);
597 String& insert(std::size_t index, const std::wstring& str, std::size_t pos, std::size_t count = npos);
598 String& insert(std::size_t index, const std::u16string& str, std::size_t pos, std::size_t count = npos);
599 String& insert(std::size_t index, const std::u32string& str, std::size_t pos, std::size_t count = npos);
600 String& insert(std::size_t index, const String& str, std::size_t pos, std::size_t count = npos);
601
602 String& insert(std::size_t index, const char* str, std::size_t count);
603 String& insert(std::size_t index, const wchar_t* str, std::size_t count);
604 String& insert(std::size_t index, const char16_t* str, std::size_t count);
605 String& insert(std::size_t index, const char32_t* str, std::size_t count);
606
607 iterator insert(const_iterator pos, char ch);
608 iterator insert(const_iterator pos, wchar_t ch);
609 iterator insert(const_iterator pos, char16_t ch);
610 iterator insert(const_iterator pos, char32_t ch);
611
612 iterator insert(const_iterator pos, std::size_t count, char ch);
613 iterator insert(const_iterator pos, std::size_t count, wchar_t ch);
614 iterator insert(const_iterator pos, std::size_t count, char16_t ch);
615 iterator insert(const_iterator pos, std::size_t count, char32_t ch);
616
617 iterator insert(const_iterator pos, std::initializer_list<char> chars);
618 iterator insert(const_iterator pos, std::initializer_list<wchar_t> chars);
619 iterator insert(const_iterator pos, std::initializer_list<char16_t> chars);
620 iterator insert(const_iterator pos, std::initializer_list<char32_t> chars);
621
622 iterator insert(const_iterator pos, std::string::const_iterator first, std::string::const_iterator last);
623 iterator insert(const_iterator pos, std::wstring::const_iterator first, std::wstring::const_iterator last);
624 iterator insert(const_iterator pos, std::u16string::const_iterator first, std::u16string::const_iterator last);
625 iterator insert(const_iterator pos, std::u32string::const_iterator first, std::u32string::const_iterator last);
626 iterator insert(const_iterator pos, StringView::const_iterator first, StringView::const_iterator last);
627
628 String& erase(std::size_t index = 0, std::size_t count = npos);
629
630 iterator erase(const_iterator position);
631 iterator erase(const_iterator first, const_iterator last);
632
633 void push_back(char ch);
634 void push_back(wchar_t ch);
635 void push_back(char16_t ch);
636 void push_back(char32_t ch);
637
638 void pop_back();
639
640 String& append(std::size_t count, char ch);
641 String& append(std::size_t count, wchar_t ch);
642 String& append(std::size_t count, char16_t ch);
643 String& append(std::size_t count, char32_t ch);
644
645 String& append(StringView sv);
646 String& append(const char32_t* str);
647 String& append(const std::u32string& str);
648 String& append(const String& str);
649
650 String& append(StringView sv, std::size_t pos, std::size_t count = npos);
651 String& append(const std::string& str, std::size_t pos, std::size_t count = npos);
652 String& append(const std::wstring& str, std::size_t pos, std::size_t count = npos);
653 String& append(const std::u16string& str, std::size_t pos, std::size_t count = npos);
654 String& append(const std::u32string& str, std::size_t pos, std::size_t count = npos);
655 String& append(const String& str, std::size_t pos, std::size_t count = npos);
656
657 String& append(const char* str, std::size_t count);
658 String& append(const wchar_t* str, std::size_t count);
659 String& append(const char16_t* str, std::size_t count);
660 String& append(const char32_t* str, std::size_t count);
661
662 String& append(std::string::const_iterator first, std::string::const_iterator last);
663 String& append(std::wstring::const_iterator first, std::wstring::const_iterator last);
664 String& append(std::u16string::const_iterator first, std::u16string::const_iterator last);
665 String& append(std::u32string::const_iterator first, std::u32string::const_iterator last);
666 String& append(StringView::const_iterator first, StringView::const_iterator last);
667
668 String& append(std::initializer_list<char> chars);
669 String& append(std::initializer_list<wchar_t> chars);
670 String& append(std::initializer_list<char16_t> chars);
671 String& append(std::initializer_list<char32_t> chars);
672
673 String& operator+=(const String& str);
674
675 TGUI_NODISCARD int compare(StringView sv) const noexcept;
676 TGUI_NODISCARD int compare(const char32_t* s) const;
677 TGUI_NODISCARD int compare(const std::u32string& str) const noexcept;
678 TGUI_NODISCARD int compare(const String& str) const noexcept;
679
680 TGUI_NODISCARD int compare(std::size_t pos1, std::size_t count1, StringView sv) const;
681 TGUI_NODISCARD int compare(std::size_t pos1, std::size_t count1, const char32_t* s) const;
682 TGUI_NODISCARD int compare(std::size_t pos1, std::size_t count1, const std::u32string& str) const;
683 TGUI_NODISCARD int compare(std::size_t pos1, std::size_t count1, const String& str) const;
684
685 TGUI_NODISCARD int compare(std::size_t pos1, std::size_t count1, StringView sv, std::size_t pos2, std::size_t count2 = npos) const;
686 TGUI_NODISCARD int compare(std::size_t pos1, std::size_t count1, const std::string& str, std::size_t pos2, std::size_t count2 = npos) const;
687 TGUI_NODISCARD int compare(std::size_t pos1, std::size_t count1, const std::wstring& str, std::size_t pos2, std::size_t count2 = npos) const;
688 TGUI_NODISCARD int compare(std::size_t pos1, std::size_t count1, const std::u16string& str, std::size_t pos2, std::size_t count2 = npos) const;
689 TGUI_NODISCARD int compare(std::size_t pos1, std::size_t count1, const std::u32string& str, std::size_t pos2, std::size_t count2 = npos) const;
690 TGUI_NODISCARD int compare(std::size_t pos1, std::size_t count1, const String& str, std::size_t pos2, std::size_t count2 = npos) const;
691
692 TGUI_NODISCARD int compare(std::size_t pos1, std::size_t count1, const char* s, std::size_t count2) const;
693 TGUI_NODISCARD int compare(std::size_t pos1, std::size_t count1, const wchar_t* s, std::size_t count2) const;
694 TGUI_NODISCARD int compare(std::size_t pos1, std::size_t count1, const char16_t* s, std::size_t count2) const;
695 TGUI_NODISCARD int compare(std::size_t pos1, std::size_t count1, const char32_t* s, std::size_t count2) const;
696
697 String& replace(std::size_t pos, std::size_t count, StringView sv);
698 String& replace(std::size_t pos, std::size_t count, const char32_t* cstr);
699 String& replace(std::size_t pos, std::size_t count, const std::u32string& str);
700 String& replace(std::size_t pos, std::size_t count, const String& str);
701
702 String& replace(const_iterator first, const_iterator last, StringView sv);
703 String& replace(const_iterator first, const_iterator last, const char32_t* cstr);
704 String& replace(const_iterator first, const_iterator last, const std::u32string& str);
705 String& replace(const_iterator first, const_iterator last, const String& str);
706
707 String& replace(std::size_t pos, std::size_t count, StringView sv, std::size_t pos2, std::size_t count2 = npos);
708 String& replace(std::size_t pos, std::size_t count, const std::string& str, std::size_t pos2, std::size_t count2 = npos);
709 String& replace(std::size_t pos, std::size_t count, const std::wstring& str, std::size_t pos2, std::size_t count2 = npos);
710 String& replace(std::size_t pos, std::size_t count, const std::u16string& str, std::size_t pos2, std::size_t count2 = npos);
711 String& replace(std::size_t pos, std::size_t count, const std::u32string& str, std::size_t pos2, std::size_t count2 = npos);
712 String& replace(std::size_t pos, std::size_t count, const String& str, std::size_t pos2, std::size_t count2 = npos);
713
714 String& replace(const_iterator first, const_iterator last, std::string::const_iterator first2, std::string::const_iterator last2);
715 String& replace(const_iterator first, const_iterator last, std::wstring::const_iterator first2, std::wstring::const_iterator last2);
716 String& replace(const_iterator first, const_iterator last, std::u16string::const_iterator first2, std::u16string::const_iterator last2);
717 String& replace(const_iterator first, const_iterator last, std::u32string::const_iterator first2, std::u32string::const_iterator last2);
718 String& replace(const_iterator first, const_iterator last, StringView::const_iterator first2, StringView::const_iterator last2);
719
720 String& replace(std::size_t pos, std::size_t count, const char* cstr, std::size_t count2);
721 String& replace(std::size_t pos, std::size_t count, const wchar_t* cstr, std::size_t count2);
722 String& replace(std::size_t pos, std::size_t count, const char16_t* cstr, std::size_t count2);
723 String& replace(std::size_t pos, std::size_t count, const char32_t* cstr, std::size_t count2);
724
725 String& replace(const_iterator first, const_iterator last, const char* cstr, std::size_t count2);
726 String& replace(const_iterator first, const_iterator last, const wchar_t* cstr, std::size_t count2);
727 String& replace(const_iterator first, const_iterator last, const char16_t* cstr, std::size_t count2);
728 String& replace(const_iterator first, const_iterator last, const char32_t* cstr, std::size_t count2);
729
730 String& replace(std::size_t pos, std::size_t count, std::size_t count2, char ch);
731 String& replace(std::size_t pos, std::size_t count, std::size_t count2, wchar_t ch);
732 String& replace(std::size_t pos, std::size_t count, std::size_t count2, char16_t ch);
733 String& replace(std::size_t pos, std::size_t count, std::size_t count2, char32_t ch);
734
735 String& replace(const_iterator first, const_iterator last, std::size_t count2, char ch);
736 String& replace(const_iterator first, const_iterator last, std::size_t count2, wchar_t ch);
737 String& replace(const_iterator first, const_iterator last, std::size_t count2, char16_t ch);
738 String& replace(const_iterator first, const_iterator last, std::size_t count2, char32_t ch);
739
740 String& replace(const_iterator first, const_iterator last, std::initializer_list<char> chars);
741 String& replace(const_iterator first, const_iterator last, std::initializer_list<wchar_t> chars);
742 String& replace(const_iterator first, const_iterator last, std::initializer_list<char16_t> chars);
743 String& replace(const_iterator first, const_iterator last, std::initializer_list<char32_t> chars);
744
745 TGUI_NODISCARD String substr(std::size_t pos = 0, std::size_t count = npos) const;
746
747 std::size_t copy(char32_t* dest, std::size_t count, std::size_t pos = 0) const;
748
749 void resize(std::size_t count);
750 void resize(std::size_t count, char ch);
751 void resize(std::size_t count, wchar_t ch);
752 void resize(std::size_t count, char16_t ch);
753 void resize(std::size_t count, char32_t ch);
754
755 void swap(String& other);
756
757 TGUI_NODISCARD bool contains(char c) const noexcept;
758 TGUI_NODISCARD bool contains(wchar_t c) const noexcept;
759 TGUI_NODISCARD bool contains(char16_t c) const noexcept;
760 TGUI_NODISCARD bool contains(char32_t c) const noexcept;
761
762 TGUI_NODISCARD bool contains(StringView sv) const noexcept;
763 TGUI_NODISCARD bool contains(const char32_t* s) const;
764 TGUI_NODISCARD bool contains(const std::u32string& s) const;
765 TGUI_NODISCARD bool contains(const String& s) const;
766
767 TGUI_NODISCARD std::size_t find(StringView sv, std::size_t pos = 0) const noexcept;
768 TGUI_NODISCARD std::size_t find(const char32_t* s, std::size_t pos = 0) const;
769 TGUI_NODISCARD std::size_t find(const std::u32string& str, std::size_t pos = 0) const noexcept;
770 TGUI_NODISCARD std::size_t find(const String& str, std::size_t pos = 0) const noexcept;
771
772 TGUI_NODISCARD std::size_t find(const char* s, std::size_t pos, std::size_t count) const;
773 TGUI_NODISCARD std::size_t find(const wchar_t* s, std::size_t pos, std::size_t count) const;
774 TGUI_NODISCARD std::size_t find(const char16_t* s, std::size_t pos, std::size_t count) const;
775 TGUI_NODISCARD std::size_t find(const char32_t* s, std::size_t pos, std::size_t count) const;
776
777 TGUI_NODISCARD std::size_t find(char ch, std::size_t pos = 0) const noexcept;
778 TGUI_NODISCARD std::size_t find(wchar_t ch, std::size_t pos = 0) const noexcept;
779 TGUI_NODISCARD std::size_t find(char16_t ch, std::size_t pos = 0) const noexcept;
780 TGUI_NODISCARD std::size_t find(char32_t ch, std::size_t pos = 0) const noexcept;
781
782 TGUI_NODISCARD std::size_t find_first_of(StringView sv, std::size_t pos = 0) const noexcept;
783 TGUI_NODISCARD std::size_t find_first_of(const char32_t* s, std::size_t pos = 0) const;
784 TGUI_NODISCARD std::size_t find_first_of(const std::u32string& str, std::size_t pos = 0) const noexcept;
785 TGUI_NODISCARD std::size_t find_first_of(const String& str, std::size_t pos = 0) const noexcept;
786
787 TGUI_NODISCARD std::size_t find_first_of(const char* s, std::size_t pos, std::size_t count) const;
788 TGUI_NODISCARD std::size_t find_first_of(const wchar_t* s, std::size_t pos, std::size_t count) const;
789 TGUI_NODISCARD std::size_t find_first_of(const char16_t* s, std::size_t pos, std::size_t count) const;
790 TGUI_NODISCARD std::size_t find_first_of(const char32_t* s, std::size_t pos, std::size_t count) const;
791
792 TGUI_NODISCARD std::size_t find_first_of(char ch, std::size_t pos = 0) const noexcept;
793 TGUI_NODISCARD std::size_t find_first_of(wchar_t ch, std::size_t pos = 0) const noexcept;
794 TGUI_NODISCARD std::size_t find_first_of(char16_t ch, std::size_t pos = 0) const noexcept;
795 TGUI_NODISCARD std::size_t find_first_of(char32_t ch, std::size_t pos = 0) const noexcept;
796
797 TGUI_NODISCARD std::size_t find_first_not_of(StringView sv, std::size_t pos = 0) const noexcept;
798 TGUI_NODISCARD std::size_t find_first_not_of(const char32_t* s, std::size_t pos = 0) const;
799 TGUI_NODISCARD std::size_t find_first_not_of(const std::u32string& str, std::size_t pos = 0) const noexcept;
800 TGUI_NODISCARD std::size_t find_first_not_of(const String& str, std::size_t pos = 0) const noexcept;
801
802 TGUI_NODISCARD std::size_t find_first_not_of(const char* s, std::size_t pos, std::size_t count) const;
803 TGUI_NODISCARD std::size_t find_first_not_of(const wchar_t* s, std::size_t pos, std::size_t count) const;
804 TGUI_NODISCARD std::size_t find_first_not_of(const char16_t* s, std::size_t pos, std::size_t count) const;
805 TGUI_NODISCARD std::size_t find_first_not_of(const char32_t* s, std::size_t pos, std::size_t count) const;
806
807 TGUI_NODISCARD std::size_t find_first_not_of(char ch, std::size_t pos = 0) const noexcept;
808 TGUI_NODISCARD std::size_t find_first_not_of(wchar_t ch, std::size_t pos = 0) const noexcept;
809 TGUI_NODISCARD std::size_t find_first_not_of(char16_t ch, std::size_t pos = 0) const noexcept;
810 TGUI_NODISCARD std::size_t find_first_not_of(char32_t ch, std::size_t pos = 0) const noexcept;
811
812 TGUI_NODISCARD std::size_t rfind(StringView sv, std::size_t pos = npos) const noexcept;
813 TGUI_NODISCARD std::size_t rfind(const char32_t* s, std::size_t pos = npos) const;
814 TGUI_NODISCARD std::size_t rfind(const std::u32string& str, std::size_t pos = npos) const noexcept;
815 TGUI_NODISCARD std::size_t rfind(const String& str, std::size_t pos = npos) const noexcept;
816
817 TGUI_NODISCARD std::size_t rfind(const char* s, std::size_t pos, std::size_t count) const;
818 TGUI_NODISCARD std::size_t rfind(const wchar_t* s, std::size_t pos, std::size_t count) const;
819 TGUI_NODISCARD std::size_t rfind(const char16_t* s, std::size_t pos, std::size_t count) const;
820 TGUI_NODISCARD std::size_t rfind(const char32_t* s, std::size_t pos, std::size_t count) const;
821
822 TGUI_NODISCARD std::size_t rfind(char ch, std::size_t pos = npos) const noexcept;
823 TGUI_NODISCARD std::size_t rfind(wchar_t ch, std::size_t pos = npos) const noexcept;
824 TGUI_NODISCARD std::size_t rfind(char16_t ch, std::size_t pos = npos) const noexcept;
825 TGUI_NODISCARD std::size_t rfind(char32_t ch, std::size_t pos = npos) const noexcept;
826
827 TGUI_NODISCARD std::size_t find_last_of(StringView sv, std::size_t pos = npos) const noexcept;
828 TGUI_NODISCARD std::size_t find_last_of(const char32_t* s, std::size_t pos = npos) const;
829 TGUI_NODISCARD std::size_t find_last_of(const std::u32string& str, std::size_t pos = npos) const noexcept;
830 TGUI_NODISCARD std::size_t find_last_of(const String& str, std::size_t pos = npos) const noexcept;
831
832 TGUI_NODISCARD std::size_t find_last_of(const char* s, std::size_t pos, std::size_t count) const;
833 TGUI_NODISCARD std::size_t find_last_of(const wchar_t* s, std::size_t pos, std::size_t count) const;
834 TGUI_NODISCARD std::size_t find_last_of(const char16_t* s, std::size_t pos, std::size_t count) const;
835 TGUI_NODISCARD std::size_t find_last_of(const char32_t* s, std::size_t pos, std::size_t count) const;
836
837 TGUI_NODISCARD std::size_t find_last_of(char ch, std::size_t pos = npos) const noexcept;
838 TGUI_NODISCARD std::size_t find_last_of(wchar_t ch, std::size_t pos = npos) const noexcept;
839 TGUI_NODISCARD std::size_t find_last_of(char16_t ch, std::size_t pos = npos) const noexcept;
840 TGUI_NODISCARD std::size_t find_last_of(char32_t ch, std::size_t pos = npos) const noexcept;
841
842 TGUI_NODISCARD std::size_t find_last_not_of(StringView sv, std::size_t pos = npos) const noexcept;
843 TGUI_NODISCARD std::size_t find_last_not_of(const char32_t* s, std::size_t pos = npos) const;
844 TGUI_NODISCARD std::size_t find_last_not_of(const std::u32string& str, std::size_t pos = npos) const noexcept;
845 TGUI_NODISCARD std::size_t find_last_not_of(const String& str, std::size_t pos = npos) const noexcept;
846
847 TGUI_NODISCARD std::size_t find_last_not_of(const char* s, std::size_t pos, std::size_t count) const;
848 TGUI_NODISCARD std::size_t find_last_not_of(const wchar_t* s, std::size_t pos, std::size_t count) const;
849 TGUI_NODISCARD std::size_t find_last_not_of(const char16_t* s, std::size_t pos, std::size_t count) const;
850 TGUI_NODISCARD std::size_t find_last_not_of(const char32_t* s, std::size_t pos, std::size_t count) const;
851
852 TGUI_NODISCARD std::size_t find_last_not_of(char ch, std::size_t pos = npos) const noexcept;
853 TGUI_NODISCARD std::size_t find_last_not_of(wchar_t ch, std::size_t pos = npos) const noexcept;
854 TGUI_NODISCARD std::size_t find_last_not_of(char16_t ch, std::size_t pos = npos) const noexcept;
855 TGUI_NODISCARD std::size_t find_last_not_of(char32_t ch, std::size_t pos = npos) const noexcept;
856
857 TGUI_NODISCARD inline bool starts_with(StringView sv) const noexcept;
858 TGUI_NODISCARD inline bool starts_with(const char32_t* s) const;
859 TGUI_NODISCARD inline bool starts_with(const std::u32string& s) const;
860 TGUI_NODISCARD inline bool starts_with(const String& s) const;
861
862 TGUI_NODISCARD inline bool starts_with(char ch) const noexcept;
863 TGUI_NODISCARD inline bool starts_with(wchar_t ch) const noexcept;
864 TGUI_NODISCARD inline bool starts_with(char16_t ch) const noexcept;
865 TGUI_NODISCARD inline bool starts_with(char32_t ch) const noexcept;
866
867 TGUI_NODISCARD inline bool ends_with(StringView sv) const noexcept;
868 TGUI_NODISCARD inline bool ends_with(const char32_t* s) const;
869 TGUI_NODISCARD inline bool ends_with(const std::u32string& s) const;
870 TGUI_NODISCARD inline bool ends_with(const String& s) const;
871
872 TGUI_NODISCARD inline bool ends_with(char ch) const noexcept;
873 TGUI_NODISCARD inline bool ends_with(wchar_t ch) const noexcept;
874 TGUI_NODISCARD inline bool ends_with(char16_t ch) const noexcept;
875 TGUI_NODISCARD inline bool ends_with(char32_t ch) const noexcept;
876
877 TGUI_NODISCARD std::size_t count(const char ch, const std::size_t pos = 0) const noexcept;
878 TGUI_NODISCARD std::size_t count(const wchar_t ch, const std::size_t pos = 0) const noexcept;
879 TGUI_NODISCARD std::size_t count(const char16_t ch, const std::size_t pos = 0) const noexcept;
880 TGUI_NODISCARD std::size_t count(const char32_t ch, const std::size_t pos = 0) const noexcept;
881
882 inline friend bool operator==(const String& left, StringView right);
883 inline friend bool operator==(const String& left, const char32_t* right);
884 inline friend bool operator==(const String& left, const std::u32string& right);
885 inline friend bool operator==(const String& left, const String& right);
886
887 inline friend bool operator!=(const String& left, StringView right);
888 inline friend bool operator!=(const String& left, const char32_t* right);
889 inline friend bool operator!=(const String& left, const std::u32string& right);
890 inline friend bool operator!=(const String& left, const String& right);
891
892 inline friend bool operator<(const String& left, StringView right);
893 inline friend bool operator<(const String& left, const char32_t* right);
894 inline friend bool operator<(const String& left, const std::u32string& right);
895 inline friend bool operator<(const String& left, const String& right);
896
897 inline friend bool operator<=(const String& left, StringView right);
898 inline friend bool operator<=(const String& left, const char32_t* right);
899 inline friend bool operator<=(const String& left, const std::u32string& right);
900 inline friend bool operator<=(const String& left, const String& right);
901
902 inline friend bool operator>(const String& left, StringView right);
903 inline friend bool operator>(const String& left, const char32_t* right);
904 inline friend bool operator>(const String& left, const std::u32string& right);
905 inline friend bool operator>(const String& left, const String& right);
906
907 inline friend bool operator>=(const String& left, StringView right);
908 inline friend bool operator>=(const String& left, const char32_t* right);
909 inline friend bool operator>=(const String& left, const std::u32string& right);
910 inline friend bool operator>=(const String& left, const String& right);
911
912 inline friend String operator+(const String& left, const String& right);
913 inline friend String operator+(const String& left, String&& right);
914 inline friend String operator+(String&& left, const String& right);
915 inline friend String operator+(String&& left, String&& right);
916
917#if defined(__cpp_lib_char8_t) && (__cpp_lib_char8_t >= 201811L)
918 inline String(const std::u8string& str);
919 inline String(char8_t utfChar);
920 inline String(const char8_t* str);
921 inline String(std::size_t count, char8_t ch);
922 inline String(const std::u8string& str, std::size_t pos);
923 inline String(const std::u8string& str, std::size_t pos, std::size_t count);
924 inline String(const char8_t* str, std::size_t count);
925 inline explicit String(std::initializer_list<char8_t> chars);
926 inline explicit String(std::u8string::const_iterator first, std::u8string::const_iterator last);
927
928 inline explicit operator std::u8string() const;
929
930 TGUI_NODISCARD inline std::u8string toUtf8() const;
931
932 inline String& assign(std::size_t count, char8_t ch);
933 inline String& assign(const std::u8string& str, std::size_t pos, std::size_t count = npos);
934 inline String& assign(const char8_t* str, std::size_t count);
935 inline String& assign(std::initializer_list<char8_t> chars);
936 inline String& assign(std::u8string::const_iterator first, std::u8string::const_iterator last);
937
938 inline String& insert(std::size_t index, std::size_t count, char8_t ch);
939 inline String& insert(std::size_t index, const std::u8string& str, std::size_t pos, std::size_t count = npos);
940 inline String& insert(std::size_t index, const char8_t* str, std::size_t count);
941 inline iterator insert(const_iterator pos, char8_t ch);
942 inline iterator insert(const_iterator pos, std::size_t count, char8_t ch);
943 inline iterator insert(const_iterator pos, std::initializer_list<char8_t> chars);
944 inline iterator insert(const_iterator pos, std::u8string::const_iterator first, std::u8string::const_iterator last);
945
946 inline String& append(std::size_t count, char8_t ch);
947 inline String& append(const std::u8string& str, std::size_t pos, std::size_t count = npos);
948 inline String& append(const char8_t* str, std::size_t count);
949 inline String& append(std::initializer_list<char8_t> chars);
950 inline String& append(std::u8string::const_iterator first, std::u8string::const_iterator last);
951
952 TGUI_NODISCARD inline int compare(std::size_t pos1, std::size_t count1, const std::u8string& str, std::size_t pos2, std::size_t count2 = npos) const;
953 TGUI_NODISCARD inline int compare(std::size_t pos1, std::size_t count1, const char8_t* s, std::size_t count2) const;
954
955 inline String& replace(std::size_t pos, std::size_t count, const std::u8string& str, std::size_t pos2, std::size_t count2 = npos);
956 inline String& replace(const_iterator first, const_iterator last, std::u8string::const_iterator first2, std::u8string::const_iterator last2);
957 inline String& replace(std::size_t pos, std::size_t count, const char8_t* cstr, std::size_t count2);
958 inline String& replace(const_iterator first, const_iterator last, const char8_t* cstr, std::size_t count2);
959 inline String& replace(std::size_t pos, std::size_t count, std::size_t count2, char8_t ch);
960 inline String& replace(const_iterator first, const_iterator last, std::size_t count2, char8_t ch);
961 inline String& replace(const_iterator first, const_iterator last, std::initializer_list<char8_t> chars);
962
963 inline void resize(std::size_t count, char8_t ch);
964
965 TGUI_NODISCARD inline bool contains(char8_t c) const noexcept;
966
967 TGUI_NODISCARD inline std::size_t find(const char8_t* s, std::size_t pos, std::size_t count) const;
968 TGUI_NODISCARD inline std::size_t find(char8_t ch, std::size_t pos = 0) const noexcept;
969
970 TGUI_NODISCARD inline std::size_t find_first_of(const char8_t* s, std::size_t pos, std::size_t count) const;
971 TGUI_NODISCARD inline std::size_t find_first_of(char8_t ch, std::size_t pos = 0) const noexcept;
972
973 TGUI_NODISCARD inline std::size_t find_first_not_of(const char8_t* s, std::size_t pos, std::size_t count) const;
974 TGUI_NODISCARD inline std::size_t find_first_not_of(char8_t ch, std::size_t pos = 0) const noexcept;
975
976 TGUI_NODISCARD inline std::size_t rfind(const char8_t* s, std::size_t pos, std::size_t count) const;
977 TGUI_NODISCARD inline std::size_t rfind(char8_t ch, std::size_t pos = npos) const noexcept;
978
979 TGUI_NODISCARD inline std::size_t find_last_of(const char8_t* s, std::size_t pos, std::size_t count) const;
980 TGUI_NODISCARD inline std::size_t find_last_of(char8_t ch, std::size_t pos = npos) const noexcept;
981
982 TGUI_NODISCARD inline std::size_t find_last_not_of(const char8_t* s, std::size_t pos, std::size_t count) const;
983 TGUI_NODISCARD inline std::size_t find_last_not_of(char8_t ch, std::size_t pos = npos) const noexcept;
984
985 TGUI_NODISCARD inline bool starts_with(char8_t ch) const noexcept;
986 TGUI_NODISCARD inline bool ends_with(char8_t ch) const noexcept;
987#endif
988 };
989
991
992 inline bool String::starts_with(StringView sv) const noexcept
993 {
994 return viewStartsWith(StringView(m_string), sv);
995 }
996
997 inline bool String::starts_with(const char32_t* s) const
998 {
999 return viewStartsWith(StringView(m_string), StringView(s));
1000 }
1001
1002 inline bool String::starts_with(const std::u32string& s) const
1003 {
1004 return viewStartsWith(StringView(m_string), StringView(s));
1005 }
1006
1007 inline bool String::starts_with(const String& s) const
1008 {
1009 return viewStartsWith(StringView(m_string), StringView(s));
1010 }
1011
1012 inline bool String::starts_with(char ch) const noexcept
1013 {
1014 return viewStartsWith(StringView(m_string), static_cast<char32_t>(ch));
1015 }
1016
1017 inline bool String::starts_with(wchar_t ch) const noexcept
1018 {
1019 return viewStartsWith(StringView(m_string), static_cast<char32_t>(ch));
1020 }
1021
1022 inline bool String::starts_with(char16_t ch) const noexcept
1023 {
1024 return viewStartsWith(StringView(m_string), static_cast<char32_t>(ch));
1025 }
1026
1027 inline bool String::starts_with(char32_t ch) const noexcept
1028 {
1029 return viewStartsWith(StringView(m_string), ch);
1030 }
1031
1032 inline bool String::ends_with(StringView sv) const noexcept
1033 {
1034 return viewEndsWith(StringView(m_string), sv);
1035 }
1036
1037 inline bool String::ends_with(const char32_t* s) const
1038 {
1039 return viewEndsWith(StringView(m_string), StringView(s));
1040 }
1041
1042 inline bool String::ends_with(const std::u32string& s) const
1043 {
1044 return viewEndsWith(StringView(m_string), StringView(s));
1045 }
1046
1047 inline bool String::ends_with(const String& s) const
1048 {
1049 return viewEndsWith(StringView(m_string), StringView(s));
1050 }
1051
1052 inline bool String::ends_with(char ch) const noexcept
1053 {
1054 return viewEndsWith(StringView(m_string), static_cast<char32_t>(ch));
1055 }
1056
1057 inline bool String::ends_with(wchar_t ch) const noexcept
1058 {
1059 return viewEndsWith(StringView(m_string), static_cast<char32_t>(ch));
1060 }
1061
1062 inline bool String::ends_with(char16_t ch) const noexcept
1063 {
1064 return viewEndsWith(StringView(m_string), static_cast<char32_t>(ch));
1065 }
1066
1067 inline bool String::ends_with(char32_t ch) const noexcept
1068 {
1069 return viewEndsWith(StringView(m_string), ch);
1070 }
1071
1072 TGUI_NODISCARD inline bool operator==(const String& left, StringView right)
1073 {
1074 return StringView(left) == right;
1075 }
1076
1077 TGUI_NODISCARD inline bool operator==(const String& left, const char32_t* right)
1078 {
1079 return StringView(left) == right;
1080 }
1081
1082 TGUI_NODISCARD inline bool operator==(const String& left, const std::u32string& right)
1083 {
1084 return StringView(left) == right;
1085 }
1086
1087 TGUI_NODISCARD inline bool operator==(const String& left, const String& right)
1088 {
1089 return left.m_string == right.m_string;
1090 }
1091
1092 TGUI_NODISCARD inline bool operator!=(const String& left, StringView right)
1093 {
1094 return StringView(left) != right;
1095 }
1096
1097 TGUI_NODISCARD inline bool operator!=(const String& left, const char32_t* right)
1098 {
1099 return StringView(left) != right;
1100 }
1101
1102 TGUI_NODISCARD inline bool operator!=(const String& left, const std::u32string& right)
1103 {
1104 return StringView(left) != right;
1105 }
1106
1107 TGUI_NODISCARD inline bool operator!=(const String& left, const String& right)
1108 {
1109 return left.m_string != right.m_string;
1110 }
1111
1112 TGUI_NODISCARD inline bool operator<(const String& left, StringView right)
1113 {
1114 return StringView(left) < right;
1115 }
1116
1117 TGUI_NODISCARD inline bool operator<(const String& left, const char32_t* right)
1118 {
1119 return StringView(left) < right;
1120 }
1121
1122 TGUI_NODISCARD inline bool operator<(const String& left, const std::u32string& right)
1123 {
1124 return StringView(left) < right;
1125 }
1126
1127 TGUI_NODISCARD inline bool operator<(const String& left, const String& right)
1128 {
1129 return left.m_string < right.m_string;
1130 }
1131
1132 TGUI_NODISCARD inline bool operator<=(const String& left, StringView right)
1133 {
1134 return StringView(left) <= right;
1135 }
1136
1137 TGUI_NODISCARD inline bool operator<=(const String& left, const char32_t* right)
1138 {
1139 return StringView(left) <= right;
1140 }
1141
1142 TGUI_NODISCARD inline bool operator<=(const String& left, const std::u32string& right)
1143 {
1144 return StringView(left) <= right;
1145 }
1146
1147 TGUI_NODISCARD inline bool operator<=(const String& left, const String& right)
1148 {
1149 return left.m_string <= right.m_string;
1150 }
1151
1152 TGUI_NODISCARD inline bool operator>(const String& left, StringView right)
1153 {
1154 return StringView(left) > right;
1155 }
1156
1157 TGUI_NODISCARD inline bool operator>(const String& left, const char32_t* right)
1158 {
1159 return StringView(left) > right;
1160 }
1161
1162 TGUI_NODISCARD inline bool operator>(const String& left, const std::u32string& right)
1163 {
1164 return StringView(left) > right;
1165 }
1166
1167 TGUI_NODISCARD inline bool operator>(const String& left, const String& right)
1168 {
1169 return left.m_string > right.m_string;
1170 }
1171
1172 TGUI_NODISCARD inline bool operator>=(const String& left, StringView right)
1173 {
1174 return StringView(left) >= right;
1175 }
1176
1177 TGUI_NODISCARD inline bool operator>=(const String& left, const char32_t* right)
1178 {
1179 return StringView(left) >= right;
1180 }
1181
1182 TGUI_NODISCARD inline bool operator>=(const String& left, const std::u32string& right)
1183 {
1184 return StringView(left) >= right;
1185 }
1186
1187 TGUI_NODISCARD inline bool operator>=(const String& left, const String& right)
1188 {
1189 return left.m_string >= right.m_string;
1190 }
1191
1192 TGUI_NODISCARD inline String operator+(const String& left, const String& right)
1193 {
1194 return {left.m_string + right.m_string};
1195 }
1196 TGUI_NODISCARD inline String operator+(String&& left, String&& right)
1197 {
1198 return {std::move(left.m_string) + std::move(right.m_string)};
1199 }
1200 TGUI_NODISCARD inline String operator+(String&& left, const String& right)
1201 {
1202 return {std::move(left.m_string) + right.m_string};
1203 }
1204 TGUI_NODISCARD inline String operator+(const String& left, String&& right)
1205 {
1206 return {left.m_string + std::move(right.m_string)};
1207 }
1208
1209 // We don't provide operator<< implmentations for basic_ostream<charX_t> streams because
1210 // even clang 15 can't compile them when using libc++.
1211 // We could define them for VS, GCC and for clang with libstdc++, but there is no real use for them.
1212 TGUI_API std::ostream& operator<<(std::ostream& os, const String& str);
1213 TGUI_API std::wostream& operator<<(std::wostream& os, const String& str);
1214
1215 TGUI_API std::istream& operator>>(std::istream& os, String& str);
1216 TGUI_API std::wistream& operator>>(std::wistream& os, String& str);
1217
1218 // UTF-8 function are defined in the header so that they can be enabled/disabled based on
1219 // the compiler settings without having to recompile TGUI with a different c++ standard.
1220#if defined(__cpp_lib_char8_t) && (__cpp_lib_char8_t >= 201811L)
1221 inline String::String(const std::u8string& str) :
1222 m_string(utf::convertUtf8toUtf32(str.begin(), str.end()))
1223 {
1224 }
1225
1226 inline String::String(char8_t utfChar)
1227 : m_string(1, static_cast<char32_t>(utfChar))
1228 {
1229 }
1230
1231 inline String::String(const char8_t* str)
1232 : String{utf::convertUtf8toUtf32(str, str + std::char_traits<char8_t>::length(str))}
1233 {
1234 }
1235
1236 inline String::String(std::size_t count, char8_t ch)
1237 : m_string(count, static_cast<char32_t>(ch))
1238 {
1239 }
1240
1241 inline String::String(const std::u8string& str, std::size_t pos)
1242 : String{std::u8string(str, pos)}
1243 {
1244 }
1245
1246 inline String::String(const std::u8string& str, std::size_t pos, std::size_t count)
1247 : String{std::u8string(str, pos, count)}
1248 {
1249 }
1250
1251 inline String::String(const char8_t* str, std::size_t count)
1252 : String{std::u8string{str, count}}
1253 {
1254 }
1255
1256 inline String::String(std::initializer_list<char8_t> chars)
1257 : String(std::u8string(chars.begin(), chars.end()))
1258 {
1259 }
1260
1261 inline String::String(std::u8string::const_iterator first, std::u8string::const_iterator last)
1262 : String{std::u8string(first, last)}
1263 {
1264 }
1265
1266 inline String::operator std::u8string() const
1267 {
1268 return utf::convertUtf32toUtf8(m_string);
1269 }
1270
1271 inline std::u8string String::toUtf8() const
1272 {
1273 return utf::convertUtf32toUtf8(m_string);
1274 }
1275
1276 inline String& String::assign(const std::u8string& str, std::size_t pos, std::size_t count)
1277 {
1278 m_string.assign(String{str, pos, count}.m_string);
1279 return *this;
1280 }
1281
1282 inline String& String::assign(const char8_t* str, std::size_t count)
1283 {
1284 m_string.assign(String{str, count}.m_string);
1285 return *this;
1286 }
1287
1288 inline String& String::assign(std::size_t count, char8_t ch)
1289 {
1290 m_string.assign(count, static_cast<char32_t>(ch));
1291 return *this;
1292 }
1293
1294 inline String& String::assign(std::initializer_list<char8_t> chars)
1295 {
1296 m_string.assign(String{chars}.m_string);
1297 return *this;
1298 }
1299
1300 inline String& String::assign(std::u8string::const_iterator first, std::u8string::const_iterator last)
1301 {
1302 m_string.assign(String{first, last}.m_string);
1303 return *this;
1304 }
1305
1306 inline String& String::insert(std::size_t index, std::size_t count, char8_t ch)
1307 {
1308 m_string.insert(index, count, static_cast<char32_t>(ch));
1309 return *this;
1310 }
1311
1312 inline String& String::insert(std::size_t index, const std::u8string& str, std::size_t pos, std::size_t count)
1313 {
1314 m_string.insert(index, String{str, pos, count}.m_string);
1315 return *this;
1316 }
1317
1318 inline String& String::insert(std::size_t index, const char8_t* str, std::size_t count)
1319 {
1320 m_string.insert(index, String{str, count}.m_string);
1321 return *this;
1322 }
1323
1324 inline String::iterator String::insert(String::const_iterator pos, char8_t ch)
1325 {
1326 return m_string.insert(pos, static_cast<char32_t>(ch));
1327 }
1328
1329 inline String::iterator String::insert(String::const_iterator pos, std::size_t count, char8_t ch)
1330 {
1331 return m_string.insert(pos, count, static_cast<char32_t>(ch));
1332 }
1333
1334 inline String::iterator String::insert(String::const_iterator pos, std::initializer_list<char8_t> chars)
1335 {
1336 const std::u32string tmpStr(utf::convertUtf8toUtf32(chars.begin(), chars.end()));
1337 return m_string.insert(pos, tmpStr.begin(), tmpStr.end());
1338 }
1339
1340 inline String::iterator String::insert(String::const_iterator pos, std::u8string::const_iterator first, std::u8string::const_iterator last)
1341 {
1342 const std::u32string tmpStr(utf::convertUtf8toUtf32(first, last));
1343 return m_string.insert(pos, tmpStr.begin(), tmpStr.end());
1344 }
1345
1346 inline String& String::append(std::size_t count, char8_t ch)
1347 {
1348 m_string.append(count, static_cast<char32_t>(ch));
1349 return *this;
1350 }
1351
1352 inline String& String::append(const std::u8string& str, std::size_t pos, std::size_t count)
1353 {
1354 m_string.append(String{str, pos, count}.m_string);
1355 return *this;
1356 }
1357
1358 inline String& String::append(const char8_t* str, std::size_t count)
1359 {
1360 m_string.append(String{str, count}.m_string);
1361 return *this;
1362 }
1363
1364 inline String& String::append(std::initializer_list<char8_t> chars)
1365 {
1366 m_string.append(String{chars}.m_string);
1367 return *this;
1368 }
1369
1370 inline String& String::append(std::u8string::const_iterator first, std::u8string::const_iterator last)
1371 {
1372 m_string.append(String{first, last}.m_string);
1373 return *this;
1374 }
1375
1376 inline int String::compare(std::size_t pos1, std::size_t count1, const std::u8string& str, std::size_t pos2, std::size_t count2) const
1377 {
1378 return m_string.compare(pos1, count1, String{str, pos2, count2}.m_string);
1379 }
1380
1381 inline int String::compare(std::size_t pos1, std::size_t count1, const char8_t* s, std::size_t count2) const
1382 {
1383 return m_string.compare(pos1, count1, String{s, count2}.m_string);
1384 }
1385
1386 inline String& String::replace(std::size_t pos, std::size_t count, const std::u8string& str, std::size_t pos2, std::size_t count2)
1387 {
1388 m_string.replace(pos, count, String{str, pos2, count2}.m_string);
1389 return *this;
1390 }
1391
1392 inline String& String::replace(const_iterator first, const_iterator last, std::u8string::const_iterator first2, std::u8string::const_iterator last2)
1393 {
1394 m_string.replace(first, last, String{first2, last2}.m_string);
1395 return *this;
1396 }
1397
1398 inline String& String::replace(std::size_t pos, std::size_t count, const char8_t* cstr, std::size_t count2)
1399 {
1400 m_string.replace(pos, count, String{cstr, count2}.m_string);
1401 return *this;
1402 }
1403
1404 inline String& String::replace(const_iterator first, const_iterator last, const char8_t* cstr, std::size_t count2)
1405 {
1406 m_string.replace(first, last, String{cstr, count2}.m_string);
1407 return *this;
1408 }
1409
1410 inline String& String::replace(std::size_t pos, std::size_t count, std::size_t count2, char8_t ch)
1411 {
1412 m_string.replace(pos, count, String(count2, ch).m_string);
1413 return *this;
1414 }
1415
1416 inline String& String::replace(const_iterator first, const_iterator last, std::size_t count2, char8_t ch)
1417 {
1418 m_string.replace(first, last, String(count2, ch).m_string);
1419 return *this;
1420 }
1421
1422 inline String& String::replace(const_iterator first, const_iterator last, std::initializer_list<char8_t> chars)
1423 {
1424 m_string.replace(first, last, String{chars}.m_string);
1425 return *this;
1426 }
1427
1428 inline void String::resize(std::size_t count, char8_t ch)
1429 {
1430 m_string.resize(count, static_cast<char32_t>(ch));
1431 }
1432
1433 inline bool String::contains(char8_t c) const noexcept
1434 {
1435 return contains(static_cast<char32_t>(c));
1436 }
1437
1438 inline std::size_t String::find(const char8_t* s, std::size_t pos, std::size_t count) const
1439 {
1440 return m_string.find(String{s, count}.m_string, pos);
1441 }
1442
1443 inline std::size_t String::find(char8_t ch, std::size_t pos) const noexcept
1444 {
1445 return m_string.find(static_cast<char32_t>(ch), pos);
1446 }
1447
1448 inline std::size_t String::find_first_of(const char8_t* s, std::size_t pos, std::size_t count) const
1449 {
1450 return m_string.find_first_of(String{s, count}.m_string, pos);
1451 }
1452
1453 inline std::size_t String::find_first_of(char8_t ch, std::size_t pos) const noexcept
1454 {
1455 return m_string.find_first_of(static_cast<char32_t>(ch), pos);
1456 }
1457
1458 inline std::size_t String::find_first_not_of(const char8_t* s, std::size_t pos, std::size_t count) const
1459 {
1460 return m_string.find_first_not_of(String{s, count}.m_string, pos);
1461 }
1462
1463 inline std::size_t String::find_first_not_of(char8_t ch, std::size_t pos) const noexcept
1464 {
1465 return m_string.find_first_not_of(static_cast<char32_t>(ch), pos);
1466 }
1467
1468 inline std::size_t String::rfind(const char8_t* s, std::size_t pos, std::size_t count) const
1469 {
1470 return m_string.rfind(String{s, count}.m_string, pos);
1471 }
1472
1473 inline std::size_t String::rfind(char8_t ch, std::size_t pos) const noexcept
1474 {
1475 return m_string.rfind(static_cast<char32_t>(ch), pos);
1476 }
1477
1478 inline std::size_t String::find_last_of(const char8_t* s, std::size_t pos, std::size_t count) const
1479 {
1480 return m_string.find_last_of(String{s, count}.m_string, pos);
1481 }
1482
1483 inline std::size_t String::find_last_of(char8_t ch, std::size_t pos) const noexcept
1484 {
1485 return m_string.find_last_of(static_cast<char32_t>(ch), pos);
1486 }
1487
1488 inline std::size_t String::find_last_not_of(const char8_t* s, std::size_t pos, std::size_t count) const
1489 {
1490 return m_string.find_last_not_of(String{s, count}.m_string, pos);
1491 }
1492
1493 inline std::size_t String::find_last_not_of(char8_t ch, std::size_t pos) const noexcept
1494 {
1495 return m_string.find_last_not_of(static_cast<char8_t>(ch), pos);
1496 }
1497
1498 inline bool String::starts_with(char8_t ch) const noexcept
1499 {
1500 return viewStartsWith(StringView(m_string), static_cast<char32_t>(ch));
1501 }
1502
1503 inline bool String::ends_with(char8_t ch) const noexcept
1504 {
1505 return viewEndsWith(StringView(m_string), static_cast<char32_t>(ch));
1506 }
1507#endif
1508
1510}
1511
1513
1514#endif // TGUI_STRING_HPP
Wrapper class to store strings.
Definition String.hpp:96
bool endsWithIgnoreCase(const String &substring) const
Checks whether the last part of the string matches the given substring, case-insensitive.
bool attemptToFloat(float &result) const
Converts the string to a float.
float toFloat(float defaultValue=0) const
Converts the string to a float.
bool equalIgnoreCase(const String &other) const
Compares this string to another and checks if they are equal if ASCII letters would have been lowerca...
static String fromNumber(T value)
Construct the string from a number.
Definition String.hpp:302
static String join(const std::vector< String > &segments, const String &separator)
Joins multiple string segments into a single string.
bool attemptToUInt(unsigned int &result) const
Converts the string to an unsigned int.
String trim() const
Returns a string with the whitespace at the start and end of this string removed.
bool startsWithIgnoreCase(const String &substring) const
Checks whether the first part of the string matches the given substring, case-insensitive.
unsigned int toUInt(unsigned int defaultValue=0) const
Converts the string to an unsigned int.
static String fromNumberRounded(T value, unsigned int decimals)
Construct the string from a floating point number, keeping only a certain amount of decimals behind t...
Definition String.hpp:329
int toInt(int defaultValue=0) const
Converts the string to an integer.
void remove(const String &substring)
Removes all occurrences of the given substring.
std::vector< String > split(const String &delimiter, bool trim=false) const
Splits the string into multiple substrings given the delimiter that separates them.
String toLower() const
Converts the ASCII characters in the string to lowercase.
bool attemptToInt(int &result) const
Converts the string to an integer.
String & replace(const String &searchFor, const String &replaceWith)
Replaces all occurrences of a substring with a replacement string.
String toUpper() const
Converts the ASCII characters in the string to uppercase.
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
TGUI_API bool isWhitespace(char character)
Checks if a character is a whitespace character (e.g. space, tab, carriage return,...
bool viewEndsWith(CharStringView viewToLookInto, CharStringView viewToLookFor)
Checks whether the view ends with the given substring.
Definition StringView.hpp:493
TGUI_API bool isDigit(char32_t character)
Checks whether a character is digit (only 0 to 9 are considered digits, no unicode characters are)
TGUI_API bool isAlpha(char32_t character)
Checks whether a character is an alphabetic character.