40 |
40 |
41 // ============================================================================= |
41 // ============================================================================= |
42 // |
42 // |
43 class String |
43 class String |
44 { |
44 { |
45 public: |
45 public: |
46 using StringType = std::string; |
46 String() {} |
47 using Iterator = typename StringType::iterator; |
47 |
48 using ConstIterator = StringType::const_iterator; |
48 explicit String (char a) : |
49 |
49 _string ({ a, '\0' }) {} |
50 String() {} |
50 |
51 |
51 String (const char* data) : |
52 explicit String (char a) : |
52 _string (data) {} |
53 m_string (&a) {} |
53 |
54 |
54 String (const std::string& data) : |
55 String (const char* data) : |
55 _string (data) {} |
56 m_string (data) {} |
56 |
57 |
57 inline void append (const char* data); |
58 String (const StringType& data) : |
58 inline void append (char data); |
59 m_string (data) {} |
59 inline void append (const String& data); |
60 |
60 inline std::string::iterator begin(); |
61 void dump() const; |
61 inline std::string::const_iterator begin() const; |
62 int compare (const String& other) const; |
62 void dump() const; |
63 bool endsWith (const String& other); |
63 inline void clear(); |
64 int count (char needle) const; |
64 int compare (const String& other) const; |
65 int firstIndexOf (const char* c, int a = 0) const; |
65 int count (char needle) const; |
66 int lastIndexOf (const char* c, int a = -1) const; |
66 inline const char* c_str() const; |
67 String toLowercase() const; |
67 inline std::string::iterator end(); |
68 bool isNumeric() const; |
68 inline std::string::const_iterator end() const; |
69 bool maskAgainst (const String& pattern) const; |
69 bool endsWith (const String& other); |
70 int wordPosition (int n) const; |
70 int firstIndexOf (const char* c, int a = 0) const; |
71 void replace (const char* a, const char* b); |
71 String toLowercase() const; |
72 StringList split (const String& del) const; |
72 inline int indexDifference (int a, int b); |
73 StringList split (char del) const; |
73 inline void insert (int pos, char c); |
74 void sprintf (const char* fmtstr, ...); |
74 inline bool isEmpty() const; |
75 bool startsWith (const String& other); |
75 bool isNumeric() const; |
76 String strip (const List<char>& unwanted); |
76 int lastIndexOf (const char* c, int a = -1) const; |
77 String mid (long a, long b = -1) const; |
77 inline int length() const; |
78 double toDouble (bool* ok = nullptr) const; |
78 bool maskAgainst (const String& pattern) const; |
79 float toFloat (bool* ok = nullptr) const; |
79 String mid (long a, long b = -1) const; |
80 long toLong (bool* ok = nullptr, int base = 10) const; |
80 inline void modifyIndex (int& a); |
81 void trim (int n); |
81 inline void prepend (String a); |
82 String toUppercase() const; |
82 inline void removeAt (int pos); |
83 |
83 inline void remove (int pos, int len); |
84 String operator+ (const String& data) const; |
84 inline void removeFromEnd (int len); |
85 String operator+ (const char* data) const; |
85 inline void removeFromStart (int len); |
86 |
86 void replace (const char* a, const char* b); |
87 static String fromNumber (int a); |
87 inline void replace (int pos, int n, const String& a); |
88 static String fromNumber (long a); |
88 inline void shrinkToFit(); |
89 |
89 StringList split (const String& del) const; |
90 inline bool isEmpty() const |
90 StringList split (char del) const; |
91 { |
91 void sprintf (const char* fmtstr, ...); |
92 return m_string[0] == '\0'; |
92 bool startsWith (const String& other); |
93 } |
93 inline const std::string& stdString() const; |
94 |
94 inline String strip (char unwanted); |
95 inline void append (const char* data) |
95 String strip (const List<char>& unwanted); |
96 { |
96 double toDouble (bool* ok = nullptr) const; |
97 m_string.append (data); |
97 float toFloat (bool* ok = nullptr) const; |
98 } |
98 long toLong (bool* ok = nullptr, int base = 10) const; |
99 |
99 void trim (int n); |
100 inline void append (char data) |
100 String toUppercase() const; |
101 { |
101 int wordPosition (int n) const; |
102 m_string.push_back (data); |
102 |
103 } |
103 static String fromNumber (int a); |
104 |
104 static String fromNumber (long a); |
105 inline void append (const String& data) |
105 |
106 { |
106 String operator+ (const String& data) const; |
107 m_string.append (data.chars()); |
107 String operator+ (const char* data) const; |
108 } |
108 inline String operator+ (int num) const; |
109 |
109 inline String& operator+= (const String data); |
110 inline Iterator begin() |
110 inline String& operator+= (const char* data); |
111 { |
111 inline String& operator+= (int num); |
112 return m_string.begin(); |
112 inline String& operator+= (const char data); |
113 } |
113 inline String operator- (int n) const; |
114 |
114 inline String& operator-= (int n); |
115 inline ConstIterator begin() const |
115 inline bool operator== (const String& other) const; |
116 { |
116 inline bool operator== (const char* other) const; |
117 return m_string.cbegin(); |
117 inline bool operator!= (const String& other) const; |
118 } |
118 inline bool operator!= (const char* other) const; |
119 |
119 inline bool operator> (const String& other) const; |
120 inline const char* chars() const |
120 inline bool operator< (const String& other) const; |
121 { |
121 inline operator const char*() const; |
122 return m_string.c_str(); |
122 inline operator const std::string&() const; |
123 } |
123 |
124 |
124 private: |
125 inline const char* c_str() const |
125 std::string _string; |
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 void modifyIndex (int& a) |
|
328 { |
|
329 if (a < 0) |
|
330 a = length() - a; |
|
331 } |
|
332 |
|
333 int indexDifference (int a, int b) |
|
334 { |
|
335 modifyIndex (a); |
|
336 modifyIndex (b); |
|
337 return b - a; |
|
338 } |
|
339 |
|
340 private: |
|
341 StringType m_string; |
|
342 }; |
126 }; |
343 |
127 |
344 // ============================================================================= |
|
345 // |
|
346 class StringList : public List<String> |
128 class StringList : public List<String> |
347 { |
129 { |
348 public: |
130 public: |
349 StringList() {} |
131 StringList() {} |
350 StringList (std::initializer_list<String> vals) : |
132 StringList (std::initializer_list<String> vals) : |
353 StringList (const std::deque<String>& a) : List<String> (a) {} |
135 StringList (const std::deque<String>& a) : List<String> (a) {} |
354 |
136 |
355 String join (const String& delim); |
137 String join (const String& delim); |
356 }; |
138 }; |
357 |
139 |
|
140 inline bool operator== (const char* a, const String& b); |
|
141 inline String operator+ (const char* a, const String& b); |
358 |
142 |
359 // ============================================================================= |
143 // ============================================================================= |
360 // |
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 |
361 inline bool operator== (const char* a, const String& b) |
347 inline bool operator== (const char* a, const String& b) |
362 { |
348 { |
363 return b == a; |
349 return b == a; |
364 } |
350 } |
365 |
351 |
366 |
|
367 // ============================================================================= |
|
368 // |
|
369 inline String operator+ (const char* a, const String& b) |
352 inline String operator+ (const char* a, const String& b) |
370 { |
353 { |
371 return String (a) + b; |
354 return String (a) + b; |
372 } |
355 } |
373 |
356 |