SFML - Simple and Fast Multimedia Library

SFML 3.0.2
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-2025 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
30#include <SFML/System/Export.hpp>
31
32#include <SFML/System/Utf.hpp>
33
34#include <locale>
35#include <string>
36
37#include <cstddef>
38#include <cstdint>
39
40
41namespace sf
42{
47 struct SFML_SYSTEM_API U8StringCharTraits
48{
49 // NOLINTBEGIN(readability-identifier-naming)
50 using char_type = std::uint8_t;
51 using int_type = std::char_traits<char>::int_type;
52 using off_type = std::char_traits<char>::off_type;
53 using pos_type = std::char_traits<char>::pos_type;
54 using state_type = std::char_traits<char>::state_type;
55
56 static void assign(char_type& c1, char_type c2) noexcept;
57 static char_type* assign(char_type* s, std::size_t n, char_type c);
58 static bool eq(char_type c1, char_type c2) noexcept;
59 static bool lt(char_type c1, char_type c2) noexcept;
60 static char_type* move(char_type* s1, const char_type* s2, std::size_t n);
61 static char_type* copy(char_type* s1, const char_type* s2, std::size_t n);
62 static int compare(const char_type* s1, const char_type* s2, std::size_t n);
63 static std::size_t length(const char_type* s);
64 static const char_type* find(const char_type* s, std::size_t n, const char_type& c);
65 static char_type to_char_type(int_type i) noexcept;
66 static int_type to_int_type(char_type c) noexcept;
67 static bool eq_int_type(int_type i1, int_type i2) noexcept;
68 static int_type eof() noexcept;
69 static int_type not_eof(int_type i) noexcept;
70 // NOLINTEND(readability-identifier-naming)
71};
72
81using U8String = std::basic_string<std::uint8_t, U8StringCharTraits>;
82
88class SFML_SYSTEM_API String
89{
90public:
92 // Types
94 using Iterator = std::u32string::iterator;
95 using ConstIterator = std::u32string::const_iterator;
96
98 // Static member data
100 // NOLINTBEGIN(readability-identifier-naming)
102 static inline const std::size_t InvalidPos{std::u32string::npos};
103 // NOLINTEND(readability-identifier-naming)
104
111 String() = default;
112
119 String(std::nullptr_t, const std::locale& = {}) = delete;
120
131 String(char ansiChar, const std::locale& locale = {});
132
139 String(wchar_t wideChar);
140
147 String(char32_t utf32Char);
148
159 String(const char* ansiString, const std::locale& locale = {});
160
171 String(const std::string& ansiString, const std::locale& locale = {});
172
179 String(const wchar_t* wideString);
180
187 String(const std::wstring& wideString);
188
195 String(const char32_t* utf32String);
196
203 String(std::u32string utf32String);
204
216 template <typename T>
217 [[nodiscard]] static String fromUtf8(T begin, T end);
218
230 template <typename T>
231 [[nodiscard]] static String fromUtf16(T begin, T end);
232
248 template <typename T>
249 [[nodiscard]] static String fromUtf32(T begin, T end);
250
266 operator std::string() const;
267
281 operator std::wstring() const;
282
298 [[nodiscard]] std::string toAnsiString(const std::locale& locale = {}) const;
299
311 [[nodiscard]] std::wstring toWideString() const;
312
321 [[nodiscard]] sf::U8String toUtf8() const;
322
331 [[nodiscard]] std::u16string toUtf16() const;
332
344 [[nodiscard]] std::u32string toUtf32() const;
345
354 String& operator+=(const String& right);
355
367 [[nodiscard]] char32_t operator[](std::size_t index) const;
368
380 [[nodiscard]] char32_t& operator[](std::size_t index);
381
390 void clear();
391
400 [[nodiscard]] std::size_t getSize() const;
401
410 [[nodiscard]] bool isEmpty() const;
411
422 void erase(std::size_t position, std::size_t count = 1);
423
434 void insert(std::size_t position, const String& str);
435
448 [[nodiscard]] std::size_t find(const String& str, std::size_t start = 0) const;
449
462 void replace(std::size_t position, std::size_t length, const String& replaceWith);
463
474 void replace(const String& searchFor, const String& replaceWith);
475
491 [[nodiscard]] String substring(std::size_t position, std::size_t length = InvalidPos) const;
492
504 [[nodiscard]] const char32_t* getData() const;
505
514 [[nodiscard]] Iterator begin();
515
524 [[nodiscard]] ConstIterator begin() const;
525
538 [[nodiscard]] Iterator end();
539
552 [[nodiscard]] ConstIterator end() const;
553
554private:
555 friend SFML_SYSTEM_API bool operator==(const String& left, const String& right);
556 friend SFML_SYSTEM_API bool operator<(const String& left, const String& right);
557
559 // Member data
561 std::u32string m_string;
562};
563
574[[nodiscard]] SFML_SYSTEM_API bool operator==(const String& left, const String& right);
575
586[[nodiscard]] SFML_SYSTEM_API bool operator!=(const String& left, const String& right);
587
598[[nodiscard]] SFML_SYSTEM_API bool operator<(const String& left, const String& right);
599
610[[nodiscard]] SFML_SYSTEM_API bool operator>(const String& left, const String& right);
611
622[[nodiscard]] SFML_SYSTEM_API bool operator<=(const String& left, const String& right);
623
634[[nodiscard]] SFML_SYSTEM_API bool operator>=(const String& left, const String& right);
635
646[[nodiscard]] SFML_SYSTEM_API String operator+(const String& left, const String& right);
647
648} // namespace sf
649
650#include <SFML/System/String.inl>
651
652
Export.hpp
SFML_SYSTEM_API
#define SFML_SYSTEM_API
Definition System/Export.hpp:42
Utf.hpp
sf::String::begin
ConstIterator begin() const
Return an iterator to the beginning of the string.
sf::String::getData
const char32_t * getData() const
Get a pointer to the C-style array of characters.
sf::String::String
String(const std::string &ansiString, const std::locale &locale={})
Construct from an ANSI string and a locale.
sf::String::operator>=
bool operator>=(const String &left, const String &right)
Overload of operator>= to compare two UTF-32 strings.
sf::String::toAnsiString
std::string toAnsiString(const std::locale &locale={}) const
Convert the Unicode string to an ANSI string.
sf::String::String
String()=default
Default constructor.
sf::String::toUtf8
sf::U8String toUtf8() const
Convert the Unicode string to a UTF-8 string.
sf::String::isEmpty
bool isEmpty() const
Check whether the string is empty or not.
sf::String::String
String(std::u32string utf32String)
Construct from an UTF-32 string.
sf::String::clear
void clear()
Clear the string.
sf::String::operator!=
bool operator!=(const String &left, const String &right)
Overload of operator!= to compare two UTF-32 strings.
sf::String::operator==
friend bool operator==(const String &left, const String &right)
sf::String::substring
String substring(std::size_t position, std::size_t length=InvalidPos) const
Return a part of the string.
sf::String::String
String(char ansiChar, const std::locale &locale={})
Construct from a single ANSI character and a locale.
sf::String::operator<
friend bool operator<(const String &left, const String &right)
sf::String::String
String(const wchar_t *wideString)
Construct from null-terminated C-style wide string.
sf::String::toUtf32
std::u32string toUtf32() const
Convert the Unicode string to a UTF-32 string.
sf::String::String
String(const std::wstring &wideString)
Construct from a wide string.
sf::String::operator[]
char32_t operator[](std::size_t index) const
Overload of operator[] to access a character by its position.
sf::String::String
String(const char *ansiString, const std::locale &locale={})
Construct from a null-terminated C-style ANSI string and a locale.
sf::String::fromUtf16
static String fromUtf16(T begin, T end)
Create a new sf::String from a UTF-16 encoded string.
sf::String::replace
void replace(const String &searchFor, const String &replaceWith)
Replace all occurrences of a substring with a replacement string.
sf::String::begin
Iterator begin()
Return an iterator to the beginning of the string.
sf::String::toWideString
std::wstring toWideString() const
Convert the Unicode string to a wide string.
sf::String::find
std::size_t find(const String &str, std::size_t start=0) const
Find a sequence of one or more characters in the string.
sf::String::fromUtf8
static String fromUtf8(T begin, T end)
Create a new sf::String from a UTF-8 encoded string.
sf::String::erase
void erase(std::size_t position, std::size_t count=1)
Erase one or more characters from the string.
sf::String::String
String(char32_t utf32Char)
Construct from single UTF-32 character.
sf::String::fromUtf32
static String fromUtf32(T begin, T end)
Create a new sf::String from a UTF-32 encoded string.
sf::String::toUtf16
std::u16string toUtf16() const
Convert the Unicode string to a UTF-16 string.
sf::String::InvalidPos
static const std::size_t InvalidPos
Represents an invalid position in the string.
Definition String.hpp:102
sf::String::operator<=
bool operator<=(const String &left, const String &right)
Overload of operator<= to compare two UTF-32 strings.
sf::String::operator[]
char32_t & operator[](std::size_t index)
Overload of operator[] to access a character by its position.
sf::String::ConstIterator
std::u32string::const_iterator ConstIterator
Read-only iterator type.
Definition String.hpp:95
sf::String::end
Iterator end()
Return an iterator to the end of the string.
sf::String::operator>
bool operator>(const String &left, const String &right)
Overload of operator> to compare two UTF-32 strings.
sf::String::String
String(const char32_t *utf32String)
Construct from a null-terminated C-style UTF-32 string.
sf::String::insert
void insert(std::size_t position, const String &str)
Insert one or more characters into the string.
sf::String::replace
void replace(std::size_t position, std::size_t length, const String &replaceWith)
Replace a substring with another string.
sf::String::getSize
std::size_t getSize() const
Get the size of the string.
sf::String::Iterator
std::u32string::iterator Iterator
Iterator type.
Definition String.hpp:94
sf::String::String
String(wchar_t wideChar)
Construct from single wide character.
sf::String::operator+
String operator+(const String &left, const String &right)
Overload of binary operator+ to concatenate two strings.
sf::String::end
ConstIterator end() const
Return an iterator to the end of the string.
sf::String::String
String(std::nullptr_t, const std::locale &={})=delete
Deleted std::nullptr_t constructor.
sf::String::operator+=
String & operator+=(const String &right)
Overload of operator+= to append an UTF-32 string.
sf
Definition AudioResource.hpp:36
sf::U8String
std::basic_string< std::uint8_t, U8StringCharTraits > U8String
Portable replacement for std::basic_string<std::uint8_t>
Definition String.hpp:81
sf::U8StringCharTraits
Character traits for std::uint8_t
Definition String.hpp:48
sf::U8StringCharTraits::eq_int_type
static bool eq_int_type(int_type i1, int_type i2) noexcept
sf::U8StringCharTraits::assign
static void assign(char_type &c1, char_type c2) noexcept
sf::U8StringCharTraits::copy
static char_type * copy(char_type *s1, const char_type *s2, std::size_t n)
sf::U8StringCharTraits::eq
static bool eq(char_type c1, char_type c2) noexcept
sf::U8StringCharTraits::to_int_type
static int_type to_int_type(char_type c) noexcept
sf::U8StringCharTraits::eof
static int_type eof() noexcept
sf::U8StringCharTraits::move
static char_type * move(char_type *s1, const char_type *s2, std::size_t n)
sf::U8StringCharTraits::int_type
std::char_traits< char >::int_type int_type
Definition String.hpp:51
sf::U8StringCharTraits::compare
static int compare(const char_type *s1, const char_type *s2, std::size_t n)
sf::U8StringCharTraits::length
static std::size_t length(const char_type *s)
sf::U8StringCharTraits::state_type
std::char_traits< char >::state_type state_type
Definition String.hpp:54
sf::U8StringCharTraits::lt
static bool lt(char_type c1, char_type c2) noexcept
sf::U8StringCharTraits::not_eof
static int_type not_eof(int_type i) noexcept
sf::U8StringCharTraits::pos_type
std::char_traits< char >::pos_type pos_type
Definition String.hpp:53
sf::U8StringCharTraits::find
static const char_type * find(const char_type *s, std::size_t n, const char_type &c)
sf::U8StringCharTraits::to_char_type
static char_type to_char_type(int_type i) noexcept
sf::U8StringCharTraits::off_type
std::char_traits< char >::off_type off_type
Definition String.hpp:52
sf::U8StringCharTraits::assign
static char_type * assign(char_type *s, std::size_t n, char_type c)
sf::U8StringCharTraits::char_type
std::uint8_t char_type
Definition String.hpp:50