edelib  2.1.0
String.h
1 /*
2  * $Id: String.h 3383 2012-08-22 13:18:02Z karijes $
3  *
4  * A simple string class
5  * Copyright (c) 2005-2007 edelib authors
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef __EDELIB_STRING_H__
22 #define __EDELIB_STRING_H__
23 
24 #include "edelib-global.h"
25 #include <string.h>
26 
27 EDELIB_NS_BEGIN
28 
82 class EDELIB_API String {
83 public:
84 #ifndef SKIP_DOCS
85  typedef unsigned int size_type;
86 #endif
87 
88 private:
89 #ifndef SKIP_DOCS
90  struct StringData {
91  size_type length;
92  size_type capacity;
93  char *chars;
94  };
95 #endif
96  static StringData null_data;
97  StringData* sdata;
98 
99  void init(size_type len, size_type cap);
100  void dispose(void);
101 
102 public:
111  static const size_type npos;
112 
116  String();
117 
123  String(const char* str);
124 
130  String(const String& str);
131 
135  ~String();
136 
144  String& assign(const char* str, size_type len);
145 
152  String& assign(const char* str);
153 
160  String& assign(const String& str);
161 
169  String& append(const char* str, size_type len);
170 
177  String& append(const char* str);
178 
185  String& append(const String& str);
186 
187 
195  String& append(size_type num, const char& ch);
196 
202  void reserve(size_type len);
203 
209  void swap(String& from);
210 
219  String substr(size_type index, size_type num = npos) const;
220 
228  size_type find(const char* str, size_type offset) const;
229 
238  size_type find(char ch, size_type offset) const;
239 
243  size_type find(const char* str) const;
244 
248  void clear(void);
249 
253  void printf(const char* fmt, ...);
254 
258  void trim_left(void);
259 
263  void trim_right(void);
264 
268  void trim(void);
269 
279  const char* c_str(void) { return sdata->chars; }
280 
282  const char* c_str(void) const { return sdata->chars; }
283 
289  const char* data(void) const { return sdata->chars; }
290 
292  size_type length(void) const { return sdata->length; }
293 
295  size_type capacity(void) const { return sdata->capacity; }
296 
298  bool empty(void) const { return length() == 0; }
299 
307  String& replace(char c1, char c2);
308 
310  char& operator[](size_type index);
311 
313  char operator[](size_type index) const;
314 
316  String& operator=(const char* str);
317 
319  String& operator=(const String& str);
320 
322  String& operator+=(const char* str);
323 
325  String& operator+=(const String& str);
326 
328  String& operator+=(const char& ch);
329 };
330 
335 EDELIB_API String operator+(const String& s1, const String& s2);
336 
341 EDELIB_API String operator+(const char* s1, const String& s2);
342 
347 EDELIB_API String operator+(const String& s1, const char* s2);
348 
353 inline bool operator==(const String& str1, const char* str2) { return (strcmp(str1.c_str(), str2) == 0); }
354 
359 inline bool operator!=(const String& str1, const char* str2) { return (strcmp(str1.c_str(), str2) != 0); }
360 
365 inline bool operator>(const String& str1, const char* str2) { return (strcmp(str1.c_str(), str2) > 0); }
366 
371 inline bool operator>=(const String& str1, const char* str2) { return (strcmp(str1.c_str(), str2) >= 0); }
372 
377 inline bool operator<(const String& str1, const char* str2) { return (strcmp(str1.c_str(), str2) < 0); }
378 
383 inline bool operator<=(const String& str1, const char* str2) { return (strcmp(str1.c_str(), str2) <= 0); }
384 
389 inline bool operator==(const char* str1, const String& str2) { return (strcmp(str1, str2.c_str()) == 0); }
390 
395 inline bool operator!=(const char* str1, const String& str2) { return (strcmp(str1, str2.c_str()) != 0); }
396 
401 inline bool operator>(const char* str1, const String& str2) { return (strcmp(str1, str2.c_str()) > 0); }
402 
407 inline bool operator>=(const char* str1, const String& str2) { return (strcmp(str1, str2.c_str()) >= 0); }
408 
413 inline bool operator<(const char* str1, const String& str2) { return (strcmp(str1, str2.c_str()) < 0); }
414 
419 inline bool operator<=(const char* str1, const String& str2) { return (strcmp(str1, str2.c_str()) <= 0); }
420 
425 inline bool operator==(const String& str1, const String& str2)
426 { return (str1.length() == str2.length()) && (strcmp(str1.c_str(), str2.c_str()) == 0); }
427 
432 inline bool operator!=(const String& str1, const String& str2) { return (strcmp(str1.c_str(), str2.c_str()) != 0); }
433 
438 inline bool operator>(const String& str1, const String& str2) { return (strcmp(str1.c_str(), str2.c_str()) > 0); }
439 
444 inline bool operator>=(const String& str1, const String& str2) { return (strcmp(str1.c_str(), str2.c_str()) >= 0); }
445 
450 inline bool operator<(const String& str1, const String& str2) { return (strcmp(str1.c_str(), str2.c_str()) < 0); }
451 
456 inline bool operator<=(const String& str1, const String& str2) { return (strcmp(str1.c_str(), str2.c_str()) <= 0); }
457 
458 EDELIB_NS_END
459 #endif
bool empty(void) const
Definition: String.h:298
bool operator<=(const String &str1, const String &str2)
Definition: String.h:456
bool operator>(const String &str1, const String &str2)
Definition: String.h:438
bool operator>=(const char *str1, const String &str2)
Definition: String.h:407
const char * c_str(void) const
Definition: String.h:282
bool operator>(const String &str1, const char *str2)
Definition: String.h:365
bool operator==(const char *str1, const String &str2)
Definition: String.h:389
bool operator==(const String &str1, const char *str2)
Definition: String.h:353
const char * c_str(void)
Definition: String.h:279
String operator+(const String &s1, const String &s2)
bool operator==(const String &str1, const String &str2)
Definition: String.h:425
bool operator>(const char *str1, const String &str2)
Definition: String.h:401
bool operator<=(const char *str1, const String &str2)
Definition: String.h:419
bool operator<(const char *str1, const String &str2)
Definition: String.h:413
bool operator<=(const String &str1, const char *str2)
Definition: String.h:383
bool operator>=(const String &str1, const String &str2)
Definition: String.h:444
size_type length(void) const
Definition: String.h:292
A (relatively simple) string implementation.
Definition: String.h:82
const char * data(void) const
Definition: String.h:289
bool operator!=(const String &str1, const String &str2)
Definition: String.h:432
static const size_type npos
Definition: String.h:111
bool operator!=(const String &str1, const char *str2)
Definition: String.h:359
bool operator<(const String &str1, const String &str2)
Definition: String.h:450
size_type capacity(void) const
Definition: String.h:295
bool operator<(const String &str1, const char *str2)
Definition: String.h:377
bool operator>=(const String &str1, const char *str2)
Definition: String.h:371
bool operator!=(const char *str1, const String &str2)
Definition: String.h:395