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 m_string (&a) {} |
|
54 |
|
55 String (const char* data) : |
|
56 m_string (data) {} |
|
57 |
|
58 String (const StringType& data) : |
|
59 m_string (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 (const 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 m_string[0] == '\0'; |
|
93 } |
|
94 |
|
95 inline void append (const char* data) |
|
96 { |
|
97 m_string.append (data); |
|
98 } |
|
99 |
|
100 inline void append (char data) |
|
101 { |
|
102 m_string.push_back (data); |
|
103 } |
|
104 |
|
105 inline void append (const String& data) |
|
106 { |
|
107 m_string.append (data.chars()); |
|
108 } |
|
109 |
|
110 inline Iterator begin() |
|
111 { |
|
112 return m_string.begin(); |
|
113 } |
|
114 |
|
115 inline ConstIterator begin() const |
|
116 { |
|
117 return m_string.cbegin(); |
|
118 } |
|
119 |
|
120 inline const char* chars() const |
|
121 { |
|
122 return m_string.c_str(); |
|
123 } |
|
124 |
|
125 inline const char* c_str() const |
|
126 { |
|
127 return m_string.c_str(); |
|
128 } |
|
129 |
|
130 inline Iterator end() |
|
131 { |
|
132 return m_string.end(); |
|
133 } |
|
134 |
|
135 inline ConstIterator end() const |
|
136 { |
|
137 return m_string.end(); |
|
138 } |
|
139 |
|
140 inline void clear() |
|
141 { |
|
142 m_string.clear(); |
|
143 } |
|
144 |
|
145 inline void removeAt (int pos) |
|
146 { |
|
147 m_string.erase (m_string.begin() + pos); |
|
148 } |
|
149 |
|
150 inline void insert (int pos, char c) |
|
151 { |
|
152 m_string.insert (m_string.begin() + pos, c); |
|
153 } |
|
154 |
|
155 inline int length() const |
|
156 { |
|
157 return m_string.length(); |
|
158 } |
|
159 |
|
160 inline void remove (int pos, int len) |
|
161 { |
|
162 m_string.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 m_string.replace (pos, n, a.chars()); |
|
178 } |
|
179 |
|
180 inline void shrinkToFit() |
|
181 { |
|
182 m_string.shrink_to_fit(); |
|
183 } |
|
184 |
|
185 inline const StringType& stdString() const |
|
186 { |
|
187 return m_string; |
|
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 m_string = (a + m_string).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 = m_string; |
|
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 chars(); |
|
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 m_string; |
|
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.deque()) {} |
|
351 StringList (const WrappedList& 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 |
|