|
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) : |
|
53 mString (&a) {} |
|
54 |
|
55 String (const char* data) : |
|
56 mString (data) {} |
|
57 |
|
58 String (const StringType& data) : |
|
59 mString (data) {} |
|
60 |
|
61 void Dump() const; |
|
62 int Compare (const String& other) const; |
|
63 bool EndsWith (const String& other); |
|
64 int Count (char needle) const; |
|
65 int FirstIndexOf (const char* c, int a = 0) const; |
|
66 int LastIndexOf (const char* c, int a = -1) const; |
|
67 String ToLowercase() const; |
|
68 bool IsNumeric() const; |
|
69 bool MaskAgainst (const String& pattern) const; |
|
70 int WordPosition (int n) const; |
|
71 void Replace (const char* a, const char* b); |
|
72 StringList Split (String del) const; |
|
73 StringList Split (char del) const; |
|
74 void SPrintf (const char* fmtstr, ...); |
|
75 bool StartsWith (const String& other); |
|
76 String Strip (const List<char>& unwanted); |
|
77 String Mid (long a, long b = -1) const; |
|
78 double ToDouble (bool* ok = nullptr) const; |
|
79 float ToFloat (bool* ok = nullptr) const; |
|
80 long ToLong (bool* ok = nullptr, int base = 10) const; |
|
81 void Trim (int n); |
|
82 String ToUppercase() const; |
|
83 |
|
84 String operator+ (const String& data) const; |
|
85 String operator+ (const char* data) const; |
|
86 |
|
87 static String FromNumber (int a); |
|
88 static String FromNumber (long a); |
|
89 |
|
90 inline bool IsEmpty() const |
|
91 { |
|
92 return mString[0] == '\0'; |
|
93 } |
|
94 |
|
95 inline void Append (const char* data) |
|
96 { |
|
97 mString.append (data); |
|
98 } |
|
99 |
|
100 inline void Append (const char data) |
|
101 { |
|
102 mString.push_back (data); |
|
103 } |
|
104 |
|
105 inline void Append (const String& data) |
|
106 { |
|
107 mString.append (data.CString()); |
|
108 } |
|
109 |
|
110 inline Iterator begin() |
|
111 { |
|
112 return mString.begin(); |
|
113 } |
|
114 |
|
115 inline ConstIterator begin() const |
|
116 { |
|
117 return mString.cbegin(); |
|
118 } |
|
119 |
|
120 inline const char* CString() const |
|
121 { |
|
122 return mString.c_str(); |
|
123 } |
|
124 |
|
125 inline const char* c_str() const |
|
126 { |
|
127 return mString.c_str(); |
|
128 } |
|
129 |
|
130 inline Iterator end() |
|
131 { |
|
132 return mString.end(); |
|
133 } |
|
134 |
|
135 inline ConstIterator end() const |
|
136 { |
|
137 return mString.end(); |
|
138 } |
|
139 |
|
140 inline void Clear() |
|
141 { |
|
142 mString.clear(); |
|
143 } |
|
144 |
|
145 inline void RemoveAt (int pos) |
|
146 { |
|
147 mString.erase (mString.begin() + pos); |
|
148 } |
|
149 |
|
150 inline void Insert (int pos, char c) |
|
151 { |
|
152 mString.insert (mString.begin() + pos, c); |
|
153 } |
|
154 |
|
155 inline int Length() const |
|
156 { |
|
157 return mString.length(); |
|
158 } |
|
159 |
|
160 inline void Remove (int pos, int len) |
|
161 { |
|
162 mString.replace (pos, len, ""); |
|
163 } |
|
164 |
|
165 inline void RemoveFromStart (int len) |
|
166 { |
|
167 Remove (0, len); |
|
168 } |
|
169 |
|
170 inline void RemoveFromEnd (int len) |
|
171 { |
|
172 Remove (Length() - len, len); |
|
173 } |
|
174 |
|
175 inline void Replace (int pos, int n, const String& a) |
|
176 { |
|
177 mString.replace (pos, n, a.CString()); |
|
178 } |
|
179 |
|
180 inline void ShrinkToFit() |
|
181 { |
|
182 mString.shrink_to_fit(); |
|
183 } |
|
184 |
|
185 inline const StringType& STDString() const |
|
186 { |
|
187 return mString; |
|
188 } |
|
189 |
|
190 inline String Strip (char unwanted) |
|
191 { |
|
192 return Strip ({unwanted}); |
|
193 } |
|
194 |
|
195 // ============================================================================= |
|
196 // |
|
197 inline String operator+ (int num) const |
|
198 { |
|
199 return *this + String::FromNumber (num); |
|
200 } |
|
201 |
|
202 // ============================================================================= |
|
203 // |
|
204 inline String& operator+= (const String data) |
|
205 { |
|
206 Append (data); |
|
207 return *this; |
|
208 } |
|
209 |
|
210 // ============================================================================= |
|
211 // |
|
212 inline String& operator+= (const char* data) |
|
213 { |
|
214 Append (data); |
|
215 return *this; |
|
216 } |
|
217 |
|
218 // ============================================================================= |
|
219 // |
|
220 inline String& operator+= (int num) |
|
221 { |
|
222 return operator+= (String::FromNumber (num)); |
|
223 } |
|
224 |
|
225 // ============================================================================= |
|
226 // |
|
227 inline void Prepend (String a) |
|
228 { |
|
229 mString = (a + mString).STDString(); |
|
230 } |
|
231 |
|
232 // ============================================================================= |
|
233 // |
|
234 inline String& operator+= (const char data) |
|
235 { |
|
236 Append (data); |
|
237 return *this; |
|
238 } |
|
239 |
|
240 // ============================================================================= |
|
241 // |
|
242 inline String operator- (int n) const |
|
243 { |
|
244 String newString = mString; |
|
245 newString -= n; |
|
246 return newString; |
|
247 } |
|
248 |
|
249 // ============================================================================= |
|
250 // |
|
251 inline String& operator-= (int n) |
|
252 { |
|
253 Trim (n); |
|
254 return *this; |
|
255 } |
|
256 |
|
257 // ============================================================================= |
|
258 // |
|
259 inline String operator+() const |
|
260 { |
|
261 return ToUppercase(); |
|
262 } |
|
263 |
|
264 // ============================================================================= |
|
265 // |
|
266 inline String operator-() const |
|
267 { |
|
268 return ToLowercase(); |
|
269 } |
|
270 |
|
271 // ============================================================================= |
|
272 // |
|
273 inline bool operator== (const String& other) const |
|
274 { |
|
275 return STDString() == other.STDString(); |
|
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 { |
|
289 return STDString() != other.STDString(); |
|
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 { |
|
303 return STDString() > other.STDString(); |
|
304 } |
|
305 |
|
306 // ============================================================================= |
|
307 // |
|
308 inline bool operator< (const String& other) const |
|
309 { |
|
310 return STDString() < other.STDString(); |
|
311 } |
|
312 |
|
313 // ============================================================================= |
|
314 // |
|
315 inline operator const char*() const |
|
316 { |
|
317 return CString(); |
|
318 } |
|
319 |
|
320 // ============================================================================= |
|
321 // |
|
322 inline operator const StringType&() const |
|
323 { |
|
324 return STDString(); |
|
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 // |
|
332 inline int IndexDifference (int a, int b) |
|
333 { |
|
334 assert (b == -1 || b >= a); |
|
335 return (b != -1 ? b - a : Length() - 1 - a); |
|
336 } |
|
337 |
|
338 private: |
|
339 StringType mString; |
|
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) {} |
|
350 StringList (const List<String>& a) : List<String> (a.GetDeque()) {} |
|
351 StringList (const ListType& a) : List<String> (a) {} |
|
352 |
|
353 String Join (const String& delim); |
|
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 |