|
1 /* |
|
2 Copyright 2014 Teemu 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. Neither the name of the copyright holder nor the names of its |
|
15 contributors may be used to endorse or promote products derived from |
|
16 this software without specific prior written permission. |
|
17 |
|
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
19 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
|
20 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
|
21 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER |
|
22 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
|
23 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
24 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
|
25 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
|
26 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
|
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 */ |
|
30 |
|
31 #pragma once |
|
32 #include <deque> |
|
33 #include <string> |
|
34 #include <stdarg.h> |
|
35 #include "basics.h" |
|
36 #include "list.h" |
|
37 |
|
38 class String; |
|
39 class StringList; |
|
40 |
|
41 class String |
|
42 { |
|
43 public: |
|
44 String() {} |
|
45 |
|
46 explicit String (char a) : |
|
47 m_string ({ a, '\0' }) {} |
|
48 |
|
49 String (const char* data) : |
|
50 m_string (data) {} |
|
51 |
|
52 String (const std::string& data) : |
|
53 m_string (data) {} |
|
54 |
|
55 String (const Vector<char>& data) : |
|
56 m_string (data.data(), data.size()) {} |
|
57 |
|
58 inline METHOD append (const char* data) -> void; |
|
59 inline METHOD append (char data) -> void; |
|
60 inline METHOD append (const String& data) -> void; |
|
61 inline METHOD begin() -> std::string::iterator; |
|
62 inline METHOD begin() const -> std::string::const_iterator; |
|
63 inline METHOD clear() -> void; |
|
64 METHOD compare (const String& other) const -> int; |
|
65 METHOD count (char needle) const -> int; |
|
66 inline METHOD chars() const -> const char*; |
|
67 inline METHOD end() -> std::string::iterator; |
|
68 inline METHOD end() const -> std::string::const_iterator; |
|
69 METHOD ends_with (const String& other) -> bool; |
|
70 METHOD find (const char* c, int a = 0) const -> int; |
|
71 METHOD to_lowercase() const -> String; |
|
72 inline METHOD index_difference (int a, int b) -> int; |
|
73 inline METHOD insert (int pos, char c) -> void; |
|
74 inline METHOD is_empty() const -> bool; |
|
75 METHOD is_numeric() const -> bool; |
|
76 METHOD find_last (const char* c, int a = -1) const -> int; |
|
77 inline METHOD length() const -> int; |
|
78 METHOD mask_against (const String& pattern) const -> bool; |
|
79 METHOD mid (long a, long b = -1) const -> String; |
|
80 inline METHOD modify_index (int& a) -> void; |
|
81 inline METHOD prepend (String a) -> void; |
|
82 inline METHOD remove (int pos) -> void; |
|
83 inline METHOD remove (int pos, int len) -> void; |
|
84 inline METHOD remove_from_end (int len) -> void; |
|
85 inline METHOD remove_from_start (int len) -> void; |
|
86 METHOD replace (const char* a, const char* b) -> void; |
|
87 inline METHOD replace (int pos, int n, const String& a) -> void; |
|
88 inline METHOD shrink_to_fit() -> void; |
|
89 METHOD split (const String& del) const -> StringList; |
|
90 METHOD split (char del) const -> StringList; |
|
91 METHOD sprintf (const char* fmtstr, ...) -> void; |
|
92 METHOD starts_with (const String& other) -> bool; |
|
93 inline METHOD std_string() const -> const std::string&; |
|
94 inline METHOD strip (char unwanted) -> String; |
|
95 METHOD strip (const List<char>& unwanted) -> String; |
|
96 METHOD to_double (bool* ok = nullptr) const -> double; |
|
97 METHOD to_float (bool* ok = nullptr) const -> float; |
|
98 METHOD to_int (bool* ok = nullptr, int base = 10) const -> long; |
|
99 METHOD trim (int n) -> void; |
|
100 METHOD to_uppercase() const -> String; |
|
101 METHOD word_position (int n) const -> int; |
|
102 |
|
103 static METHOD from_number (int a) -> String; |
|
104 static METHOD from_number (long a) -> String; |
|
105 static METHOD from_number (unsigned long a) -> String; |
|
106 static METHOD from_number (double a) -> String; |
|
107 |
|
108 METHOD operator+ (const String& data) const -> String; |
|
109 METHOD operator+ (const char* data) const -> String; |
|
110 inline METHOD operator+ (int num) const -> String; |
|
111 inline METHOD operator+= (const String& data) -> String&; |
|
112 inline METHOD operator+= (const char* data) -> String&; |
|
113 inline METHOD operator+= (int num) -> String&; |
|
114 inline METHOD operator+= (char data) -> String&; |
|
115 inline METHOD operator== (const String& other) const -> bool; |
|
116 inline METHOD operator== (const char* other) const -> bool; |
|
117 inline METHOD operator!= (const String& other) const -> bool; |
|
118 inline METHOD operator!= (const char* other) const -> bool; |
|
119 inline METHOD operator> (const String& other) const -> bool; |
|
120 inline METHOD operator< (const String& other) const -> bool; |
|
121 inline operator const char*() const; |
|
122 inline operator const std::string&() const; |
|
123 |
|
124 private: |
|
125 std::string m_string; |
|
126 }; |
|
127 |
|
128 class StringList : public List<String> |
|
129 { |
|
130 public: |
|
131 template<typename... Args> |
|
132 StringList (Args... args) : |
|
133 List<String> (args...) {} |
|
134 |
|
135 METHOD join (const String& delim) -> String; |
|
136 }; |
|
137 |
|
138 inline FUNCTION operator== (const char* a, const String& b) -> bool; |
|
139 inline FUNCTION operator+ (const char* a, const String& b) -> String; |
|
140 |
|
141 // |
|
142 // ------------------------------------------------------------------------------------------------- |
|
143 // |
|
144 // IMPLEMENTATIONS |
|
145 // |
|
146 |
|
147 inline METHOD |
|
148 String::is_empty() const -> bool |
|
149 { |
|
150 return m_string[0] == '\0'; |
|
151 } |
|
152 |
|
153 // |
|
154 // ------------------------------------------------------------------------------------------------- |
|
155 // |
|
156 |
|
157 inline METHOD |
|
158 String::append (const char* data) -> void |
|
159 { |
|
160 m_string.append (data); |
|
161 } |
|
162 |
|
163 // |
|
164 // ------------------------------------------------------------------------------------------------- |
|
165 // |
|
166 |
|
167 inline METHOD |
|
168 String::append (char data) -> void |
|
169 { |
|
170 m_string.push_back (data); |
|
171 } |
|
172 |
|
173 // |
|
174 // ------------------------------------------------------------------------------------------------- |
|
175 // |
|
176 |
|
177 inline METHOD |
|
178 String::append (const String& data) -> void |
|
179 { |
|
180 m_string.append (data.chars()); |
|
181 } |
|
182 |
|
183 // |
|
184 // ------------------------------------------------------------------------------------------------- |
|
185 // |
|
186 |
|
187 inline METHOD |
|
188 String::begin() -> std::string::iterator |
|
189 { |
|
190 return m_string.begin(); |
|
191 } |
|
192 |
|
193 // |
|
194 // ------------------------------------------------------------------------------------------------- |
|
195 // |
|
196 |
|
197 inline METHOD |
|
198 String::begin() const -> std::string::const_iterator |
|
199 { |
|
200 return m_string.cbegin(); |
|
201 } |
|
202 |
|
203 // |
|
204 // ------------------------------------------------------------------------------------------------- |
|
205 // |
|
206 |
|
207 inline const char* String::chars() const |
|
208 { |
|
209 return m_string.c_str(); |
|
210 } |
|
211 |
|
212 // |
|
213 // ------------------------------------------------------------------------------------------------- |
|
214 // |
|
215 |
|
216 inline METHOD |
|
217 String::end() -> std::string::iterator |
|
218 { |
|
219 return m_string.end(); |
|
220 } |
|
221 |
|
222 // |
|
223 // ------------------------------------------------------------------------------------------------- |
|
224 // |
|
225 |
|
226 inline METHOD |
|
227 String::end() const -> std::string::const_iterator |
|
228 { |
|
229 return m_string.end(); |
|
230 } |
|
231 |
|
232 // |
|
233 // ------------------------------------------------------------------------------------------------- |
|
234 // |
|
235 |
|
236 inline METHOD |
|
237 String::clear() -> void |
|
238 { |
|
239 m_string.clear(); |
|
240 } |
|
241 |
|
242 // |
|
243 // ------------------------------------------------------------------------------------------------- |
|
244 // |
|
245 |
|
246 inline METHOD |
|
247 String::remove (int pos) -> void |
|
248 { |
|
249 m_string.erase (m_string.begin() + pos); |
|
250 } |
|
251 |
|
252 // |
|
253 // ------------------------------------------------------------------------------------------------- |
|
254 // |
|
255 |
|
256 inline METHOD |
|
257 String::insert (int pos, char c) -> void |
|
258 { |
|
259 m_string.insert (m_string.begin() + pos, c); |
|
260 } |
|
261 |
|
262 // |
|
263 // ------------------------------------------------------------------------------------------------- |
|
264 // |
|
265 |
|
266 inline METHOD |
|
267 String::length() const -> int |
|
268 { |
|
269 return m_string.length(); |
|
270 } |
|
271 |
|
272 // |
|
273 // ------------------------------------------------------------------------------------------------- |
|
274 // |
|
275 |
|
276 inline METHOD |
|
277 String::remove (int pos, int len) -> void |
|
278 { |
|
279 m_string.replace (pos, len, ""); |
|
280 } |
|
281 |
|
282 // |
|
283 // ------------------------------------------------------------------------------------------------- |
|
284 // |
|
285 |
|
286 inline METHOD |
|
287 String::remove_from_start (int len) -> void |
|
288 { |
|
289 remove (0, len); |
|
290 } |
|
291 |
|
292 // |
|
293 // ------------------------------------------------------------------------------------------------- |
|
294 // |
|
295 |
|
296 inline METHOD |
|
297 String::remove_from_end (int len) -> void |
|
298 { |
|
299 remove (length() - len, len); |
|
300 } |
|
301 |
|
302 // |
|
303 // ------------------------------------------------------------------------------------------------- |
|
304 // |
|
305 |
|
306 inline METHOD |
|
307 String::replace (int pos, int n, const String& a) -> void |
|
308 { |
|
309 m_string.replace (pos, n, a.chars()); |
|
310 } |
|
311 |
|
312 // |
|
313 // ------------------------------------------------------------------------------------------------- |
|
314 // |
|
315 |
|
316 inline METHOD |
|
317 String::shrink_to_fit() -> void |
|
318 { |
|
319 m_string.shrink_to_fit(); |
|
320 } |
|
321 |
|
322 // |
|
323 // ------------------------------------------------------------------------------------------------- |
|
324 // |
|
325 |
|
326 inline const std::string& String::std_string() const |
|
327 { |
|
328 return m_string; |
|
329 } |
|
330 |
|
331 // |
|
332 // ------------------------------------------------------------------------------------------------- |
|
333 // |
|
334 |
|
335 inline METHOD |
|
336 String::strip (char unwanted) -> String |
|
337 { |
|
338 return strip ({unwanted}); |
|
339 } |
|
340 |
|
341 // |
|
342 // ------------------------------------------------------------------------------------------------- |
|
343 // |
|
344 |
|
345 inline METHOD |
|
346 String::operator+ (int num) const -> String |
|
347 { |
|
348 return *this + String::from_number (num); |
|
349 } |
|
350 |
|
351 // |
|
352 // ------------------------------------------------------------------------------------------------- |
|
353 // |
|
354 |
|
355 inline METHOD |
|
356 String::operator+= (const String& data) -> String& |
|
357 { |
|
358 append (data); |
|
359 return *this; |
|
360 } |
|
361 |
|
362 // |
|
363 // ------------------------------------------------------------------------------------------------- |
|
364 // |
|
365 |
|
366 inline METHOD |
|
367 String::operator+= (const char* data) -> String& |
|
368 { |
|
369 append (data); |
|
370 return *this; |
|
371 } |
|
372 |
|
373 // |
|
374 // ------------------------------------------------------------------------------------------------- |
|
375 // |
|
376 |
|
377 inline METHOD |
|
378 String::operator+= (int num) -> String& |
|
379 { |
|
380 return operator+= (String::from_number (num)); |
|
381 } |
|
382 |
|
383 // |
|
384 // ------------------------------------------------------------------------------------------------- |
|
385 // |
|
386 |
|
387 inline METHOD |
|
388 String::prepend (String a) -> void |
|
389 { |
|
390 m_string = (a + m_string).std_string(); |
|
391 } |
|
392 |
|
393 // |
|
394 // ------------------------------------------------------------------------------------------------- |
|
395 // |
|
396 |
|
397 inline METHOD |
|
398 String::operator+= (char data) -> String& |
|
399 { |
|
400 append (data); |
|
401 return *this; |
|
402 } |
|
403 |
|
404 // |
|
405 // ------------------------------------------------------------------------------------------------- |
|
406 // |
|
407 |
|
408 inline METHOD |
|
409 String::operator== (const String& other) const -> bool |
|
410 { |
|
411 return std_string() == other.std_string(); |
|
412 } |
|
413 |
|
414 // |
|
415 // ------------------------------------------------------------------------------------------------- |
|
416 // |
|
417 |
|
418 inline METHOD |
|
419 String::operator== (const char* other) const -> bool |
|
420 { |
|
421 return operator== (String (other)); |
|
422 } |
|
423 |
|
424 // |
|
425 // ------------------------------------------------------------------------------------------------- |
|
426 // |
|
427 |
|
428 inline METHOD |
|
429 String::operator!= (const String& other) const -> bool |
|
430 { |
|
431 return std_string() != other.std_string(); |
|
432 } |
|
433 |
|
434 // |
|
435 // ------------------------------------------------------------------------------------------------- |
|
436 // |
|
437 |
|
438 inline METHOD |
|
439 String::operator!= (const char* other) const -> bool |
|
440 { |
|
441 return operator!= (String (other)); |
|
442 } |
|
443 |
|
444 // |
|
445 // ------------------------------------------------------------------------------------------------- |
|
446 // |
|
447 |
|
448 inline METHOD |
|
449 String::operator> (const String& other) const -> bool |
|
450 { |
|
451 return std_string() > other.std_string(); |
|
452 } |
|
453 |
|
454 // |
|
455 // ------------------------------------------------------------------------------------------------- |
|
456 // |
|
457 |
|
458 inline METHOD |
|
459 String::operator< (const String& other) const -> bool |
|
460 { |
|
461 return std_string() < other.std_string(); |
|
462 } |
|
463 |
|
464 // |
|
465 // ------------------------------------------------------------------------------------------------- |
|
466 // |
|
467 |
|
468 inline String::operator const char*() const |
|
469 { |
|
470 return chars(); |
|
471 } |
|
472 |
|
473 // |
|
474 // ------------------------------------------------------------------------------------------------- |
|
475 // |
|
476 |
|
477 inline String::operator const std::string&() const |
|
478 { |
|
479 return std_string(); |
|
480 } |
|
481 |
|
482 // |
|
483 // ------------------------------------------------------------------------------------------------- |
|
484 // |
|
485 |
|
486 inline METHOD |
|
487 String::modify_index (int& a) -> void |
|
488 { |
|
489 if (a < 0) |
|
490 a = length() - a; |
|
491 } |
|
492 |
|
493 // |
|
494 // ------------------------------------------------------------------------------------------------- |
|
495 // |
|
496 |
|
497 inline METHOD |
|
498 String::index_difference (int a, int b) -> int |
|
499 { |
|
500 modify_index (a); |
|
501 modify_index (b); |
|
502 return b - a; |
|
503 } |
|
504 |
|
505 // |
|
506 // ------------------------------------------------------------------------------------------------- |
|
507 // |
|
508 |
|
509 inline FUNCTION |
|
510 operator== (const char* a, const String& b) -> bool |
|
511 { |
|
512 return b == a; |
|
513 } |
|
514 |
|
515 // |
|
516 // ------------------------------------------------------------------------------------------------- |
|
517 // |
|
518 |
|
519 inline FUNCTION |
|
520 operator+ (const char* a, const String& b) -> String |
|
521 { |
|
522 return String (a) + b; |
|
523 } |