sources/mystring.h

changeset 5
146825d63b9a
parent 1
4dd5bde4e777
child 10
3874575d924d
equal deleted inserted replaced
4:ec5387e121fa 5:146825d63b9a
35 #include "basics.h" 35 #include "basics.h"
36 #include "list.h" 36 #include "list.h"
37 37
38 class String; 38 class String;
39 class StringList; 39 class StringList;
40
41 // -------------------------------------------------------------------------------------------------
40 42
41 class String 43 class String
42 { 44 {
43 public: 45 public:
44 String() {} 46 String() {}
123 125
124 private: 126 private:
125 std::string m_string; 127 std::string m_string;
126 }; 128 };
127 129
130 // -------------------------------------------------------------------------------------------------
131
128 class StringList : public List<String> 132 class StringList : public List<String>
129 { 133 {
130 public: 134 public:
131 template<typename... Args> 135 template<typename... Args>
132 StringList (Args... args) : 136 StringList (Args... args) :
133 List<String> (args...) {} 137 List<String> (args...) {}
134 138
135 METHOD join (const String& delim) -> String; 139 METHOD join (const String& delim) -> String;
136 }; 140 };
137 141
142 // -------------------------------------------------------------------------------------------------
143
138 inline FUNCTION operator== (const char* a, const String& b) -> bool; 144 inline FUNCTION operator== (const char* a, const String& b) -> bool;
139 inline FUNCTION operator+ (const char* a, const String& b) -> String; 145 inline FUNCTION operator+ (const char* a, const String& b) -> String;
140 146
141 // 147 // -------------------------------------------------------------------------------------------------
142 // -------------------------------------------------------------------------------------------------
143 //
144 // IMPLEMENTATIONS
145 //
146 148
147 inline METHOD 149 inline METHOD
148 String::is_empty() const -> bool 150 String::is_empty() const -> bool
149 { 151 {
150 return m_string[0] == '\0'; 152 return m_string[0] == '\0';
151 } 153 }
152 154
153 // 155 // -------------------------------------------------------------------------------------------------
154 // -------------------------------------------------------------------------------------------------
155 //
156 156
157 inline METHOD 157 inline METHOD
158 String::append (const char* data) -> void 158 String::append (const char* data) -> void
159 { 159 {
160 m_string.append (data); 160 m_string.append (data);
161 } 161 }
162 162
163 // 163 // -------------------------------------------------------------------------------------------------
164 // -------------------------------------------------------------------------------------------------
165 //
166 164
167 inline METHOD 165 inline METHOD
168 String::append (char data) -> void 166 String::append (char data) -> void
169 { 167 {
170 m_string.push_back (data); 168 m_string.push_back (data);
171 } 169 }
172 170
173 // 171 // -------------------------------------------------------------------------------------------------
174 // -------------------------------------------------------------------------------------------------
175 //
176 172
177 inline METHOD 173 inline METHOD
178 String::append (const String& data) -> void 174 String::append (const String& data) -> void
179 { 175 {
180 m_string.append (data.chars()); 176 m_string.append (data.chars());
181 } 177 }
182 178
183 // 179 // -------------------------------------------------------------------------------------------------
184 // -------------------------------------------------------------------------------------------------
185 //
186 180
187 inline METHOD 181 inline METHOD
188 String::begin() -> std::string::iterator 182 String::begin() -> std::string::iterator
189 { 183 {
190 return m_string.begin(); 184 return m_string.begin();
191 } 185 }
192 186
193 // 187 // -------------------------------------------------------------------------------------------------
194 // -------------------------------------------------------------------------------------------------
195 //
196 188
197 inline METHOD 189 inline METHOD
198 String::begin() const -> std::string::const_iterator 190 String::begin() const -> std::string::const_iterator
199 { 191 {
200 return m_string.cbegin(); 192 return m_string.cbegin();
201 } 193 }
202 194
203 // 195 // -------------------------------------------------------------------------------------------------
204 // -------------------------------------------------------------------------------------------------
205 //
206 196
207 inline const char* String::chars() const 197 inline const char* String::chars() const
208 { 198 {
209 return m_string.c_str(); 199 return m_string.c_str();
210 } 200 }
211 201
212 // 202 // -------------------------------------------------------------------------------------------------
213 // -------------------------------------------------------------------------------------------------
214 //
215 203
216 inline METHOD 204 inline METHOD
217 String::end() -> std::string::iterator 205 String::end() -> std::string::iterator
218 { 206 {
219 return m_string.end(); 207 return m_string.end();
220 } 208 }
221 209
222 // 210 // -------------------------------------------------------------------------------------------------
223 // -------------------------------------------------------------------------------------------------
224 //
225 211
226 inline METHOD 212 inline METHOD
227 String::end() const -> std::string::const_iterator 213 String::end() const -> std::string::const_iterator
228 { 214 {
229 return m_string.end(); 215 return m_string.end();
230 } 216 }
231 217
232 // 218 // -------------------------------------------------------------------------------------------------
233 // -------------------------------------------------------------------------------------------------
234 //
235 219
236 inline METHOD 220 inline METHOD
237 String::clear() -> void 221 String::clear() -> void
238 { 222 {
239 m_string.clear(); 223 m_string.clear();
240 } 224 }
241 225
242 // 226 // -------------------------------------------------------------------------------------------------
243 // -------------------------------------------------------------------------------------------------
244 //
245 227
246 inline METHOD 228 inline METHOD
247 String::remove (int pos) -> void 229 String::remove (int pos) -> void
248 { 230 {
249 m_string.erase (m_string.begin() + pos); 231 m_string.erase (m_string.begin() + pos);
250 } 232 }
251 233
252 // 234 // -------------------------------------------------------------------------------------------------
253 // -------------------------------------------------------------------------------------------------
254 //
255 235
256 inline METHOD 236 inline METHOD
257 String::insert (int pos, char c) -> void 237 String::insert (int pos, char c) -> void
258 { 238 {
259 m_string.insert (m_string.begin() + pos, c); 239 m_string.insert (m_string.begin() + pos, c);
260 } 240 }
261 241
262 // 242 // -------------------------------------------------------------------------------------------------
263 // -------------------------------------------------------------------------------------------------
264 //
265 243
266 inline METHOD 244 inline METHOD
267 String::length() const -> int 245 String::length() const -> int
268 { 246 {
269 return m_string.length(); 247 return m_string.length();
270 } 248 }
271 249
272 // 250 // -------------------------------------------------------------------------------------------------
273 // -------------------------------------------------------------------------------------------------
274 //
275 251
276 inline METHOD 252 inline METHOD
277 String::remove (int pos, int len) -> void 253 String::remove (int pos, int len) -> void
278 { 254 {
279 m_string.replace (pos, len, ""); 255 m_string.replace (pos, len, "");
280 } 256 }
281 257
282 // 258 // -------------------------------------------------------------------------------------------------
283 // -------------------------------------------------------------------------------------------------
284 //
285 259
286 inline METHOD 260 inline METHOD
287 String::remove_from_start (int len) -> void 261 String::remove_from_start (int len) -> void
288 { 262 {
289 remove (0, len); 263 remove (0, len);
290 } 264 }
291 265
292 // 266 // -------------------------------------------------------------------------------------------------
293 // -------------------------------------------------------------------------------------------------
294 //
295 267
296 inline METHOD 268 inline METHOD
297 String::remove_from_end (int len) -> void 269 String::remove_from_end (int len) -> void
298 { 270 {
299 remove (length() - len, len); 271 remove (length() - len, len);
300 } 272 }
301 273
302 // 274 // -------------------------------------------------------------------------------------------------
303 // -------------------------------------------------------------------------------------------------
304 //
305 275
306 inline METHOD 276 inline METHOD
307 String::replace (int pos, int n, const String& a) -> void 277 String::replace (int pos, int n, const String& a) -> void
308 { 278 {
309 m_string.replace (pos, n, a.chars()); 279 m_string.replace (pos, n, a.chars());
310 } 280 }
311 281
312 // 282 // -------------------------------------------------------------------------------------------------
313 // -------------------------------------------------------------------------------------------------
314 //
315 283
316 inline METHOD 284 inline METHOD
317 String::shrink_to_fit() -> void 285 String::shrink_to_fit() -> void
318 { 286 {
319 m_string.shrink_to_fit(); 287 m_string.shrink_to_fit();
320 } 288 }
321 289
322 // 290 // -------------------------------------------------------------------------------------------------
323 // -------------------------------------------------------------------------------------------------
324 //
325 291
326 inline const std::string& String::std_string() const 292 inline const std::string& String::std_string() const
327 { 293 {
328 return m_string; 294 return m_string;
329 } 295 }
330 296
331 // 297 // -------------------------------------------------------------------------------------------------
332 // -------------------------------------------------------------------------------------------------
333 //
334 298
335 inline METHOD 299 inline METHOD
336 String::strip (char unwanted) -> String 300 String::strip (char unwanted) -> String
337 { 301 {
338 return strip ({unwanted}); 302 return strip ({unwanted});
339 } 303 }
340 304
341 // 305 // -------------------------------------------------------------------------------------------------
342 // -------------------------------------------------------------------------------------------------
343 //
344 306
345 inline METHOD 307 inline METHOD
346 String::operator+ (int num) const -> String 308 String::operator+ (int num) const -> String
347 { 309 {
348 return *this + String::from_number (num); 310 return *this + String::from_number (num);
349 } 311 }
350 312
351 // 313 // -------------------------------------------------------------------------------------------------
352 // -------------------------------------------------------------------------------------------------
353 //
354 314
355 inline METHOD 315 inline METHOD
356 String::operator+= (const String& data) -> String& 316 String::operator+= (const String& data) -> String&
357 { 317 {
358 append (data); 318 append (data);
359 return *this; 319 return *this;
360 } 320 }
361 321
362 // 322 // -------------------------------------------------------------------------------------------------
363 // -------------------------------------------------------------------------------------------------
364 //
365 323
366 inline METHOD 324 inline METHOD
367 String::operator+= (const char* data) -> String& 325 String::operator+= (const char* data) -> String&
368 { 326 {
369 append (data); 327 append (data);
370 return *this; 328 return *this;
371 } 329 }
372 330
373 // 331 // -------------------------------------------------------------------------------------------------
374 // -------------------------------------------------------------------------------------------------
375 //
376 332
377 inline METHOD 333 inline METHOD
378 String::operator+= (int num) -> String& 334 String::operator+= (int num) -> String&
379 { 335 {
380 return operator+= (String::from_number (num)); 336 return operator+= (String::from_number (num));
381 } 337 }
382 338
383 // 339 // -------------------------------------------------------------------------------------------------
384 // -------------------------------------------------------------------------------------------------
385 //
386 340
387 inline METHOD 341 inline METHOD
388 String::prepend (String a) -> void 342 String::prepend (String a) -> void
389 { 343 {
390 m_string = (a + m_string).std_string(); 344 m_string = (a + m_string).std_string();
391 } 345 }
392 346
393 // 347 // -------------------------------------------------------------------------------------------------
394 // -------------------------------------------------------------------------------------------------
395 //
396 348
397 inline METHOD 349 inline METHOD
398 String::operator+= (char data) -> String& 350 String::operator+= (char data) -> String&
399 { 351 {
400 append (data); 352 append (data);
401 return *this; 353 return *this;
402 } 354 }
403 355
404 // 356 // -------------------------------------------------------------------------------------------------
405 // -------------------------------------------------------------------------------------------------
406 //
407 357
408 inline METHOD 358 inline METHOD
409 String::operator== (const String& other) const -> bool 359 String::operator== (const String& other) const -> bool
410 { 360 {
411 return std_string() == other.std_string(); 361 return std_string() == other.std_string();
412 } 362 }
413 363
414 // 364 // -------------------------------------------------------------------------------------------------
415 // -------------------------------------------------------------------------------------------------
416 //
417 365
418 inline METHOD 366 inline METHOD
419 String::operator== (const char* other) const -> bool 367 String::operator== (const char* other) const -> bool
420 { 368 {
421 return operator== (String (other)); 369 return operator== (String (other));
422 } 370 }
423 371
424 // 372 // -------------------------------------------------------------------------------------------------
425 // -------------------------------------------------------------------------------------------------
426 //
427 373
428 inline METHOD 374 inline METHOD
429 String::operator!= (const String& other) const -> bool 375 String::operator!= (const String& other) const -> bool
430 { 376 {
431 return std_string() != other.std_string(); 377 return std_string() != other.std_string();
432 } 378 }
433 379
434 // 380 // -------------------------------------------------------------------------------------------------
435 // -------------------------------------------------------------------------------------------------
436 //
437 381
438 inline METHOD 382 inline METHOD
439 String::operator!= (const char* other) const -> bool 383 String::operator!= (const char* other) const -> bool
440 { 384 {
441 return operator!= (String (other)); 385 return operator!= (String (other));
442 } 386 }
443 387
444 // 388 // -------------------------------------------------------------------------------------------------
445 // -------------------------------------------------------------------------------------------------
446 //
447 389
448 inline METHOD 390 inline METHOD
449 String::operator> (const String& other) const -> bool 391 String::operator> (const String& other) const -> bool
450 { 392 {
451 return std_string() > other.std_string(); 393 return std_string() > other.std_string();
452 } 394 }
453 395
454 // 396 // -------------------------------------------------------------------------------------------------
455 // -------------------------------------------------------------------------------------------------
456 //
457 397
458 inline METHOD 398 inline METHOD
459 String::operator< (const String& other) const -> bool 399 String::operator< (const String& other) const -> bool
460 { 400 {
461 return std_string() < other.std_string(); 401 return std_string() < other.std_string();
462 } 402 }
463 403
464 // 404 // -------------------------------------------------------------------------------------------------
465 // -------------------------------------------------------------------------------------------------
466 //
467 405
468 inline String::operator const char*() const 406 inline String::operator const char*() const
469 { 407 {
470 return chars(); 408 return chars();
471 } 409 }
472 410
473 // 411 // -------------------------------------------------------------------------------------------------
474 // -------------------------------------------------------------------------------------------------
475 //
476 412
477 inline String::operator const std::string&() const 413 inline String::operator const std::string&() const
478 { 414 {
479 return std_string(); 415 return std_string();
480 } 416 }
481 417
482 // 418 // -------------------------------------------------------------------------------------------------
483 // -------------------------------------------------------------------------------------------------
484 //
485 419
486 inline METHOD 420 inline METHOD
487 String::modify_index (int& a) -> void 421 String::modify_index (int& a) -> void
488 { 422 {
489 if (a < 0) 423 if (a < 0)
490 a = length() - a; 424 a = length() - a;
491 } 425 }
492 426
493 // 427 // -------------------------------------------------------------------------------------------------
494 // -------------------------------------------------------------------------------------------------
495 //
496 428
497 inline METHOD 429 inline METHOD
498 String::index_difference (int a, int b) -> int 430 String::index_difference (int a, int b) -> int
499 { 431 {
500 modify_index (a); 432 modify_index (a);
501 modify_index (b); 433 modify_index (b);
502 return b - a; 434 return b - a;
503 } 435 }
504 436
505 // 437 // -------------------------------------------------------------------------------------------------
506 // -------------------------------------------------------------------------------------------------
507 //
508 438
509 inline FUNCTION 439 inline FUNCTION
510 operator== (const char* a, const String& b) -> bool 440 operator== (const char* a, const String& b) -> bool
511 { 441 {
512 return b == a; 442 return b == a;
513 } 443 }
514 444
515 // 445 // -------------------------------------------------------------------------------------------------
516 // -------------------------------------------------------------------------------------------------
517 //
518 446
519 inline FUNCTION 447 inline FUNCTION
520 operator+ (const char* a, const String& b) -> String 448 operator+ (const char* a, const String& b) -> String
521 { 449 {
522 return String (a) + b; 450 return String (a) + b;

mercurial