xoreos  0.0.5
ustring.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #ifndef COMMON_USTRING_H
26 #define COMMON_USTRING_H
27 
28 #include <string>
29 #include <sstream>
30 #include <vector>
31 
32 #include <boost/functional/hash.hpp>
33 
34 #include "src/common/types.h"
35 #include "src/common/system.h"
36 
37 #include "utf8cpp/utf8.h"
38 
39 namespace Common {
40 
48 class UString {
49 public:
50  typedef utf8::iterator<std::string::const_iterator> iterator;
51 
52  // Case sensitive compare
53  struct sless : std::binary_function<UString, UString, bool> {
54  bool operator() (const UString &str1, const UString &str2) const {
55  return str1.less(str2);
56  }
57  };
58 
59  // Case insensitive compare
60  struct iless : std::binary_function<UString, UString, bool> {
61  bool operator() (const UString &str1, const UString &str2) const {
62  return str1.lessIgnoreCase(str2);
63  }
64  };
65 
67  UString();
69  UString(const UString &str);
71  UString(const std::string &str);
73  UString(const char *str);
75  UString(const char *str, size_t n);
77  explicit UString(uint32 c, size_t n = 1);
79  UString(iterator sBegin, iterator sEnd);
80  ~UString();
81 
82  UString &operator=(const UString &str);
83  UString &operator=(const std::string &str);
84  UString &operator=(const char *str);
85 
86  bool operator==(const UString &str) const;
87  bool operator!=(const UString &str) const;
88  bool operator<(const UString &str) const;
89  bool operator>(const UString &str) const;
90 
91  UString operator+(const UString &str) const;
92  UString operator+(const std::string &str) const;
93  UString operator+(const char *str) const;
94  UString operator+(uint32 c) const;
95 
96  UString &operator+=(const UString &str);
97  UString &operator+=(const std::string &str);
98  UString &operator+=(const char *str);
100 
101  int strcmp(const UString &str) const;
102  int stricmp(const UString &str) const;
103 
104  bool equals(const UString &str) const;
105  bool equalsIgnoreCase(const UString &str) const;
106 
107  bool less(const UString &str) const;
108  bool lessIgnoreCase(const UString &str) const;
109 
111  void swap(UString &str);
112 
114  void clear();
115 
117  size_t size() const;
118 
120  bool empty() const;
121 
123  const char *c_str() const;
124 
125  iterator begin() const;
126  iterator end() const;
127 
128  iterator findFirst(uint32 c) const;
129  iterator findFirst(const UString &what) const;
130  iterator findLast(uint32 c) const;
131 
132  bool beginsWith(const UString &with) const;
133  bool endsWith(const UString &with) const;
134 
135  bool contains(const UString &what) const;
136  bool contains(uint32 c) const;
137 
138  void truncate(const iterator &it);
139  void truncate(size_t n);
140 
141  void trimLeft();
142  void trimRight();
143  void trim();
144 
146  void replaceAll(uint32 what, uint32 with);
148  void replaceAll(const UString &what, const UString &with);
149 
151  void makeLower();
153  void makeUpper();
154 
156  UString toLower() const;
158  UString toUpper() const;
159 
161  iterator getPosition(size_t n) const;
163  size_t getPosition(iterator it) const;
164 
166  void insert(iterator pos, uint32 c);
168  void insert(iterator pos, const UString &str);
170  void replace(iterator pos, uint32 c);
172  void replace(iterator pos, const UString &str);
174  void erase(iterator from, iterator to);
176  void erase(iterator pos);
177 
178  void split(iterator splitPoint, UString &left, UString &right, bool remove = false) const;
179 
180  UString substr(iterator from, iterator to) const;
181 
183  static UString format(const char *s, ...) GCC_PRINTF(1, 2);
184 
185  static size_t split(const UString &text, uint32 delim, std::vector<UString> &texts);
186 
187  static void splitTextTokens(const UString &text, std::vector<UString> &tokens);
188 
189  static uint32 toLower(uint32 c);
190  static uint32 toUpper(uint32 c);
191 
192  static bool isASCII(uint32 c);
193 
194  static bool isSpace(uint32 c);
195  static bool isDigit(uint32 c);
196  static bool isAlpha(uint32 c);
197  static bool isAlNum(uint32 c);
198  static bool isCntrl(uint32 c);
199 
200  static uint32 fromUTF16(uint16 c);
201 
202 private:
203  std::string _string;
204 
205  size_t _size;
206 
207  void recalculateSize();
208 };
209 
210 
211 // Right-binding concatenation operators
212 static inline UString operator+(const std::string &left, const UString &right) {
213  return UString(left) + right;
214 }
215 
216 static inline UString operator+(const char *left, const UString &right) {
217  return UString(left) + right;
218 }
219 
220 
221 // Hash functions
222 
224  size_t operator()(const UString &str) const {
225  size_t seed = 0;
226 
227  for (UString::iterator it = str.begin(); it != str.end(); ++it)
228  boost::hash_combine<uint32>(seed, *it);
229 
230  return seed;
231  }
232 };
233 
235  size_t operator()(const UString &str) const {
236  size_t seed = 0;
237 
238  for (UString::iterator it = str.begin(); it != str.end(); ++it)
239  boost::hash_combine<uint32>(seed, UString::toLower(*it));
240 
241  return seed;
242  }
243 };
244 
245 } // End of namespace Common
246 
247 #endif // COMMON_USTRING_H
size_t operator()(const UString &str) const
Definition: ustring.h:224
void insert(iterator pos, uint32 c)
Insert character c in front of this position.
Definition: ustring.cpp:513
UString operator+(const UString &str) const
Definition: ustring.cpp:106
Definition: 2dafile.h:39
A class holding an UTF-8 string.
Definition: ustring.h:48
bool beginsWith(const UString &with) const
Definition: ustring.cpp:295
UString & operator=(const UString &str)
Definition: ustring.cpp:69
bool equalsIgnoreCase(const UString &str) const
Definition: ustring.cpp:218
bool endsWith(const UString &with) const
Definition: ustring.cpp:315
void truncate(const iterator &it)
Definition: ustring.cpp:343
iterator getPosition(size_t n) const
Convert a numerical position into an iterator.
Definition: ustring.cpp:501
iterator begin() const
Definition: ustring.cpp:253
size_t operator()(const UString &str) const
Definition: ustring.h:235
STL namespace.
UString & operator+=(const UString &str)
Definition: ustring.cpp:138
iterator findFirst(uint32 c) const
Definition: ustring.cpp:261
void swap(UString &str)
Swap the contents of the string with this string&#39;s.
Definition: ustring.cpp:230
void makeUpper()
Convert the string to uppercase.
Definition: ustring.cpp:477
bool lessIgnoreCase(const UString &str) const
Definition: ustring.cpp:226
bool less(const UString &str) const
Definition: ustring.cpp:222
bool contains(const UString &what) const
Definition: ustring.cpp:335
UString substr(iterator from, iterator to) const
Definition: ustring.cpp:706
utf8::iterator< std::string::const_iterator > iterator
Definition: ustring.h:50
const char * c_str() const
Return the (utf8 encoded) string data.
Definition: ustring.cpp:249
void replaceAll(uint32 what, uint32 with)
Replace all occurrences of a character with another character.
Definition: ustring.cpp:435
static UString format(const char *s,...) GCC_PRINTF(1
Print formatted data into an UString object, similar to sprintf().
Definition: ustring.cpp:718
uint16_t uint16
Definition: types.h:202
void trimRight()
Definition: ustring.cpp:410
static void splitTextTokens(const UString &text, std::vector< UString > &tokens)
Definition: ustring.cpp:645
bool operator<(const UString &str) const
Definition: ustring.cpp:98
bool equals(const UString &str) const
Definition: ustring.cpp:214
bool empty() const
Is the string empty?
Definition: ustring.cpp:245
Low-level type definitions to handle fixed width types portably.
iterator findLast(uint32 c) const
Definition: ustring.cpp:279
static bool isAlpha(uint32 c)
Is the character an ASCII alphabetic character?
Definition: ustring.cpp:797
bool operator()(const UString &str1, const UString &str2) const
Definition: ustring.h:54
void replace(iterator pos, uint32 c)
Replace the character at this position with c.
Definition: ustring.cpp:553
bool operator==(const UString &str) const
Definition: ustring.cpp:90
int strcmp(const UString &str) const
Definition: ustring.cpp:170
void recalculateSize()
Definition: ustring.cpp:756
UString()
Construct an empty string.
Definition: ustring.cpp:37
UString toLower() const
Return a lowercased copy of the string.
Definition: ustring.cpp:481
void trimLeft()
Definition: ustring.cpp:395
bool operator!=(const UString &str) const
Definition: ustring.cpp:94
static bool isASCII(uint32 c)
Is the character an ASCII character?
Definition: ustring.cpp:785
size_t size() const
Return the size of the string, in characters.
Definition: ustring.cpp:241
uint32_t uint32
Definition: types.h:204
Low-level detection of architecture/system properties.
bool operator>(const UString &str) const
Definition: ustring.cpp:102
void erase(iterator from, iterator to)
Erase the character within this range.
Definition: ustring.cpp:598
bool operator()(const UString &str1, const UString &str2) const
Definition: ustring.h:61
static bool isDigit(uint32 c)
Is the character an ASCII digit character?
Definition: ustring.cpp:793
void split(iterator splitPoint, UString &left, UString &right, bool remove=false) const
Definition: ustring.cpp:621
size_t _size
Definition: ustring.h:205
#define GCC_PRINTF(x, y)
Definition: system.h:161
iterator end() const
Definition: ustring.cpp:257
static bool isCntrl(uint32 c)
Is the character an ASCII control character?
Definition: ustring.cpp:805
UString toUpper() const
Return an uppercased copy of the string.
Definition: ustring.cpp:491
void clear()
Clear the string&#39;s contents.
Definition: ustring.cpp:236
std::string _string
Internal string holding the actual data.
Definition: ustring.h:203
void makeLower()
Convert the string to lowercase.
Definition: ustring.cpp:473
static bool isAlNum(uint32 c)
Is the character an ASCII alphanumeric character?
Definition: ustring.cpp:801
int stricmp(const UString &str) const
Definition: ustring.cpp:192
static bool isSpace(uint32 c)
Is the character an ASCII space character?
Definition: ustring.cpp:789
static uint32 fromUTF16(uint16 c)
Definition: ustring.cpp:809