Nazara Engine  0.4
A fast, complete, cross-platform API designed for game development
String.hpp
1 // Copyright (C) 2017 Jérôme Leclercq
2 // This file is part of the "Nazara Engine - Core module"
3 // For conditions of distribution and use, see copyright notice in Config.hpp
4 
5 #pragma once
6 
7 #ifndef NAZARA_STRING_HPP
8 #define NAZARA_STRING_HPP
9 
10 #include <Nazara/Prerequesites.hpp>
11 #include <Nazara/Core/Endianness.hpp>
12 #include <cstdarg>
13 #include <iosfwd>
14 #include <memory>
15 #include <string>
16 #include <vector>
17 
18 namespace Nz
19 {
20  struct SerializationContext;
21 
22  class NAZARA_CORE_API String
23  {
24  public:
25  enum Flags
26  {
27  None = 0x00, // Default mode
28  CaseInsensitive = 0x01, // Case insensitive
29  HandleUtf8 = 0x02, // Considers bytes as a list of UTF-8 characters
30  TrimOnlyLeft = 0x04, // Trim(med), only cut the left part of the string
31  TrimOnlyRight = 0x08 // Trim(med), only cut the right part of the string
32  };
33 
34  String();
35  explicit String(char character);
36  String(std::size_t rep, char character);
37  String(std::size_t rep, const char* string);
38  String(std::size_t rep, const char* string, std::size_t length);
39  String(std::size_t rep, const String& string);
40  String(const char* string);
41  String(const char* string, std::size_t length);
42  String(const std::string& string);
43  String(const String& string) = default;
44  String(String&& string) noexcept = default;
45  ~String() = default;
46 
47  String& Append(char character);
48  String& Append(const char* string);
49  String& Append(const char* string, std::size_t length);
50  String& Append(const String& string);
51 
52  void Clear(bool keepBuffer = false);
53 
54  bool Contains(char character, std::intmax_t start = 0, UInt32 flags = None) const;
55  bool Contains(const char* string, std::intmax_t start = 0, UInt32 flags = None) const;
56  bool Contains(const String& string, std::intmax_t start = 0, UInt32 flags = None) const;
57 
58  unsigned int Count(char character, std::intmax_t start = 0, UInt32 flags = None) const;
59  unsigned int Count(const char* string, std::intmax_t start = 0, UInt32 flags = None) const;
60  unsigned int Count(const String& string, std::intmax_t start = 0, UInt32 flags = None) const;
61  unsigned int CountAny(const char* string, std::intmax_t start = 0, UInt32 flags = None) const;
62  unsigned int CountAny(const String& string, std::intmax_t start = 0, UInt32 flags = None) const;
63 
64  bool EndsWith(char character, UInt32 flags = None) const;
65  bool EndsWith(const char* string, UInt32 flags = None) const;
66  bool EndsWith(const char* string, std::size_t length, UInt32 flags = None) const;
67  bool EndsWith(const String& string, UInt32 flags = None) const;
68 
69  std::size_t Find(char character, std::intmax_t start = 0, UInt32 flags = None) const;
70  std::size_t Find(const char* string, std::intmax_t start = 0, UInt32 flags = None) const;
71  std::size_t Find(const String& string, std::intmax_t start = 0, UInt32 flags = None) const;
72  std::size_t FindAny(const char* string, std::intmax_t start = 0, UInt32 flags = None) const;
73  std::size_t FindAny(const String& string, std::intmax_t start = 0, UInt32 flags = None) const;
74  std::size_t FindLast(char character, std::intmax_t start = -1, UInt32 flags = None) const;
75  std::size_t FindLast(const char* string, std::intmax_t start = -1, UInt32 flags = None) const;
76  std::size_t FindLast(const String& string, std::intmax_t start = -1, UInt32 flags = None) const;
77  std::size_t FindLastAny(const char* string, std::intmax_t start = -1, UInt32 flags = None) const;
78  std::size_t FindLastAny(const String& string, std::intmax_t start = -1, UInt32 flags = None) const;
79  std::size_t FindLastWord(const char* string, std::intmax_t start = -1, UInt32 flags = None) const;
80  std::size_t FindLastWord(const String& string, std::intmax_t start = -1, UInt32 flags = None) const;
81  std::size_t FindWord(const char* string, std::intmax_t start = 0, UInt32 flags = None) const;
82  std::size_t FindWord(const String& string, std::intmax_t start = 0, UInt32 flags = None) const;
83 
84  char* GetBuffer();
85  std::size_t GetCapacity() const;
86  std::size_t GetCharacterPosition(std::size_t characterIndex) const;
87  const char* GetConstBuffer() const;
88  std::size_t GetLength() const;
89  std::size_t GetSize() const;
90  std::string GetUtf8String() const;
91  std::u16string GetUtf16String() const;
92  std::u32string GetUtf32String() const;
93  std::wstring GetWideString() const;
94  String GetWord(unsigned int index, UInt32 flags = None) const;
95  std::size_t GetWordPosition(unsigned int index, UInt32 flags = None) const;
96 
97  String& Insert(std::intmax_t pos, char character);
98  String& Insert(std::intmax_t pos, const char* string);
99  String& Insert(std::intmax_t pos, const char* string, std::size_t length);
100  String& Insert(std::intmax_t pos, const String& string);
101 
102  bool IsEmpty() const;
103  bool IsNull() const;
104  bool IsNumber(UInt8 radix = 10, UInt32 flags = CaseInsensitive) const;
105 
106  bool Match(const char* pattern) const;
107  bool Match(const String& pattern) const;
108 
109  String& Prepend(char character);
110  String& Prepend(const char* string);
111  String& Prepend(const char* string, std::size_t length);
112  String& Prepend(const String& string);
113 
114  unsigned int Replace(char oldCharacter, char newCharacter, std::intmax_t start = 0, UInt32 flags = None);
115  unsigned int Replace(const char* oldString, const char* replaceString, std::intmax_t start = 0, UInt32 flags = None);
116  unsigned int Replace(const char* oldString, std::size_t oldLength, const char* replaceString, std::size_t replaceLength, std::intmax_t start = 0, UInt32 flags = None);
117  unsigned int Replace(const String& oldString, const String& replaceString, std::intmax_t start = 0, UInt32 flags = None);
118  unsigned int ReplaceAny(const char* oldCharacters, char replaceCharacter, std::intmax_t start = 0, UInt32 flags = None);
119  //unsigned int ReplaceAny(const char* oldCharacters, const char* replaceString, std::intmax_t start = 0, UInt32 flags = None);
120  //unsigned int ReplaceAny(const String& oldCharacters, const String& replaceString, std::intmax_t start = 0, UInt32 flags = None);
121 
122  void Reserve(std::size_t bufferSize);
123 
124  String& Resize(std::intmax_t size, UInt32 flags = None);
125  String Resized(std::intmax_t size, UInt32 flags = None) const;
126 
127  String& Reverse();
128  String Reversed() const;
129 
130  String& Set(char character);
131  String& Set(std::size_t rep, char character);
132  String& Set(std::size_t rep, const char* string);
133  String& Set(std::size_t rep, const char* string, std::size_t length);
134  String& Set(std::size_t rep, const String& string);
135  String& Set(const char* string);
136  String& Set(const char* string, std::size_t length);
137  String& Set(const std::string& string);
138  String& Set(const String& string);
139  String& Set(String&& string) noexcept;
140 
141  String Simplified(UInt32 flags = None) const;
142  String& Simplify(UInt32 flags = None);
143 
144  unsigned int Split(std::vector<String>& result, char separation = ' ', std::intmax_t start = 0, UInt32 flags = None) const;
145  unsigned int Split(std::vector<String>& result, const char* separation, std::intmax_t start = 0, UInt32 flags = None) const;
146  unsigned int Split(std::vector<String>& result, const char* separation, std::size_t length, std::intmax_t start = 0, UInt32 flags = None) const;
147  unsigned int Split(std::vector<String>& result, const String& separation, std::intmax_t start = 0, UInt32 flags = None) const;
148  unsigned int SplitAny(std::vector<String>& result, const char* separations, std::intmax_t start = 0, UInt32 flags = None) const;
149  unsigned int SplitAny(std::vector<String>& result, const String& separations, std::intmax_t start = 0, UInt32 flags = None) const;
150 
151  bool StartsWith(char character, UInt32 flags = None) const;
152  bool StartsWith(const char* string, UInt32 flags = None) const;
153  bool StartsWith(const String& string, UInt32 flags = None) const;
154 
155  String SubString(std::intmax_t startPos, std::intmax_t endPos = -1) const;
156  String SubStringFrom(char character, std::intmax_t startPos = 0, bool fromLast = false, bool include = false, UInt32 flags = None) const;
157  String SubStringFrom(const char* string, std::intmax_t startPos = 0, bool fromLast = false, bool include = false, UInt32 flags = None) const;
158  String SubStringFrom(const char* string, std::size_t length, std::intmax_t startPos = 0, bool fromLast = false, bool include = false, UInt32 flags = None) const;
159  String SubStringFrom(const String& string, std::intmax_t startPos = 0, bool fromLast = false, bool include = false, UInt32 flags = None) const;
160  String SubStringTo(char character, std::intmax_t startPos = 0, bool toLast = false, bool include = false, UInt32 flags = None) const;
161  String SubStringTo(const char* string, std::intmax_t startPos = 0, bool toLast = false, bool include = false, UInt32 flags = None) const;
162  String SubStringTo(const char* string, std::size_t length, std::intmax_t startPos = 0, bool toLast = false, bool include = false, UInt32 flags = None) const;
163  String SubStringTo(const String& string, std::intmax_t startPos = 0, bool toLast = false, bool include = false, UInt32 flags = None) const;
164 
165  void Swap(String& str);
166 
167  bool ToBool(bool* value, UInt32 flags = None) const;
168  bool ToDouble(double* value) const;
169  bool ToInteger(long long* value, UInt8 radix = 10) const;
170  String ToLower(UInt32 flags = None) const;
171  String ToUpper(UInt32 flags = None) const;
172 
173  String& Trim(UInt32 flags = None);
174  String& Trim(char character, UInt32 flags = None);
175  String Trimmed(UInt32 flags = None) const;
176  String Trimmed(char character, UInt32 flags = None) const;
177 
178  // Méthodes STD
179  char* begin();
180  const char* begin() const;
181  char* end();
182  const char* end() const;
183  void push_front(char c);
184  void push_back(char c);
185  //char* rbegin();
186  //const char* rbegin() const;
187  //char* rend();
188  //const char* rend() const;
189 
190  typedef const char& const_reference;
191  typedef char* iterator;
192  //typedef char* reverse_iterator;
193  typedef char value_type;
194  // Méthodes STD
195 
196  operator std::string() const;
197 
198  char& operator[](std::size_t pos);
199  char operator[](std::size_t pos) const;
200 
201  String& operator=(char character);
202  String& operator=(const char* string);
203  String& operator=(const std::string& string);
204  String& operator=(const String& string);
205  String& operator=(String&& string) noexcept;
206 
207  String operator+(char character) const;
208  String operator+(const char* string) const;
209  String operator+(const std::string& string) const;
210  String operator+(const String& string) const;
211 
212  String& operator+=(char character);
213  String& operator+=(const char* string);
214  String& operator+=(const std::string& string);
215  String& operator+=(const String& string);
216 
217  bool operator==(char character) const;
218  bool operator==(const char* string) const;
219  bool operator==(const std::string& string) const;
220 
221  bool operator!=(char character) const;
222  bool operator!=(const char* string) const;
223  bool operator!=(const std::string& string) const;
224 
225  bool operator<(char character) const;
226  bool operator<(const char* string) const;
227  bool operator<(const std::string& string) const;
228 
229  bool operator<=(char character) const;
230  bool operator<=(const char* string) const;
231  bool operator<=(const std::string& string) const;
232 
233  bool operator>(char character) const;
234  bool operator>(const char* string) const;
235  bool operator>(const std::string& string) const;
236 
237  bool operator>=(char character) const;
238  bool operator>=(const char* string) const;
239  bool operator>=(const std::string& string) const;
240 
241  static String Boolean(bool boolean);
242  static int Compare(const String& first, const String& second);
243  static inline String Format(const char* format, ...);
244  static String FormatVA(const char* format, va_list arg);
245  static String Number(float number);
246  static String Number(double number);
247  static String Number(long double number);
248  static String Number(signed char number, UInt8 radix = 10);
249  static String Number(unsigned char number, UInt8 radix = 10);
250  static String Number(short number, UInt8 radix = 10);
251  static String Number(unsigned short number, UInt8 radix = 10);
252  static String Number(int number, UInt8 radix = 10);
253  static String Number(unsigned int number, UInt8 radix = 10);
254  static String Number(long number, UInt8 radix = 10);
255  static String Number(unsigned long number, UInt8 radix = 10);
256  static String Number(long long number, UInt8 radix = 10);
257  static String Number(unsigned long long number, UInt8 radix = 10);
258  static String Pointer(const void* ptr);
259  static String Unicode(char32_t character);
260  static String Unicode(const char* u8String);
261  static String Unicode(const char16_t* u16String);
262  static String Unicode(const char32_t* u32String);
263  static String Unicode(const wchar_t* wString);
264 
265  NAZARA_CORE_API friend std::istream& operator>>(std::istream& in, String& string);
266  NAZARA_CORE_API friend std::ostream& operator<<(std::ostream& out, const String& string);
267 
268  NAZARA_CORE_API friend String operator+(char character, const String& string);
269  NAZARA_CORE_API friend String operator+(const char* string, const String& nstring);
270  NAZARA_CORE_API friend String operator+(const std::string& string, const String& nstring);
271 
272  NAZARA_CORE_API friend bool operator==(const String& first, const String& second);
273  NAZARA_CORE_API friend bool operator!=(const String& first, const String& second);
274  NAZARA_CORE_API friend bool operator<(const String& first, const String& second);
275  NAZARA_CORE_API friend bool operator<=(const String& first, const String& second);
276  NAZARA_CORE_API friend bool operator>(const String& first, const String& second);
277  NAZARA_CORE_API friend bool operator>=(const String& first, const String& second);
278 
279  NAZARA_CORE_API friend bool operator==(char character, const String& nstring);
280  NAZARA_CORE_API friend bool operator==(const char* string, const String& nstring);
281  NAZARA_CORE_API friend bool operator==(const std::string& string, const String& nstring);
282 
283  NAZARA_CORE_API friend bool operator!=(char character, const String& nstring);
284  NAZARA_CORE_API friend bool operator!=(const char* string, const String& nstring);
285  NAZARA_CORE_API friend bool operator!=(const std::string& string, const String& nstring);
286 
287  NAZARA_CORE_API friend bool operator<(char character, const String& nstring);
288  NAZARA_CORE_API friend bool operator<(const char* string, const String& nstring);
289  NAZARA_CORE_API friend bool operator<(const std::string& string, const String& nstring);
290 
291  NAZARA_CORE_API friend bool operator<=(char character, const String& nstring);
292  NAZARA_CORE_API friend bool operator<=(const char* string, const String& nstring);
293  NAZARA_CORE_API friend bool operator<=(const std::string& string, const String& nstring);
294 
295  NAZARA_CORE_API friend bool operator>(char character, const String& nstring);
296  NAZARA_CORE_API friend bool operator>(const char* string, const String& nstring);
297  NAZARA_CORE_API friend bool operator>(const std::string& string, const String& nstring);
298 
299  NAZARA_CORE_API friend bool operator>=(char character, const String& nstring);
300  NAZARA_CORE_API friend bool operator>=(const char* string, const String& nstring);
301  NAZARA_CORE_API friend bool operator>=(const std::string& string, const String& nstring);
302 
303  static const std::size_t npos;
304 
305  private:
306  struct SharedString;
307 
308  String(std::shared_ptr<SharedString>&& sharedString);
309 
310  void EnsureOwnership(bool discardContent = false);
311  inline void ReleaseString();
312 
313  static const std::shared_ptr<SharedString>& GetEmptyString();
314 
315  std::shared_ptr<SharedString> m_sharedString;
316 
317  struct SharedString
318  {
319  inline SharedString();
320  inline SharedString(std::size_t strSize);
321  inline SharedString(std::size_t strSize, std::size_t strCapacity);
322 
323  std::size_t capacity;
324  std::size_t size;
325  std::unique_ptr<char[]> string;
326  };
327  };
328 
329  class AbstractHash;
330 
331  inline bool HashAppend(AbstractHash* hash, const String& string);
332  NAZARA_CORE_API bool Serialize(SerializationContext& context, const String& string);
333  NAZARA_CORE_API bool Unserialize(SerializationContext& context, String* string);
334 }
335 
336 namespace std
337 {
338  NAZARA_CORE_API istream& getline(istream& is, Nz::String& str);
339  NAZARA_CORE_API istream& getline(istream& is, Nz::String& str, char delim);
340  NAZARA_CORE_API void swap(Nz::String& lhs, Nz::String& rhs);
341 
342  template<>
343  struct hash<Nz::String>;
344 }
345 
346 #include <Nazara/Core/String.inl>
347 
348 #endif // NAZARA_STRING_HPP
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
Core class that represents the behaviour of the hash classes.
Definition: AbstractHash.hpp:18
bool operator<(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1517
bool Unserialize(SerializationContext &context, bool *value)
Unserializes a boolean.
Definition: Algorithm.inl:279
String operator+(char character, const String &string)
Concatenates the character to the string.
Definition: String.cpp:5506
bool operator>(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1566
Core class that represents a string.
Definition: String.hpp:22
STL namespace.
bool operator!=(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1503
Core class that represents a Unicode character.
Definition: Unicode.hpp:15
std::istream & operator>>(std::istream &is, String &str)
Inputs the stream into the string.
Definition: String.cpp:5458
Structure containing a serialization/unserialization context states.
Definition: SerializationContext.hpp:18
bool Serialize(SerializationContext &context, bool value)
Serializes a boolean.
Definition: Algorithm.inl:214
bool operator==(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1469
bool operator>=(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1580
bool operator<=(const Bitset< Block, Allocator > &lhs, const Nz::Bitset< Block, Allocator > &rhs)
Compares two bitsets.
Definition: Bitset.inl:1552