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-2024 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 <string>
36
37#include <cstddef>
38#include <cstdint>
39
40
41namespace sf
42{
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
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
#define SFML_SYSTEM_API
Utility string class that automatically handles conversions between types and encodings.
Definition String.hpp:89
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.
std::string toAnsiString(const std::locale &locale={}) const
Convert the Unicode string to an ANSI string.
String()=default
Default constructor.
sf::U8String toUtf8() const
Convert the Unicode string to a UTF-8 string.
bool isEmpty() const
Check whether the string is empty or not.
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.
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.
String(const char *ansiString, const std::locale &locale={})
Construct from a null-terminated C-style ANSI string and a locale.
static String fromUtf16(T begin, T end)
Create a new sf::String from a UTF-16 encoded string.
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::wstring toWideString() const
Convert the Unicode string to a wide 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.
static String fromUtf8(T begin, T end)
Create a new sf::String from a UTF-8 encoded 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.
std::u16string toUtf16() const
Convert the Unicode string to a UTF-16 string.
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:95
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.
void insert(std::size_t position, const String &str)
Insert one or more characters into the string.
void replace(std::size_t position, std::size_t length, const String &replaceWith)
Replace a substring with another string.
std::size_t getSize() const
Get the size of the string.
std::u32string::iterator Iterator
Iterator type.
Definition String.hpp:94
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:81
Character traits for std::uint8_t
Definition String.hpp:48
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:51
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:54
static bool lt(char_type c1, char_type c2) noexcept
std::char_traits< char >::pos_type pos_type
Definition String.hpp:53
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:52
static char_type * assign(char_type *s, std::size_t n, char_type c)
std::uint8_t char_type
Definition String.hpp:50