Loading...
Searching...
No Matches
String.hpp
Go to the documentation of this file.
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2026 Laurent Gomila (laurent@sfml-dev.org)
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#pragma once
26
28// Headers
31
32#include <SFML/System/Utf.hpp>
33
34#include <locale>
35#include <optional>
36#include <string>
37#include <string_view>
38
39#include <cstddef>
40#include <cstdint>
41
42
43namespace sf
44{
50{
51 // NOLINTBEGIN(readability-identifier-naming)
52 using char_type = std::uint8_t;
53 using int_type = std::char_traits<char>::int_type;
54 using off_type = std::char_traits<char>::off_type;
55 using pos_type = std::char_traits<char>::pos_type;
56 using state_type = std::char_traits<char>::state_type;
57
58 static void assign(char_type& c1, char_type c2) noexcept;
59 static char_type* assign(char_type* s, std::size_t n, char_type c);
60 static bool eq(char_type c1, char_type c2) noexcept;
61 static bool lt(char_type c1, char_type c2) noexcept;
62 static char_type* move(char_type* s1, const char_type* s2, std::size_t n);
63 static char_type* copy(char_type* s1, const char_type* s2, std::size_t n);
64 static int compare(const char_type* s1, const char_type* s2, std::size_t n);
65 static std::size_t length(const char_type* s);
66 static const char_type* find(const char_type* s, std::size_t n, const char_type& c);
67 static char_type to_char_type(int_type i) noexcept;
68 static int_type to_int_type(char_type c) noexcept;
69 static bool eq_int_type(int_type i1, int_type i2) noexcept;
70 static int_type eof() noexcept;
71 static int_type not_eof(int_type i) noexcept;
72 // NOLINTEND(readability-identifier-naming)
73};
74
83using U8String = std::basic_string<std::uint8_t, U8StringCharTraits>;
84
91{
92public:
94 // Types
96 using Iterator = std::u32string::iterator;
97 using ConstIterator = std::u32string::const_iterator;
98
100 // Static member data
102 // NOLINTBEGIN(readability-identifier-naming)
104 static inline const std::size_t InvalidPos{std::u32string::npos};
105 // NOLINTEND(readability-identifier-naming)
106
113 String() = default;
114
121 String(std::nullptr_t, const std::locale& = {}) = delete;
122
133 String(char ansiChar, const std::locale& locale = {});
134
141 String(wchar_t wideChar);
142
149 String(char32_t utf32Char);
150
161 String(const char* ansiString, const std::locale& locale = {});
162
173 String(const std::string& ansiString, const std::locale& locale = {});
174
185 String(std::string_view ansiString, const std::locale& locale = {});
186
193 String(const wchar_t* wideString);
194
201 String(const std::wstring& wideString);
202
209 String(std::wstring_view wideString);
210
217 String(const char32_t* utf32String);
218
225 String(std::u32string utf32String);
226
233 String(std::u32string_view utf32String);
234
247 template <typename T>
248 [[nodiscard]] static String fromUtf8(T begin, T end, std::optional<char32_t> replacement = std::nullopt);
249
262 template <typename T>
263 [[nodiscard]] static String fromUtf16(T begin, T end, std::optional<char32_t> replacement = std::nullopt);
264
280 template <typename T>
281 [[nodiscard]] static String fromUtf32(T begin, T end);
282
298 [[deprecated("Use toAnsiString() instead")]] operator std::string() const;
299
313 [[deprecated("Use toWideString() instead")]] operator std::wstring() const;
314
329 [[nodiscard]] std::string toAnsiString(const std::locale& locale = {},
330 std::optional<char> replacement = std::nullopt) const;
331
342 [[nodiscard]] std::wstring toWideString(std::optional<wchar_t> replacement = std::nullopt) const;
343
354 [[nodiscard]] sf::U8String toUtf8(std::optional<std::uint8_t> replacement = std::nullopt) const;
355
366 [[nodiscard]] std::u16string toUtf16(std::optional<std::uint16_t> replacement = std::nullopt) const;
367
379 [[nodiscard]] std::u32string toUtf32() const;
380
389 String& operator+=(const String& right);
390
402 [[nodiscard]] char32_t operator[](std::size_t index) const;
403
415 [[nodiscard]] char32_t& operator[](std::size_t index);
416
425 void clear();
426
435 [[nodiscard]] std::size_t getSize() const;
436
445 [[nodiscard]] bool isEmpty() const;
446
457 void erase(std::size_t position, std::size_t count = 1);
458
469 void insert(std::size_t position, const String& str);
470
483 [[nodiscard]] std::size_t find(const String& str, std::size_t start = 0) const;
484
497 void replace(std::size_t position, std::size_t length, const String& replaceWith);
498
509 void replace(const String& searchFor, const String& replaceWith);
510
526 [[nodiscard]] String substring(std::size_t position, std::size_t length = InvalidPos) const;
527
539 [[nodiscard]] const char32_t* getData() const;
540
549 [[nodiscard]] Iterator begin();
550
559 [[nodiscard]] ConstIterator begin() const;
560
573 [[nodiscard]] Iterator end();
574
587 [[nodiscard]] ConstIterator end() const;
588
608 [[nodiscard]] bool isGraphemeBoundary(std::size_t position) const;
609
626 [[nodiscard]] bool isWordBoundary(std::size_t position) const;
627
641 [[nodiscard]] bool isSentenceBoundary(std::size_t position) const;
642
643private:
644#if defined(SFML_SYSTEM_WINDOWS)
645 friend SFML_SYSTEM_API bool operator==(const String& left, const String& right);
646 friend SFML_SYSTEM_API bool operator<(const String& left, const String& right);
647#else
648 friend bool operator==(const String& left, const String& right);
649 friend bool operator<(const String& left, const String& right);
650#endif
651
653 // Member data
655 std::u32string m_string;
656};
657
668[[nodiscard]] SFML_SYSTEM_API bool operator==(const String& left, const String& right);
669
680[[nodiscard]] SFML_SYSTEM_API bool operator!=(const String& left, const String& right);
681
692[[nodiscard]] SFML_SYSTEM_API bool operator<(const String& left, const String& right);
693
704[[nodiscard]] SFML_SYSTEM_API bool operator>(const String& left, const String& right);
705
716[[nodiscard]] SFML_SYSTEM_API bool operator<=(const String& left, const String& right);
717
728[[nodiscard]] SFML_SYSTEM_API bool operator>=(const String& left, const String& right);
729
740[[nodiscard]] SFML_SYSTEM_API String operator+(const String& left, const String& right);
741
742} // namespace sf
743
744#include <SFML/System/String.inl>
745
746
#define SFML_SYSTEM_API
static String fromUtf16(T begin, T end, std::optional< char32_t > replacement=std::nullopt)
Create a new sf::String from a UTF-16 encoded string.
sf::U8String toUtf8(std::optional< std::uint8_t > replacement=std::nullopt) const
Convert the Unicode string to a UTF-8 string.
ConstIterator begin() const
Return an iterator to the beginning of the string.
const char32_t * getData() const
Get a pointer to the C-style array of characters.
String(const std::string &ansiString, const std::locale &locale={})
Construct from an ANSI string and a locale.
bool operator>=(const String &left, const String &right)
Overload of operator>= to compare two UTF-32 strings.
String()=default
Default constructor.
String(std::string_view ansiString, const std::locale &locale={})
Construct from an ANSI string view and a locale.
std::string toAnsiString(const std::locale &locale={}, std::optional< char > replacement=std::nullopt) const
Convert the Unicode string to an ANSI string.
bool isEmpty() const
Check whether the string is empty or not.
String(std::wstring_view wideString)
Construct from a wide string view.
String(std::u32string utf32String)
Construct from an UTF-32 string.
void clear()
Clear the string.
bool operator!=(const String &left, const String &right)
Overload of operator!= to compare two UTF-32 strings.
friend bool operator==(const String &left, const String &right)
String substring(std::size_t position, std::size_t length=InvalidPos) const
Return a part of the string.
String(char ansiChar, const std::locale &locale={})
Construct from a single ANSI character and a locale.
std::wstring toWideString(std::optional< wchar_t > replacement=std::nullopt) const
Convert the Unicode string to a wide string.
friend bool operator<(const String &left, const String &right)
String(const wchar_t *wideString)
Construct from null-terminated C-style wide string.
std::u32string toUtf32() const
Convert the Unicode string to a UTF-32 string.
String(const std::wstring &wideString)
Construct from a wide string.
char32_t operator[](std::size_t index) const
Overload of operator[] to access a character by its position.
bool isSentenceBoundary(std::size_t position) const
Check if the position before a character is a sentence boundary.
String(const char *ansiString, const std::locale &locale={})
Construct from a null-terminated C-style ANSI string and a locale.
void replace(const String &searchFor, const String &replaceWith)
Replace all occurrences of a substring with a replacement string.
Iterator begin()
Return an iterator to the beginning of the string.
std::u16string toUtf16(std::optional< std::uint16_t > replacement=std::nullopt) const
Convert the Unicode string to a UTF-16 string.
std::size_t find(const String &str, std::size_t start=0) const
Find a sequence of one or more characters in the string.
void erase(std::size_t position, std::size_t count=1)
Erase one or more characters from the string.
String(char32_t utf32Char)
Construct from single UTF-32 character.
static String fromUtf32(T begin, T end)
Create a new sf::String from a UTF-32 encoded string.
static const std::size_t InvalidPos
Represents an invalid position in the string.
Definition String.hpp:104
bool operator<=(const String &left, const String &right)
Overload of operator<= to compare two UTF-32 strings.
char32_t & operator[](std::size_t index)
Overload of operator[] to access a character by its position.
std::u32string::const_iterator ConstIterator
Read-only iterator type.
Definition String.hpp:97
Iterator end()
Return an iterator to the end of the string.
bool operator>(const String &left, const String &right)
Overload of operator> to compare two UTF-32 strings.
String(const char32_t *utf32String)
Construct from a null-terminated C-style UTF-32 string.
bool isGraphemeBoundary(std::size_t position) const
Check if the position before a character is a grapheme boundary.
void insert(std::size_t position, const String &str)
Insert one or more characters into the string.
bool isWordBoundary(std::size_t position) const
Check if the position before a character is a word boundary.
String(std::u32string_view utf32String)
Construct from an UTF-32 string view.
void replace(std::size_t position, std::size_t length, const String &replaceWith)
Replace a substring with another string.
static String fromUtf8(T begin, T end, std::optional< char32_t > replacement=std::nullopt)
Create a new sf::String from a UTF-8 encoded string.
std::size_t getSize() const
Get the size of the string.
std::u32string::iterator Iterator
Iterator type.
Definition String.hpp:96
String(wchar_t wideChar)
Construct from single wide character.
String operator+(const String &left, const String &right)
Overload of binary operator+ to concatenate two strings.
ConstIterator end() const
Return an iterator to the end of the string.
String(std::nullptr_t, const std::locale &={})=delete
Deleted std::nullptr_t constructor.
String & operator+=(const String &right)
Overload of operator+= to append an UTF-32 string.
std::basic_string< std::uint8_t, U8StringCharTraits > U8String
Portable replacement for std::basic_string<std::uint8_t>.
Definition String.hpp:83
Character traits for std::uint8_t.
Definition String.hpp:50
static bool eq_int_type(int_type i1, int_type i2) noexcept
static void assign(char_type &c1, char_type c2) noexcept
static char_type * copy(char_type *s1, const char_type *s2, std::size_t n)
static bool eq(char_type c1, char_type c2) noexcept
static int_type to_int_type(char_type c) noexcept
static int_type eof() noexcept
static char_type * move(char_type *s1, const char_type *s2, std::size_t n)
std::char_traits< char >::int_type int_type
Definition String.hpp:53
static int compare(const char_type *s1, const char_type *s2, std::size_t n)
static std::size_t length(const char_type *s)
std::char_traits< char >::state_type state_type
Definition String.hpp:56
static bool lt(char_type c1, char_type c2) noexcept
static int_type not_eof(int_type i) noexcept
std::char_traits< char >::pos_type pos_type
Definition String.hpp:55
static const char_type * find(const char_type *s, std::size_t n, const char_type &c)
static char_type to_char_type(int_type i) noexcept
std::char_traits< char >::off_type off_type
Definition String.hpp:54
static char_type * assign(char_type *s, std::size_t n, char_type c)
std::uint8_t char_type
Definition String.hpp:52