String.hpp
1 //
3 // SFML - Simple and Fast Multimedia Library
4 // Copyright (C) 2007-2014 Laurent Gomila (laurent.gom@gmail.com)
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 CPP3DS_STRING_HPP
26 #define CPP3DS_STRING_HPP
27 
29 // Headers
31 #include <cpp3ds/System/Utf.hpp>
32 #include <string>
33 #include <locale>
34 
35 namespace cpp3ds
36 {
42 class String
43 {
44 public :
45 
47  // Types
49  typedef std::basic_string<Uint32>::iterator Iterator;
50  typedef std::basic_string<Uint32>::const_iterator ConstIterator;
51 
53  // Static member data
55  static const std::size_t InvalidPos;
56 
63  String();
64 
75  String(char ansiChar, const std::locale& locale = std::locale());
76 
83  String(wchar_t wideChar);
84 
91  String(Uint32 utf32Char);
92 
103  String(const char* ansiString, const std::locale& locale = std::locale());
104 
115  String(const std::string& ansiString, const std::locale& locale = std::locale());
116 
123  String(const wchar_t* wideString);
124 
131  String(const std::wstring& wideString);
132 
139  String(const Uint32* utf32String);
140 
147  String(const std::basic_string<Uint32>& utf32String);
148 
155  String(const String& copy);
156 
168  template <typename T>
169  static String fromUtf8(T begin, T end);
170 
182  template <typename T>
183  static String fromUtf16(T begin, T end);
184 
200  template <typename T>
201  static String fromUtf32(T begin, T end);
202 
218  operator std::string() const;
219 
233  operator std::wstring() const;
234 
250  std::string toAnsiString(const std::locale& locale = std::locale()) const;
251 
263  std::wstring toWideString() const;
264 
273  std::basic_string<Uint8> toUtf8() const;
274 
283  std::basic_string<Uint16> toUtf16() const;
284 
296  std::basic_string<Uint32> toUtf32() const;
297 
306  String& operator =(const String& right);
307 
316  String& operator +=(const String& right);
317 
329  Uint32 operator [](std::size_t index) const;
330 
342  Uint32& operator [](std::size_t index);
343 
352  void clear();
353 
362  std::size_t getSize() const;
363 
372  bool isEmpty() const;
373 
384  void erase(std::size_t position, std::size_t count = 1);
385 
396  void insert(std::size_t position, const String& str);
397 
410  std::size_t find(const String& str, std::size_t start = 0) const;
411 
424  void replace(std::size_t position, std::size_t length, const String& replaceWith);
425 
436  void replace(const String& searchFor, const String& replaceWith);
437 
453  String substring(std::size_t position, std::size_t length = InvalidPos) const;
454 
466  const Uint32* getData() const;
467 
476  Iterator begin();
477 
486  ConstIterator begin() const;
487 
500  Iterator end();
501 
514  ConstIterator end() const;
515 
516 private :
517 
518  friend bool operator ==(const String& left, const String& right);
519  friend bool operator <(const String& left, const String& right);
520 
522  // Member data
524  std::basic_string<Uint32> m_string;
525 };
526 
537 bool operator ==(const String& left, const String& right);
538 
549 bool operator !=(const String& left, const String& right);
550 
561 bool operator <(const String& left, const String& right);
562 
573 bool operator >(const String& left, const String& right);
574 
585 bool operator <=(const String& left, const String& right);
586 
597 bool operator >=(const String& left, const String& right);
598 
609 String operator +(const String& left, const String& right);
610 
611 #include <cpp3ds/System/String.inl>
612 
613 } // namespace cpp3ds
614 
615 
616 #endif
617 
618 
Uint32 operator[](std::size_t index) const
Overload of [] operator to access a character by its position.
Iterator end()
Return an iterator to the beginning of the string.
String()
Default constructor.
std::basic_string< Uint32 >::iterator Iterator
Iterator type.
Definition: String.hpp:49
bool isEmpty() const
Check whether the string is empty or not.
static String fromUtf8(T begin, T end)
Create a new cpp3ds::String from a UTF-8 encoded string.
void clear()
Clear the string.
Utility string class that automatically handles conversions between types and encodings.
Definition: String.hpp:42
const Uint32 * getData() const
Get a pointer to the C-style array of characters.
Iterator begin()
Return an iterator to the beginning of the string.
void replace(std::size_t position, std::size_t length, const String &replaceWith)
Replace a substring with another string.
String & operator+=(const String &right)
Overload of += operator to append an UTF-32 string.
std::size_t getSize() const
Get the size of the string.
static String fromUtf16(T begin, T end)
Create a new cpp3ds::String from a UTF-16 encoded string.
std::string toAnsiString(const std::locale &locale=std::locale()) const
Convert the unicode string to an ANSI string.
void insert(std::size_t position, const String &str)
Insert one or more characters into the string.
static const std::size_t InvalidPos
Represents an invalid position in the string.
Definition: String.hpp:55
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 fromUtf32(T begin, T end)
Create a new cpp3ds::String from a UTF-32 encoded string.
String substring(std::size_t position, std::size_t length=InvalidPos) const
Return a part of the string.
std::basic_string< Uint32 > toUtf32() const
Convert the unicode string to a UTF-32 string.
std::basic_string< Uint32 >::const_iterator ConstIterator
Constant iterator type.
Definition: String.hpp:50
void erase(std::size_t position, std::size_t count=1)
Erase one or more characters from the string.
std::wstring toWideString() const
Convert the unicode string to a wide string.
std::basic_string< Uint16 > toUtf16() const
Convert the unicode string to a UTF-16 string.
std::basic_string< Uint8 > toUtf8() const
Convert the unicode string to a UTF-8 string.
String & operator=(const String &right)
Overload of assignment operator.