1 /* |
|
2 Copyright 2012-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. 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 "list.h" |
|
37 |
|
38 class String; |
|
39 class StringList; |
|
40 |
|
41 // ============================================================================= |
|
42 // |
|
43 class String |
|
44 { |
|
45 public: |
|
46 String() {} |
|
47 |
|
48 explicit String (char a) : |
|
49 _string ({ a, '\0' }) {} |
|
50 |
|
51 String (const char* data) : |
|
52 _string (data) {} |
|
53 |
|
54 String (const std::string& data) : |
|
55 _string (data) {} |
|
56 |
|
57 inline void append (const char* data); |
|
58 inline void append (char data); |
|
59 inline void append (const String& data); |
|
60 inline std::string::iterator begin(); |
|
61 inline std::string::const_iterator begin() const; |
|
62 void dump() const; |
|
63 inline void clear(); |
|
64 int compare (const String& other) const; |
|
65 int count (char needle) const; |
|
66 inline const char* c_str() const; |
|
67 inline std::string::iterator end(); |
|
68 inline std::string::const_iterator end() const; |
|
69 bool endsWith (const String& other); |
|
70 int firstIndexOf (const char* c, int a = 0) const; |
|
71 String toLowercase() const; |
|
72 inline int indexDifference (int a, int b); |
|
73 inline void insert (int pos, char c); |
|
74 inline bool isEmpty() const; |
|
75 bool isNumeric() const; |
|
76 int lastIndexOf (const char* c, int a = -1) const; |
|
77 inline int length() const; |
|
78 bool maskAgainst (const String& pattern) const; |
|
79 String mid (long a, long b = -1) const; |
|
80 inline void modifyIndex (int& a); |
|
81 inline void prepend (String a); |
|
82 inline void removeAt (int pos); |
|
83 inline void remove (int pos, int len); |
|
84 inline void removeFromEnd (int len); |
|
85 inline void removeFromStart (int len); |
|
86 void replace (const char* a, const char* b); |
|
87 inline void replace (int pos, int n, const String& a); |
|
88 inline void shrinkToFit(); |
|
89 StringList split (const String& del) const; |
|
90 StringList split (char del) const; |
|
91 void sprintf (const char* fmtstr, ...); |
|
92 bool startsWith (const String& other); |
|
93 inline const std::string& stdString() const; |
|
94 inline String strip (char unwanted); |
|
95 String strip (const List<char>& unwanted); |
|
96 double toDouble (bool* ok = nullptr) const; |
|
97 float toFloat (bool* ok = nullptr) const; |
|
98 long toLong (bool* ok = nullptr, int base = 10) const; |
|
99 void trim (int n); |
|
100 String toUppercase() const; |
|
101 int wordPosition (int n) const; |
|
102 |
|
103 static String fromNumber (int a); |
|
104 static String fromNumber (long a); |
|
105 |
|
106 String operator+ (const String& data) const; |
|
107 String operator+ (const char* data) const; |
|
108 inline String operator+ (int num) const; |
|
109 inline String& operator+= (const String data); |
|
110 inline String& operator+= (const char* data); |
|
111 inline String& operator+= (int num); |
|
112 inline String& operator+= (const char data); |
|
113 inline String operator- (int n) const; |
|
114 inline String& operator-= (int n); |
|
115 inline bool operator== (const String& other) const; |
|
116 inline bool operator== (const char* other) const; |
|
117 inline bool operator!= (const String& other) const; |
|
118 inline bool operator!= (const char* other) const; |
|
119 inline bool operator> (const String& other) const; |
|
120 inline bool operator< (const String& other) const; |
|
121 inline operator const char*() const; |
|
122 inline operator const std::string&() const; |
|
123 |
|
124 private: |
|
125 std::string _string; |
|
126 }; |
|
127 |
|
128 class StringList : public List<String> |
|
129 { |
|
130 public: |
|
131 StringList() {} |
|
132 StringList (std::initializer_list<String> vals) : |
|
133 List<String> (vals) {} |
|
134 StringList (const List<String>& a) : List<String> (a.deque()) {} |
|
135 StringList (const std::deque<String>& a) : List<String> (a) {} |
|
136 |
|
137 String join (const String& delim); |
|
138 }; |
|
139 |
|
140 inline bool operator== (const char* a, const String& b); |
|
141 inline String operator+ (const char* a, const String& b); |
|
142 |
|
143 // ============================================================================= |
|
144 // |
|
145 // IMPLEMENTATIONS |
|
146 // |
|
147 |
|
148 inline bool String::isEmpty() const |
|
149 { |
|
150 return _string[0] == '\0'; |
|
151 } |
|
152 |
|
153 inline void String::append (const char* data) |
|
154 { |
|
155 _string.append (data); |
|
156 } |
|
157 |
|
158 inline void String::append (char data) |
|
159 { |
|
160 _string.push_back (data); |
|
161 } |
|
162 |
|
163 inline void String::append (const String& data) |
|
164 { |
|
165 _string.append (data.c_str()); |
|
166 } |
|
167 |
|
168 inline std::string::iterator String::begin() |
|
169 { |
|
170 return _string.begin(); |
|
171 } |
|
172 |
|
173 inline std::string::const_iterator String::begin() const |
|
174 { |
|
175 return _string.cbegin(); |
|
176 } |
|
177 |
|
178 inline const char* String::c_str() const |
|
179 { |
|
180 return _string.c_str(); |
|
181 } |
|
182 |
|
183 inline std::string::iterator String::end() |
|
184 { |
|
185 return _string.end(); |
|
186 } |
|
187 |
|
188 inline std::string::const_iterator String::end() const |
|
189 { |
|
190 return _string.end(); |
|
191 } |
|
192 |
|
193 inline void String::clear() |
|
194 { |
|
195 _string.clear(); |
|
196 } |
|
197 |
|
198 inline void String::removeAt (int pos) |
|
199 { |
|
200 _string.erase (_string.begin() + pos); |
|
201 } |
|
202 |
|
203 inline void String::insert (int pos, char c) |
|
204 { |
|
205 _string.insert (_string.begin() + pos, c); |
|
206 } |
|
207 |
|
208 inline int String::length() const |
|
209 { |
|
210 return _string.length(); |
|
211 } |
|
212 |
|
213 inline void String::remove (int pos, int len) |
|
214 { |
|
215 _string.replace (pos, len, ""); |
|
216 } |
|
217 |
|
218 inline void String::removeFromStart (int len) |
|
219 { |
|
220 remove (0, len); |
|
221 } |
|
222 |
|
223 inline void String::removeFromEnd (int len) |
|
224 { |
|
225 remove (length() - len, len); |
|
226 } |
|
227 |
|
228 inline void String::replace (int pos, int n, const String& a) |
|
229 { |
|
230 _string.replace (pos, n, a.c_str()); |
|
231 } |
|
232 |
|
233 inline void String::shrinkToFit() |
|
234 { |
|
235 _string.shrink_to_fit(); |
|
236 } |
|
237 |
|
238 inline const std::string& String::stdString() const |
|
239 { |
|
240 return _string; |
|
241 } |
|
242 |
|
243 inline String String::strip (char unwanted) |
|
244 { |
|
245 return strip ({unwanted}); |
|
246 } |
|
247 |
|
248 inline String String::operator+ (int num) const |
|
249 { |
|
250 return *this + String::fromNumber (num); |
|
251 } |
|
252 |
|
253 inline String& String::operator+= (const String data) |
|
254 { |
|
255 append (data); |
|
256 return *this; |
|
257 } |
|
258 |
|
259 inline String& String::operator+= (const char* data) |
|
260 { |
|
261 append (data); |
|
262 return *this; |
|
263 } |
|
264 |
|
265 inline String& String::operator+= (int num) |
|
266 { |
|
267 return operator+= (String::fromNumber (num)); |
|
268 } |
|
269 |
|
270 inline void String::prepend (String a) |
|
271 { |
|
272 _string = (a + _string).stdString(); |
|
273 } |
|
274 |
|
275 inline String& String::operator+= (const char data) |
|
276 { |
|
277 append (data); |
|
278 return *this; |
|
279 } |
|
280 |
|
281 inline String String::operator- (int n) const |
|
282 { |
|
283 String newString = _string; |
|
284 newString -= n; |
|
285 return newString; |
|
286 } |
|
287 |
|
288 inline String& String::operator-= (int n) |
|
289 { |
|
290 trim (n); |
|
291 return *this; |
|
292 } |
|
293 |
|
294 inline bool String::operator== (const String& other) const |
|
295 { |
|
296 return stdString() == other.stdString(); |
|
297 } |
|
298 |
|
299 inline bool String::operator== (const char* other) const |
|
300 { |
|
301 return operator== (String (other)); |
|
302 } |
|
303 |
|
304 inline bool String::operator!= (const String& other) const |
|
305 { |
|
306 return stdString() != other.stdString(); |
|
307 } |
|
308 |
|
309 inline bool String::operator!= (const char* other) const |
|
310 { |
|
311 return operator!= (String (other)); |
|
312 } |
|
313 |
|
314 inline bool String::operator> (const String& other) const |
|
315 { |
|
316 return stdString() > other.stdString(); |
|
317 } |
|
318 |
|
319 inline bool String::operator< (const String& other) const |
|
320 { |
|
321 return stdString() < other.stdString(); |
|
322 } |
|
323 |
|
324 inline String::operator const char*() const |
|
325 { |
|
326 return c_str(); |
|
327 } |
|
328 |
|
329 inline String::operator const std::string&() const |
|
330 { |
|
331 return stdString(); |
|
332 } |
|
333 |
|
334 inline void String::modifyIndex (int& a) |
|
335 { |
|
336 if (a < 0) |
|
337 a = length() - a; |
|
338 } |
|
339 |
|
340 inline int String::indexDifference (int a, int b) |
|
341 { |
|
342 modifyIndex (a); |
|
343 modifyIndex (b); |
|
344 return b - a; |
|
345 } |
|
346 |
|
347 inline bool operator== (const char* a, const String& b) |
|
348 { |
|
349 return b == a; |
|
350 } |
|
351 |
|
352 inline String operator+ (const char* a, const String& b) |
|
353 { |
|
354 return String (a) + b; |
|
355 } |
|
356 |
|
357 #endif // BOTC_STRING_H |
|