Mon, 03 Mar 2014 01:04:16 +0200
- reformatting... again
88 | 1 | /* |
2 | Copyright 2012-2014 Santeri Piippo | |
3 | All rights reserved. | |
4 | ||
5 | Redistribution and use in source and binary forms, with or without | |
6 | modification, are permitted provided that the following conditions | |
7 | are met: | |
8 | ||
9 | 1. Redistributions of source code must retain the above copyright | |
10 | notice, this list of conditions and the following disclaimer. | |
11 | 2. Redistributions in binary form must reproduce the above copyright | |
12 | notice, this list of conditions and the following disclaimer in the | |
13 | documentation and/or other materials provided with the distribution. | |
14 | 3. The name of the author may not be used to endorse or promote products | |
15 | derived from this software without specific prior written permission. | |
16 | ||
17 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
18 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
19 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
20 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
21 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
22 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
24 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
26 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
27 | */ | |
28 | ||
29 | #ifndef BOTC_STRING_H | |
30 | #define BOTC_STRING_H | |
31 | ||
32 | #include <deque> | |
33 | #include <string> | |
34 | #include <stdarg.h> | |
35 | #include "Types.h" | |
36 | #include "Containers.h" | |
37 | ||
38 | class String; | |
39 | class StringList; | |
40 | ||
41 | // ============================================================================= | |
42 | // | |
43 | class String | |
44 | { | |
45 | public: | |
46 | using StringType = std::string; | |
47 | using Iterator = typename StringType::iterator; | |
48 | using ConstIterator = StringType::const_iterator; | |
49 | ||
50 | String() {} | |
51 | ||
52 | explicit String (char a) : | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
53 | m_string (&a) {} |
88 | 54 | |
55 | String (const char* data) : | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
56 | m_string (data) {} |
88 | 57 | |
58 | String (const StringType& data) : | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
59 | m_string (data) {} |
88 | 60 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
61 | void dump() const; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
62 | int compare (const String& other) const; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
63 | bool endsWith (const String& other); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
64 | int count (char needle) const; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
65 | int firstIndexOf (const char* c, int a = 0) const; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
66 | int lastIndexOf (const char* c, int a = -1) const; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
67 | String toLowercase() const; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
68 | bool isNumeric() const; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
69 | bool maskAgainst (const String& pattern) const; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
70 | int wordPosition (int n) const; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
71 | void replace (const char* a, const char* b); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
72 | StringList split (const String& del) const; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
73 | StringList split (char del) const; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
74 | void sprintf (const char* fmtstr, ...); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
75 | bool startsWith (const String& other); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
76 | String strip (const List<char>& unwanted); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
77 | String mid (long a, long b = -1) const; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
78 | double toDouble (bool* ok = nullptr) const; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
79 | float toFloat (bool* ok = nullptr) const; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
80 | long toLong (bool* ok = nullptr, int base = 10) const; |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
81 | void trim (int n); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
82 | String toUppercase() const; |
88 | 83 | |
84 | String operator+ (const String& data) const; | |
85 | String operator+ (const char* data) const; | |
86 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
87 | static String fromNumber (int a); |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
88 | static String fromNumber (long a); |
88 | 89 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
90 | inline bool isEmpty() const |
88 | 91 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
92 | return m_string[0] == '\0'; |
88 | 93 | } |
94 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
95 | inline void append (const char* data) |
88 | 96 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
97 | m_string.append (data); |
88 | 98 | } |
99 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
100 | inline void append (char data) |
88 | 101 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
102 | m_string.push_back (data); |
88 | 103 | } |
104 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
105 | inline void append (const String& data) |
88 | 106 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
107 | m_string.append (data.chars()); |
88 | 108 | } |
109 | ||
110 | inline Iterator begin() | |
111 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
112 | return m_string.begin(); |
88 | 113 | } |
114 | ||
115 | inline ConstIterator begin() const | |
116 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
117 | return m_string.cbegin(); |
88 | 118 | } |
119 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
120 | inline const char* chars() const |
88 | 121 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
122 | return m_string.c_str(); |
88 | 123 | } |
124 | ||
125 | inline const char* c_str() const | |
126 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
127 | return m_string.c_str(); |
88 | 128 | } |
129 | ||
130 | inline Iterator end() | |
131 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
132 | return m_string.end(); |
88 | 133 | } |
134 | ||
135 | inline ConstIterator end() const | |
136 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
137 | return m_string.end(); |
88 | 138 | } |
139 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
140 | inline void clear() |
88 | 141 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
142 | m_string.clear(); |
88 | 143 | } |
144 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
145 | inline void removeAt (int pos) |
88 | 146 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
147 | m_string.erase (m_string.begin() + pos); |
88 | 148 | } |
149 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
150 | inline void insert (int pos, char c) |
88 | 151 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
152 | m_string.insert (m_string.begin() + pos, c); |
88 | 153 | } |
154 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
155 | inline int length() const |
88 | 156 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
157 | return m_string.length(); |
88 | 158 | } |
159 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
160 | inline void remove (int pos, int len) |
88 | 161 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
162 | m_string.replace (pos, len, ""); |
88 | 163 | } |
164 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
165 | inline void removeFromStart (int len) |
88 | 166 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
167 | remove (0, len); |
88 | 168 | } |
169 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
170 | inline void removeFromEnd (int len) |
88 | 171 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
172 | remove (length() - len, len); |
88 | 173 | } |
174 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
175 | inline void replace (int pos, int n, const String& a) |
88 | 176 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
177 | m_string.replace (pos, n, a.chars()); |
88 | 178 | } |
179 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
180 | inline void shrinkToFit() |
88 | 181 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
182 | m_string.shrink_to_fit(); |
88 | 183 | } |
184 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
185 | inline const StringType& stdString() const |
88 | 186 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
187 | return m_string; |
88 | 188 | } |
189 | ||
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
190 | inline String strip (char unwanted) |
88 | 191 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
192 | return strip ({unwanted}); |
88 | 193 | } |
194 | ||
195 | // ============================================================================= | |
196 | // | |
197 | inline String operator+ (int num) const | |
198 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
199 | return *this + String::fromNumber (num); |
88 | 200 | } |
201 | ||
202 | // ============================================================================= | |
203 | // | |
204 | inline String& operator+= (const String data) | |
205 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
206 | append (data); |
88 | 207 | return *this; |
208 | } | |
209 | ||
210 | // ============================================================================= | |
211 | // | |
212 | inline String& operator+= (const char* data) | |
213 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
214 | append (data); |
88 | 215 | return *this; |
216 | } | |
217 | ||
218 | // ============================================================================= | |
219 | // | |
220 | inline String& operator+= (int num) | |
221 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
222 | return operator+= (String::fromNumber (num)); |
88 | 223 | } |
224 | ||
225 | // ============================================================================= | |
226 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
227 | inline void prepend (String a) |
88 | 228 | { |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
229 | m_string = (a + m_string).stdString(); |
88 | 230 | } |
231 | ||
232 | // ============================================================================= | |
233 | // | |
234 | inline String& operator+= (const char data) | |
235 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
236 | append (data); |
88 | 237 | return *this; |
238 | } | |
239 | ||
240 | // ============================================================================= | |
241 | // | |
242 | inline String operator- (int n) const | |
243 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
244 | String newString = m_string; |
88 | 245 | newString -= n; |
246 | return newString; | |
247 | } | |
248 | ||
249 | // ============================================================================= | |
250 | // | |
251 | inline String& operator-= (int n) | |
252 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
253 | trim (n); |
88 | 254 | return *this; |
255 | } | |
256 | ||
257 | // ============================================================================= | |
258 | // | |
259 | inline String operator+() const | |
260 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
261 | return toUppercase(); |
88 | 262 | } |
263 | ||
264 | // ============================================================================= | |
265 | // | |
266 | inline String operator-() const | |
267 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
268 | return toLowercase(); |
88 | 269 | } |
270 | ||
271 | // ============================================================================= | |
272 | // | |
273 | inline bool operator== (const String& other) const | |
274 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
275 | return stdString() == other.stdString(); |
88 | 276 | } |
277 | ||
278 | // ============================================================================= | |
279 | // | |
280 | inline bool operator== (const char* other) const | |
281 | { | |
282 | return operator== (String (other)); | |
283 | } | |
284 | ||
285 | // ============================================================================= | |
286 | // | |
287 | inline bool operator!= (const String& other) const | |
288 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
289 | return stdString() != other.stdString(); |
88 | 290 | } |
291 | ||
292 | // ============================================================================= | |
293 | // | |
294 | inline bool operator!= (const char* other) const | |
295 | { | |
296 | return operator!= (String (other)); | |
297 | } | |
298 | ||
299 | // ============================================================================= | |
300 | // | |
301 | inline bool operator> (const String& other) const | |
302 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
303 | return stdString() > other.stdString(); |
88 | 304 | } |
305 | ||
306 | // ============================================================================= | |
307 | // | |
308 | inline bool operator< (const String& other) const | |
309 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
310 | return stdString() < other.stdString(); |
88 | 311 | } |
312 | ||
313 | // ============================================================================= | |
314 | // | |
315 | inline operator const char*() const | |
316 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
317 | return chars(); |
88 | 318 | } |
319 | ||
320 | // ============================================================================= | |
321 | // | |
322 | inline operator const StringType&() const | |
323 | { | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
324 | return stdString(); |
88 | 325 | } |
326 | ||
327 | // ============================================================================= | |
328 | // | |
329 | // Difference between indices @a and @b. @b can be -1, in which | |
330 | // case it will be length() - 1. | |
331 | // | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
332 | inline int indexDifference (int a, int b) |
88 | 333 | { |
334 | assert (b == -1 || b >= a); | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
335 | return (b != -1 ? b - a : length() - 1 - a); |
88 | 336 | } |
337 | ||
338 | private: | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
339 | StringType m_string; |
88 | 340 | }; |
341 | ||
342 | // ============================================================================= | |
343 | // | |
344 | class StringList : public List<String> | |
345 | { | |
346 | public: | |
347 | StringList() {} | |
348 | StringList (std::initializer_list<String> vals) : | |
349 | List<String> (vals) {} | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
350 | StringList (const List<String>& a) : List<String> (a.deque()) {} |
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
351 | StringList (const WrappedList& a) : List<String> (a) {} |
88 | 352 | |
115
9be16e1c1e44
- reformatting... again
Teemu Piippo <crimsondusk64@gmail.com>
parents:
88
diff
changeset
|
353 | String join (const String& delim); |
88 | 354 | }; |
355 | ||
356 | ||
357 | // ============================================================================= | |
358 | // | |
359 | inline bool operator== (const char* a, const String& b) | |
360 | { | |
361 | return b == a; | |
362 | } | |
363 | ||
364 | ||
365 | // ============================================================================= | |
366 | // | |
367 | inline String operator+ (const char* a, const String& b) | |
368 | { | |
369 | return String (a) + b; | |
370 | } | |
371 | ||
372 | #endif // BOTC_STRING_H |