String.hpp
1 //
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2018 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 #ifndef SFML_STRING_HPP
26 #define SFML_STRING_HPP
27 
29 // Headers
31 #include <SFML/System/Export.hpp>
32 #include <SFML/System/Utf.hpp>
33 #include <iterator>
34 #include <locale>
35 #include <string>
36 
37 
38 namespace sf
39 {
45 class SFML_SYSTEM_API String
46 {
47 public:
48 
50  // Types
52  typedef std::basic_string<Uint32>::iterator Iterator;
53  typedef std::basic_string<Uint32>::const_iterator ConstIterator;
54 
56  // Static member data
58  static const std::size_t InvalidPos;
59 
66  String();
67 
78  String(char ansiChar, const std::locale& locale = std::locale());
79 
86  String(wchar_t wideChar);
87 
94  String(Uint32 utf32Char);
95 
106  String(const char* ansiString, const std::locale& locale = std::locale());
107 
118  String(const std::string& ansiString, const std::locale& locale = std::locale());
119 
126  String(const wchar_t* wideString);
127 
134  String(const std::wstring& wideString);
135 
142  String(const Uint32* utf32String);
143 
150  String(const std::basic_string<Uint32>& utf32String);
151 
158  String(const String& copy);
159 
171  template <typename T>
172  static String fromUtf8(T begin, T end);
173 
185  template <typename T>
186  static String fromUtf16(T begin, T end);
187 
203  template <typename T>
204  static String fromUtf32(T begin, T end);
205 
221  operator std::string() const;
222 
236  operator std::wstring() const;
237 
253  std::string toAnsiString(const std::locale& locale = std::locale()) const;
254 
266  std::wstring toWideString() const;
267 
276  std::basic_string<Uint8> toUtf8() const;
277 
286  std::basic_string<Uint16> toUtf16() const;
287 
299  std::basic_string<Uint32> toUtf32() const;
300 
309  String& operator =(const String& right);
310 
319  String& operator +=(const String& right);
320 
332  Uint32 operator [](std::size_t index) const;
333 
345  Uint32& operator [](std::size_t index);
346 
355  void clear();
356 
365  std::size_t getSize() const;
366 
375  bool isEmpty() const;
376 
387  void erase(std::size_t position, std::size_t count = 1);
388 
399  void insert(std::size_t position, const String& str);
400 
413  std::size_t find(const String& str, std::size_t start = 0) const;
414 
427  void replace(std::size_t position, std::size_t length, const String& replaceWith);
428 
439  void replace(const String& searchFor, const String& replaceWith);
440 
456  String substring(std::size_t position, std::size_t length = InvalidPos) const;
457 
469  const Uint32* getData() const;
470 
480 
490 
504 
518 
519 private:
520 
521  friend SFML_SYSTEM_API bool operator ==(const String& left, const String& right);
522  friend SFML_SYSTEM_API bool operator <(const String& left, const String& right);
523 
525  // Member data
527  std::basic_string<Uint32> m_string;
528 };
529 
540 SFML_SYSTEM_API bool operator ==(const String& left, const String& right);
541 
552 SFML_SYSTEM_API bool operator !=(const String& left, const String& right);
553 
564 SFML_SYSTEM_API bool operator <(const String& left, const String& right);
565 
576 SFML_SYSTEM_API bool operator >(const String& left, const String& right);
577 
588 SFML_SYSTEM_API bool operator <=(const String& left, const String& right);
589 
600 SFML_SYSTEM_API bool operator >=(const String& left, const String& right);
601 
612 SFML_SYSTEM_API String operator +(const String& left, const String& right);
613 
614 #include <SFML/System/String.inl>
615 
616 } // namespace sf
617 
618 
619 #endif // SFML_STRING_HPP
620 
621 
Utility string class that automatically handles conversions between types and encodings.
Definition: String.hpp:46
String(const std::string &ansiString, const std::locale &locale=std::locale())
Construct from an ANSI string and a locale.
const Uint32 * getData() const
Get a pointer to the C-style array of characters.
ConstIterator begin() const
Return an iterator to the beginning of the string.
bool isEmpty() const
Check whether the string is empty or not.
void clear()
Clear the string.
std::basic_string< Uint16 > toUtf16() const
Convert the Unicode string to a UTF-16 string.
String substring(std::size_t position, std::size_t length=InvalidPos) const
Return a part of the string.
String(const wchar_t *wideString)
Construct from null-terminated C-style wide string.
String(const char *ansiString, const std::locale &locale=std::locale())
Construct from a null-terminated C-style ANSI string and a locale.
String(const std::wstring &wideString)
Construct from a wide string.
String(const std::basic_string< Uint32 > &utf32String)
Construct from an UTF-32 string.
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.
std::basic_string< Uint32 >::const_iterator ConstIterator
Read-only iterator type.
Definition: String.hpp:53
String(Uint32 utf32Char)
Construct from single UTF-32 character.
Iterator begin()
Return an iterator to the beginning of the string.
String()
Default constructor.
std::wstring toWideString() const
Convert the Unicode string to a wide string.
std::basic_string< Uint8 > toUtf8() const
Convert the Unicode string to a UTF-8 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.
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:58
Iterator end()
Return an iterator to the end of the string.
std::basic_string< Uint32 >::iterator Iterator
Iterator type.
Definition: String.hpp:52
String(char ansiChar, const std::locale &locale=std::locale())
Construct from a single ANSI character and a locale.
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::string toAnsiString(const std::locale &locale=std::locale()) const
Convert the Unicode string to an ANSI string.
std::size_t getSize() const
Get the size of the string.
String(const Uint32 *utf32String)
Construct from a null-terminated C-style UTF-32 string.
String(wchar_t wideChar)
Construct from single wide character.
ConstIterator end() const
Return an iterator to the end of the string.
std::basic_string< Uint32 > toUtf32() const
Convert the Unicode string to a UTF-32 string.
String(const String &copy)
Copy constructor.