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
27EDELIB_NS_BEGIN
28
82class EDELIB_API String {
83public:
84#ifndef SKIP_DOCS
85 typedef unsigned int size_type;
86#endif
87
88private:
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
102public:
111 static const size_type npos;
112
117
123 String(const char* str);
124
130 String(const String& str);
131
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
326
328 String& operator+=(const char& ch);
329};
330
335EDELIB_API String operator+(const String& s1, const String& s2);
336
341EDELIB_API String operator+(const char* s1, const String& s2);
342
347EDELIB_API String operator+(const String& s1, const char* s2);
348
353inline bool operator==(const String& str1, const char* str2) { return (strcmp(str1.c_str(), str2) == 0); }
354
359inline bool operator!=(const String& str1, const char* str2) { return (strcmp(str1.c_str(), str2) != 0); }
360
365inline bool operator>(const String& str1, const char* str2) { return (strcmp(str1.c_str(), str2) > 0); }
366
371inline bool operator>=(const String& str1, const char* str2) { return (strcmp(str1.c_str(), str2) >= 0); }
372
377inline bool operator<(const String& str1, const char* str2) { return (strcmp(str1.c_str(), str2) < 0); }
378
383inline bool operator<=(const String& str1, const char* str2) { return (strcmp(str1.c_str(), str2) <= 0); }
384
389inline bool operator==(const char* str1, const String& str2) { return (strcmp(str1, str2.c_str()) == 0); }
390
395inline bool operator!=(const char* str1, const String& str2) { return (strcmp(str1, str2.c_str()) != 0); }
396
401inline bool operator>(const char* str1, const String& str2) { return (strcmp(str1, str2.c_str()) > 0); }
402
407inline bool operator>=(const char* str1, const String& str2) { return (strcmp(str1, str2.c_str()) >= 0); }
408
413inline bool operator<(const char* str1, const String& str2) { return (strcmp(str1, str2.c_str()) < 0); }
414
419inline bool operator<=(const char* str1, const String& str2) { return (strcmp(str1, str2.c_str()) <= 0); }
420
425inline bool operator==(const String& str1, const String& str2)
426{ return (str1.length() == str2.length()) && (strcmp(str1.c_str(), str2.c_str()) == 0); }
427
432inline bool operator!=(const String& str1, const String& str2) { return (strcmp(str1.c_str(), str2.c_str()) != 0); }
433
438inline bool operator>(const String& str1, const String& str2) { return (strcmp(str1.c_str(), str2.c_str()) > 0); }
439
444inline bool operator>=(const String& str1, const String& str2) { return (strcmp(str1.c_str(), str2.c_str()) >= 0); }
445
450inline bool operator<(const String& str1, const String& str2) { return (strcmp(str1.c_str(), str2.c_str()) < 0); }
451
456inline bool operator<=(const String& str1, const String& str2) { return (strcmp(str1.c_str(), str2.c_str()) <= 0); }
457
458EDELIB_NS_END
459#endif
A (relatively simple) string implementation.
Definition: String.h:82
String & append(size_type num, const char &ch)
String & operator=(const String &str)
bool empty(void) const
Definition: String.h:298
bool operator<(const String &str1, const char *str2)
Definition: String.h:377
bool operator>(const String &str1, const char *str2)
Definition: String.h:365
void printf(const char *fmt,...)
String & operator+=(const String &str)
bool operator<=(const char *str1, const String &str2)
Definition: String.h:419
char & operator[](size_type index)
char operator[](size_type index) const
bool operator==(const String &str1, const String &str2)
Definition: String.h:425
bool operator==(const char *str1, const String &str2)
Definition: String.h:389
bool operator>(const String &str1, const String &str2)
Definition: String.h:438
String & append(const char *str, size_type len)
String(const String &str)
String & append(const char *str)
bool operator<=(const String &str1, const char *str2)
Definition: String.h:383
void trim_left(void)
String & assign(const char *str)
bool operator>(const char *str1, const String &str2)
Definition: String.h:401
bool operator!=(const String &str1, const String &str2)
Definition: String.h:432
String & assign(const char *str, size_type len)
bool operator<(const String &str1, const String &str2)
Definition: String.h:450
String operator+(const char *s1, const String &s2)
bool operator<=(const String &str1, const String &str2)
Definition: String.h:456
size_type find(char ch, size_type offset) const
const char * c_str(void)
Definition: String.h:279
void trim_right(void)
const char * data(void) const
Definition: String.h:289
String operator+(const String &s1, const char *s2)
String & replace(char c1, char c2)
String & append(const String &str)
String & assign(const String &str)
void swap(String &from)
String & operator+=(const char *str)
bool operator>=(const String &str1, const String &str2)
Definition: String.h:444
String & operator+=(const char &ch)
String operator+(const String &s1, const String &s2)
void reserve(size_type len)
bool operator!=(const String &str1, const char *str2)
Definition: String.h:359
size_type capacity(void) const
Definition: String.h:295
String substr(size_type index, size_type num=npos) const
void trim(void)
static const size_type npos
Definition: String.h:111
size_type length(void) const
Definition: String.h:292
size_type find(const char *str, size_type offset) const
bool operator!=(const char *str1, const String &str2)
Definition: String.h:395
size_type find(const char *str) const
bool operator>=(const String &str1, const char *str2)
Definition: String.h:371
bool operator==(const String &str1, const char *str2)
Definition: String.h:353
void clear(void)
bool operator<(const char *str1, const String &str2)
Definition: String.h:413
bool operator>=(const char *str1, const String &str2)
Definition: String.h:407
String & operator=(const char *str)
const char * c_str(void) const
Definition: String.h:282
String(const char *str)