Elm  2
ELM is a library providing generic data structures, OS-independent interface, plugins and XML.
CString.h
1 /*
2  * $Id$
3  * Copyright (c) 2004-07, IRIT - UPS.
4  *
5  * CString class
6  */
7 #ifndef ELM_STRING_CSTRING_H
8 #define ELM_STRING_CSTRING_H
9 
10 #include <string.h>
11 
12 namespace elm {
13 
14 class String;
15 
16 // CString class
17 class CString {
18 protected:
19  const char *buf;
20 public:
21  inline CString(void): buf("") { };
22  inline CString(const char *str): buf(str ? str : "") { };
23  inline CString(const CString& str): buf(str.buf) { };
24  inline CString& operator=(const CString& str) { buf = str.buf; return *this; };
25 
26  inline int length(void) const { return strlen(buf); };
27  inline const char *chars(void) const { return buf; };
28  inline int compare(const CString& str) const { return strcmp(buf, str.buf); };
29 
30  inline bool isEmpty(void) const { return !*buf; };
31  inline operator bool(void) const { return !isEmpty(); };
32 
33  inline char charAt(int index) const { return buf[index]; };
34  inline char operator[](int index) const { return charAt(index); };
35  inline CString substring(int pos) const;
36  inline String substring(int pos, int len) const;
37 
38  inline String concat(const CString str) const;
39  inline String concat(const String& str) const;
40 
41  inline int indexOf(char chr) const { return indexOf(chr, 0); };
42  inline int indexOf(char chr, int pos) const
43  { for(const char *p = buf + pos; *p; p++) if(*p == chr) return p - buf; return -1; };
44  inline int lastIndexOf(char chr) const { return lastIndexOf(chr, length()); };
45  inline int lastIndexOf(char chr, int pos) const
46  { for(const char *p = buf + pos - 1; p >= buf; p--) if(*p == chr) return p - buf; return -1; };
47 
48  // Suffix and prefix
49  inline bool startsWith(const char *str) const;
50  inline bool startsWith(const CString str) const;
51  inline bool startsWith(const String& str) const;
52  inline bool endsWith(const char *str) const;
53  inline bool endsWith(const CString str) const;
54  inline bool endsWith(const String& str) const;
55 
56  // Automatic conversion
57  inline operator const char *(void) const { return buf; };
58 };
59 
60 // Type shortcut
61 #ifndef ELM_NO_STRING_SHORTCUT
62  typedef CString cstring;
63  inline cstring cstr(const char *s) { return cstring(s); }
64 #endif
65 
66 } // elm
67 
68 #endif // ELM_STRING_CSTRING_H
elm::io::p
Printable< T, M > p(const T &data, const M &man)
Definition: Output.h:302
elm::CString::operator[]
char operator[](int index) const
Definition: CString.h:34
elm::CString::substring
CString substring(int pos) const
Definition: string.h:20
elm::CString::compare
int compare(const CString &str) const
Definition: CString.h:28
elm::CString::buf
const char * buf
Definition: CString.h:19
elm::cstring
CString cstring
Definition: CString.h:62
void
elm::CString::CString
CString(const CString &str)
Definition: CString.h:23
elm::cstr
cstring cstr(const char *s)
Definition: CString.h:63
bool
elm::CString
Definition: CString.h:17
elm::CString::CString
CString(void)
Definition: CString.h:21
elm::CString::indexOf
int indexOf(char chr) const
Definition: CString.h:41
elm::CString::concat
String concat(const CString str) const
Definition: string.h:22
elm::CString::startsWith
bool startsWith(const char *str) const
Definition: string.h:24
elm
Definition: adapter.h:26
elm::CString::chars
const char * chars(void) const
Definition: CString.h:27
elm::CString::lastIndexOf
int lastIndexOf(char chr, int pos) const
Definition: CString.h:45
elm::CString::indexOf
int indexOf(char chr, int pos) const
Definition: CString.h:42
elm::CString::operator=
CString & operator=(const CString &str)
Definition: CString.h:24
elm::CString::length
int length(void) const
Definition: CString.h:26
elm::String
Definition: String.h:30
elm::str
string str(const char *s)
Definition: String.h:150
elm::CString::CString
CString(const char *str)
Definition: CString.h:22
elm::CString::charAt
char charAt(int index) const
Definition: CString.h:33
elm::CString::endsWith
bool endsWith(const char *str) const
Definition: string.h:27
elm::CString::lastIndexOf
int lastIndexOf(char chr) const
Definition: CString.h:44
elm::CString::isEmpty
bool isEmpty(void) const
Definition: CString.h:30